subscribeoptions.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 v1.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-v10.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. import sys
  16. class MQTTException(Exception):
  17. pass
  18. class SubscribeOptions(object):
  19. """The MQTT v5.0 subscribe options class.
  20. The options are:
  21. qos: As in MQTT v3.1.1.
  22. noLocal: True or False. If set to True, the subscriber will not receive its own publications.
  23. retainAsPublished: True or False. If set to True, the retain flag on received publications will be as set
  24. by the publisher.
  25. retainHandling: RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB or RETAIN_DO_NOT_SEND
  26. Controls when the broker should send retained messages:
  27. - RETAIN_SEND_ON_SUBSCRIBE: on any successful subscribe request
  28. - RETAIN_SEND_IF_NEW_SUB: only if the subscribe request is new
  29. - RETAIN_DO_NOT_SEND: never send retained messages
  30. """
  31. # retain handling options
  32. RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB, RETAIN_DO_NOT_SEND = range(
  33. 0, 3)
  34. def __init__(self, qos=0, noLocal=False, retainAsPublished=False, retainHandling=RETAIN_SEND_ON_SUBSCRIBE):
  35. """
  36. qos: 0, 1 or 2. 0 is the default.
  37. noLocal: True or False. False is the default and corresponds to MQTT v3.1.1 behavior.
  38. retainAsPublished: True or False. False is the default and corresponds to MQTT v3.1.1 behavior.
  39. retainHandling: RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB or RETAIN_DO_NOT_SEND
  40. RETAIN_SEND_ON_SUBSCRIBE is the default and corresponds to MQTT v3.1.1 behavior.
  41. """
  42. object.__setattr__(self, "names",
  43. ["QoS", "noLocal", "retainAsPublished", "retainHandling"])
  44. self.QoS = qos # bits 0,1
  45. self.noLocal = noLocal # bit 2
  46. self.retainAsPublished = retainAsPublished # bit 3
  47. self.retainHandling = retainHandling # bits 4 and 5: 0, 1 or 2
  48. assert self.QoS in [0, 1, 2]
  49. assert self.retainHandling in [
  50. 0, 1, 2], "Retain handling should be 0, 1 or 2"
  51. def __setattr__(self, name, value):
  52. if name not in self.names:
  53. raise MQTTException(
  54. name + " Attribute name must be one of "+str(self.names))
  55. object.__setattr__(self, name, value)
  56. def pack(self):
  57. assert self.QoS in [0, 1, 2]
  58. assert self.retainHandling in [
  59. 0, 1, 2], "Retain handling should be 0, 1 or 2"
  60. noLocal = 1 if self.noLocal else 0
  61. retainAsPublished = 1 if self.retainAsPublished else 0
  62. data = [(self.retainHandling << 4) | (retainAsPublished << 3) |
  63. (noLocal << 2) | self.QoS]
  64. if sys.version_info[0] >= 3:
  65. buffer = bytes(data)
  66. else:
  67. buffer = bytearray(data)
  68. return buffer
  69. def unpack(self, buffer):
  70. b0 = buffer[0]
  71. self.retainHandling = ((b0 >> 4) & 0x03)
  72. self.retainAsPublished = True if ((b0 >> 3) & 0x01) == 1 else False
  73. self.noLocal = True if ((b0 >> 2) & 0x01) == 1 else False
  74. self.QoS = (b0 & 0x03)
  75. assert self.retainHandling in [
  76. 0, 1, 2], "Retain handling should be 0, 1 or 2, not %d" % self.retainHandling
  77. assert self.QoS in [
  78. 0, 1, 2], "QoS should be 0, 1 or 2, not %d" % self.QoS
  79. return 1
  80. def __repr__(self):
  81. return str(self)
  82. def __str__(self):
  83. return "{QoS="+str(self.QoS)+", noLocal="+str(self.noLocal) +\
  84. ", retainAsPublished="+str(self.retainAsPublished) +\
  85. ", retainHandling="+str(self.retainHandling)+"}"
  86. def json(self):
  87. data = {
  88. "QoS": self.QoS,
  89. "noLocal": self.noLocal,
  90. "retainAsPublished": self.retainAsPublished,
  91. "retainHandling": self.retainHandling,
  92. }
  93. return data