-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_manager.py
160 lines (119 loc) · 4.94 KB
/
task_manager.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from collections import defaultdict
import threading
import time
from aonit_log import IntegrationAonitLog
from era import Ent
from aonit import IntegrationAonit
from aonit_log import IntegrationAonitLog
from datetime import date, datetime,timedelta
import schedule
import json
from hikvision import HikvisionApi
from perco import Perco
import sqlite3
from perco_web import PercoWebApi
from rusguard import RusGuardApi
class Task(threading.Thread):
def __init__(self,seconds):
super().__init__()
with open('conf.json') as f:
self.data = json.load(f)
self.delay = seconds
self.aonitTp = IntegrationAonit()
self.aonitLog = IntegrationAonitLog()
self.con = sqlite3.connect("mydb.db",check_same_thread=False)
self.cursor = self.con.cursor()
self.era = Ent()
self.hikvision = HikvisionApi()
self.perco_web = PercoWebApi()
self.rusguard = RusGuardApi()
if self.data['selected_version'] == "perco":
self.perco = Perco()
self.is_done = False
def done(self):
self.is_done = True
def getJob(self, today):
# today = date.today()
self.cursor.execute("DELETE FROM events WHERE created_at = ?;", (today, ))
if self.data['selected_version'] == "perco":
events = self.perco
elif self.data['selected_version'] == "era":
events = self.era
elif self.data['selected_version'] == "rusguard":
events = self.rusguard
elif self.data['selected_version'] == "hikvision":
events = self.hikvision
elif self.data['selected_version'] == "perco_web":
events = self.perco_web
new_events = events.collect_events(today)
self.cursor.executemany("INSERT INTO events VALUES (NULL,?,?,?,?,?)", new_events)
self.con.commit()
def sendAonit(self, today):
print(today)
if self.data['aonit_type'] == 'log':
send= self.aonitLog.sendRequestToAonit(today)
else:
send = self.aonitTp.sendRequestToAonit(today)
if send['code'] == 200:
print("Успешно отправлено")
else:
print("ПРОВЕРТЕ СЕТЬ")
def sendProcced(self):
events = self.hikvision.collect_events(processed=True)
grouped_by_date = defaultdict(list)
for record in events:
date = datetime.strptime(record[3], '%d.%m.%Y %H:%M:%S').date() # Извлекаем дату из datetime
grouped_by_date[date].append(record)
self.cursor.execute("DELETE FROM events WHERE created_at = ?;", (date, ))
self.con.commit()
# Вывод сгруппированных данных
for date, records in grouped_by_date.items():
self.cursor.executemany("INSERT INTO events VALUES (NULL,?,?,?,?,?)", records)
self.con.commit()
self.sendAonit(date)
self.updateSended(records)
def updateSended(self, records):
username = 'sa'
password = '123456qA'
server = self.data["hikvision_server_name"]
conn = HikvisionApi().connectionToDb(server, 'thirdparty', username, password)
cur = conn.cursor()
for record in records:
cur.execute(f"""UPDATE attlog SET isSended = 1
WHERE
cardNo = '{record[0]}' AND
authDateTime = '{record[3]}';
""")
events = cur.commit()
return events
def sendJob(self):
if datetime.now().weekday() > 5:
print('Выходные')
return
today = date.today()
last_time = datetime.strptime(self.data['last_process_time'], '%Y-%m-%d').date()
if last_time >= today:
self.getJob(today=today)
self.sendAonit(today)
else:
start_date = last_time
day = timedelta(days=1)
while start_date <= today:
print(start_date)
self.getJob(start_date)
self.sendAonit(start_date)
start_date += day
self.data['last_process_time'] = today.strftime('%Y-%m-%d')
with open('conf.json', 'w') as f:
json.dump(self.data,f, ensure_ascii=False, indent=4)
def run(self):
datetime_obj = datetime.strptime(self.data['year'], '%Y-%m-%d').date()
if (datetime.now().date() > datetime_obj):
return
schedule.every(1).hours.do(self.sendJob)
schedule.every(5).minutes.do(self.sendProcced)
# schedule.every().hour.at("08:05").do(self.sendJob)
while not self.is_done:
time.sleep(self.delay)
schedule.run_pending()
print('thread done')