source: tmcsimulator/trunk/src/python/unifiedlogger/har_watcher.py @ 687

Revision 687, 3.3 KB checked in by jdalbey, 4 years ago (diff)

logging_service.py, et.al. Revise how it handles a missing CADcomments.log file - just skip the file read but continue. Also update all watchers to use config dir.

Line 
1import json, time, ConfigParser
2from copy import deepcopy
3
4# HAR Message Watcher
5# Look for changes in the HAR message file
6# jdalbey  2022.9.2
7idList = []    # list of CMS ID's
8prevList = []  # previous messages
9currList = []  # current messages
10locationMap = {} # map of HAR ID's to locations
11filepath = "webapps/dynamicdata" # default during development
12
13# Utility functions
14def isEmpty(cmsitem):
15     return cmsitem == ""
16def isFull(cmsitem):
17     return not isEmpty(cmsitem)
18
19# Read the har message file
20def readFile():
21    global filepath
22    with open (filepath+"/har_messages.json",'r') as myfile:
23          jsonData=myfile.read()
24 
25    return json.loads(jsonData)['data']
26
27# Read the static file of cms locations and create a lookup map
28def loadLocations():
29     with open ("webapps/cptms/data_layers/har_locations_D12.gjson",'r') as myfile:
30          jsonData=myfile.read()
31 
32     list = json.loads(jsonData)['features']
33     # add each item to a lookup map
34     for item in list:
35          locationMap [item['id']] = item['properties']['location'] + " " + item['properties']['street']
36     
37# Setup the ID list and initialize the previous messages to empty
38def initialize():
39     loadLocations()
40     msgList = readFile()
41     global prevList, idList
42     for idx in range(0,len(msgList)):
43          idList.append(msgList[idx]['har']['index'])
44          prevList.append("")
45     return msgList
46
47# Extract the current messages into a list
48def extractMessages(msgList):
49     global currList
50     currList = []
51     for idx in range(0,len(msgList)):
52          aMessage = msgList[idx]['har']['message']['phase1']['Line1']
53          currList.append(aMessage.replace("\n"," ").strip())
54
55def setup(dir):
56    global filepath
57    # get path to input file from configuration
58    config = ConfigParser.ConfigParser()
59    config.read(dir+'/logging_service.cfg')
60    filepath  = config.get('Paths', 'UnifiedLogPath')
61
62    extractMessages(initialize())
63
64# compare previous messages to current messages to look for changes
65def getLogEntries():
66     global prevList, currList
67     msgList = readFile()
68     extractMessages(msgList)
69     size = len(currList)
70     
71     results = []
72     # Consider each HAR message
73     for idx in range(0,size):
74          # Is a new message activated?
75          if isEmpty(prevList[idx]) and isFull(currList[idx]):
76               results.append("HAR Activated," + idList[idx] + ", '" + currList[idx] +"'")
77          # Is an existing message turned off?
78          if isEmpty(currList[idx]) and isFull(prevList[idx]):
79               results.append("HAR Deactivated, " + idList[idx])
80          # Did a message change?
81          if isFull(currList[idx]) and isFull(prevList[idx]) and currList[idx] != prevList[idx]:
82               results.append("HAR Updated, " + idList[idx] + ", '" + currList[idx] +"'")
83
84     # Save the current list as previous
85     prevList = deepcopy(currList)
86     return results
87
88# Local main for unit testing
89def main():
90     global currList
91     setup("config/devlinux")
92     # Loop Forever
93     while True:
94          # Look for changed messages
95          answer = getLogEntries()
96          # Output results
97          for item in answer:
98               print item
99          # wait
100          time.sleep(5)
101          # Get the current messages
102          extractMessages(readFile())
103
104if __name__ == "__main__":
105     main()
106     
Note: See TracBrowser for help on using the repository browser.