This repository was archived by the owner on Aug 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnips_timer.py
More file actions
314 lines (288 loc) · 9.85 KB
/
Copy pathsnips_timer.py
File metadata and controls
314 lines (288 loc) · 9.85 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
from pprint import pprint
import json, time, os, io
from dateutil import tz
import datetime
from dateutil import tz
global timers_file
timers_file = './timers.json'
alarms_file = './alarms.json'
def fix_time(time):
values = {'pierwszej': '1',
'pierwszą': '1',
'drugiej ': '2',
'drugą ': '2',
'trzeciej ': '3',
'trzecią ': '3',
'czwartej ': '4',
'czwartą ': '4',
'piątej ': '5',
'piątą ': '5',
'szóstej ': '6',
'szóstą ': '6',
'siódmej ': '7',
'siódmą ': '7',
'ósmej ': '8',
'ósmą ': '8',
'dziewiątej ': '9',
'dziewiątą ': '9',
'dzisiątej ': '10',
'dzisiątą ': '10',
'jedenastej ': '11',
'jedenastą ': '11',
'dwunastej ': '12',
'dwunastą ': '12',
'trzynastej ': '13',
'trzynastą ': '13',
'czternastej ': '14',
'czternastą ': '14',
'piętnastej ': '15',
'piętnastą ': '15',
'szesnastej ': '16',
'szesnastą ': '16',
'siedemnastej ': '17',
'siedemnastą ': '17',
'osiemnastej ': '18',
'osiemnastą ': '18',
'dziewiętnastej ': '19',
'dziewiętnastą ': '19',
'dwudziestej ': '20',
'dwudziestą ': '20',
'dwudziestej pierwszej ': '21',
'dwudzuestą pierwszą ': '21',
'dwudziestej drugiej ': '22',
'dwudzuestą drugą ': '22',
'dwudziestej trzeciej ': '23',
'dwudzuestą trzecią ': '23'}
for search in values:
replace = values[search]
time = time.replace(search, replace + ':')
return time
def call_timer(site_id, total_amount, end_time, target):
os.system('./timer.py ' + site_id + ' ' + str(int(total_amount)) + ' ' + str(end_time) + ' "' + str(target) + '" &')
def call_alarm(site_id, hour, target):
os.system('./timer.py ' + site_id + ' alarm "' + str(hour) + '" "' + str(target) + '" &')
def handle_file(file_path):
try:
fp = open(file_path)
except IOError:
# If not exists, create the file
fp = open(file_path, 'w+')
data = []
json.dump(data, fp)
def check_timers(call = False):
handle_file(timers_file)
with open(timers_file) as json_file:
data = json.load(json_file)
new_data = []
for timer in data:
if int(timer['end_time']) > int(time.time() * 1000):
new_data.append(timer)
if call:
call_timer(timer['site_id'], timer['amount'], timer['end_time'], timer['target'])
with open(timers_file, 'w') as outfile:
json.dump(new_data, outfile)
def check_alarms(call = False):
handle_file(alarms_file)
with open(alarms_file) as json_file:
data = json.load(json_file)
new_data = []
for alarm in data:
if alarm['hour'] is None:
continue
alarm_datetime = datetime.datetime.strptime(alarm['hour'], "%Y-%m-%d %H:%M")
if datetime.datetime.timestamp(alarm_datetime) > datetime.datetime.timestamp(datetime.datetime.now()):
new_data.append(alarm)
if call:
call_alarm(alarm['site_id'], alarm['hour'], alarm['target'])
with open(alarms_file, 'w') as outfile:
json.dump(new_data, outfile)
def add_timer(site_id, amount, end_time, target):
handle_file(timers_file)
with open(timers_file) as json_file:
data = json.load(json_file)
data_json = {}
data_json["site_id"] = site_id
data_json["amount"] = amount
data_json["end_time"] = end_time
data_json["target"] = target
data.append(data_json)
with open(timers_file, 'w') as outfile:
json.dump(data, outfile)
def add_alarm(site_id, hour, target):
handle_file(alarms_file)
with open(alarms_file) as json_file:
data = json.load(json_file)
data_json = {}
data_json["site_id"] = site_id
data_json["hour"] = hour
data_json["target"] = target
data.append(data_json)
with open(alarms_file, 'w') as outfile:
json.dump(data, outfile)
def remove_timer(site_id, amount, end_time, target):
handle_file(timers_file)
with open(timers_file) as json_file:
data = json.load(json_file)
new_data = []
for timer in data:
if site_id != timer['site_id'] and amount != timer['amount'] and end_time != timer['end_time']:
new_data.append(timer)
with open(timers_file, 'w') as outfile:
json.dump(new_data, outfile)
def remove_alarm(site_id, hour, target):
handle_file(alarms_file)
with open(alarms_file) as json_file:
data = json.load(json_file)
new_data = []
for alarm in data:
if site_id != alarm['site_id'] and hour != alarm['hour']:
new_data.append(alarm)
with open(alarms_file, 'w') as outfile:
json.dump(new_data, outfile)
def get_intent_amount(x):
if isinstance(x, int):
return x
else:
try:
return float(x.replace(" i pół", ".5").replace(",", "."))
except ValueError:
return {
"pół": 0.5,
"jedną": 1,
"dwie": 2,
"półtorej": 1.5,
"jedną i pół": 1.5,
"dwie i pół" : 2.5,
"trzy i pół" : 3.5,
"cztery i pół" : 4.5,
"pięć i pół" : 5.5,
"sześć i pół" : 6.5,
"siedem i pół" : 7.5,
"osiem i pół" : 8.5,
"dziewięć i pół": 9.5,
"dziesięć i pół": 10.5
}.get(x, x)
def get_intent_slots(intent_message):
slots_count = len(intent_message.slots.intent_slot)
slots = []
for x in range(slots_count):
slots.append(intent_message.slots.intent_slot[x].slot_value.value.value)
return slots
def get_time_units(intent_message):
slots_count = len(intent_message.slots.time_unit)
slots = []
for x in range(slots_count):
slots.append(intent_message.slots.time_unit[x].slot_value.value.value)
return slots
def get_locations(intent_message):
slots = []
if (intent_message.slots is None):
return slots
slots_count = len(intent_message.slots.location)
object = intent_message.slots
object_methods = [method_name for method_name in dir(object)
if callable(getattr(object, method_name))]
# pprint(object_methods)
# pprint(intent_message.slots.items())
# pprint(intent_message.slots.values())
for x in range(slots_count):
slots.append(intent_message.slots.location[x].slot_value.value.value)
return slots
def get_hours(intent_message):
slots = []
if (intent_message.slots is None):
return slots
slots_count = len(intent_message.slots.hour)
for x in range(slots_count):
slots.append(intent_message.slots.hour[x].slot_value.value.value)
return slots
def get_targets(intent_message):
slots = []
if (intent_message.slots is None):
return slots
slots_count = len(intent_message.slots.time_target)
for x in range(slots_count):
slots.append(intent_message.slots.time_target[x].slot_value.value.value)
return slots
def get_unit_multiplier(unit):
return {
"second": 1,
"minute": 60,
"hour": 3600,
"day" : 86400
}.get(unit, 1)
def format_unit_days(amount):
return {
1: "dzień"
}.get(amount, "dni")
def format_unit_hour(amount):
return {
1: "godzina",
2: "godziny",
3: "godziny",
4: "godziny"
}.get(amount, "godzin")
def format_unit_minutes(amount):
return {
1: "minuta",
2: "minuty",
3: "minuty",
4: "minuty"
}.get(amount, "minut")
def format_unit_seconds(amount):
return {
1: "sekunda",
2: "sekundy",
3: "sekundy",
4: "sekundy"
}.get(amount, "sekund")
def format_amount(amount):
return {
1: "jedna",
2: "dwie"
}.get(amount, str(amount))
def get_amount_say(amount):
days = int(amount // 86400)
amount = amount % 86400
hours = int(amount // 3600)
amount = amount % 3600
minutes = int(amount // 60)
amount = amount % 60
seconds = int(amount)
# print(days)
# print(hours)
# print(minutes)
# print(seconds)
amount_say = []
if days > 0:
amount_say.append(str(days) + "- " + format_unit_days(days))
if hours > 0:
amount_say.append(format_amount(hours) + "- " + format_unit_hour(hours))
if minutes > 0:
amount_say.append(format_amount(minutes) + "- " + format_unit_minutes(minutes))
if seconds > 0:
amount_say.append(format_amount(seconds) + "- " + format_unit_seconds(seconds))
return amount_say
def get_amount_say_string(amount):
amount_say = get_amount_say(amount)
text_all = ""
for num, text in enumerate(amount_say, start=1):
if num == 1:
text_all = text_all + text
elif num != len(amount_say):
text_all = text_all + ", " + text
else:
text_all = text_all + ", " + text
return text_all
def get_local_datetime(utc_time = None, format = "%Y-%m-%d %H:%M:%S"):
if utc_time is None:
utc_time = time.gmtime()
utc_time_h_m = time.strftime("%Y-%m-%d %H:%M:%S+00:00", utc_time)
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
utc = datetime.datetime.strptime(utc_time_h_m, "%Y-%m-%d %H:%M:%S+00:00")
utc = utc.replace(tzinfo=from_zone)
local = utc.astimezone(to_zone)
return_time = local.strftime(format)
# print(return_time)
return return_time