-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutils.py
More file actions
78 lines (59 loc) · 1.85 KB
/
utils.py
File metadata and controls
78 lines (59 loc) · 1.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
import json
import uuid
from redis import Redis
import config
store = None
def format_value(value, split=False):
result = str(int(value)/100.0)
if split:
return result.split('.')
return result
def get_store():
global store
if store is None:
store = Redis(host=config.REDIS_HOST, port=config.REDIS_PORT, db=config.REDIS_DB)
return store
def time_in_range(start, end, check_time):
if start <= end:
return start <= check_time <= end
else:
return start <= check_time or check_time <= end
class Notifications:
STORE_KEY = 'mihome_notifications'
store = get_store()
@staticmethod
def list():
stored = Notifications.store.get(Notifications.STORE_KEY)
return json.loads(stored.decode() if stored else '[]')
@staticmethod
def add(type, title, text):
from web.w import UpdatesHandler
notifications = Notifications.list()
id = str(uuid.uuid4())
notifications.append({
'uuid': id,
'type': type,
'title': title,
'text': text
})
Notifications.store.set(Notifications.STORE_KEY, json.dumps(notifications))
notifications[-1].update(kind='notification', command='show')
UpdatesHandler.send_updates(notifications[-1])
return id
@staticmethod
def remove(uuid):
from web.w import UpdatesHandler
notifications = Notifications.list()
for item in notifications[:]:
if item['uuid'] == uuid:
notifications.remove(item)
break
else:
return
Notifications.store.set(Notifications.STORE_KEY, json.dumps(notifications))
data = {
'kind': 'notification',
'command': 'remove',
'uuid': uuid
}
UpdatesHandler.send_updates(data)