-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontext.py
209 lines (169 loc) · 5.51 KB
/
context.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
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
"""
Mark Liebrand 2024
This file is part of FindMyGUI which is released under the Apache 2.0 License
See file LICENSE or go to for full license details https://github.com/liebrandapps/FindMyGUI
"""
import json
import os
import threading
from datetime import datetime
from os.path import exists
class Context:
statusFile = "./data/findMyGUI.json"
def __init__(self, cfg, log):
self.__log = log
self.__cfg = cfg
self.__threadMonitor = {}
self.startTime = datetime.now()
self.__airtags = {}
self._signInDone = False
self._requestCreds = 0
self._requestAuth = 0
self._userName = ""
self._password = ""
self._ndFactor = ""
self._errMsg = ""
self._lastLocationUpdate = 0
self._usedReports = 0
self._privKey = None
self._mqtt = None
self._aesKeys = {}
self._automaticUpdatePossible = False
self._queryInProgress = False
def load(self):
if exists(Context.statusFile):
with open(Context.statusFile) as f:
dta = json.load(f)
self._lastLocationUpdate = dta['lastLocationUpdate']
if 'usedReports' in dta:
self._usedReports = dta['usedReports']
if 'privKey' in dta:
self._privKey = dta['privKey']
if 'automaticUpdatesPossible' in dta:
self._automaticUpdatePossible = dta['automaticUpdatesPossible']
def save(self):
j = {"lastLocationUpdate": self._lastLocationUpdate, "usedReports": self._usedReports,
"privKey": self._privKey, "automaticUpdatesPossible": self._automaticUpdatePossible}
if exists(Context.statusFile):
os.rename(Context.statusFile, Context.statusFile + ".bak")
with open(Context.statusFile, 'w') as f:
print(json.dumps(j, indent=4), file=f)
@property
def log(self):
return self.__log
@property
def cfg(self):
return self.__cfg
@property
def airtags(self):
return self.__airtags
@property
def privKey(self):
return self._privKey
@privKey.setter
def privKey(self, value):
self._privKey = value
@property
def mqtt(self):
return self.__mqtt
@mqtt.setter
def mqtt(self, value):
self.__mqtt = value
@property
def aesKeys(self):
return self._aesKeys
@property
def queryInProgress(self):
return self._queryInProgress
@queryInProgress.setter
def queryInProgress(self, value):
self._queryInProgress = value
@property
def threadMonitor(self):
return self.__threadMonitor
def checkThreads(self, now):
missing = []
for k in self.__threadMonitor.keys():
if (now - self.__threadMonitor[k][0]).seconds > 900:
# thread has not updated since 15 minutes
self.__log.warn("[CTX] Thread for class %s has not sent an alive message for %d seconds" %
(k, (now - self.__threadMonitor[k][0]).seconds))
missing.append(self.__threadMonitor[k])
return missing
def uptime(self, now):
days = (now - self.startTime).days
secs = (now - self.startTime).seconds
hours = int((secs % 86400) / 3600)
minutes = int((secs % 3600) / 60)
seconds = int(secs % 60)
up = ""
if days > 0:
up += str(days) + " " + (days == 1 and "day" or "days") + ", "
if len(up) > 0 or hours > 0:
up += str(hours) + " " + (hours == 1 and "hour" or "hours") + ", "
if len(up) > 0 or minutes > 0:
up += str(minutes) + " " + (minutes == 1 and "minute" or "minutes") + ", "
up += str(seconds) + " " + (seconds == 1 and "second" or "seconds")
return up
@property
def requestCreds(self):
return self._requestCreds
@requestCreds.setter
def requestCreds(self, value):
self._requestCreds = value
@property
def requestAuth(self):
return self._requestAuth
@requestAuth.setter
def requestAuth(self, value):
self._requestAuth = value
@property
def signInDone(self):
return self._signInDone
@signInDone.setter
def signInDone(self, value):
self._signInDone = value
@property
def userName(self):
return self._userName
@userName.setter
def userName(self, value):
self._userName = value
@property
def password(self):
return self._password
@password.setter
def password(self, value):
self._password = value
@property
def ndFactor(self):
return self._ndFactor
@ndFactor.setter
def ndFactor(self, value):
self._ndFactor = value
@property
def errMsg(self):
return self._errMsg
@errMsg.setter
def errMsg(self, value):
self._errMsg = value
@property
def lastLocationUpdate(self):
return self._lastLocationUpdate
@lastLocationUpdate.setter
def lastLocationUpdate(self, value):
self._lastLocationUpdate = value
self.save()
@property
def usedReports(self):
return self._usedReports
@usedReports.setter
def usedReports(self, value):
self._usedReports = value
self.save()
@property
def automaticUpdatePossible(self):
return self._automaticUpdatePossible
@automaticUpdatePossible.setter
def automaticUpdatesPossible(self, value):
self._automaticUpdatePossible = value