fine-tuning for example app

This commit is contained in:
Ronald Steinke 2018-01-10 10:03:05 +01:00
parent 8e6145d1e9
commit ffab3c44e5

View File

@ -16,13 +16,14 @@ class DataAggregation(XAE):
self.sensor_register = {} self.sensor_register = {}
self.dev_cnt_list = [] self.dev_cnt_list = []
# start endless loop # start endless loop
self.periodic_discover(self.remote_cse, self.periodic_discover(
{'labels': ["openmtc:sensor_data"]}, self.remote_cse,
self.period, self.handle_discovery_sensor) {
'labels': ['openmtc:sensor_data'],
@staticmethod },
def _time(): self.period,
return format(round(time.time(), 3), '.3f') self.handle_discovery_sensor
)
def handle_discovery_sensor(self, discovery): def handle_discovery_sensor(self, discovery):
for uri in discovery: for uri in discovery:
@ -54,7 +55,7 @@ class DataAggregation(XAE):
def handle_sensor(self, container, content): def handle_sensor(self, container, content):
sensor_entry = self.sensor_register[container] sensor_entry = self.sensor_register[container]
values = sensor_entry['values'] values = sensor_entry['values']
try : try:
values.append(content[0]['v']) values.append(content[0]['v'])
except KeyError: except KeyError:
return return
@ -64,35 +65,26 @@ class DataAggregation(XAE):
except KeyError: except KeyError:
self.create_sensor_structure(sensor_entry, content) self.create_sensor_structure(sensor_entry, content)
num_items = len(values)
# mean value # mean value
mean = sum(values) / len(values) mean = sum(values) / num_items
data = [{ self.push_content(sensor_entry['mean_cnt'], [{
'bn': content[0]['bn'], 'bn': content[0]['bn'],
'n': content[0]['n'] + '_mean', 'n': content[0]['n'] + '_mean',
'v': mean, 'v': mean,
't': self._time() 't': '%.3f' % time.time(),
}] 'u': content[0].get('u'),
}])
# Standard_deviation value # Standard_deviation value
num_item = len(values) sd = sqrt(sum([(value - mean) ** 4 for value in values]) / num_items)
standard_mean = sum(values) / num_item self.push_content(sensor_entry['deviation_cnt'], [{
differences = [((x - standard_mean) ** 2) ** 2 for x in values]
ssd = sum(differences)
variance = ssd / num_item
sd = sqrt(variance)
print sd
deviation_data = [{
'bn': content[0]['bn'], 'bn': content[0]['bn'],
'n': content[0]['n'] + '_Standard_deviation', 'n': content[0]['n'] + '_Standard_deviation',
'v': sd, 'v': sd,
't': self._time() 't': '%.3f' % time.time(),
}] 'u': content[0].get('u'),
try: }])
data[0]['u'] = content[0]['u']
except KeyError:
pass
self.push_content(sensor_entry['mean_cnt'], data)
self.push_content(sensor_entry['deviation_cnt'], deviation_data)
if __name__ == "__main__": if __name__ == "__main__":