1+ #!/usr/bin/env python3
2+ """
3+ Demonstration script to show MQTT-PAHO 2.0 compatibility.
4+
5+ This script demonstrates that the MqttTransport can work with both
6+ paho-mqtt 1.x and 2.x versions automatically.
7+ """
8+
9+ import sys
10+ import os
11+
12+ # Add the src directory to the path to import compas_eve
13+ sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), '..' , 'src' ))
14+
15+ def main ():
16+ print ("COMPAS Eve MQTT-PAHO 2.0 Compatibility Demonstration" )
17+ print ("=" * 55 )
18+
19+ try :
20+ # Import and check version compatibility
21+ from compas_eve .mqtt .mqtt_paho import PAHO_MQTT_V2_AVAILABLE
22+ import paho .mqtt
23+
24+ print (f"paho-mqtt version: { paho .mqtt .__version__ } " )
25+ print (f"MQTT-PAHO 2.0 support available: { PAHO_MQTT_V2_AVAILABLE } " )
26+ print ()
27+
28+ # Try to create transport (will fail due to network but shows client creation works)
29+ try :
30+ from compas_eve .mqtt .mqtt_paho import MqttTransport
31+ print ("Attempting to create MqttTransport (will fail due to no broker)..." )
32+ transport = MqttTransport ('nonexistent-broker.local' )
33+ print ("✓ Transport created successfully" )
34+ except Exception as e :
35+ if "No address associated with hostname" in str (e ) or "gaierror" in str (e ):
36+ print ("✓ Client creation successful (expected network error)" )
37+ else :
38+ print (f"❌ Unexpected error: { e } " )
39+ raise
40+
41+ print ()
42+ print ("Compatibility verification:" )
43+ if PAHO_MQTT_V2_AVAILABLE :
44+ from paho .mqtt .enums import CallbackAPIVersion
45+ print (f"✓ Using MQTT-PAHO 2.0 with CallbackAPIVersion.VERSION1" )
46+ print (f"✓ CallbackAPIVersion available: { hasattr (CallbackAPIVersion , 'VERSION1' )} " )
47+ else :
48+ print ("✓ Using MQTT-PAHO 1.x legacy mode" )
49+ print ("✓ No CallbackAPIVersion required" )
50+
51+ print ()
52+ print ("🎉 All compatibility checks passed!" )
53+
54+ except ImportError as e :
55+ print (f"❌ Import error: { e } " )
56+ return 1
57+ except Exception as e :
58+ print (f"❌ Unexpected error: { e } " )
59+ return 1
60+
61+ return 0
62+
63+ if __name__ == "__main__" :
64+ sys .exit (main ())
0 commit comments