New in the Community? Get started here

Schneider Electric Exchange Community

Discuss and solve problems in energy management and automation. Join conversations and share insights on products and solutions. Co-innovate and collaborate with a global network of peers.

Register Now
Knowledge Base
cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements

- Who are the top users of September? Discover them here!

- Check out the ESS Active Energy Management Blog now! 

- Join us on October 23rd for a live Panel session to discuss The Power of a Sustainable Ecosystem with Hervé Coureil, Chief Digital Officer of Schneider Electric and other great speakers. Register here!

Sample PE Program that reads/writes files

Issue

How to read and write from/to files using PE

Product Line

Andover Continuum

Environment

  • CyberStation
  • Plain English

Cause

Site needed a CSV report of personnel accessing the building that included access event information such as time stamp, door, area, card number BUT also data such as employee number etc., a simple list view could not be used since list views can only show data from ONE table in the ContinuumDB.

Resolution

Sample CyberStation program that reads/writes from/to file and aggregate data from different tables in the ContinuumDB

IMPORTANT:
PE Program is provided as a sample proof of concept ONLY and it is not to be taken as a complete working solution.

REQUIREMENTS:
Create STRINGS ReportEventsPath and T_A_Path in the CyberStation where the program will run and set to the path to the input and output files respectively, the input file must exist, the output file will be automatically created if it doesn’t exist.

PROGRAM CODE:

'This Cyberstation program takes as an input a CSV file generated from an Access Event list
'and creates a second file with the EMPLOYEE NUMBER added.
'Program is FALL-THRU and NOT autostart
'IMPORTANT
'The Input file MUST be formatted like this...
'AccessEvent
'PersonId,TimeStamp,CardNumber,AreaId
'Root\MyPersonnel\Meran_Abe,3/31/2014 10:16:11 PM,3540,Root\MyAreas\Area1

File inputFILE, outputFILE
String 132 DATALINE, RESULT, theEMPLOYEE, empNUMBER, localSTR
Numeric CHARSREAD, CHARSWRITTEN, OK, RECORDLINE
Numeric DEBUG_FLAG

Line INITIALIZE
  DEBUG_FLAG = 0 'SET THIS TO 1 TO HAVE PROGRAM PRINT DEBUG INFO

Line OPENING
  RESULT = ""
  RECORDLINE = 0
  CHARSREAD = 0

  If OpenFile(ReportEventsPath, ReadOnly, inputFILE) = Failure then 'OPEN THE INPUT FILE TO READ FROM
    RESULT = "COULD NOT OPEN inputFILE FILE"
    Goto FAILING
  Endif

  If OpenFile(T_A_Path, WriteOnly, outputFILE) = Failure then 'OPEN THE OUTPUT FILE TO WRITE TO
    RESULT = "COULD NOT OPEN outputFILE FILE"
    Goto FAILING
  Else
    Goto COPYING
  Endif

Line COPYING
  RECORDLINE = RECORDLINE + 1
  If DEBUG_FLAG then Print "Record line = " ; RECORDLINE
  If RECORDLINE = 1 then
    OK = ReadFile(inputFILE, DATALINE, 132, CHARSREAD, "|10") 'READ FIRST LINE FROM THE INPUT FILE UP TO THE LF
  Else
    OK = ReadFile(inputFILE, DATALINE, 132, CHARSREAD, "|13") 'READ ONE LINE FROM THE INPUT FILE UP TO THE CR
  Endif
  If OK = Failure then
    RESULT = "COULD NOT READ FILE"
    Goto FAILING
  Else
    If DEBUG_FLAG then Print "Read..." ; DATALINE
    If OK = Eof then
      RESULT = "END OF FILE"
      Goto CLOSING
    Endif
  Endif

'INSERT THE EMPLOYEE NUMBER
  If RECORDLINE = 2 then empNUMBER = "EmpNumber" 'MODIFY THE HEADER WITH THE NAME OF THE FIELD(S) WE ARE ADDING
  If RECORDLINE > 2 then
    theEMPLOYEE = (left(DATALINE, search(DATALINE, ",")))
    theEMPLOYEE = (left(theEMPLOYEE, (search(theEMPLOYEE, ",") - 1))) 'FOR EACH LINE IN THE INPUT FILE GET THE PATH TO THE PERSONNEL OBJECT
    Print getname(theEMPLOYEE ; " EmpNumber") to empNUMBER 'AND GET THE EMPLOYEE NUMBER
  Endif

'WRITE THE LINE WITH THE ADDED NEW FIELD(S) TO THE OUTPUT FILE
  If RECORDLINE = 1 then
    Print DATALINE ; chr(10) to localSTR
  Else
    Print DATALINE ; "," ; empNUMBER ; chr(10) to localSTR
  Endif
  OK = WriteFile(outputFILE, localSTR, 132, CHARSWRITTEN)
  If OK = Failure then
    RESULT = "COULD NOT WRITE FILE"
    Goto FAILING
  Else
    If DEBUG_FLAG then
      Print "Wrote..." ; localSTR

    Print "CHARSWRITTEN=" ; CHARSWRITTEN
  Endif
'POINT TO THE NEXT LINE IN THE FILES
  If RECORDLINE = 1 then
    If PositionFile(inputFILE, 1, FileCurrent) = Failure then Goto CLOSING 'ADVANCE PAST THE LF
  Else
    If PositionFile(inputFILE, 2, FileCurrent) = Failure then Goto CLOSING 'ADVANCE PAST THE CR and LF
  Endif
  If PositionFile(outputFILE, 0, FileCurrent) = Failure then Goto CLOSING 'WRITE AT THE CURRENT FILE LOCATION
    Goto COPYING 'GO DO THE NEXT LINE
  Endif

Line FAILING 'SOMETHING WENT WRONG
  If DEBUG_FLAG then Print RESULT
  Goto CLOSING

Line CLOSING
'CLOSE THE FILES
  If CloseFile(inputFILE) is Failure then RESULT = "COULD NOT CLOSE FILE"
  If CloseFile(outputFILE) is Failure then RESULT = "COULD NOT CLOSE FILE"
  If DEBUG_FLAG then PEDebug = PEDebug + 1 'IF DEBUGGING INCREMENTED PEDebug MEANS NO ERRORS
  Stop

Line E 'HANDLE RUNTIME ERROR
  If DEBUG_FLAG then
    Print RESULT
    PEDebug = 666
  Endif
  CloseFile(inputFILE)
  CloseFile(outputFILE)
  Stop 

After the program runs if the input file looks like:
'AccessEvent
'PersonId,TimeStamp,CardNumber,AreaId
'Root\MyPersonnel\Meran_Abe,3/31/2014 10:16:11 PM,3540,Root\MyAreas\Area1

The output file will be:
'AccessEvent,
'PersonId,TimeStamp,CardNumber,AreaId,EmpNumber,
'Root\MyPersonnel\Meran_Abe,3/31/2014 10:16:11 PM,3540,Root\MyAreas\Area1,12345

 

Tags (1)
Labels (1)
No ratings