Files
ThingsBoard/Code Snippets/last-will-test.py
Nico Melone f9df74cd97 new file: Code Snippets/input-test.py
new file:   Code Snippets/ivs-mqtt-client.ipynb
new file:   Code Snippets/ivs-player.html
new file:   Code Snippets/last-will-test.py
2025-07-29 16:49:01 -05:00

36 lines
1.0 KiB
Python

from paho.mqtt import client as mqtt
import time
import random
import os
import signal
import json
BROKER = 'hp.henrypump.cloud' # or your broker IP/domain
PORT = 1883
TOPIC = "v1/devices/me/attributes"
WILL_MSG = json.dumps({"isActive": False})
CLIENT_A_ID = f'client-a-{random.randint(0, 1000)}'
def start_client_a_with_will():
client = mqtt.Client(client_id=CLIENT_A_ID, clean_session=True)
client.will_set(TOPIC, WILL_MSG, qos=1, retain=False)
client.username_pw_set("e5xv3wfi1oa44ty2ydv2")
client.connect(BROKER, PORT)
print("[Client A] Connected with last will set.")
client.publish("v1/devices/me/attributes", json.dumps({"isActive": True}))
client.loop_start()
return client
def simulate_crash(client):
print("[Client A] Simulating crash (kill connection)...")
client._sock.close() # simulate ungraceful disconnect
client.loop_stop()
if __name__ == "__main__":
client_a = start_client_a_with_will()
time.sleep(2) # wait for connections to establish
raise Exception