MQTTSession.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { distinctUntilChanged, pluck } from "rxjs/operators"
  2. import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
  3. import { MQTTConnection } from "~/helpers/realtime/MQTTConnection"
  4. import {
  5. HoppRealtimeLog,
  6. HoppRealtimeLogLine,
  7. } from "~/helpers/types/HoppRealtimeLog"
  8. type HoppMQTTRequest = {
  9. endpoint: string
  10. }
  11. type HoppMQTTSession = {
  12. request: HoppMQTTRequest
  13. subscriptionState: boolean
  14. log: HoppRealtimeLog
  15. socket: MQTTConnection
  16. }
  17. const defaultMQTTRequest: HoppMQTTRequest = {
  18. endpoint: "wss://test.mosquitto.org:8081",
  19. }
  20. const defaultMQTTSession: HoppMQTTSession = {
  21. request: defaultMQTTRequest,
  22. subscriptionState: false,
  23. socket: new MQTTConnection(),
  24. log: [],
  25. }
  26. const dispatchers = defineDispatchers({
  27. setRequest(
  28. _: HoppMQTTSession,
  29. { newRequest }: { newRequest: HoppMQTTRequest }
  30. ) {
  31. return {
  32. request: newRequest,
  33. }
  34. },
  35. setEndpoint(_: HoppMQTTSession, { newEndpoint }: { newEndpoint: string }) {
  36. return {
  37. request: {
  38. endpoint: newEndpoint,
  39. },
  40. }
  41. },
  42. setConn(_: HoppMQTTSession, { socket }: { socket: MQTTConnection }) {
  43. return {
  44. socket,
  45. }
  46. },
  47. setSubscriptionState(_: HoppMQTTSession, { state }: { state: boolean }) {
  48. return {
  49. subscriptionState: state,
  50. }
  51. },
  52. setLog(_: HoppMQTTSession, { log }: { log: HoppRealtimeLog }) {
  53. return {
  54. log,
  55. }
  56. },
  57. addLogLine(curr: HoppMQTTSession, { line }: { line: HoppRealtimeLogLine }) {
  58. return {
  59. log: [...curr.log, line],
  60. }
  61. },
  62. })
  63. const MQTTSessionStore = new DispatchingStore(defaultMQTTSession, dispatchers)
  64. export function setMQTTRequest(newRequest?: HoppMQTTRequest) {
  65. MQTTSessionStore.dispatch({
  66. dispatcher: "setRequest",
  67. payload: {
  68. newRequest: newRequest ?? defaultMQTTRequest,
  69. },
  70. })
  71. }
  72. export function setMQTTEndpoint(newEndpoint: string) {
  73. MQTTSessionStore.dispatch({
  74. dispatcher: "setEndpoint",
  75. payload: {
  76. newEndpoint,
  77. },
  78. })
  79. }
  80. export function setMQTTConn(socket: MQTTConnection) {
  81. MQTTSessionStore.dispatch({
  82. dispatcher: "setConn",
  83. payload: {
  84. socket,
  85. },
  86. })
  87. }
  88. export function setMQTTSubscriptionState(state: boolean) {
  89. MQTTSessionStore.dispatch({
  90. dispatcher: "setSubscriptionState",
  91. payload: {
  92. state,
  93. },
  94. })
  95. }
  96. export function setMQTTLog(log: HoppRealtimeLog) {
  97. MQTTSessionStore.dispatch({
  98. dispatcher: "setLog",
  99. payload: {
  100. log,
  101. },
  102. })
  103. }
  104. export function addMQTTLogLine(line: HoppRealtimeLogLine) {
  105. MQTTSessionStore.dispatch({
  106. dispatcher: "addLogLine",
  107. payload: {
  108. line,
  109. },
  110. })
  111. }
  112. export const MQTTRequest$ = MQTTSessionStore.subject$.pipe(
  113. pluck("request"),
  114. distinctUntilChanged()
  115. )
  116. export const MQTTEndpoint$ = MQTTSessionStore.subject$.pipe(
  117. pluck("request", "endpoint"),
  118. distinctUntilChanged()
  119. )
  120. export const MQTTConnectingState$ = MQTTSessionStore.subject$.pipe(
  121. pluck("connectingState"),
  122. distinctUntilChanged()
  123. )
  124. export const MQTTConnectionState$ = MQTTSessionStore.subject$.pipe(
  125. pluck("connectionState"),
  126. distinctUntilChanged()
  127. )
  128. export const MQTTSubscriptionState$ = MQTTSessionStore.subject$.pipe(
  129. pluck("subscriptionState"),
  130. distinctUntilChanged()
  131. )
  132. export const MQTTConn$ = MQTTSessionStore.subject$.pipe(
  133. pluck("socket"),
  134. distinctUntilChanged()
  135. )
  136. export const MQTTLog$ = MQTTSessionStore.subject$.pipe(
  137. pluck("log"),
  138. distinctUntilChanged()
  139. )