added new device types
This commit is contained in:
97
code snippets/basicSendTest.ipynb
Normal file
97
code snippets/basicSendTest.ipynb
Normal file
@@ -0,0 +1,97 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from datetime import datetime as dt\n",
|
||||
"import json, time\n",
|
||||
"import random"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Helper function to split the payload into chunks\n",
|
||||
"def chunk_payload(payload, chunk_size=20):\n",
|
||||
" chunked_values = list(payload[\"values\"].items())\n",
|
||||
" for i in range(0, len(chunked_values), chunk_size):\n",
|
||||
" yield {\n",
|
||||
" \"ts\": payload[\"ts\"],\n",
|
||||
" \"values\": dict(chunked_values[i:i+chunk_size])\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
"def sendData(message):\n",
|
||||
" payload = {}\n",
|
||||
" payload[\"ts\"] = (round(dt.timestamp(dt.now())/600)*600)*1000\n",
|
||||
" payload[\"values\"] = {}\n",
|
||||
" \n",
|
||||
" for measure in message[\"measures\"]:\n",
|
||||
" try:\n",
|
||||
" #print(measure)\n",
|
||||
" if measure[\"health\"] == 1:\n",
|
||||
" payload[\"values\"][measure[\"name\"]] = measure[\"value\"]\n",
|
||||
" except Exception as e:\n",
|
||||
" print(e)\n",
|
||||
" for chunk in chunk_payload(payload=payload):\n",
|
||||
" print(json.dumps(chunk, indent=4))\n",
|
||||
" time.sleep(2)\n",
|
||||
" print(\"v1/devices/me/attributes\", json.dumps({\"latestReportTime\": (round(dt.timestamp(dt.now())/600)*600)*1000},indent=4))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "KeyboardInterrupt",
|
||||
"evalue": "",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
|
||||
"Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m 2\u001b[0m message \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmeasures\u001b[39m\u001b[38;5;124m\"\u001b[39m: [\n\u001b[1;32m 3\u001b[0m {\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mctrlName\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mplcpond\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mair_comp_spt\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhealth\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtimestamp\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1725973793\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtimestampMsec\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1725973793190\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m40.0\u001b[39m},\n\u001b[0;32m----> 4\u001b[0m {\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mctrlName\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mplcpond\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mair_comp_val\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhealth\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtimestamp\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1725973793\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtimestampMsec\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1725973793190\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m0.23999999999999999\u001b[39m},\n\u001b[1;32m 5\u001b[0m {\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mctrlName\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mplcpond\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mair_comp_low_alm\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhealth\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtimestamp\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1725973793\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtimestampMsec\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1725973793190\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;241m1\u001b[39m}] \n\u001b[1;32m 6\u001b[0m }\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mint\u001b[39m(time\u001b[38;5;241m.\u001b[39mtime()) \u001b[38;5;241m%\u001b[39m \u001b[38;5;241m600\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 8\u001b[0m sendData(message)\n",
|
||||
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"while True:\n",
|
||||
" message = {\"measures\": [\n",
|
||||
" {'ctrlName': 'plcpond', 'name': 'air_comp_spt', 'health': 1, 'timestamp': 1725973793, 'timestampMsec': 1725973793190, 'value': 40.0},\n",
|
||||
"{'ctrlName': 'plcpond', 'name': 'air_comp_val', 'health': 1, 'timestamp': 1725973793, 'timestampMsec': 1725973793190, 'value': -0.23999999999999999},\n",
|
||||
"{'ctrlName': 'plcpond', 'name': 'air_comp_low_alm', 'health': 1, 'timestamp': 1725973793, 'timestampMsec': 1725973793190, 'value': 1}] \n",
|
||||
" }\n",
|
||||
" if int(time.time()) % 600 == 0:\n",
|
||||
" sendData(message)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "base",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
139
code snippets/sendBackfillData.ipynb
Normal file
139
code snippets/sendBackfillData.ipynb
Normal file
@@ -0,0 +1,139 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pytz\n",
|
||||
"from datetime import datetime\n",
|
||||
"import json, csv, time\n",
|
||||
"import paho.mqtt.client as mqtt\n",
|
||||
"from datetime import datetime, timedelta"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Set the MQTT broker connection details\n",
|
||||
"MQTT_BROKER = \"hp.henrypump.cloud\"\n",
|
||||
"MQTT_PORT = 1883\n",
|
||||
"\n",
|
||||
"# Create an MQTT client instance\n",
|
||||
"client = mqtt.Client(client_id=\"faskens-bp-compressor\")\n",
|
||||
"client.username_pw_set(\"faskensmqtt\", \"faskensmqtt@1903\" )\n",
|
||||
"client.connect(MQTT_BROKER, MQTT_PORT)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"# Define a function to convert datetime to timestamp and round to nearest 10 minutes\n",
|
||||
"def convert_datetime_to_timestamp(dt_str):\n",
|
||||
" cst_tz = pytz.timezone('America/Chicago')\n",
|
||||
" dt_cst = cst_tz.localize(datetime.strptime(dt_str, '%Y-%m-%d/%H:%M:%S'))\n",
|
||||
" \n",
|
||||
" # Calculate the remainder of minutes in the hour\n",
|
||||
" minute_remainder = dt_cst.minute % 10\n",
|
||||
" \n",
|
||||
" if minute_remainder >= 5:\n",
|
||||
" # Round up to the next 10-minute interval\n",
|
||||
" dt_cst += timedelta(minutes=10 - minute_remainder)\n",
|
||||
" else:\n",
|
||||
" # Round down to the previous 10-minute interval\n",
|
||||
" dt_cst -= timedelta(minutes=minute_remainder)\n",
|
||||
" \n",
|
||||
" # Ensure the resulting datetime is valid (no 23:60:00 or similar issues)\n",
|
||||
" while dt_cst.minute % 10 != 0:\n",
|
||||
" if dt_cst.minute > 50:\n",
|
||||
" dt_cst += timedelta(hours=1)\n",
|
||||
" dt_cst -= timedelta(minutes=dt_cst.minute % 60)\n",
|
||||
" else:\n",
|
||||
" dt_cst -= timedelta(minutes=dt_cst.minute % 10)\n",
|
||||
" \n",
|
||||
" return int(dt_cst.timestamp()*1000)\n",
|
||||
"\n",
|
||||
"# Transform the data and send it to the MQTT broker in chunks of 20\n",
|
||||
"transformed_data = []\n",
|
||||
"with open('/Users/nico/Downloads/history_data_default.csv', 'r') as csvfile:\n",
|
||||
" reader = csv.DictReader(csvfile)\n",
|
||||
" for row in reader:\n",
|
||||
" dt = row['Time']\n",
|
||||
" value = json.loads(row[' Value'])[\"plcpond\"][\"air_comp_val\"]\n",
|
||||
" if value[0] == 1:\n",
|
||||
" transformed_data.append({\"ts\": convert_datetime_to_timestamp(dt), \"values\": {\"air_comp_val\": value[1]}})\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"chunk_size = 20\n",
|
||||
"for i in range(0, len(transformed_data), chunk_size):\n",
|
||||
" chunk = transformed_data[i:i + chunk_size]\n",
|
||||
" for x in chunk:\n",
|
||||
" client.publish(\"v1/devices/me/telemetry\", json.dumps(x))\n",
|
||||
" time.sleep(1)\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"\n",
|
||||
"client.disconnect()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "aws",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user