MQTTSession.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { pluck, distinctUntilChanged } from "rxjs/operators"
  2. import { Client as MQTTClient } from "paho-mqtt"
  3. import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
  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. connectingState: boolean
  14. connectionState: boolean
  15. subscriptionState: boolean
  16. log: HoppRealtimeLog
  17. socket: MQTTClient | null
  18. }
  19. const defaultMQTTRequest: HoppMQTTRequest = {
  20. endpoint: "wss://test.mosquitto.org:8081",
  21. }
  22. const defaultMQTTSession: HoppMQTTSession = {
  23. request: defaultMQTTRequest,
  24. connectionState: false,
  25. connectingState: false,
  26. subscriptionState: false,
  27. socket: null,
  28. log: [],
  29. }
  30. const dispatchers = defineDispatchers({
  31. setRequest(
  32. _: HoppMQTTSession,
  33. { newRequest }: { newRequest: HoppMQTTRequest }
  34. ) {
  35. return {
  36. request: newRequest,
  37. }
  38. },
  39. setEndpoint(_: HoppMQTTSession, { newEndpoint }: { newEndpoint: string }) {
  40. return {
  41. request: {
  42. endpoint: newEndpoint,
  43. },
  44. }
  45. },
  46. setSocket(_: HoppMQTTSession, { socket }: { socket: MQTTClient }) {
  47. return {
  48. socket,
  49. }
  50. },
  51. setConnectionState(_: HoppMQTTSession, { state }: { state: boolean }) {
  52. return {
  53. connectionState: state,
  54. }
  55. },
  56. setConnectingState(_: HoppMQTTSession, { state }: { state: boolean }) {
  57. return {
  58. connectingState: state,
  59. }
  60. },
  61. setSubscriptionState(_: HoppMQTTSession, { state }: { state: boolean }) {
  62. return {
  63. subscriptionState: state,
  64. }
  65. },
  66. setLog(_: HoppMQTTSession, { log }: { log: HoppRealtimeLog }) {
  67. return {
  68. log,
  69. }
  70. },
  71. addLogLine(curr: HoppMQTTSession, { line }: { line: HoppRealtimeLogLine }) {
  72. return {
  73. log: [...curr.log, line],
  74. }
  75. },
  76. })
  77. const MQTTSessionStore = new DispatchingStore(defaultMQTTSession, dispatchers)
  78. export function setMQTTRequest(newRequest?: HoppMQTTRequest) {
  79. MQTTSessionStore.dispatch({
  80. dispatcher: "setRequest",
  81. payload: {
  82. newRequest: newRequest ?? defaultMQTTRequest,
  83. },
  84. })
  85. }
  86. export function setMQTTEndpoint(newEndpoint: string) {
  87. MQTTSessionStore.dispatch({
  88. dispatcher: "setEndpoint",
  89. payload: {
  90. newEndpoint,
  91. },
  92. })
  93. }
  94. export function setMQTTSocket(socket: MQTTClient) {
  95. MQTTSessionStore.dispatch({
  96. dispatcher: "setSocket",
  97. payload: {
  98. socket,
  99. },
  100. })
  101. }
  102. export function setMQTTConnectionState(state: boolean) {
  103. MQTTSessionStore.dispatch({
  104. dispatcher: "setConnectionState",
  105. payload: {
  106. state,
  107. },
  108. })
  109. }
  110. export function setMQTTConnectingState(state: boolean) {
  111. MQTTSessionStore.dispatch({
  112. dispatcher: "setConnectingState",
  113. payload: {
  114. state,
  115. },
  116. })
  117. }
  118. export function setMQTTSubscriptionState(state: boolean) {
  119. MQTTSessionStore.dispatch({
  120. dispatcher: "setSubscriptionState",
  121. payload: {
  122. state,
  123. },
  124. })
  125. }
  126. export function setMQTTLog(log: HoppRealtimeLog) {
  127. MQTTSessionStore.dispatch({
  128. dispatcher: "setLog",
  129. payload: {
  130. log,
  131. },
  132. })
  133. }
  134. export function addMQTTLogLine(line: HoppRealtimeLogLine) {
  135. MQTTSessionStore.dispatch({
  136. dispatcher: "addLogLine",
  137. payload: {
  138. line,
  139. },
  140. })
  141. }
  142. export const MQTTRequest$ = MQTTSessionStore.subject$.pipe(
  143. pluck("request"),
  144. distinctUntilChanged()
  145. )
  146. export const MQTTEndpoint$ = MQTTSessionStore.subject$.pipe(
  147. pluck("request", "endpoint"),
  148. distinctUntilChanged()
  149. )
  150. export const MQTTConnectingState$ = MQTTSessionStore.subject$.pipe(
  151. pluck("connectingState"),
  152. distinctUntilChanged()
  153. )
  154. export const MQTTConnectionState$ = MQTTSessionStore.subject$.pipe(
  155. pluck("connectionState"),
  156. distinctUntilChanged()
  157. )
  158. export const MQTTSubscriptionState$ = MQTTSessionStore.subject$.pipe(
  159. pluck("subscriptionState"),
  160. distinctUntilChanged()
  161. )
  162. export const MQTTSocket$ = MQTTSessionStore.subject$.pipe(
  163. pluck("socket"),
  164. distinctUntilChanged()
  165. )
  166. export const MQTTLog$ = MQTTSessionStore.subject$.pipe(
  167. pluck("log"),
  168. distinctUntilChanged()
  169. )