-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.py
More file actions
47 lines (37 loc) · 1.6 KB
/
Copy pathevent.py
File metadata and controls
47 lines (37 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import logging
import os
import json
from datetime import datetime
class Event:
def __init__(self, json_string):
self.json = json.loads(json_string)
self.response_type = self.json['response_type']
self.message = self.json['message']
self.data = self.json['data']
self.timestamp = datetime.now().strftime("%H:%M:%S")
self.datestamp = datetime.now().strftime("%Y_%m_%d")
logging.info(f"Event from {self.rtu_id()} received")
def should_be_logged(self):
# return self.response_type in ['RTUUpdateResult', 'DeviceEnactResult', 'DeviceUpdateResult']
# For now just log all event types
# Might change this later
return True
def rtu_id(self):
return self.data['RTU']['id']
def ip(self):
return self.data['RTU']['addr']
def file_name(self):
return f"{self.datestamp}_{self.rtu_id()}.wiretap"
def write_to_file(self):
# Get the file path
file_path = self.file_name()
# If it doesn't exist, create it with a basic csv header
# use | as a delimeter because there's commas in the data
if not os.path.exists(file_path):
with open(file_path, 'w') as file:
file.write('timestamp|data\n')
logging.info(f"File {file_path} created")
# Now that we know it exists, just append the new data with a timestamp
with open(file_path, 'a') as file:
file.write(f"{self.datestamp} {self.timestamp}|{json.dumps(self.json)}\n")
logging.info(f"Event from {self.rtu_id()} written to file {file_path}")