-
Notifications
You must be signed in to change notification settings - Fork 0
/
CanMessage.py
37 lines (28 loc) · 982 Bytes
/
CanMessage.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
import json
import logging
log = logging.getLogger(__name__)
class CanMessage(object):
def __init__(self):
self.last_revid = 0
self.memory_file = 'last_revid.json'
try:
with open(self.memory_file) as file_handle:
self.last_revid = json.load(file_handle)
except Exception as ex:
# TODO: empty file or no file at all, get the proper error
pass
log.debug("initialized with %s as the last revid", self.last_revid)
def can_send(self, revid):
return revid > self.last_revid
def set_last_revid(self, revid):
self.last_revid = revid
def save(self):
log.debug("saving")
with open(self.memory_file, 'w') as file_handle:
json.dump(self.last_revid, file_handle)
if __name__ == '__main__':
can_message = CanMessage()
if can_message.can_send(124):
print('OK')
can_message.set_last_revid(124)
can_message.save()