source: tmcsimulator/trunk/src/python/unifiedlogger/cms_watcher.py @ 549

Revision 549, 3.4 KB checked in by jdalbey, 6 years ago (diff)

unifiedlogger Correct messages to have similar capitalization and punctuation.

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