mirror of
https://github.com/OpenMTC/OpenMTC.git
synced 2025-06-18 14:28:09 +00:00
initial commit
This commit is contained in:
46
doc/example-apps/IoT-data-visualization.py
Normal file
46
doc/example-apps/IoT-data-visualization.py
Normal file
@ -0,0 +1,46 @@
|
||||
|
||||
import urllib
|
||||
from openmtc_app.onem2m import XAE
|
||||
import uuid
|
||||
|
||||
|
||||
class DataVisualization(XAE):
|
||||
remove_registration = True
|
||||
remote_cse = '/mn-cse-1/onem2m'
|
||||
period = 10
|
||||
|
||||
def _on_register(self):
|
||||
# init variables
|
||||
self.sensor_register = {}
|
||||
self.sensor_register = []
|
||||
self.sensor_values = []
|
||||
self.name = uuid.uuid1()
|
||||
self.things_name = urllib.urlopen("https://dweet.io/follow/%s" % self.name)
|
||||
print "Thing name :", self.name
|
||||
print "link for the current data type and values :", self.things_name.geturl()
|
||||
# start endless loop
|
||||
self.periodic_discover(self.remote_cse,
|
||||
{'labels': ["openmtc:sensor_data"]},
|
||||
self.period, self.handle_discovery_sensor)
|
||||
|
||||
def handle_discovery_sensor(self, discovery):
|
||||
for uri in discovery:
|
||||
self.add_container_subscription(uri, self.handle_sensor_data)
|
||||
|
||||
def handle_sensor_data(self, container, content):
|
||||
data ={}
|
||||
self.sensor_register.append(content[0]['n'])
|
||||
self.sensor_values.append(content[0]['v'])
|
||||
for i, k in zip(self.sensor_register , self.sensor_values):
|
||||
data.update({i: k})
|
||||
params = urllib.urlencode(data)
|
||||
urllib.urlopen("https://dweet.io/dweet/for/%s?%s" % (self.name, params))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from openmtc_app.flask_runner import SimpleFlaskRunner as Runner
|
||||
|
||||
ep = "http://localhost:8000"
|
||||
Runner(DataVisualization(), port=6050, host='auto').run(ep)
|
||||
|
||||
|
102
doc/example-apps/data-aggregation.py
Normal file
102
doc/example-apps/data-aggregation.py
Normal file
@ -0,0 +1,102 @@
|
||||
import time
|
||||
from collections import deque
|
||||
from math import sqrt
|
||||
|
||||
from openmtc_app.onem2m import XAE
|
||||
from openmtc_onem2m.model import Container
|
||||
|
||||
|
||||
class DataAggregation(XAE):
|
||||
remove_registration = True
|
||||
remote_cse = '/mn-cse-1/onem2m'
|
||||
period = 10
|
||||
|
||||
def _on_register(self):
|
||||
# init variables
|
||||
self.sensor_register = {}
|
||||
self.dev_cnt_list = []
|
||||
# start endless loop
|
||||
self.periodic_discover(self.remote_cse,
|
||||
{'labels': ["openmtc:sensor_data"]},
|
||||
self.period, self.handle_discovery_sensor)
|
||||
|
||||
@staticmethod
|
||||
def _time():
|
||||
return format(round(time.time(), 3), '.3f')
|
||||
|
||||
def handle_discovery_sensor(self, discovery):
|
||||
for uri in discovery:
|
||||
self.sensor_register[uri] = {
|
||||
'values': deque([], 10)
|
||||
}
|
||||
content = self.get_content(uri)
|
||||
if content:
|
||||
self.handle_sensor(uri, content)
|
||||
self.add_container_subscription(uri, self.handle_sensor)
|
||||
|
||||
def create_sensor_structure(self, sensor_entry, content):
|
||||
# dev_cnt
|
||||
cnt_name = '_'.join(content[0]['bn'].split(':')[2:])
|
||||
cnt_name += '_' + content[0]['n']
|
||||
dev_cnt = Container(resourceName=cnt_name)
|
||||
if dev_cnt not in self.dev_cnt_list:
|
||||
sensor_entry['dev_cnt'] = dev_cnt = self.create_container(None, dev_cnt)
|
||||
# mean cnt
|
||||
mean_cnt = Container(resourceName='mean', labels=["openmtc:mean_data"])
|
||||
sensor_entry['mean_cnt'] = self.create_container(dev_cnt, mean_cnt)
|
||||
# Standard_deviation cnt
|
||||
deviation_cnt = Container(resourceName='Standard_deviation', labels=["openmtc:Standard_deviation_data"])
|
||||
sensor_entry['deviation_cnt'] = self.create_container(dev_cnt, deviation_cnt)
|
||||
self.dev_cnt_list.append(dev_cnt)
|
||||
else:
|
||||
return dev_cnt,"already exists "
|
||||
|
||||
def handle_sensor(self, container, content):
|
||||
sensor_entry = self.sensor_register[container]
|
||||
values = sensor_entry['values']
|
||||
try :
|
||||
values.append(content[0]['v'])
|
||||
except KeyError:
|
||||
return
|
||||
# check if container exists
|
||||
try:
|
||||
sensor_entry['dev_cnt']
|
||||
except KeyError:
|
||||
self.create_sensor_structure(sensor_entry, content)
|
||||
|
||||
# mean value
|
||||
mean = sum(values) / len(values)
|
||||
data = [{
|
||||
'bn': content[0]['bn'],
|
||||
'n': content[0]['n'] + '_mean',
|
||||
'v': mean,
|
||||
't': self._time()
|
||||
}]
|
||||
|
||||
# Standard_deviation value
|
||||
num_item = len(values)
|
||||
standard_mean = sum(values) / num_item
|
||||
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'],
|
||||
'n': content[0]['n'] + '_Standard_deviation',
|
||||
'v': sd,
|
||||
't': self._time()
|
||||
}]
|
||||
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__":
|
||||
from openmtc_app.flask_runner import SimpleFlaskRunner as Runner
|
||||
|
||||
ep = "http://localhost:8000"
|
||||
Runner(DataAggregation(), port=6050, host='auto').run(ep)
|
22
doc/example-apps/simple-decision-2.py
Normal file
22
doc/example-apps/simple-decision-2.py
Normal file
@ -0,0 +1,22 @@
|
||||
from openmtc_app.onem2m import XAE
|
||||
|
||||
|
||||
class SimpleDecision2(XAE):
|
||||
remove_registration = True
|
||||
sensor = "onem2m/zigbeeipe-0/devices/ZBS122009491/sensor_data/brightness"
|
||||
actuator = "onem2m/cul868ipe-0/FS20_ST3_16108_1/Switch"
|
||||
|
||||
def _on_register(self):
|
||||
|
||||
def handle_brightness(container, content):
|
||||
command = "ON" if content[0]['v'] < 100.0 else "OFF"
|
||||
self.push_content(self.actuator, command)
|
||||
|
||||
self.add_container_subscription(self.sensor, handle_brightness)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from openmtc_app.flask_runner import SimpleFlaskRunner as Runner
|
||||
|
||||
ep = "http://localhost:8000"
|
||||
Runner(SimpleDecision2(), port=6050, host='auto').run(ep)
|
50
doc/example-apps/simple-decision.py
Normal file
50
doc/example-apps/simple-decision.py
Normal file
@ -0,0 +1,50 @@
|
||||
from openmtc_app.onem2m import XAE
|
||||
|
||||
|
||||
class SimpleDecision(XAE):
|
||||
remove_registration = True
|
||||
remote_cse = '/mn-cse-1/onem2m'
|
||||
period = 10
|
||||
|
||||
def _on_register(self):
|
||||
# init variables
|
||||
self.switchContainers = []
|
||||
# start endless loop
|
||||
self.periodic_discover(self.remote_cse,
|
||||
{'labels': ["openmtc:actuator_data"]},
|
||||
self.period, self.handle_discovery_switch)
|
||||
self.periodic_discover(self.remote_cse,
|
||||
{'labels': ["openmtc:sensor_data:command"]},
|
||||
self.period, self.handle_discovery_command)
|
||||
self.periodic_discover(self.remote_cse,
|
||||
{'labels': ["openmtc:sensor_data:brightness"]},
|
||||
self.period, self.handle_discovery_brightness)
|
||||
|
||||
def handle_discovery_switch(self, discovery):
|
||||
for uri in discovery:
|
||||
self.switchContainers.append(uri)
|
||||
|
||||
def handle_discovery_command(self, discovery):
|
||||
for uri in discovery:
|
||||
self.add_container_subscription(uri, self.handle_command)
|
||||
|
||||
def handle_discovery_brightness(self, discovery):
|
||||
for uri in discovery:
|
||||
self.add_container_subscription(uri, self.handle_brightness)
|
||||
|
||||
def handle_command(self, container, content):
|
||||
command = "ON" if content[0]['v'] == 1 else "OFF"
|
||||
for switch in self.switchContainers:
|
||||
self.push_content(switch, command)
|
||||
|
||||
def handle_brightness(self, container, content):
|
||||
command = "ON" if content[0]['v'] < 500.0 else "OFF"
|
||||
for switch in self.switchContainers:
|
||||
self.push_content(switch, command)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from openmtc_app.flask_runner import SimpleFlaskRunner as Runner
|
||||
|
||||
ep = "http://localhost:8000"
|
||||
Runner(SimpleDecision(), port=6050, host='auto').run(ep)
|
35
doc/example-apps/start-simple-app
Executable file
35
doc/example-apps/start-simple-app
Executable file
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
base_path=$(dirname "$(readlink -f "${0}")")
|
||||
|
||||
################################################################################
|
||||
# set app_file
|
||||
declare -a app_array
|
||||
|
||||
app_array=($(find ${base_path} -name "*.py"))
|
||||
array_length=${#app_array[@]}
|
||||
|
||||
# print possibilities
|
||||
for i in $(seq 1 ${array_length}); do
|
||||
path=${app_array[$[${i}-1]]}
|
||||
echo "[${i}] $(basename ${path})"
|
||||
done
|
||||
|
||||
# read choice
|
||||
while true; do
|
||||
read -n 2 -p "Choose the app to start: " choice
|
||||
|
||||
[[ ${choice} =~ ^[0-9]+$ ]] && \
|
||||
[ ${choice} -gt 0 -a ${choice} -le ${array_length} ] && \
|
||||
echo && break
|
||||
|
||||
echo " Wrong choice. Do it again."
|
||||
done
|
||||
|
||||
app_file=${app_array[$[${choice}-1]]}
|
||||
|
||||
################################################################################
|
||||
# run app_file
|
||||
cd ${base_path}
|
||||
. ../../common/prep-env.sh
|
||||
python ${app_file}
|
Reference in New Issue
Block a user