import paho.mqtt.publish as publish
import json
import pandas as pd
import time
from datetime import datetime, timezone
host = "mqtt.swx.altairone.com"
space = "EnterValue"
thing_uid = "EnterValue"
username = 'EnterValue'
password = 'EnterValue'
def handle(req):
path = "EnterValue"
data_pd = pd.read_csv(path, delimiter=",")
df = pd.DataFrame(data_pd)
properties_history_topic = "spaces/{}/things/{}/properties-history".format(space, thing_uid)
for i in range(len(df)):
current_time = datetime.now(timezone.utc)
formatted_time = current_time.isoformat()
machine_failure_data = {
"HDF": float(df["HDF"][i]),
"OSF": float(df["OSF"][i]),
"PWF": float(df["PWF"][i]),
"RNF": float(df["RNF"][i]),
"TWF": float(df["TWF"][i])
}
payload = {
"at": formatted_time,
"properties": {
"UID": float(df["UID"][i]),
"air_temperature": float(df["air_temperature"][i]),
"machine_failure": machine_failure_data,
"process_temperature": float(df["process_temperature"][i]),
"product_ID": df["product_ID"][i],
"rotational_speed": float(df["rotational_speed"][i]),
"tool_wear": float(df["tool_wear"][i]),
"torque": float(df["torque"][i]),
"type": df["type"][i]
}
}
print(payload)
time.sleep(0.5)
print("Data:", payload)
publish.single(properties_history_topic, payload=json.dumps(payload), qos=2, retain=False, hostname=host, port=1883, client_id="",
keepalive=120, will=None, auth={'username': username, 'password': password}, tls=None)
return {
"status_code": 200,
"body": "upload finished!"
}