MethodChannel.js

  1. import { logger, validateKeyCode, validateScooter } from './utils';
  2. import {} from 'react-native';
  3. import { EventType } from './BleResType';
  4. import { eventReceiver, Spec } from './BleModule';
  5. /**
  6. * Function that takes an event type and a listener,
  7. * check the listener count to have only single subscription,
  8. * and returns a event emitter subscription.
  9. *
  10. * @see EmitterSubscription {@link https://reactnative.dev/docs/emittersubscription}
  11. * @param {EventType} eventType - The event type to listen for.
  12. * @param {EventListener<EventType>} listener - The function to be called when the event is emitted.
  13. * @returns {EmitterSubscription} A function that takes a single argument of type T and returns void.
  14. * @example
  15. * registerListener(EventType.INITIALIZE, (data) => {
  16. * console.debug(data)
  17. * })
  18. */
  19. export function registerListener(eventType, listener) {
  20. if (eventReceiver.listenerCount(eventType) > 0) {
  21. eventReceiver.removeAllListeners(eventType);
  22. }
  23. return eventReceiver.addListener(eventType, listener);
  24. }
  25. /**
  26. * Function that calls the initializing method named `SegwayBleManagerModule.init` and then calls the
  27. * `registerListener` function with the `EventType.INITIALIZE` parameter.
  28. *
  29. * @param {string} secretKey - The secret key you received from the Spec.ai team.
  30. * @param {string} operatorCode - The operator code you received from the Spec team.
  31. * @param {boolean} isDebug - If true, the SDK will log all the events to the console.
  32. * @example
  33. * init(e0382c1944874be7a1ed7f4546e0f412, B40006, true);
  34. */
  35. export function initialize(secretKey, operatorCode, isDebug) {
  36. validateKeyCode(operatorCode, secretKey, isDebug);
  37. registerListener(EventType.INITIALIZE, function (data) {
  38. logger.debug(JSON.stringify(data));
  39. return data?.result;
  40. });
  41. Spec.init(secretKey, operatorCode, isDebug);
  42. }
  43. /**
  44. * Function that calls the `SegwayBleManagerModule.connect` function and then calls the
  45. * `registerListener` function with the `EventType.CONNECT` parameter.
  46. *
  47. * @param {string} deviceMac - The MAC address of the scooter.
  48. * @param {string} deviceKey - The device key is a unique identifier for the scooter.
  49. * It is a 16-character string.
  50. * @param {string} iotImei - The IMEI number of the scooter.
  51. */
  52. export function ioTConnect(deviceMac, deviceKey, iotImei) {
  53. validateScooter(deviceMac, deviceKey, iotImei);
  54. registerListener(EventType.CONNECT, function (data) {
  55. logger.debug(JSON.stringify(data));
  56. return data?.result;
  57. });
  58. Spec.connect(deviceMac, deviceKey, iotImei);
  59. }
  60. /**
  61. * Function that calls the `SegwayBleManagerModule.disconnect` function and then calls the
  62. * `registerListener` function with the `EventType.DISCONNECT` parameter.
  63. *
  64. * @example
  65. * registerListener();
  66. */
  67. export function ioTDisconnect() {
  68. registerListener(EventType.DISCONNECT, function (data) {
  69. logger.debug(JSON.stringify(data));
  70. return data?.result;
  71. });
  72. Spec.disconnect();
  73. }
  74. /**
  75. * Function that calls the `SegwayBleManagerModule.unLock` function and then calls the
  76. * `registerListener` function with the `EventType.UNLOCK` parameter.
  77. *
  78. * @example
  79. * registerListener();
  80. */
  81. export function unLockScooter() {
  82. registerListener(EventType.UNLOCK, function (data) {
  83. logger.debug(JSON.stringify(data));
  84. return data?.result;
  85. });
  86. Spec.unLock();
  87. }
  88. /**
  89. * Function that calls the `SegwayBleManagerModule.lock` function and then calls the
  90. * `registerListener` function with the `EventType.LOCK` parameter.
  91. *
  92. * @example
  93. * lockScooter();
  94. */
  95. export function lockScooter() {
  96. registerListener(EventType.LOCK, function (data) {
  97. logger.debug(JSON.stringify(data));
  98. return data?.result;
  99. });
  100. Spec.lock();
  101. }
  102. /**
  103. * Function that calls the `SegwayBleManagerModule.openBatteryCover` function and then calls the
  104. * `registerListener` function with the `EventType.OPEN_COVER` parameter.
  105. *
  106. * @example
  107. * onBatteryCover();
  108. */
  109. export function openBatteryCover() {
  110. registerListener(EventType.OPEN_COVER, function (data) {
  111. logger.debug(JSON.stringify(data));
  112. return data?.result;
  113. });
  114. Spec.openBatteryCover();
  115. }
  116. /**
  117. * Function that calls the `SegwayBleManagerModule.openSaddle` function and then calls the
  118. * `registerListener` function with the `EventType.OPEN_SADDLE` parameter.
  119. *
  120. * @example
  121. * openSaddle();
  122. */
  123. export function openSaddle() {
  124. registerListener(EventType.OPEN_SADDLE, function (data) {
  125. logger.debug(JSON.stringify(data));
  126. return data?.result;
  127. });
  128. Spec.openSaddle();
  129. }
  130. /**
  131. * Function that calls the `SegwayBleManagerModule.openTailBox` function and then calls the
  132. * `registerListener` function with the `EventType.OPEN_TAIL_BOX` parameter.
  133. *
  134. * @example
  135. * openTailBox();
  136. */
  137. export function openTailBox() {
  138. registerListener(EventType.OPEN_TAIL_BOX, function (data) {
  139. logger.debug(JSON.stringify(data));
  140. return data?.result;
  141. });
  142. Spec.openTailBox();
  143. }
  144. /**
  145. * Function that calls the `SegwayBleManagerModule.queryVehicleInformation` function and then calls the
  146. * `registerListener` function with the `EventType.VEHICLE_INFO` parameter.
  147. *
  148. * @param {EventListener<EventType.VEHICLE_INFO>} listener - The function to be called when the event is emitted.
  149. * @example
  150. * queryVehicleInfo((data) => console.debug(data));
  151. */
  152. export function queryVehicleInfo(listener) {
  153. Spec.queryVehicleInformation();
  154. return registerListener(EventType.VEHICLE_INFO, listener);
  155. }
  156. /**
  157. * Function that calls the `SegwayBleManagerModule.queryIoTInformation` function and then calls the
  158. * `registerListener` function with the `EventType.IOT_INFO` parameter.
  159. *
  160. * @param {EventListener<EventType.IOT_INFO>} listener - The function to be called when the event is emitted.
  161. * @example
  162. * queryIoTInfo((data) => console.debug(data));
  163. */
  164. export function queryIoTInfo(listener) {
  165. Spec.queryIoTInformation();
  166. return registerListener(EventType.IOT_INFO, listener);
  167. }