add: agent template

This commit is contained in:
Toolf 2024-02-02 22:51:54 +02:00
commit 1f92b43879
16 changed files with 1749 additions and 0 deletions

2
agent/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
venv
__pycache__

12
agent/Dockerfile Normal file
View File

@ -0,0 +1,12 @@
# set base image (host OS)
FROM python:latest
# set the working directory in the container
WORKDIR /usr/agent
# copy the dependencies file to the working directory
COPY requirements.txt .
# install dependencies
RUN pip install -r requirements.txt
# copy the content of the local src directory to the working directory
COPY src/ .
# command to run on container start
CMD ["python", "main.py"]

View File

@ -0,0 +1,33 @@
version: "3.9"
name: "road_vision"
services:
mqtt:
image: eclipse-mosquitto
container_name: mqtt
volumes:
- ./mosquitto:/mosquitto
- ./mosquitto/data:/mosquitto/data
- ./mosquitto/log:/mosquitto/log
ports:
- 1883:1883
- 9001:9001
networks:
mqtt_network:
fake_agent:
container_name: agent
build: ../
depends_on:
- mqtt
environment:
MQTT_BROKER_HOST: "mqtt"
MQTT_BROKER_PORT: 1883
MQTT_TOPIC: "agent_data_topic"
DELAY: 0.1
networks:
mqtt_network:
networks:
mqtt_network:

View File

@ -0,0 +1,11 @@
persistence true
persistence_location /mosquitto/data/
listener 1883
## Authentication ##
allow_anonymous true
# allow_anonymous false
# password_file /mosquitto/config/password.txt
## Log ##
log_dest file /mosquitto/log/mosquitto.log
log_dest stdout
# listener 1883

3
agent/requirements.txt Normal file
View File

@ -0,0 +1,3 @@
marshmallow==3.20.2
packaging==23.2
paho-mqtt==1.6.1

17
agent/src/config.py Normal file
View File

@ -0,0 +1,17 @@
import os
def try_parse(type, value: str):
try:
return type(value)
except Exception:
return None
# MQTT config
MQTT_BROKER_HOST = os.environ.get("MQTT_BROKER_HOST") or "mqtt"
MQTT_BROKER_PORT = try_parse(int, os.environ.get("MQTT_BROKER_PORT")) or 1883
MQTT_TOPIC = os.environ.get("MQTT_TOPIC") or "agent"
# Delay for sending data to mqtt in seconds
DELAY = try_parse(float, os.environ.get("DELAY")) or 1

File diff suppressed because it is too large Load Diff

126
agent/src/data/gps.csv Normal file
View File

@ -0,0 +1,126 @@
longitude,latitude
50.450386085935094,30.524547100067142
50.45050063818424,30.52432932092009
50.45061519043338,30.524111541773035
50.450729742682526,30.52389376262598
50.45084429493167,30.523675983478928
50.450958847180814,30.523458204331874
50.45107339942996,30.52324042518482
50.4511879516791,30.523022646037766
50.45130250392825,30.522804866890713
50.45141705617739,30.52258708774366
50.45158184958234,30.52247389776916
50.451757508676074,30.522413170763514
50.45193316776981,30.522352443757867
50.452108826863544,30.52229171675222
50.45228448595728,30.522230989746575
50.45246014505101,30.52217026274093
50.45263580414475,30.522109535735282
50.45281146323848,30.522048808729636
50.45298712233222,30.52198808172399
50.45316278142595,30.521927354718343
50.45333844051969,30.521866627712697
50.4535017075372,30.521748100680377
50.45366497455471,30.521629573648056
50.453828241572225,30.521511046615736
50.45399150858974,30.521392519583415
50.45415477560725,30.521273992551095
50.45431804262476,30.521155465518774
50.454481309642276,30.521036938486453
50.45464457665979,30.520918411454133
50.4548078436773,30.520799884421812
50.454971110694814,30.520681357389492
50.455093555427055,30.520474423051503
50.4552160001593,30.520267488713515
50.45523090797679,30.51998595727611
50.455245815794285,30.519704425838704
50.45526072361178,30.5194228944013
50.455275631429274,30.519141362963893
50.45529053924677,30.518859831526488
50.45530544706426,30.518578300089082
50.455320354881756,30.518296768651677
50.45533526269925,30.51801523721427
50.455350170516745,30.517733705776866
50.45536507833424,30.51745217433946
50.45535571063255,30.517170053920086
50.45518133729636,30.517100783785818
50.455006963960166,30.51703151365155
50.45483259062397,30.51696224351728
50.45465821728778,30.516892973383012
50.454483843951586,30.516823703248743
50.45430947061539,30.516754433114475
50.4541350972792,30.516685162980206
50.45397769437711,30.51682186381075
50.45382029147502,30.516958564641296
50.45366288857293,30.51709526547184
50.45348323217105,30.517081686354636
50.45336278271238,30.51687189126274
50.45324233325371,30.51666209617084
50.45312188379504,30.516452301078942
50.45297828058652,30.516282205294928
50.452804445806855,30.516209678584172
50.45263061102719,30.516137151873416
50.45245677624752,30.51606462516266
50.45228294146786,30.515992098451903
50.45210910668819,30.515919571741147
50.451935271908525,30.51584704503039
50.45176143712886,30.515774518319635
50.45158760234919,30.51570199160888
50.45141376756953,30.515629464898122
50.45123993278986,30.515556938187366
50.451066098010195,30.51548441147661
50.45089226323053,30.515411884765854
50.45071842845086,30.515339358055098
50.4505445936712,30.51526683134434
50.45037075889153,30.515194304633585
50.450196924111864,30.51512177792283
50.4500230893322,30.515049251212073
50.44984925455253,30.514976724501317
50.449675419772866,30.51490419779056
50.4495015849932,30.514831671079804
50.449327750213534,30.514759144369048
50.44915391543387,30.514686617658292
50.4489800806542,30.514614090947536
50.44893464360986,30.51488739159867
50.44888920656552,30.515160692249804
50.448843769521176,30.515433992900938
50.448798332476834,30.515707293552072
50.44875289543249,30.515980594203207
50.44870745838815,30.51625389485434
50.44866202134381,30.516527195505475
50.448616584299465,30.51680049615661
50.44857114725512,30.517073796807743
50.44852571021078,30.517347097458877
50.44848027316644,30.51762039811001
50.4484348361221,30.517893698761146
50.448389399077755,30.51816699941228
50.44834396203341,30.518440300063414
50.44829852498907,30.51871360071455
50.44825308794473,30.518986901365682
50.448207650900386,30.519260202016817
50.448162213856044,30.51953350266795
50.4481167768117,30.519806803319085
50.44807133976736,30.52008010397022
50.44802590272302,30.520353404621353
50.447980465678675,30.520626705272488
50.44793502863433,30.52090000592362
50.44788959158999,30.521173306574756
50.44784415454565,30.52144660722589
50.44779871750131,30.521719907877024
50.447753280456965,30.52199320852816
50.4479216063945,30.52209274810735
50.44808993233204,30.522192287686543
50.448258258269576,30.522291827265736
50.44842658420711,30.52239136684493
50.44859491014465,30.52249090642412
50.448763236082186,30.522590446003314
50.44893156201972,30.522689985582506
50.44907610254229,30.52285809923405
50.44922064306486,30.523026212885593
50.44936518358743,30.523194326537137
50.44950972411,30.52336244018868
50.44965426463257,30.523530553840224
50.44979880515514,30.523698667491768
50.44994334567771,30.52386678114331
50.450069433207545,30.52406822530458
50.450204300386,30.52425511880424
1 longitude latitude
2 50.450386085935094 30.524547100067142
3 50.45050063818424 30.52432932092009
4 50.45061519043338 30.524111541773035
5 50.450729742682526 30.52389376262598
6 50.45084429493167 30.523675983478928
7 50.450958847180814 30.523458204331874
8 50.45107339942996 30.52324042518482
9 50.4511879516791 30.523022646037766
10 50.45130250392825 30.522804866890713
11 50.45141705617739 30.52258708774366
12 50.45158184958234 30.52247389776916
13 50.451757508676074 30.522413170763514
14 50.45193316776981 30.522352443757867
15 50.452108826863544 30.52229171675222
16 50.45228448595728 30.522230989746575
17 50.45246014505101 30.52217026274093
18 50.45263580414475 30.522109535735282
19 50.45281146323848 30.522048808729636
20 50.45298712233222 30.52198808172399
21 50.45316278142595 30.521927354718343
22 50.45333844051969 30.521866627712697
23 50.4535017075372 30.521748100680377
24 50.45366497455471 30.521629573648056
25 50.453828241572225 30.521511046615736
26 50.45399150858974 30.521392519583415
27 50.45415477560725 30.521273992551095
28 50.45431804262476 30.521155465518774
29 50.454481309642276 30.521036938486453
30 50.45464457665979 30.520918411454133
31 50.4548078436773 30.520799884421812
32 50.454971110694814 30.520681357389492
33 50.455093555427055 30.520474423051503
34 50.4552160001593 30.520267488713515
35 50.45523090797679 30.51998595727611
36 50.455245815794285 30.519704425838704
37 50.45526072361178 30.5194228944013
38 50.455275631429274 30.519141362963893
39 50.45529053924677 30.518859831526488
40 50.45530544706426 30.518578300089082
41 50.455320354881756 30.518296768651677
42 50.45533526269925 30.51801523721427
43 50.455350170516745 30.517733705776866
44 50.45536507833424 30.51745217433946
45 50.45535571063255 30.517170053920086
46 50.45518133729636 30.517100783785818
47 50.455006963960166 30.51703151365155
48 50.45483259062397 30.51696224351728
49 50.45465821728778 30.516892973383012
50 50.454483843951586 30.516823703248743
51 50.45430947061539 30.516754433114475
52 50.4541350972792 30.516685162980206
53 50.45397769437711 30.51682186381075
54 50.45382029147502 30.516958564641296
55 50.45366288857293 30.51709526547184
56 50.45348323217105 30.517081686354636
57 50.45336278271238 30.51687189126274
58 50.45324233325371 30.51666209617084
59 50.45312188379504 30.516452301078942
60 50.45297828058652 30.516282205294928
61 50.452804445806855 30.516209678584172
62 50.45263061102719 30.516137151873416
63 50.45245677624752 30.51606462516266
64 50.45228294146786 30.515992098451903
65 50.45210910668819 30.515919571741147
66 50.451935271908525 30.51584704503039
67 50.45176143712886 30.515774518319635
68 50.45158760234919 30.51570199160888
69 50.45141376756953 30.515629464898122
70 50.45123993278986 30.515556938187366
71 50.451066098010195 30.51548441147661
72 50.45089226323053 30.515411884765854
73 50.45071842845086 30.515339358055098
74 50.4505445936712 30.51526683134434
75 50.45037075889153 30.515194304633585
76 50.450196924111864 30.51512177792283
77 50.4500230893322 30.515049251212073
78 50.44984925455253 30.514976724501317
79 50.449675419772866 30.51490419779056
80 50.4495015849932 30.514831671079804
81 50.449327750213534 30.514759144369048
82 50.44915391543387 30.514686617658292
83 50.4489800806542 30.514614090947536
84 50.44893464360986 30.51488739159867
85 50.44888920656552 30.515160692249804
86 50.448843769521176 30.515433992900938
87 50.448798332476834 30.515707293552072
88 50.44875289543249 30.515980594203207
89 50.44870745838815 30.51625389485434
90 50.44866202134381 30.516527195505475
91 50.448616584299465 30.51680049615661
92 50.44857114725512 30.517073796807743
93 50.44852571021078 30.517347097458877
94 50.44848027316644 30.51762039811001
95 50.4484348361221 30.517893698761146
96 50.448389399077755 30.51816699941228
97 50.44834396203341 30.518440300063414
98 50.44829852498907 30.51871360071455
99 50.44825308794473 30.518986901365682
100 50.448207650900386 30.519260202016817
101 50.448162213856044 30.51953350266795
102 50.4481167768117 30.519806803319085
103 50.44807133976736 30.52008010397022
104 50.44802590272302 30.520353404621353
105 50.447980465678675 30.520626705272488
106 50.44793502863433 30.52090000592362
107 50.44788959158999 30.521173306574756
108 50.44784415454565 30.52144660722589
109 50.44779871750131 30.521719907877024
110 50.447753280456965 30.52199320852816
111 50.4479216063945 30.52209274810735
112 50.44808993233204 30.522192287686543
113 50.448258258269576 30.522291827265736
114 50.44842658420711 30.52239136684493
115 50.44859491014465 30.52249090642412
116 50.448763236082186 30.522590446003314
117 50.44893156201972 30.522689985582506
118 50.44907610254229 30.52285809923405
119 50.44922064306486 30.523026212885593
120 50.44936518358743 30.523194326537137
121 50.44950972411 30.52336244018868
122 50.44965426463257 30.523530553840224
123 50.44979880515514 30.523698667491768
124 50.44994334567771 30.52386678114331
125 50.450069433207545 30.52406822530458
126 50.450204300386 30.52425511880424

View File

@ -0,0 +1,8 @@
from dataclasses import dataclass
@dataclass
class Accelerometer:
x: int
y: int
z: int

View File

@ -0,0 +1,12 @@
from dataclasses import dataclass
from datetime import datetime
from domain.accelerometer import Accelerometer
from domain.gps import Gps
@dataclass
class AggregatedData:
accelerometer: Accelerometer
gps: Gps
time: datetime

7
agent/src/domain/gps.py Normal file
View File

@ -0,0 +1,7 @@
from dataclasses import dataclass
@dataclass
class Gps:
longitude: float
latitude: float

View File

@ -0,0 +1,21 @@
from csv import reader
from datetime import datetime
from domain.aggregated_data import AggregatedData
class FileDatasource:
def __init__(
self,
accelerometer_filename: str,
gps_filename: str,
) -> None:
pass
def read(self) -> AggregatedData:
"""Метод повертає дані отримані з датчиків"""
def startReading(self, *args, **kwargs):
"""Метод повинен викликатись перед початком читання даних"""
def stopReading(self, *args, **kwargs):
"""Метод повинен викликатись для закінчення читання даних"""

53
agent/src/main.py Normal file
View File

@ -0,0 +1,53 @@
from paho.mqtt import client as mqtt_client
import json
import time
from schema.aggregated_data_schema import AggregatedDataSchema
from file_datasource import FileDatasource
import config
def connect_mqtt(broker, port):
"""Create MQTT client"""
print(f"CONNECT TO {broker}:{port}")
def on_connect(client, userdata, flags, rc):
if rc == 0:
print(f"Connected to MQTT Broker ({broker}:{port})!")
else:
print("Failed to connect {broker}:{port}, return code %d\n", rc)
exit(rc) # Stop execution
client = mqtt_client.Client()
client.on_connect = on_connect
client.connect(broker, port)
client.loop_start()
return client
def publish(client, topic, datasource, delay):
datasource.startReading()
while True:
time.sleep(delay)
data = datasource.read()
msg = AggregatedDataSchema().dumps(data)
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
pass
# print(f"Send `{msg}` to topic `{topic}`")
else:
print(f"Failed to send message to topic {topic}")
def run():
# Prepare mqtt client
client = connect_mqtt(config.MQTT_BROKER_HOST, config.MQTT_BROKER_PORT)
# Prepare datasource
datasource = FileDatasource("data/data.csv", "data/gps_data.csv")
# Infinity publish data
publish(client, config.MQTT_TOPIC, datasource, config.DELAY)
if __name__ == "__main__":
run()

View File

@ -0,0 +1,7 @@
from marshmallow import Schema, fields
class AccelerometerSchema(Schema):
x = fields.Int()
y = fields.Int()
z = fields.Int()

View File

@ -0,0 +1,9 @@
from marshmallow import Schema, fields
from schema.accelerometer_schema import AccelerometerSchema
from schema.gps_schema import GpsSchema
class AggregatedDataSchema(Schema):
accelerometer = fields.Nested(AccelerometerSchema)
gps = fields.Nested(GpsSchema)
time = fields.DateTime("iso")

View File

@ -0,0 +1,6 @@
from marshmallow import Schema, fields
class GpsSchema(Schema):
longitude = fields.Number()
latitude = fields.Number()