-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
419 lines (344 loc) · 14.1 KB
/
test.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python
# from datetime import datetime
# import sys
# print("hello world")
# print(datetime.utcnow())
# print(sys.version_info)
# print(sys.path)
import asyncio
import json
import signal
import sys
# from asyncio_mqtt import Client, MqttError
# from cloudevents.http import CloudEvent, from_dict, from_json, to_structured
# from cloudevents.conversion import to_json#, from_dict, from_json#, to_structured
# from cloudevents.exceptions import InvalidStructuredJSON
# from envds.message.message import Message
# from envds.message.client import MessageClientManager
# from envds.event.event import envdsEvent
# from envds.core import envdsBase
# import aioredis
# import redis.asyncio as redis
# from redis.commands.json.path import Path
# from datetime import datetime
# import random
import argparse
# from envds.daq.registration import init_sensor_registration, register_sensor
from aredis_om import (
EmbeddedJsonModel,
JsonModel,
Field,
Migrator,
get_redis_connection,
)
from pydantic import ValidationError
# class TestApp2(envdsBase):
# def __init__(self):
# super(TestApp2, self).__init__()
# self.task_list = []
# self.do_run = True
# self.task_list.append(asyncio.create_task(self.publish()))
# self.task_list.append(asyncio.create_task(self.run()))
# self._setup()
# asyncio.create_task(self.run())
# def _setup(self):
# self.message_client.subscribe("sensor/all")
# self.message_client.subscribe("sensor/instrument/trh/#")
# self.message_client.subscribe("sensor/instruments/all")
# self.message_client.subscribe("sensor/instrumentgroup/trhgroup/#")
# async def publish(self):
# # while pub_client:
# # pub_client = MessageClientManager.create()
# while self.do_run:
# # attributes = {
# # "type": "com.example.sampletype1",
# # "source": "https://example.com/event-producer",
# # }
# data = {"data": 45.1, "units": "%"}
# event = envdsEvent.create_data_update(source="test/app", data=data)
# msg = Message(dest_path="sensor/instrument/trh/humidity", data=event)
# # event = CloudEvent(attributes, data)
# await self.message_client.send(msg)
# data = {"data": 24.3, "units": "degC"}
# event = envdsEvent.create_data_update(source="test/app", data=data)
# msg = Message(dest_path="sensor/instrument/trh/temperature", data=event)
# # event = CloudEvent(attributes, data)
# await self.message_client.send(msg)
# await asyncio.sleep(1)
# # pub_client.request_shutdown()
# class TestApp(object):
# """docstring for TestClient."""
# def __init__(self):
# super(TestApp, self).__init__()
# # self.client = None
# # self.message = None
# # self.reconnect_interval = 5
# # self.client_ready = False
# self.task_list = []
# self.do_run = True
# self.task_list.append(asyncio.create_task(self.publish()))
# self.task_list.append(asyncio.create_task(self.run()))
# # def subscribe(self, topic: str):
# # asyncio.create_task(self._subscribe(topic))
# # async def _subscribe(self, topic: str):
# # while self.client is None or not self.client_ready:
# # await asyncio.sleep(1) # wait for client to be ready
# # await self.client.subscribe(topic)
# # def unsubscribe(self, topic: str):
# # asyncio.create_task(self._unsubscribe(topic))
# # async def _unsubscribe(self, topic: str):
# # while self.client is None or not self.client_ready:
# # await asyncio.sleep(1) # wait for client to be ready
# # await self.client.subscribe(topic)
# async def publish(self):
# # while pub_client:
# pub_client = MessageClientManager.create()
# while self.do_run:
# # attributes = {
# # "type": "com.example.sampletype1",
# # "source": "https://example.com/event-producer",
# # }
# data = {"data": 45.1, "units": "%"}
# event = envdsEvent.create_data_update(source="test/app", data=data)
# msg = Message(dest_path="measurements/instrument/trh/humidity", data=event)
# # event = CloudEvent(attributes, data)
# await pub_client.send(msg)
# await asyncio.sleep(1)
# pub_client.request_shutdown()
# async def run(self):
# client = MessageClientManager.create()
# # client.start()
# client.subscribe("measurements/all")
# client.subscribe("measurements/instrument/trh/#")
# client.subscribe("measurements/instruments/all")
# client.subscribe("measurements/instrumentgroup/trhgroup/#")
# while self.do_run:
# msg = await client.get()
# print(f"message: {msg.source_path}: {msg.data}")
# # try:
# # async with Client("localhost") as client:
# # async with client.unfiltered_messages() as messages:
# # self.client_ready = True
# # async for message in messages:
# # if self.do_run:
# # # data = from_dict(json.loads(message.payload.decode()))
# # data = from_json(message.payload.decode())
# # print(f"topic: {message.topic}, message: {data}")
# # # print(f"topic: {message.topic}, message: {message.payload.decode()}")
# # else:
# # print("close messages")
# # # self.client_ready = False
# # await messages.aclose()
# # # print(message.payload.decode())
# # # test_count += 1
# # except MqttError as error:
# # # self.client_ready = False
# # print(f'Error "{error}". Reconnecting sub in {self.reconnect_interval} seconds.')
# # await asyncio.sleep(self.reconnect_interval)
# client.request_shutdown()
# async def shutdown(self):
# # self.client.disconnect()
# # await self.messages.aclose()
# self.do_run = False
# async def publish(pub_client):
# # event_loop = asyncio.get_running_loop()
# # reconnect_interval = 5
# # pub_client = MessageClientManager.create()
# # pub_client.start()
# # pub_client.run()
# # do_run = True
# # def shutdown_handler(*args):
# # # print(f"shutting down client: {client}")
# # print(f"signal caught: {args}, shutting down client")
# # asyncio.create_task(pub_client.shutdown())
# # # do_run = False
# # event_loop.add_signal_handler(signal.SIGINT, shutdown_handler)
# # event_loop.add_signal_handler(signal.SIGTERM, shutdown_handler)
# # while pub_client.do_run:
# while pub_client:
# # attributes = {
# # "type": "com.example.sampletype1",
# # "source": "https://example.com/event-producer",
# # }
# data = {"data": 45.1, "units": "%"}
# event = envdsEvent.create_data_update(source="test/app", data=data)
# msg = Message(dest_path="measurements/instrument/trh/humidity", data=event)
# # event = CloudEvent(attributes, data)
# await pub_client.send(msg)
# await asyncio.sleep(1)
# async def print_messages(messages, template):
# async for message in messages:
# print(template.format(message.payload))
# async def cancel_tasks(tasks):
# for task in tasks:
# if task.done():
# continue
# try:
# task.cancel()
# await task
# except asyncio.CancelledError:
# pass
# async def main():
# event_loop = asyncio.get_running_loop()
# # app = TestApp2()
# # app.client.subscribe("measurements/all")
# # app.client.subscribe("measurements/instrument/trh/#")
# # client.subscribe("measurements/instruments/all")
# # client.subscribe("measurements/instrumentgroup/trhgroup/#")
# r = redis.from_url("redis://localhost")
# # r = redis.Redis(host="localhost", port=6379)#, db="test-registry")
# index = 0
# while True:
# val1 = random.random()
# val2 = random.random()
# # print(f"{index}, {val}")
# record = {
# "time": datetime.utcnow().isoformat(),
# "temperature": val1,
# "rh": val2
# }
# await r.json().set(f"{index}", Path.root_path(), record)
# await r.expire(f"{index}", time=60)
# # await r.set(f"{index}", f"{val}", ex=60)
# # bin_value = await r.get(f"{index}")
# # assert bin_value == b"string-value"
# # r = redis.from_url("redis://localhost", decode_responses=True)
# output = await r.json().get(f"{index}")
# # assert str_value == "string-value"
# print(output)
# index += 1
# await asyncio.sleep(1)
# await r.close()
# # do_run = True
# # while app.do_run:
# # def shutdown_handler(*args):
# # # print(f"shutting down client: {client}")
# # print(f"signal caught: {args}, shutting down client")
# # # client.request_shutdown()
# # asyncio.create_task(app.shutdown())
# # # pub_client.request_shutdown()
# # do_run = False
# # event_loop.add_signal_handler(signal.SIGINT, shutdown_handler)
# # event_loop.add_signal_handler(signal.SIGTERM, shutdown_handler)
# # await asyncio.sleep(1)
# print("done")
class SensorRegistration(JsonModel):
make: str = Field(index=True)
model: str = Field(index=True)
version: str = Field(index=True)
checksum: int
metadata: dict | None = {}
async def init_sensor_registration():
SensorRegistration.Meta.database = await get_redis_connection(
url="redis://@127.0.0.1:6379"
)
print(SensorRegistration.Meta.database)
res = await Migrator().run()
print(res)
async def register_sensor(
make: str, model: str, version: str = "1.0", metadata: dict = {}
):
try:
reg = SensorRegistration(
make=make,
model=model,
version=version,
checksum=12345,
metadata=metadata,
)
print(f"reg: {reg}")
print(SensorRegistration.Meta.database)
# current = await SensorRegistration.find(SensorRegistration.make == make and SensorRegistration.model == model).first()
# if current and current.checksum == reg.checksum:
# print(f"current exists: {current}")
# return
print(f"pk: {reg.pk}")
await reg.save()
except ValidationError as e:
print(f"Could not register sensor: \n{e}")
async def run2():
# await init_sensor_registration()
await register_sensor(make="make", model="model", metadata={})
def run(args):
pass
# print(args[1:])
# foo_parent = argparse.ArgumentParser(add_help=False)
# foo_parent.add_argument("--foo", type=str, help="foo test")
# bar_parent = argparse.ArgumentParser(add_help=False)
# bar_parent.add_argument("--bar", type=str, help="bar test")
# id_parent = argparse.ArgumentParser(add_help=False)
# id_parent.add_argument("-id", "--envds-id", type=str, help="envds-id")
# system_parent = argparse.ArgumentParser(add_help=False)
# system_parent.add_argument(
# "-dv",
# "--envds_data_volume",
# type=str,
# help="data volume",
# # default=(os.path.join(os.getcwd(), "data")),
# )
# system_parent.add_argument(
# "-crtdv",
# "--envds_certs_volume",
# type=str,
# help="certs volume",
# # default=(os.path.join(os.getcwd(), "data")),
# )
# system_parent.add_argument(
# "-cfgdv",
# "--envds_cfg_volume",
# type=str,
# help="config volume",
# # default=(os.path.join(os.getcwd(), "data")),
# )
# system_parent.add_argument(
# "-ho",
# "--host",
# type=str,
# help="host",
# # default=(os.path.join(os.getcwd(), "data")),
# )
# system_parent.add_argument(
# "-http",
# "--http_port",
# type=str,
# help="http port",
# # default=(os.path.join(os.getcwd(), "data")),
# )
# system_parent.add_argument(
# "-https",
# "--https_port",
# type=str,
# help="https port",
# # default=(os.path.join(os.getcwd(), "data")),
# )
# system_parent.add_argument(
# "-mqtt",
# "--mqtt_port",
# type=str,
# help="mqtt port",
# # default=(os.path.join(os.getcwd(), "data")),
# )
# system_parent.add_argument(
# "-mqtts",
# "--mqtts_port",
# type=str,
# help="mqtts port",
# # default=(os.path.join(os.getcwd(), "data")),
# )
# parser = argparse.ArgumentParser(prog="envds")
# command_sp = parser.add_subparsers(dest="command", help="sub-command help")
# create_parser = command_sp.add_parser("create", help="create command", parents=[id_parent])
# create_target_sp = create_parser.add_subparsers(dest="target", help="create <target>")
# create_system_parser = create_target_sp.add_parser("system", parents=[id_parent, system_parent])
# delete_parser = command_sp.add_parser("delete", help="delete command", parents=[foo_parent, bar_parent])
# delete_target_sp = delete_parser.add_subparsers(dest="target", help="delete <target>")
# delete_system_parser = delete_target_sp.add_parser("system", parents=[id_parent])
# cl_args = parser.parse_args()
# print(cl_args)
# print(cl_args.command)
# print(vars(cl_args))
# # create_args = create_parser.parse_args(args[1:])
if __name__ == "__main__":
# asyncio.run(main())
# run(sys.argv)
asyncio.run(run2())