Warning: Can't use blame annotator:
svn blame failed on trunk/src/python/unifiedlogger/cms_watcher.py: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 687, 3.7 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.

RevLine 
1import json, time, ConfigParser
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
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 cms message file
20def readFile():
21    global filepath
22    with open (filepath+"/cms_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/cms_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]['cms']['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          currList.append(
53                    msgList[idx]['cms']['message']['phase1']['Line1'] + ':' + 
54                    msgList[idx]['cms']['message']['phase1']['Line2'] + ':' +
55                    msgList[idx]['cms']['message']['phase1']['Line3'] + ':' +
56                    msgList[idx]['cms']['message']['phase2']['Line1'] + ':' +
57                    msgList[idx]['cms']['message']['phase2']['Line2'] + ':' +
58                    msgList[idx]['cms']['message']['phase2']['Line3'])
59
60def setup(dir):
61    global filepath
62    # get path to input file from configuration
63    config = ConfigParser.ConfigParser()
64    config.read(dir+'/logging_service.cfg')
65    filepath  = config.get('Paths', 'UnifiedLogPath')
66   
67    extractMessages(initialize())
68
69# compare previous messages to current messages to look for changes
70def getLogEntries():
71     global prevList, currList
72     msgList = readFile()
73     extractMessages(msgList)
74     size = len(currList)
75     
76     results = []
77     # Consider each CMS message
78     for idx in range(0,size):
79          # Is a new message activated?
80          if isEmpty(prevList[idx]) and isFull(currList[idx]):
81               results.append("CMS Activated," + locationMap[idList[idx]] + ", '" + currList[idx] +"'")
82          # Is an existing message turned off?
83          if isEmpty(currList[idx]) and isFull(prevList[idx]):
84               results.append("CMS Deactivated, " + locationMap[idList[idx]])
85          # Did a message change?
86          if isFull(currList[idx]) and isFull(prevList[idx]) and currList[idx] != prevList[idx]:
87               results.append("CMS Updated, " + locationMap[idList[idx]] + ", '" + currList[idx] +"'")
88
89     # Save the current list as previous
90     prevList = deepcopy(currList)
91     return results
92
93# Local main for unit testing
94def main():
95     global currList
96     setup("config/devlinux")
97     # Loop Forever
98     while True:
99          # Look for changed messages
100          answer = getLogEntries()
101          # Output results
102          for item in answer:
103               print item
104          # wait
105          time.sleep(5)
106          # Get the current messages
107          extractMessages(readFile())
108
109if __name__ == "__main__":
110     main()
111     
Note: See TracBrowser for help on using the repository browser.