new file: Code Snippets/ivs-mqtt-client.ipynb new file: Code Snippets/ivs-player.html new file: Code Snippets/last-will-test.py
36 lines
1.0 KiB
Python
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
|
|
|