Реалізовую базовий FileReader та переношу sleep до FileDatasource.read()
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
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
|
||||
@@ -24,19 +22,14 @@ def connect_mqtt(broker, port):
|
||||
return client
|
||||
|
||||
|
||||
def publish(client, topic, datasource, delay):
|
||||
def publish(client, topic, datasource):
|
||||
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:
|
||||
if status != 0:
|
||||
print(f"Failed to send message to topic {topic}")
|
||||
|
||||
|
||||
@@ -44,10 +37,10 @@ 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")
|
||||
datasource = FileDatasource("data/accelerometer.csv", "data/gps.csv")
|
||||
# Infinity publish data
|
||||
publish(client, config.MQTT_TOPIC, datasource, config.DELAY)
|
||||
publish(client, config.MQTT_TOPIC, datasource)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
run()
|
||||
Reference in New Issue
Block a user