Added tools - mockup of cloud and injector. Changed code to support task-worker.
[apps/native/tizen-things-daemon.git] / tools / CloudMockup / clouddata.py
1 from command import Command
2 from deviceinfo import DeviceInfo
3 from threading import Lock
4 import regex
5 from jsonpickle import encode, decode
6 from logger import cloudlog
7
8
9 def _get_device_name(commandId):
10     (deviceId, commandNo) = commandId.split(" ")
11     return deviceId
12
13
14 class CloudData:
15     'TTD cloud data'
16
17     def __init__(self, clouddata_filename="data/clouddata",
18                  commands_filename="data/commands"):
19         self.idlock = Lock()
20         self.cmdlock = Lock()
21         self.zwidth = 7
22         self._load_data(clouddata_filename, commands_filename)
23
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()
28
29     def enqueue(self, deviceId, ctype, ccontent, source_device=""):
30         self._queue_command(
31             Command(deviceId + " " + str(self._get_id()), ctype, ccontent, source_device))
32
33     def process_report(self, dict_json):
34         self._update_command(dict_json)
35         try:
36             devicename = _get_device_name(dict_json["commandId"])
37             cloudlog("Got report from " + devicename)
38         except KeyError:
39             cloudlog("No commandId in JSON", "Error")
40
41     def register_device(self, deviceId):
42         if(deviceId in self.devices_dict):
43             cloudlog("Device " + deviceId + " already registered")
44         else:
45             cloudlog("Registering device " + deviceId)
46             self.devices_dict[deviceId] = DeviceInfo(deviceId)
47
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)
52         try:
53             self.file = open(clouddata_filename, "r")
54         except BaseException:
55             cloudlog(
56                 "No file " +
57                 clouddata_filename +
58                 " found - creating default",
59                 "Warning")
60             self.file = open(clouddata_filename, "w+")
61             self._maxid = 1
62             self.file.write("maxid=" + str(self._maxid).zfill(self.zwidth))
63             self.file.close()
64             return
65         m = regex.search('^maxid=(\d+)$', self.file.readline())
66         try:
67             self._maxid = int(m.group(1))
68         except AttributeError:
69             cloudlog(
70                 "%s corrupted. Maxid=1" %
71                 self.clouddata_filename, "Error")
72             self._maxid = 1
73         self.devices_dict = {}
74         cloudlog("Loading data")
75         cloudlog("Opening " + commands_filename)
76         try:
77             self.file = open(commands_filename, 'r')
78         except BaseException:
79             cloudlog(
80                 "No commands file %s - generating new" %
81                 commands_filename, "Warning")
82             return
83         for line in self.file:
84             try:
85                 cmd = decode(line)
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)
91             except BaseException:
92                 cloudlog(
93                     "%s is corrupted - stopped reading" %
94                     self.commands_filename, "Error")
95                 break
96         self.file.close()
97
98     def _get_id(self):
99         self.idlock.acquire()
100         try:
101             self._maxid = self._maxid + 1
102             self.file = open(self.clouddata_filename, "w+")
103             self.file.write(
104                 "maxid=" + str(self._maxid).zfill(self.zwidth) + "\n")
105             self.file.close()
106             _id = self._maxid
107         finally:
108             self.idlock.release()
109         return _id
110
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)
119
120     def _update_command(self, dict_json):
121         commandId = ""
122         self.cmdlock.acquire()
123         try:
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")
132         except KeyError:
133             cloudlog("_update_command: Wrong Fromat of JSON", "Error")
134             raise
135         finally:
136             self.cmdlock.release()
137
138     def _get_device_from_command(self, commandId):
139         return self._get_device(_get_device_name(commandId))
140
141     def _get_device(self, deviceId):
142         try:
143             device = self.devices_dict[deviceId]
144             return device
145         except KeyError:
146             cloudlog("_get_device: No device " + deviceId + " found", "Error")
147             raise
148
149     def _save_command(self, command):
150         self.file = open(self.commands_filename, 'a+')
151         self.file.seek(0, 2)
152         self.file.write(encode(command) + "\n")
153         self.file.close()