Created
January 8, 2019 20:42
-
-
Save ov1d1u/bdd39875644cb05dc42655af8d4b4c60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import csv | |
import re | |
import requests | |
import json | |
import os | |
import sys | |
from datetime import datetime | |
API_HOST = 'instanta.de.home.assistant:1234' | |
MY_PC_IDENTIFIER = '12 Foi' # Punctul 12 Foișor | |
MY_ADDRESS_IDENTIFIER = 'C13' # bloc C13 | |
INC_TEXT = u'A fost anunțată o avarie în rețeaua de încălzire. Căldura va fi oprită în perioada {0} - {1}' | |
ACC_TEXT = u'A fost anunțată o avarie în rețeaua de apă caldă. Apa caldă va fi oprită în perioada {0} - {1}' | |
class AvariiParser: | |
def __init__(self, filename='Opriri_avarii.csv'): | |
self.filename = filename | |
self.reader = None | |
self.csv_data = [] | |
with open(filename, 'r') as fh: | |
reader = csv.reader(fh) | |
for line in reader: | |
self.csv_data.append(line) | |
self.pc_column = self.find_pc_column() | |
self.areas_column = self.find_areas_column() | |
self.my_pc_lines = self.find_my_pc_lines() | |
def get_notifications(self): | |
start_date_column = self.find_start_date_column() | |
end_date_column = self.find_end_date_column() | |
type_column = self.find_type_column() | |
start_date = None | |
end_date = None | |
notifications = [] | |
for line in self.my_pc_lines: | |
try: | |
start_date = datetime.strptime(line[start_date_column], '%d.%m.%y/%H.%M') | |
except ValueError: | |
continue | |
try: | |
end_date = datetime.strptime(line[end_date_column], '%d.%m.%y/%H.%M') | |
except ValueError: | |
continue | |
if line[type_column] == 'ÎNC': | |
notifications.append(INC_TEXT.format( | |
start_date.strftime("%d.%m.%Y %H:%M"), | |
end_date.strftime("%d.%m.%Y %H:%M") | |
)) | |
if line[type_column] == 'ACC': | |
notifications.append(ACC_TEXT.format( | |
start_date.strftime("%d.%m.%Y %H:%M"), | |
end_date.strftime("%d.%m.%Y %H:%M") | |
)) | |
return notifications | |
def find_pc_column(self): | |
for line in self.csv_data: | |
for index, column in enumerate(line): | |
if 'PT' in column and 'CT' in column: | |
return index | |
def find_areas_column(self): | |
for line in self.csv_data: | |
for index, column in enumerate(line): | |
if 'afectate' in column: | |
return index | |
def find_start_date_column(self): | |
for line in self.csv_data: | |
for index, column in enumerate(line): | |
if 'opririi' in column: | |
return index | |
def find_end_date_column(self): | |
for line in self.csv_data: | |
for index, column in enumerate(line): | |
if 'punerii' in column: | |
return index | |
def find_type_column(self): | |
for line in self.csv_data: | |
for index, column in enumerate(line): | |
if 'Agentul' in column: | |
return index | |
def find_my_pc_lines(self): | |
start = -1 | |
stop = -1 | |
for row, line in enumerate(self.csv_data): | |
for column in line: | |
result = re.match('(\\d+) (\\w+){2,}', column) | |
if result: | |
if MY_PC_IDENTIFIER in column: | |
start = row | |
continue | |
if start > -1: | |
stop = row | |
return self.csv_data[start:stop] | |
def is_affected(self): | |
if not self.pc_column or not self.areas_column or not self.my_pc_lines: | |
return False | |
for line in self.find_my_pc_lines(): | |
if MY_ADDRESS_IDENTIFIER in line[self.areas_column]: | |
return True | |
return False | |
def __del__(self): | |
if hasattr(self, 'fh'): | |
self.fh.close() | |
if __name__ == '__main__': | |
# TODO: do not process file if it was already processed | |
filename = sys.argv[1] | |
history = [] | |
if not os.path.exists(filename): | |
print("File {0} does not exists!".format(filename)) | |
sys.exit(1) | |
if os.path.exists('notifications.json'): | |
history_file = open('notifications.json', 'r') | |
try: | |
history = json.loads(history_file.read()) | |
except ValueError: | |
pass # Empty file, likely? | |
history_file.close() | |
history_file = open('notifications.json', 'w') | |
ap = AvariiParser(filename) | |
if ap.is_affected(): | |
notifications = ap.get_notifications() | |
for notification in notifications: | |
if notification in history: | |
print("Already sent this notification, skip...") | |
continue | |
headers = { | |
'Authorization': 'Bearer - aici e o cheie secreta pt a comunica cu o instanta de HomeAssistant -' | |
} | |
payload = { | |
"title": "Avarie rețea termoficare", | |
"message": notification | |
} | |
r = requests.post( | |
"https://{0}/api/services/notify/rest".format(API_HOST), | |
headers=headers, | |
data=json.dumps(payload) | |
) | |
history.append(notification) | |
history_file.seek(0) | |
history_file.write(json.dumps(history)) | |
history_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment