-
Notifications
You must be signed in to change notification settings - Fork 3
/
TagActor.py
44 lines (35 loc) · 1.3 KB
/
TagActor.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
import pykka, logging, json
TAGS_FILE = 'data/tags.json'
class TagActor(pykka.ThreadingActor):
def __init__(self, stateActor):
super(TagActor, self).__init__()
self.stateActor = stateActor
def playByTag(self, tag, fromStart=False):
try:
tags = self.loadTags()
self.stateActor.playFromLastState(tags[tag], fromStart)
except KeyError:
logging.getLogger('zbap').error('No such tag %s' % tag)
def addTag(self, name):
tag = self.getTagActor().getTag().get()
if tag != None:
tags = self.loadTags()
tags[tag] = name
self.saveTags(tags)
def removeTag(self, tag):
tags = self.loadTags()
del tags[tag]
self.saveTags(tags)
def getTagActor(self):
return pykka.ActorRegistry.get_by_class_name("NfcActor")[0].proxy()
def loadTags(self):
try:
with open(TAGS_FILE, 'r') as tagsFile:
return json.load(tagsFile)
except (IOError, ValueError) as e:
logging.getLogger('zbap').error('Unable to load tag file %s' % TAGS_FILE)
logging.getLogger('zbap').exception(e)
return {}
def saveTags(self, state):
with open(TAGS_FILE, 'w') as tagsFile:
json.dump(state, tagsFile)