No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

00_sample.py 3.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 on_validate_client_connection(context):
  9. """
  10. This function is invoked whenever a client wants to connect. It can be used to validate the connection.
  11. """
  12. print(context)
  13. mqtt_net_server.write_shared_data(context["client_id"], {"custom_value_1": 1, "custom_value_2": True})
  14. return
  15. if context["client_id"] != "test_client":
  16. context["result"] = "connection_refused_not_authorized"
  17. return
  18. if context["username"] != "bud spencer":
  19. context["result"] = "connection_refused_not_authorized"
  20. return
  21. if context["password"] != "secret":
  22. context["result"] = "connection_refused_not_authorized"
  23. print(context)
  24. def on_intercept_subscription(context):
  25. """
  26. This function is invoked whenever a client wants to subscribe to a topic.
  27. """
  28. print("Client '{client_id}' want's to subscribe to topic '{topic}'.".format(client_id=context["client_id"], topic=context["topic"]))
  29. def on_intercept_application_message(context):
  30. """
  31. This function is invoked for every processed application message. It also allows modifying
  32. the message or cancel processing at all.
  33. """
  34. client_id = context["client_id"]
  35. if client_id != None:
  36. shared_data = mqtt_net_server.read_shared_data(context["client_id"], {})
  37. print(shared_data)
  38. if context["topic"] == "topic_with_response":
  39. json_payload = {
  40. "hello": "world",
  41. "x": 1,
  42. "y": True,
  43. "z": None
  44. }
  45. application_message = {
  46. "retain": False,
  47. "topic": "reply",
  48. "payload": json.dumps(json_payload)
  49. }
  50. mqtt_net_server.publish(application_message)
  51. print("Client '{client_id}' published topic '{topic}'.".format(client_id=context["client_id"], topic=context["topic"]))
  52. def on_client_connected(event_args):
  53. """
  54. This function is called whenever a client has passed the validation is connected.
  55. """
  56. print("Client '{client_id}' is now connected.".format(client_id=event_args["client_id"]))
  57. def on_client_disconnected(event_args):
  58. """
  59. This function is called whenever a client has disconnected.
  60. """
  61. print("Client '{client_id}' is now disconnected (type = {type}).".format(client_id=event_args["client_id"], type=event_args["type"]))
  62. def on_client_subscribed_topic(event_args):
  63. """
  64. This function is called whenever a client has subscribed to a topic (when allowed).
  65. """
  66. print("Client '{client_id}' has subscribed to '{topic}'.".format(client_id=event_args["client_id"], topic=event_args["topic"]))
  67. def on_client_unsubscribed_topic(event_args):
  68. """
  69. This function is called whenever a client has unsubscribed from a topic.
  70. """
  71. print("Client '{client_id}' has unsubscribed from '{topic}'.".format(client_id=event_args["client_id"], topic=event_args["topic"]))