| 1 | import json, time |
|---|
| 2 | from copy import deepcopy |
|---|
| 3 | |
|---|
| 4 | # HAR Message Watcher |
|---|
| 5 | # Look for changes in the HAR message file |
|---|
| 6 | # jdalbey 2022.9.2 |
|---|
| 7 | idList = [] # list of CMS ID's |
|---|
| 8 | prevList = [] # previous messages |
|---|
| 9 | currList = [] # current messages |
|---|
| 10 | locationMap = {} # map of HAR 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 har message file |
|---|
| 19 | def readFile(): |
|---|
| 20 | with open ("webapps/dynamicdata/har_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/har_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]['har']['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 | aMessage = msgList[idx]['har']['message']['phase1']['Line1'] |
|---|
| 51 | currList.append(aMessage.replace("\n"," ").strip()) |
|---|
| 52 | |
|---|
| 53 | def setup(): |
|---|
| 54 | extractMessages(initialize()) |
|---|
| 55 | |
|---|
| 56 | # compare previous messages to current messages to look for changes |
|---|
| 57 | def getLogEntries(): |
|---|
| 58 | global prevList, currList |
|---|
| 59 | msgList = readFile() |
|---|
| 60 | extractMessages(msgList) |
|---|
| 61 | size = len(currList) |
|---|
| 62 | |
|---|
| 63 | results = [] |
|---|
| 64 | # Consider each HAR message |
|---|
| 65 | for idx in range(0,size): |
|---|
| 66 | # Is a new message activated? |
|---|
| 67 | if isEmpty(prevList[idx]) and isFull(currList[idx]): |
|---|
| 68 | results.append("HAR Activated," + idList[idx] + ", '" + currList[idx] +"'") |
|---|
| 69 | # Is an existing message turned off? |
|---|
| 70 | if isEmpty(currList[idx]) and isFull(prevList[idx]): |
|---|
| 71 | results.append("HAR Deactivated, " + idList[idx]) |
|---|
| 72 | # Did a message change? |
|---|
| 73 | if isFull(currList[idx]) and isFull(prevList[idx]) and currList[idx] != prevList[idx]: |
|---|
| 74 | results.append("HAR Updated, " + idList[idx] + ", '" + currList[idx] +"'") |
|---|
| 75 | |
|---|
| 76 | # Save the current list as previous |
|---|
| 77 | prevList = deepcopy(currList) |
|---|
| 78 | return results |
|---|
| 79 | |
|---|
| 80 | # Local main for unit testing |
|---|
| 81 | def main(): |
|---|
| 82 | global currList |
|---|
| 83 | setup() |
|---|
| 84 | # Loop Forever |
|---|
| 85 | while True: |
|---|
| 86 | # Look for changed messages |
|---|
| 87 | answer = getLogEntries() |
|---|
| 88 | # Output results |
|---|
| 89 | for item in answer: |
|---|
| 90 | print item |
|---|
| 91 | # wait |
|---|
| 92 | time.sleep(5) |
|---|
| 93 | # Get the current messages |
|---|
| 94 | extractMessages(readFile()) |
|---|
| 95 | |
|---|
| 96 | if __name__ == "__main__": |
|---|
| 97 | main() |
|---|
| 98 | |
|---|