Update listing from IoT Hub
This commit is contained in:
@@ -3,15 +3,15 @@ from azure.iot.hub.protocol.models import QuerySpecification, Module
|
|||||||
from azure.iot.hub.models import CloudToDeviceMethod, CloudToDeviceMethodResult
|
from azure.iot.hub.models import CloudToDeviceMethod, CloudToDeviceMethodResult
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from isight_device import iSightDevice
|
from isight_device import iSightDevice
|
||||||
|
import pandas as pd
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
module_id = "thingspro-agent"
|
now = datetime.now(timezone.utc)
|
||||||
method_name = "thingspro-api-v1"
|
|
||||||
payload = '{"method":"GET", "path":"/device/general"}'
|
|
||||||
|
|
||||||
CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_INOX_PROD"))
|
CONNECTION_STRING = str(os.getenv("CONNECTION_STRING_INOX_PROD"))
|
||||||
if CONNECTION_STRING == "":
|
if CONNECTION_STRING == "":
|
||||||
@@ -24,6 +24,36 @@ query_spec = QuerySpecification(query="SELECT * FROM devices")
|
|||||||
|
|
||||||
query_result = registry_manager.query_iot_hub(query_spec)
|
query_result = registry_manager.query_iot_hub(query_spec)
|
||||||
|
|
||||||
devices = []
|
rows = []
|
||||||
for item in query_result.items:
|
for twin in query_result.items:
|
||||||
print(item)
|
rows.append({
|
||||||
|
"device_id": twin.device_id,
|
||||||
|
"number": twin.tags.get("number") if twin.tags else None,
|
||||||
|
"site": twin.tags.get("site") if twin.tags else None,
|
||||||
|
"connection_state": twin.connection_state,
|
||||||
|
"last_activity_time": twin.last_activity_time,
|
||||||
|
})
|
||||||
|
|
||||||
|
df = pd.DataFrame(rows)
|
||||||
|
|
||||||
|
df_sorted = df.sort_values(by=["site", "number"]).reset_index(drop=True)
|
||||||
|
|
||||||
|
print(df_sorted)
|
||||||
|
|
||||||
|
# Compute difference in hours (float)
|
||||||
|
df_sorted["time_since_last_activity_hours"] = df_sorted["last_activity_time"].apply(
|
||||||
|
lambda x: (now - x).total_seconds() / 3600 if pd.notnull(x) else None
|
||||||
|
)
|
||||||
|
|
||||||
|
# Also add a readable string
|
||||||
|
df_sorted["time_since_last_activity_str"] = df_sorted["last_activity_time"].apply(
|
||||||
|
lambda x: str(now - x).split(".")[0] if pd.notnull(x) else None
|
||||||
|
)
|
||||||
|
|
||||||
|
if "last_activity_time" in df_sorted.columns:
|
||||||
|
df_sorted["last_activity_time"] = df_sorted["last_activity_time"].apply(
|
||||||
|
lambda x: x.replace(tzinfo=None) if pd.notnull(x) else x
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
df_sorted.to_excel("iot_devices.xlsx", index=False)
|
||||||
Reference in New Issue
Block a user