Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetropolisControlSystem.py
More file actions
Latest commit
47 lines (40 loc) · 1.66 KB
/
Copy pathMetropolisControlSystem.py
File metadata and controls
47 lines (40 loc) · 1.66 KB
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
importjson
fromdatetimeimportdatetime
fromthreadingimportThread
importrequests
importsys
fromkafkaimportKafkaConsumer
fromMetropolisStorage.StorageimportStorage
classMetropolisControlSystem(Thread):
def__init__(self, name, kafka_server, storage):
"""
This class will be the handler of the control requests
coming from the data processing system
:param name: string, name of the control unit
:param kafka_server: string, kafka server address
:param storage: MetropolisStorage.Storage
"""
Thread.__init__(self)
self._name=name
self._server=kafka_server
self._storage=storage
self._consumer=None
definitialize(self):
try:
self._consumer=KafkaConsumer(self._name, bootstrap_servers=self._server)
except:
self._consumer=None
returnself._consumer
defrun(self):
ifself._consumerisnotNone:
formsginself._consumer:
# convert the message as a json object to get the id attribute
jsonlamp=json.loads(str(msg.value, 'utf-8'), encoding='utf-8')
# for debug purposes ... TODO remove in production
# print(json.loads(str(msg.value)))
# get the ip address linked to the given id
ip_addr=self._storage.control().get_object(int(jsonlamp["id"]))
ifint(jsonlamp["id"]) ==100:
print("Returned:", datetime.now().timestamp())
# # send the message to the rightful lamp
requests.get("http://"+str(ip_addr), msg)