source: tmcsimulator/trunk/webapps/unifiedlogger/cad_watcher.py @ 436

Revision 436, 1.1 KB checked in by jdalbey, 7 years ago (diff)

cad_watcher.py added exception handling for missing log file

Line 
1import json, time
2from copy import deepcopy
3
4# CAD comment log Watcher
5# Look for changes in the CAD comment log
6# jdalbey  7/6/2019
7
8lastLineNum = 0
9
10# Utility functions
11def isEmpty(cmsitem):
12    return cmsitem == ",,,,,"
13def isFull(cmsitem):
14    return not isEmpty(cmsitem)
15
16# Read the cms message file
17def readFile():
18    lines = []
19    try:
20        text_file = open("../../CADcomments.log", "r")
21    except IOError:
22        print "Error: missing CADcomments.log file."
23    else:
24        lines = text_file.read().split('\n')
25        text_file.close()   
26    return lines
27   
28def setup():
29    # nothing needed for setup
30    return
31
32# Retrieve new messages from CAD comment log
33def getLogEntries():
34    global lastLineNum
35    msgList = readFile()
36    currList = []
37    currList = msgList[lastLineNum:]    # new items since last file read
38    lastLineNum = len(msgList)-1
39    return currList
40
41def main():
42    setup()
43    # Loop Forever
44    while True:
45        # Look for changed messages
46        answer = getLogEntries()
47        # Output results
48        for item in answer:
49            print item
50        # wait
51        time.sleep(5)
52
53if __name__ == "__main__":
54    main()
Note: See TracBrowser for help on using the repository browser.