| 1 | import json, time |
|---|
| 2 | from copy import deepcopy |
|---|
| 3 | |
|---|
| 4 | # CMS Message Watcher |
|---|
| 5 | # Look for changes in the CMS message file |
|---|
| 6 | # jdalbey 7/6/2019 |
|---|
| 7 | idList = [] # list of CMS ID's |
|---|
| 8 | prevList = [] # previous messages |
|---|
| 9 | currList = [] # current messages |
|---|
| 10 | locationMap = {} # map of CMS ID's to locations |
|---|
| 11 | |
|---|
| 12 | # Utility functions |
|---|
| 13 | def isEmpty(cmsitem): |
|---|
| 14 | return cmsitem == ":::::" |
|---|
| 15 | def isFull(cmsitem): |
|---|
| 16 | return not isEmpty(cmsitem) |
|---|
| 17 | |
|---|
| 18 | # Read the cms message file |
|---|
| 19 | def 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 |
|---|
| 26 | def 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 |
|---|
| 36 | def 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 |
|---|
| 46 | def 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 | def setup(): |
|---|
| 59 | extractMessages(initialize()) |
|---|
| 60 | |
|---|
| 61 | # compare previous messages to current messages to look for changes |
|---|
| 62 | def 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 |
|---|
| 86 | def 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 | |
|---|
| 101 | if __name__ == "__main__": |
|---|
| 102 | main() |
|---|
| 103 | |
|---|