Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

00_sample.py 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import json
  2. def initialize():
  3. """
  4. This function is invoked after the script file has been loaded.
  5. It will be executed only one time.
  6. """
  7. print("Hello World from sample script.")
  8. def destroy():
  9. """
  10. This function is invoked when the script is unloaded due to a script file update etc.
  11. """
  12. print("Bye from sample script.")
  13. def on_validate_client_connection(context):
  14. """
  15. This function is invoked whenever a client wants to connect. It can be used to validate the connection.
  16. """
  17. print(context)
  18. # Access some custom data here which was inserted upon connect and may use it for validation.
  19. mqtt_net_server.write_shared_data(context["client_id"], {"custom_value_1": 1, "custom_value_2": True})
  20. return
  21. # Supported results:
  22. # * success
  23. # * unspecified_error
  24. # * malformed_packet
  25. # * protocol_error
  26. # * implementation_specific_error
  27. # * unsupported_protocol_version
  28. # * client_identifier_not_valid
  29. # * bad_user_name_or_password
  30. # * not_authorized
  31. # * server_unavailable
  32. # * server_busy
  33. # * banned
  34. # * bad_authentication_method
  35. # * topic_name_invalid
  36. # * packet_too_large
  37. # * quota_exceeded
  38. # * payload_format_invalid
  39. # * retain_not_supported
  40. # * qos_not_supported
  41. # * use_another_server
  42. # * server_moved
  43. # * connection_rate_exceeded
  44. if context["client_id"] != "test_client":
  45. context["result"] = "bad_user_name_or_password"
  46. return
  47. if context["username"] != "bud spencer":
  48. context["result"] = "bad_user_name_or_password"
  49. return
  50. if context["password_string"] != "secret":
  51. context["result"] = "bad_user_name_or_password"
  52. print(context)
  53. def on_intercept_subscription(context):
  54. """
  55. This function is invoked whenever a client wants to subscribe to a topic.
  56. """
  57. print("Client '{client_id}' want's to subscribe to topic '{topic}'.".format(client_id=context["client_id"], topic=context["topic"]))
  58. def on_intercept_application_message(context):
  59. """
  60. This function is invoked for every processed application message. It also allows modifying
  61. the message or cancel processing at all.
  62. """
  63. client_id = context["client_id"]
  64. if client_id != None:
  65. shared_data = mqtt_net_server.read_shared_data(context["client_id"], {})
  66. print(shared_data)
  67. if context["topic"] == "topic_with_response":
  68. json_payload = {
  69. "hello": "world",
  70. "x": 1,
  71. "y": True,
  72. "z": None
  73. }
  74. application_message = {
  75. "retain": False,
  76. "topic": "reply",
  77. "payload": json.dumps(json_payload)
  78. }
  79. mqtt_net_server.publish(application_message)
  80. print("Client '{client_id}' published topic '{topic}'.".format(client_id=context["client_id"], topic=context["topic"]))
  81. def on_client_connected(event_args):
  82. """
  83. This function is called whenever a client has passed the validation is connected.
  84. """
  85. print("Client '{client_id}' is now connected.".format(client_id=event_args["client_id"]))
  86. def on_client_disconnected(event_args):
  87. """
  88. This function is called whenever a client has disconnected.
  89. """
  90. print("Client '{client_id}' is now disconnected (type = {type}).".format(client_id=event_args["client_id"], type=event_args["type"]))
  91. def on_client_subscribed_topic(event_args):
  92. """
  93. This function is called whenever a client has subscribed to a topic (when allowed).
  94. """
  95. print("Client '{client_id}' has subscribed to '{topic}'.".format(client_id=event_args["client_id"], topic=event_args["topic"]))
  96. def on_client_unsubscribed_topic(event_args):
  97. """
  98. This function is called whenever a client has unsubscribed from a topic.
  99. """
  100. print("Client '{client_id}' has unsubscribed from '{topic}'.".format(client_id=event_args["client_id"], topic=event_args["topic"]))