1 from command import Command
2 from deviceinfo import DeviceInfo
3 from threading import Lock
5 from jsonpickle import encode, decode
6 from logger import cloudlog
9 def _get_device_name(commandId):
10 (deviceId, commandNo) = commandId.split(" ")
17 def __init__(self, clouddata_filename="data/clouddata",
18 commands_filename="data/commands"):
22 self._load_data(clouddata_filename, commands_filename)
24 def get_commands(self, deviceId):
25 if(deviceId not in self.devices_dict):
26 self.register_device(deviceId)
27 return self.devices_dict[deviceId].get_prepared_commands()
29 def enqueue(self, deviceId, ctype, ccontent, source_device=""):
31 Command(deviceId + " " + str(self._get_id()), ctype, ccontent, source_device))
33 def process_report(self, dict_json):
34 self._update_command(dict_json)
36 devicename = _get_device_name(dict_json["commandId"])
37 cloudlog("Got report from " + devicename)
39 cloudlog("No commandId in JSON", "Error")
41 def register_device(self, deviceId):
42 if(deviceId in self.devices_dict):
43 cloudlog("Device " + deviceId + " already registered")
45 cloudlog("Registering device " + deviceId)
46 self.devices_dict[deviceId] = DeviceInfo(deviceId)
48 def _load_data(self, clouddata_filename, commands_filename):
49 self.clouddata_filename = clouddata_filename
50 self.commands_filename = commands_filename
51 cloudlog("Opening " + clouddata_filename)
53 self.file = open(clouddata_filename, "r")
58 " found - creating default",
60 self.file = open(clouddata_filename, "w+")
62 self.file.write("maxid=" + str(self._maxid).zfill(self.zwidth))
65 m = regex.search('^maxid=(\d+)$', self.file.readline())
67 self._maxid = int(m.group(1))
68 except AttributeError:
70 "%s corrupted. Maxid=1" %
71 self.clouddata_filename, "Error")
73 self.devices_dict = {}
74 cloudlog("Loading data")
75 cloudlog("Opening " + commands_filename)
77 self.file = open(commands_filename, 'r')
80 "No commands file %s - generating new" %
81 commands_filename, "Warning")
83 for line in self.file:
86 deviceId = _get_device_name(cmd.id)
87 if(deviceId not in self.devices_dict):
88 self.register_device(deviceId)
89 device = self._get_device(deviceId)
90 device.add_replace_command(cmd)
93 "%s is corrupted - stopped reading" %
94 self.commands_filename, "Error")
101 self._maxid = self._maxid + 1
102 self.file = open(self.clouddata_filename, "w+")
104 "maxid=" + str(self._maxid).zfill(self.zwidth) + "\n")
108 self.idlock.release()
111 def _queue_command(self, command):
112 devicename = _get_device_name(command.id)
113 if(devicename not in self.devices_dict):
114 self.register_device(devicename)
115 device = self._get_device(devicename)
116 self._save_command(command)
117 device.add_command(command)
118 cloudlog("Enqueued command " + command.id + " on " + devicename)
120 def _update_command(self, dict_json):
122 self.cmdlock.acquire()
124 commandId = dict_json["commandId"]
125 cloudlog("Updating command %s" % commandId)
126 device = self._get_device_from_command(commandId)
127 command = device.pop_command(commandId)
128 command.state = dict_json["state"]
129 self._save_command(command)
130 device.add_command(command)
131 cloudlog("Updated %s" % commandId, "Info")
133 cloudlog("_update_command: Wrong Fromat of JSON", "Error")
136 self.cmdlock.release()
138 def _get_device_from_command(self, commandId):
139 return self._get_device(_get_device_name(commandId))
141 def _get_device(self, deviceId):
143 device = self.devices_dict[deviceId]
146 cloudlog("_get_device: No device " + deviceId + " found", "Error")
149 def _save_command(self, command):
150 self.file = open(self.commands_filename, 'a+')
152 self.file.write(encode(command) + "\n")