source: tmcsimulator/trunk/webapps/unifiedlogger/cms_watcher.py @ 431

Revision 431, 3.3 KB checked in by jdalbey, 7 years ago (diff)

Add new files for prototype of unified logging service

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 = {}
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 ("../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 ("../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
58# Compare previous messages to current messages to look for changes
59def compare():
60     global prevList, currList
61     msgList = readFile()
62     extractMessages(msgList)
63     size = len(currList)
64     
65     results = []
66     # Consider each CMS message
67     for idx in range(0,size):
68          # Is a new message activated?
69          if isEmpty(prevList[idx]) and isFull(currList[idx]):
70               results.append("CMS Activated. " + locationMap[idList[idx]] + " '" + currList[idx] +"'")
71          # Is an existing message turned off?
72          if isEmpty(currList[idx]) and isFull(prevList[idx]):
73               results.append("CMS Deactivated. " + locationMap[idList[idx]])
74          # Did a message change?
75          if isFull(currList[idx]) and isFull(prevList[idx]) and currList[idx] != prevList[idx]:
76               results.append("CMS Updated. " + locationMap[idList[idx]] + " '" + currList[idx] +"'")
77
78     # Save the current list as previous
79     prevList = deepcopy(currList)
80     return results
81
82def setup():
83     msgList = initialize()
84     extractMessages(msgList)
85
86def main():
87     global currList
88     setup()
89     # Loop Forever
90     while True:
91          # Look for changed messages
92          answer = compare()
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.