subscribeoptions.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """
  2. *******************************************************************
  3. Copyright (c) 2017, 2019 IBM Corp.
  4. All rights reserved. This program and the accompanying materials
  5. are made available under the terms of the Eclipse Public License v2.0
  6. and Eclipse Distribution License v1.0 which accompany this distribution.
  7. The Eclipse Public License is available at
  8. http://www.eclipse.org/legal/epl-v20.html
  9. and the Eclipse Distribution License is available at
  10. http://www.eclipse.org/org/documents/edl-v10.php.
  11. Contributors:
  12. Ian Craggs - initial implementation and/or documentation
  13. *******************************************************************
  14. """
  15. class MQTTException(Exception):
  16. pass
  17. class SubscribeOptions:
  18. """The MQTT v5.0 subscribe options class.
  19. The options are:
  20. qos: As in MQTT v3.1.1.
  21. noLocal: True or False. If set to True, the subscriber will not receive its own publications.
  22. retainAsPublished: True or False. If set to True, the retain flag on received publications will be as set
  23. by the publisher.
  24. retainHandling: RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB or RETAIN_DO_NOT_SEND
  25. Controls when the broker should send retained messages:
  26. - RETAIN_SEND_ON_SUBSCRIBE: on any successful subscribe request
  27. - RETAIN_SEND_IF_NEW_SUB: only if the subscribe request is new
  28. - RETAIN_DO_NOT_SEND: never send retained messages
  29. """
  30. # retain handling options
  31. RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB, RETAIN_DO_NOT_SEND = range(
  32. 3)
  33. def __init__(
  34. self,
  35. qos: int = 0,
  36. noLocal: bool = False,
  37. retainAsPublished: bool = False,
  38. retainHandling: int = RETAIN_SEND_ON_SUBSCRIBE,
  39. ):
  40. """
  41. qos: 0, 1 or 2. 0 is the default.
  42. noLocal: True or False. False is the default and corresponds to MQTT v3.1.1 behavior.
  43. retainAsPublished: True or False. False is the default and corresponds to MQTT v3.1.1 behavior.
  44. retainHandling: RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB or RETAIN_DO_NOT_SEND
  45. RETAIN_SEND_ON_SUBSCRIBE is the default and corresponds to MQTT v3.1.1 behavior.
  46. """
  47. object.__setattr__(self, "names",
  48. ["QoS", "noLocal", "retainAsPublished", "retainHandling"])
  49. self.QoS = qos # bits 0,1
  50. self.noLocal = noLocal # bit 2
  51. self.retainAsPublished = retainAsPublished # bit 3
  52. self.retainHandling = retainHandling # bits 4 and 5: 0, 1 or 2
  53. if self.retainHandling not in (0, 1, 2):
  54. raise AssertionError(f"Retain handling should be 0, 1 or 2, not {self.retainHandling}")
  55. if self.QoS not in (0, 1, 2):
  56. raise AssertionError(f"QoS should be 0, 1 or 2, not {self.QoS}")
  57. def __setattr__(self, name, value):
  58. if name not in self.names:
  59. raise MQTTException(
  60. f"{name} Attribute name must be one of {self.names}")
  61. object.__setattr__(self, name, value)
  62. def pack(self):
  63. if self.retainHandling not in (0, 1, 2):
  64. raise AssertionError(f"Retain handling should be 0, 1 or 2, not {self.retainHandling}")
  65. if self.QoS not in (0, 1, 2):
  66. raise AssertionError(f"QoS should be 0, 1 or 2, not {self.QoS}")
  67. noLocal = 1 if self.noLocal else 0
  68. retainAsPublished = 1 if self.retainAsPublished else 0
  69. data = [(self.retainHandling << 4) | (retainAsPublished << 3) |
  70. (noLocal << 2) | self.QoS]
  71. return bytes(data)
  72. def unpack(self, buffer):
  73. b0 = buffer[0]
  74. self.retainHandling = ((b0 >> 4) & 0x03)
  75. self.retainAsPublished = ((b0 >> 3) & 0x01) == 1
  76. self.noLocal = ((b0 >> 2) & 0x01) == 1
  77. self.QoS = (b0 & 0x03)
  78. if self.retainHandling not in (0, 1, 2):
  79. raise AssertionError(f"Retain handling should be 0, 1 or 2, not {self.retainHandling}")
  80. if self.QoS not in (0, 1, 2):
  81. raise AssertionError(f"QoS should be 0, 1 or 2, not {self.QoS}")
  82. return 1
  83. def __repr__(self):
  84. return str(self)
  85. def __str__(self):
  86. return "{QoS="+str(self.QoS)+", noLocal="+str(self.noLocal) +\
  87. ", retainAsPublished="+str(self.retainAsPublished) +\
  88. ", retainHandling="+str(self.retainHandling)+"}"
  89. def json(self):
  90. data = {
  91. "QoS": self.QoS,
  92. "noLocal": self.noLocal,
  93. "retainAsPublished": self.retainAsPublished,
  94. "retainHandling": self.retainHandling,
  95. }
  96. return data