source: tmcsimulator/trunk/src/python/unifiedlogger/cad_watcher.py @ 439

Revision 439, 1.5 KB checked in by jdalbey, 7 years ago (diff)

setup wing project for logging service python files. Add config file for file paths.

Line 
1import json, time
2from copy import deepcopy
3
4# CAD comment log Watcher
5# Look for changes in the CAD comment log.
6# The CADserver will append new comments to the log as they arrive
7# from clients.  We only need to keep track of the log length in order to
8# determine if a new comment has been added.  We will output the
9# new messages that arrived during the last wait interval.
10# jdalbey  7/6/2019
11
12lastLineNum = 0
13
14# Utility functions
15def isEmpty(cmsitem):
16    return cmsitem == ",,,,,"
17def isFull(cmsitem):
18    return not isEmpty(cmsitem)
19
20# Read the cad comments log
21def readFile():
22    lines = []
23    try:
24        text_file = open("CADcomments.log", "r")
25    except IOError:
26        print "Error: missing CADcomments.log file."
27    else:
28        lines = text_file.read().split('\n')
29        text_file.close()   
30    return lines
31   
32def setup():
33    # nothing needed for setup
34    return
35
36# Retrieve new messages from CAD comment log
37def getLogEntries():
38    global lastLineNum
39    msgList = readFile()
40    currList = []
41    currList = msgList[lastLineNum:]    # new items since last file read
42    lastLineNum = len(msgList)-1
43    return currList
44
45# Local main for unit testing
46def main():
47    setup()
48    # Loop Forever, checking every five seconds
49    while True:
50        # Look for new messages
51        answer = getLogEntries()
52        # Output results
53        for item in answer:
54            print item
55        # wait
56        time.sleep(5)
57
58if __name__ == "__main__":
59    main()
Note: See TracBrowser for help on using the repository browser.