This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
simulated_graphite_temp.py
126 lines (109 loc) · 5.61 KB
/
simulated_graphite_temp.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
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------#
# Copyright © 2015-2016 VMware, Inc. All Rights Reserved. #
# #
# Licensed under the BSD 2-Clause License (the “License”); you may not use #
# this file except in compliance with the License. #
# #
# The BSD 2-Clause License #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions are met:#
# #
# - Redistributions of source code must retain the above copyright notice, #
# this list of conditions and the following disclaimer. #
# #
# - Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in the #
# documentation and/or other materials provided with the distribution. #
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"#
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE #
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE #
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE #
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR #
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF #
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS #
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN #
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) #
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF #
# THE POSSIBILITY OF SUCH DAMAGE. #
# ----------------------------------------------------------------------------#
from pint import UnitRegistry
from temperusb import TemperHandler
from liota.dcc_comms.socket_comms import SocketDccComms
from liota.dccs.graphite import Graphite
from liota.entities.metrics.metric import Metric
from liota.entities.edge_systems.dk300_edge_system import Dk300EdgeSystem
from liota.lib.utilities.utility import read_user_config
# getting values from conf file
config = read_user_config('sampleProp.conf')
ureg = UnitRegistry()
quantity = ureg.Quantity
# ---------------------------------------------------------------------------
# Below we showcase how easily SI units can be used in the code with help of
# Pint library which is part of Liota and how easily conversion of the units
# can be done such as Celsius to Fahrenheit or Kelvin as shown below with
# help of this library.
def getTemp():
th = TemperHandler()
devs = th.get_devices()
t = devs[0].get_temperatures()
temp = quantity(t[0]['temperature_c'], ureg.degC)
return temp
def getTempDegC():
temp_degC = getTemp()
return temp_degC.magnitude
def getTempDegF():
temp = getTemp()
temp_degF = temp.to('degF')
return temp_degF.magnitude
def getTempKelvin():
temp = getTemp()
temp_kelvin = temp.to('kelvin')
return temp_kelvin.magnitude
# --------------------------------------------------------------------------
# In this example, we demonstrate how data from a simulated device generating
# random numbers can be directed to graphite data center component using Liota.
# The program illustrates the ease of use Liota brings to IoT application developers.
if __name__ == '__main__':
edge_system = Dk300EdgeSystem(config['EdgeSystemName'])
# Sending data to Graphite data center component
# Socket is the underlying transport used to connect to the Graphite
# instance
graphite = Graphite(SocketDccComms(ip=config['GraphiteIP'],
port=config['GraphitePort']))
graphite_reg_edge_system = graphite.register(edge_system)
metric_name_temp_degC = config['MetricNameTempDegC']
temp_metric_degC = Metric(
name=metric_name_temp_degC,
unit=ureg.degC,
interval=33,
aggregation_size=6,
sampling_function=getTempDegC
)
reg_temp_metric_degC = graphite.register(temp_metric_degC)
graphite.create_relationship(graphite_reg_edge_system, reg_temp_metric_degC)
reg_temp_metric_degC.start_collecting()
metric_name_temp_degF = config['MetricNameTempDegF']
temp_metric_degF = Metric(
name=metric_name_temp_degF,
unit=ureg.degF,
interval=62,
aggregation_size=1,
sampling_function=getTempDegF
)
reg_temp_metric_degF = graphite.register(temp_metric_degF)
graphite.create_relationship(graphite_reg_edge_system, reg_temp_metric_degF)
reg_temp_metric_degF.start_collecting()
metric_name_temp_kelvin = config['MetricNameTempKelvin']
temp_metric_kelvin = Metric(
name=metric_name_temp_kelvin,
unit=ureg.kelvin,
interval=0,
aggregation_size=1,
sampling_function=getTempKelvin
)
reg_temp_metric_kelvin = graphite.register(temp_metric_kelvin)
graphite.create_relationship(graphite_reg_edge_system, reg_temp_metric_kelvin)
reg_temp_metric_kelvin.start_collecting()