This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
iot.process.final.py
162 lines (139 loc) · 3.86 KB
/
iot.process.final.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
153
154
155
156
157
158
159
160
161
162
import time
import locale
import json
#import ast
# Number of max devices
max_devices_qty = 100
locale.setlocale(locale.LC_ALL,"")
def main(payload):
last_timestamp = int(round(time.time()))
ubody = payload.body.decode("utf-8") # Must be converted to Unicode first
#body = ast.literal_eval(ubody) #ast.literal_eval works only with strings, but can be improperly formatted JSON, like a result of str(json_object)
body=json.loads(ubody)
body['loc_timestmp'] = last_timestamp
send_blob(json.dumps([body]))
guid = body["guid"]
del body["guid"]
devices[guid] = body
message = ""
message = generate_html_head(message)
message = generate_html_body(message, last_timestamp)
send_html(message)
# Generates head of HTML/CSS, including Style parameters
def generate_html_head(message):
message += '''
<head>
<style>
body {
font-family: Verdana, Geneva, sans-serif;
background: #b5b5bf;
}
table.dataTable {
border: 1px solid #1C6EA4;
background-color: #EEEEEE;
text-align: center;
border-collapse: collapse;
}
table.dataTable td, table.dataTable th {
border: 2px solid #AAAAAA;
padding: 3px 2px;
}
table.dataTable tbody td {
font-size: 15px;
}
table.dataTable thead {
background: #1C6EA4;
background: -moz-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
background: -webkit-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
background: linear-gradient(to bottom, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
border-bottom: 2px solid #888888;
}
table.dataTable thead th {
font-size: 15px;
font-weight: bold;
color: #FFFFFF;
text-align: center;
border-left: 2px solid #D0E4F5;
}
table.dataTable thead th:first-child {
border-left: none;
}
table.dataTable tfoot td {
font-size: 14px;
}
table.dataTable tfoot .links {
text-align: right;
}
table.dataTable tfoot .links a{
display: inline-block;
background: #1C6EA4;
color: #FFFFFF;
padding: 2px 8px;
border-radius: 5px;
}
</style>
</head>
'''
return message
# Concatenates HTML body to previously generated head.
def generate_html_body(message, last_timestamp):
# Title, description and table header
message += '''
<body>
<center>
<h2> Prototype IoT Data Viewer </h2>
<br>
</center>
<p style="margin-left:15%; margin-right:15%"> The purpose of this Graph is to serve as a stub for the HTML Viewer operator.
Combining the HTML Viewer with a Python Operator, it is possible to adapt IoT data for real time display in a simple and flexible manner.
<br><br>
The data structure generation is happening in the Python3 Operator, which is messaging a String containing an HTML page to the HTML Viewer
through WebSocket with every update. The HTML Viewer then updates the display as soon as a message is received.
<br><br>
</p>
<center>
Last time stamp: {}
<br><br>
<table class="dataTable" id="salesTable">
<thead>
<tr>
<th style="width:400px"> Device UUID </th>
<th style="width:300px"> Last timestamp </th>
<th style="width:100px"> CPU % </th>
<th style="width:100px"> Mem % </th>
</tr>
</thead>
<tbody>
'''.format(time.ctime(round(time.time())))
# Iterates each store to add them to table
for i in devices:
message += '''
<tr>
<td> {} </td>
<td> {} </td>
<td> {} %</td>
<td> {} %</td>
</tr>
'''.format(
i,
time.ctime(int(devices[i]["timestmp"])),
locale.format("%.2f", devices[i]["cpu_load"], 1),
locale.format("%.2f", devices[i]["mem_load"], 1)
)
message += '''
</tbody>
</table>
<br><br>
</center>
</body>
'''
return message
# Sends the String containing HTML page through WebSocket
def send_html(message):
api.send("outhtml", message)
# Sends the String containing HTML page through WebSocket
def send_blob(blb):
api.send("outblob", blb)
api.send("outhtml", "Waiting for data...")
devices = {} #Empty dictionary
api.set_port_callback("inmsg", main)