-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo1.py
346 lines (302 loc) · 10.2 KB
/
demo1.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python
# -*- coding: utf8 -*-
import random
from pprint import pprint
from swagger_client import ApiClient
from swagger_client import SensorSystemApi
from swagger_client import DataStreamApi
from swagger_client import ObservationApi
from swagger_client import SensorSystemAdd
from swagger_client import DataStreamAdd
from swagger_client import ObservationsAdd
from swagger_client.models import VariableDef
from swagger_client.rest import ApiException
from datetime import datetime
BASE_TIMESTAMP_MS = 1503090553000
random.seed(1341)
def time_ms_to_iso(time_ms):
dt = datetime.fromtimestamp(float(time_ms) / 1000.0)
return dt.isoformat() + 'Z'
def myrandom():
return random.randrange(0, 32767)
class Demo(object):
def __init__(self, sysid, host):
self.sysid = sysid
self.api_client = ApiClient(host=host)
self.system_api = SensorSystemApi(self.api_client)
self.stream_api = DataStreamApi(self.api_client)
self.observations_api = ObservationApi(self.api_client)
def run(self):
# self.delete_system()
self.register_system()
self.generate_str1()
self.generate_str2()
self.generate_str3()
self.generate_str4()
def delete_system(self):
try:
api_response = self.system_api.delete_system(self.sysid)
if False:
pprint(api_response)
except ApiException as e:
print("Exception when calling SystemApi->delete_system: %s\n" % e)
def register_system(self):
body = SensorSystemAdd(
sysid=self.sysid,
name="Generated from python client",
)
try:
api_response = self.system_api.register_system(body)
if False:
pprint(api_response)
except ApiException as e:
print("Exception when calling SystemApi->register_system: %s\n" % e)
def generate_str1(self):
self.add_str1()
now = BASE_TIMESTAMP_MS
secs = 60
timestamp = now - secs * 1000
self.add_str1_polygon(timestamp)
self.add_scalars("str1", timestamp + 1000, secs - 1)
def add_str1(self):
print("add stream str1")
body = DataStreamAdd(
strid="str1",
name="Stream one",
description="Description of Stream one",
chart_style={
"useChartPopup": True,
"height": 500,
"yAxis": [{
"height": "50%",
"title": {"text": "baz (m)"},
"opposite": False,
"offset": -10
}, {
"top": "55%",
"height": "45%",
"title": {"text": "temperature (°)"},
"opposite": False,
"offset": -10
}]
},
variables=[
VariableDef(
name="baz",
units="m",
chart_style={
"yAxis": 0,
"type": "column"
}
),
VariableDef(
name="temperature",
units="°",
chart_style={
"yAxis": 1
}
)
],
map_style={
"color": "green",
"dashArray": "5,5"
}
)
try:
api_response = self.stream_api.register_stream(self.sysid, body)
if False:
pprint(api_response)
except ApiException as e:
print("Exception when calling StreamApi->register_stream: %s\n" % e)
def add_str1_polygon(self, timestamp):
observations = {
time_ms_to_iso(timestamp): [{
"geometry": {
"type": "Polygon",
"coordinates": [
[[-121.8564, 36.9], [-122.2217, 36.9574], [-122.0945, 36.6486], [-121.8674, 36.6858]]
]
}
}]
}
self.add_observations("str1", observations)
def add_observations(self, strid, observations):
body = ObservationsAdd(observations)
try:
api_response = self.observations_api.add_observations(self.sysid, strid, body)
if False:
pprint(api_response)
except ApiException as e:
print("Exception when calling ObservationsApi->add_observations: %s\n" % e)
def add_scalars(self, strid, timestamp, secs):
print("scalarData: %s time_iso=%s secs=%s" % (strid, time_ms_to_iso(timestamp), secs))
observations = {}
for i in range(secs):
val0 = myrandom() % 1000 - 500
val1 = myrandom() % 100
lat = 36.8 + (myrandom() % 1000) / 10000.0
lon = -122.1 + (myrandom() % 1000) / 10000.0
observations[time_ms_to_iso(timestamp)] = [{
"scalarData": {
"vars": ["baz", "temperature"],
"vals": [val0, val1],
"position": {"lat": lat, "lon": lon}
}}]
timestamp += 1000
self.add_observations(strid, observations)
def generate_str2(self):
self.add_str2()
secs = 30
timestamp = BASE_TIMESTAMP_MS - secs * 1000
# geometries:
observations = {
time_ms_to_iso(timestamp): [
{
"geometry": {
"type": "Point",
"coordinates": [-121.906, 36.882]
}
}, {
"feature": {
"properties": {
"style": {"color": "cyan", "radius": 20, "dashArray": "5,5"}
},
"geometry": {
"type": "Point",
"coordinates": [-121.965, 36.81]
}
}
}]
}
self.add_observations("str2", observations)
# data:
print("chart data: str2")
observations = {}
for i in range(secs):
timestamp += 1000
val0 = myrandom() % 100 + 1
val1 = myrandom() % 100 + 1
observations[time_ms_to_iso(timestamp)] = [{
"scalarData": {
"vars": ["foo", "bar"],
"vals": [val0, val1]
}}]
self.add_observations("str2", observations)
def add_str2(self):
print("add stream str2")
body = DataStreamAdd(
strid="str2",
variables=[
VariableDef(
name="foo"
),
VariableDef(
name="bar"
)
],
map_style={
"color": "red",
"radius": 14
},
z_order=10
)
try:
api_response = self.stream_api.register_stream(self.sysid, body)
if False:
pprint(api_response)
except ApiException as e:
print("Exception when calling StreamApi->register_stream: %s\n" % e)
def generate_str3(self):
self.add_str3()
timestamp = BASE_TIMESTAMP_MS
observations = {
time_ms_to_iso(timestamp): [{
"geometry": {
"type": "LineString",
"coordinates": [
[-122.123, 36.92], [-122.186, 36.774], [-121.9, 36.7]
]
}
}]
}
self.add_observations("str3", observations)
def add_str3(self):
print("add stream str3")
body = DataStreamAdd(
strid="str3",
map_style={
"color": "blue"
}
)
try:
api_response = self.stream_api.register_stream(self.sysid, body)
if False:
pprint(api_response)
except ApiException as e:
print("Exception when calling StreamApi->register_stream: %s\n" % e)
def generate_str4(self):
self.add_str4()
timestamp = BASE_TIMESTAMP_MS
observations = {
time_ms_to_iso(timestamp): [{
"geometry": {
"type": "Point",
"coordinates": [-122.09, 36.865]
}
}]
}
self.add_observations("str4", observations)
self.add_delayed_data("str4", "temperature", 10)
def add_str4(self):
print("add stream str4")
body = DataStreamAdd(
strid="str4",
variables=[
VariableDef(
name="temperature",
units="°C"
)
],
map_style={
"color": "yellow",
"radius": 10
},
z_order=10
)
try:
api_response = self.stream_api.register_stream(self.sysid, body)
if False:
pprint(api_response)
except ApiException as e:
print("Exception when calling StreamApi->register_stream: %s\n" % e)
def add_delayed_data(self, strid, var_name, secs):
timestamp = BASE_TIMESTAMP_MS
for i in range(secs):
import time
time.sleep(1)
timestamp += 1000
val = myrandom() % 100 + 1
time_iso = time_ms_to_iso(timestamp)
observations = {
time_iso: [{
"scalarData": {
"vars": [var_name],
"vals": [val]
}}]
}
self.add_observations("str4", observations)
print("added observation to %s: time_iso=%s" % (strid, time_iso))
if __name__ == "__main__": # pragma: no cover
import sys
if len(sys.argv) > 1:
gpdviz_host = sys.argv[1]
else:
print("""
USAGE:
python demo1.py <gpdviz_host>
Example:
python demo1.py http://localhost:5050/api
""")
sys.exit(1)
print("Using gpdviz server " + gpdviz_host)
Demo(sysid="py_demo1", host=gpdviz_host).run()