-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_handler.py
152 lines (124 loc) · 7.24 KB
/
data_handler.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import sys
from typing import List
from apscheduler.job import Job
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
from influxdb import InfluxDBClient
from source.device_manager import device_manager
scheduler = BackgroundScheduler()
scheduler.start()
def save_commands(commands_to_call):
for device_uuid in commands_to_call.keys():
dev_manager = device_manager.DeviceManager()
sila_device = dev_manager.get_device_instance(device_uuid)
sila_device.connect()
device_info = dev_manager.get_device_info(device_uuid)
if device_info.databaseId is not None:
database_info = dev_manager.get_database_info(device_info.databaseId)
for command_info in commands_to_call[device_uuid]:
command = command_info[0]
feature = command_info[1]
parameters = {}
for parameter in command.parameters:
parameters[parameter.identifier.lower() + '/' + parameter.type] = parameter.value
try:
try:
responses = sila_device.call_command(feature_id=feature.identifier, command_id=command.identifier,
parameters=parameters)
except:
print(feature.originator, feature.category, feature.identifier, feature.feature_version_major)
qualified_feature_id = f'{feature.originator}/{feature.category}/{feature.identifier}/v{feature.feature_version_major}'
responses = sila_device.call_command(feature_id=qualified_feature_id,
command_id=command.identifier,
parameters=parameters)
# TODO experiment
# TODO for a device write all points at the end
# TODO timeout call
# TODO meta / non-meta indicator
# TODO decide if we need to do something if responses == {}
# TODO report on different possible exceptions
# TODO check if possible to make these 2 functions into a single one
# TODO EmptyParameters must not be passed; simply pass {} instead
if responses != {}:
client = InfluxDBClient(database_info.address, database_info.port, 'root', 'root',
database_info.name)
client.create_database(database_info.name)
point = {}
tags = {'device': device_uuid, 'feature': feature.identifier, 'command': command.identifier}
point['measurement'] = 'device_manager'
point['tags'] = tags
point['time'] = datetime.now()
point['fields'] = responses
points = [point]
client.write_points(points)
print(responses)
except:
print(sys.exc_info())
else:
print("Device " + device_info.name + " with UUID " + device_uuid + " does not have a database assigned")
def save_properties(properties_to_call):
for device_uuid in properties_to_call.keys():
dev_manager = device_manager.DeviceManager()
sila_device = dev_manager.get_device_instance(device_uuid)
sila_device.connect()
device_info = dev_manager.get_device_info(device_uuid)
if device_info.databaseId is not None:
database_info = dev_manager.get_database_info(device_info.databaseId)
for property_info in properties_to_call[device_uuid]:
property = property_info[0]
feature = property_info[1]
try:
try:
responses = sila_device.call_property(feature_id=feature.identifier,
property_id=property.identifier)
except:
print(feature.originator, feature.category, feature.identifier, feature.feature_version_major)
# qualified_feature_id = feature.originator + '/' + feature.category + '/' + feature.identifier + '/' + feature.feature_version_major
qualified_feature_id = f'{feature.originator}/{feature.category}/{feature.identifier}/v{feature.feature_version_major}'
responses = sila_device.call_property(feature_id=qualified_feature_id,
property_id=property.identifier)
# TODO experiment
# TODO for a device write all points at the end
# TODO timeout call
# TODO meta / non-meta indicator
# TODO decide if we need to do something if responses == {}
# TODO report on different possible exceptions
# TODO check if possible to make these 2 functions into a single one
# TODO EmptyParameters must not be passed; simply pass {} instead
print(feature.identifier, responses)
if responses != {}:
client = InfluxDBClient(database_info.address, database_info.port, 'root', 'root',
database_info.name)
client.create_database(database_info.name)
point = {}
tags = {'device': device_uuid, 'feature': feature.identifier, 'property': property.identifier}
point['measurement'] = 'device_manager'
point['tags'] = tags
point['time'] = datetime.now()
point['fields'] = responses
points = [point]
client.write_points(points)
print(responses)
except:
print(sys.exc_info())
else:
print("Device " + device_info.name + " with UUID " + device_uuid + " does not have a database assigned")
def create_jobs(commands_to_call, properties_to_call) -> List[Job]:
jobs = []
for key in commands_to_call.keys():
job = scheduler.add_job(save_commands,
'interval',
seconds=key[0],
start_date=datetime.fromtimestamp(datetime.timestamp(datetime.now()) + 1),
end_date=datetime.fromtimestamp(key[1]),
args=[commands_to_call[key]])
jobs.append(job)
for key in properties_to_call.keys():
job = scheduler.add_job(save_properties,
'interval',
seconds=key[0],
start_date=datetime.fromtimestamp(datetime.timestamp(datetime.now()) + 1),
end_date=datetime.fromtimestamp(key[1]),
args=[properties_to_call[key]])
jobs.append(job)
return jobs