@eohjsc/react-native-smart-city 0.2.99 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/README.md +1 -1
  2. package/package.json +6 -4
  3. package/react-native-smart-city.podspec +1 -0
  4. package/src/commons/Action/ItemQuickAction.js +11 -2
  5. package/src/commons/Action/__test__/ItemQuickAction.test.js +11 -6
  6. package/src/commons/ActionGroup/CurtainButtonTemplate.js +10 -5
  7. package/src/commons/ActionGroup/NumberUpDownActionTemplate.js +31 -20
  8. package/src/commons/ActionGroup/OnOffSmartLock/AutoLock/__test__/index.test.js +4 -0
  9. package/src/commons/ActionGroup/OnOffSmartLock/PasscodeList/ItemPasscode.js +1 -1
  10. package/src/commons/ActionGroup/OnOffSmartLock/PasscodeList/__test__/ItemPasscode.test.js +24 -0
  11. package/src/commons/ActionGroup/OnOffSmartLock/SetupGeneratePasscode/__test__/index.test.js +14 -0
  12. package/src/commons/ActionGroup/OptionsDropdownActionTemplate.js +1 -1
  13. package/src/commons/ActionGroup/SmartTiviActionTemplate/SmartTiviActionTemplate.js +3 -2
  14. package/src/commons/ActionGroup/StatesGridActionTemplate.js +8 -4
  15. package/src/commons/ActionGroup/TimerActionTemplate.js +2 -2
  16. package/src/commons/ActionGroup/TwoButtonTemplate/index.js +0 -1
  17. package/src/commons/ActionGroup/__test__/CurtainButtonTemplate.test.js +53 -4
  18. package/src/commons/ActionGroup/__test__/NumberUpDownTemplate.test.js +45 -48
  19. package/src/commons/ActionGroup/__test__/StatesGridActionTemplate.test.js +77 -0
  20. package/src/commons/ActionGroup/__test__/TimerActionTemplate.test.js +58 -6
  21. package/src/commons/ActionGroup/__test__/TwoButtonTemplate.test.js +49 -1
  22. package/src/commons/ActionGroup/__test__/index.test.js +137 -2
  23. package/src/commons/Automate/ItemAutomate.js +1 -3
  24. package/src/commons/Calendar/__test__/Calendar.test.js +33 -0
  25. package/src/commons/Connecting/__test__/Connecting.test.js +19 -2
  26. package/src/commons/ConnectingProcess/DeviceItem/DeviceItem.js +7 -3
  27. package/src/commons/ConnectingProcess/DeviceItem/DeviceItemStyles.js +8 -11
  28. package/src/commons/ConnectingProcess/__test__/Connecting.test.js +136 -3
  29. package/src/commons/ConnectingProcess/__test__/DeviceItem.test.js +3 -2
  30. package/src/commons/ConnectingProcess/index.js +72 -25
  31. package/src/commons/Dashboard/MyPinnedSharedUnit/__test__/MyPinnedSharedUnit.test.js +16 -13
  32. package/src/commons/Dashboard/MyPinnedSharedUnit/index.js +1 -1
  33. package/src/commons/Dashboard/MyUnit/__test__/MyUnit.test.js +0 -5
  34. package/src/commons/Device/ConnectedViewHeader.js +1 -1
  35. package/src/commons/Device/Hanet/ItemHanetDevice.test.js +58 -0
  36. package/src/commons/Device/HistoryChart.js +3 -3
  37. package/src/commons/Device/ItemDevice.js +15 -11
  38. package/src/commons/Device/LinearChart.js +15 -0
  39. package/src/commons/Device/SonosSpeaker/index.js +1 -1
  40. package/src/commons/Explore/__test__/CityItem.test.js +33 -54
  41. package/src/commons/FieldTemplate/ChooseUserField/__test__/index.test.js +19 -14
  42. package/src/commons/FieldTemplate/PasscodeField/__test__/index.test.js +0 -3
  43. package/src/commons/FieldTemplate/ScheduleField/__test__/index.test.js +0 -3
  44. package/src/commons/FieldTemplate/ScheduleField/index.js +2 -2
  45. package/src/commons/Header/HeaderCustom.js +2 -1
  46. package/src/commons/HorizontalPicker/index.js +2 -2
  47. package/src/commons/MediaPlayerDetail/Styles/MediaPlayerDetailStyles.js +0 -6
  48. package/src/commons/MediaPlayerDetail/index.js +24 -55
  49. package/src/commons/Sharing/__test__/DevicePermissionsCheckbox.test.js +0 -1
  50. package/src/commons/SubUnit/Favorites/index.js +2 -3
  51. package/src/commons/SubUnit/OneTap/__test__/SubUnitAutomate.test.js +8 -35
  52. package/src/commons/SubUnit/OneTap/index.js +1 -2
  53. package/src/commons/SubUnit/ShortDetail.js +25 -9
  54. package/src/commons/SubUnit/__test__/Item.test.js +0 -1
  55. package/src/commons/SubUnit/__test__/ShortDetail.test.js +8 -1
  56. package/src/commons/Unit/SharedUnit.js +1 -0
  57. package/src/commons/Unit/__test__/SharedUnit.test.js +38 -183
  58. package/src/commons/UnitSummary/ConfigHistoryChart/index.js +2 -13
  59. package/src/commons/UnitSummary/ConfigHistoryChart.js +22 -13
  60. package/src/commons/WheelDateTimePicker/index.js +2 -2
  61. package/src/configs/API.js +85 -144
  62. package/src/configs/Constants.js +24 -0
  63. package/src/configs/SCConfig.js +2 -0
  64. package/src/context/actionType.ts +8 -0
  65. package/src/context/mockStore.ts +10 -0
  66. package/src/context/reducer.ts +38 -2
  67. package/src/hooks/Common/index.js +2 -0
  68. package/src/hooks/Common/useGGHomeDeviceConnected.js +16 -0
  69. package/src/hooks/Common/useGetIdUser.js +1 -5
  70. package/src/hooks/Common/useSensorsStatus.js +4 -4
  71. package/src/hooks/IoT/__test__/useGGHomeConnection.test.js +198 -0
  72. package/src/hooks/IoT/__test__/useRemoteControl.test.js +198 -0
  73. package/src/hooks/IoT/index.js +4 -0
  74. package/src/hooks/IoT/useGGHomeConnection.js +91 -0
  75. package/src/hooks/IoT/useRemoteControl.js +79 -0
  76. package/src/hooks/index.js +4 -0
  77. package/src/hooks/useReceiveNotifications.js +9 -5
  78. package/src/iot/Monitor.js +3 -2
  79. package/src/iot/RemoteControl/Bluetooth.js +1 -1
  80. package/src/iot/RemoteControl/GoogleHome.js +75 -49
  81. package/src/iot/RemoteControl/Internet.js +1 -1
  82. package/src/iot/RemoteControl/__test__/GoogleHome.test.js +95 -48
  83. package/src/iot/RemoteControl/__test__/Internet.test.js +18 -7
  84. package/src/iot/RemoteControl/__test__/LgThinq.test.js +36 -177
  85. package/src/iot/RemoteControl/index.js +52 -52
  86. package/src/screens/ActivityLog/__test__/index.test.js +38 -23
  87. package/src/screens/ActivityLog/hooks/__test__/index.test.js +51 -90
  88. package/src/screens/ActivityLog/hooks/index.js +1 -1
  89. package/src/screens/ActivityLog/index.js +2 -2
  90. package/src/screens/AddCommon/__test__/SelectSubUnit.test.js +13 -24
  91. package/src/screens/AddCommon/__test__/SelectUnit.test.js +9 -33
  92. package/src/screens/AddLocationMaps/index.js +5 -4
  93. package/src/screens/AddNewAction/SelectAction.js +8 -8
  94. package/src/screens/AddNewAction/SetupSensor.js +7 -7
  95. package/src/screens/AddNewAction/__test__/SelectAction.test.js +10 -91
  96. package/src/screens/AddNewAction/__test__/SelectSensorDevices.test.js +40 -26
  97. package/src/screens/AddNewDevice/ConnectingDevices.js +3 -3
  98. package/src/screens/AddNewDevice/__test__/AddNewDevice.test.js +34 -33
  99. package/src/screens/AddNewDevice/__test__/ConnectDevices.test.js +0 -4
  100. package/src/screens/AddNewDevice/__test__/ConnectingDevices.test.js +21 -21
  101. package/src/screens/AddNewDevice/hooks/ConnectDevices.js +1 -1
  102. package/src/screens/AddNewGateway/PlugAndPlay/ConnectWifiWarning.js +23 -17
  103. package/src/screens/AddNewGateway/PlugAndPlay/GatewayWifiList.js +3 -3
  104. package/src/screens/AddNewGateway/SetupGatewayWifi.js +1 -0
  105. package/src/screens/AddNewGateway/__test__/AddNewGateway.test.js +4 -6
  106. package/src/screens/AddNewGateway/__test__/ConnectedGateway.test.js +0 -4
  107. package/src/screens/AddNewGateway/__test__/ConnectingGateway.test.js +5 -29
  108. package/src/screens/AddNewGateway/__test__/SelectGateway.test.js +0 -4
  109. package/src/screens/AddNewGateway/__test__/SetupGateway.test.js +0 -4
  110. package/src/screens/AddNewOneTap/__test__/AddNewOneTap.test.js +9 -23
  111. package/src/screens/AllCamera/index.js +4 -4
  112. package/src/screens/Automate/MultiUnits.js +8 -8
  113. package/src/screens/Automate/__test__/MultiUnits.test.js +6 -9
  114. package/src/screens/Automate/__test__/index.test.js +7 -12
  115. package/src/screens/Automate/index.js +3 -3
  116. package/src/screens/ConfirmUnitDeletion/__test__/ConfirmUnitDeletion.test.js +36 -8
  117. package/src/screens/ConfirmUnitDeletion/index.js +7 -1
  118. package/src/screens/Device/EditDevice/__test__/EditDevice.test.js +71 -22
  119. package/src/screens/Device/EditDevice/index.js +2 -2
  120. package/src/screens/Device/__test__/detail.test.js +32 -85
  121. package/src/screens/Device/components/DetailHistoryChart.js +1 -1
  122. package/src/screens/Device/components/SensorConnectStatusViewHeader.js +1 -0
  123. package/src/screens/Device/components/SensorDisplayItem.js +5 -2
  124. package/src/screens/Device/detail.js +53 -22
  125. package/src/screens/Device/hooks/useDisconnectedDevice.js +4 -4
  126. package/src/screens/Device/hooks/useFavoriteDevice.js +5 -9
  127. package/src/screens/DeviceInfo/__test__/index.test.js +0 -2
  128. package/src/screens/EditActionsList/index.js +1 -1
  129. package/src/screens/EmergencyContacts/EmergencyContactsSelectContacts.js +1 -1
  130. package/src/screens/EmergencyContacts/__test__/EmergencyContactAddNew.test.js +7 -19
  131. package/src/screens/EmergencyContacts/__test__/EmergencyContactList.test.js +18 -14
  132. package/src/screens/EmergencyContacts/__test__/EmergencyContactsSelectContacts.test.js +30 -31
  133. package/src/screens/EmergencySetting/components/DropDownItem.js +2 -2
  134. package/src/screens/EnterPassword/__test__/EnterPassword.test.js +41 -25
  135. package/src/screens/GuestInfo/__test__/index.test.js +13 -40
  136. package/src/screens/HanetCamera/Detail.js +1 -1
  137. package/src/screens/HanetCamera/__test__/CaptureFaceID.test.js +8 -12
  138. package/src/screens/HanetCamera/__test__/Detail.test.js +27 -42
  139. package/src/screens/HanetCamera/__test__/ManageAccess.test.js +8 -5
  140. package/src/screens/HanetCamera/__test__/MemberInfo.test.js +10 -32
  141. package/src/screens/HanetCamera/components/RequestFaceIDPopup.js +3 -2
  142. package/src/screens/HanetCamera/hooks/__test__/useHanetCheckinData.test.js +43 -35
  143. package/src/screens/HanetCamera/hooks/__test__/useHanetPlaceMembers.test.js +10 -21
  144. package/src/screens/HanetCamera/hooks/useHanetCheckinData.js +11 -11
  145. package/src/screens/HanetCamera/hooks/useHanetPlaceMembers.js +11 -11
  146. package/src/screens/ManageAccess/__test__/ManageAccess.test.js +33 -22
  147. package/src/screens/ManageAccess/hooks/__test__/useManageAccess.test.js +44 -45
  148. package/src/screens/ManageAccess/hooks/index.js +7 -4
  149. package/src/screens/ManageAccess/index.js +1 -1
  150. package/src/screens/MoveToAnotherSubUnit/__test__/index.test.js +35 -12
  151. package/src/screens/MoveToAnotherSubUnit/index.js +5 -5
  152. package/src/screens/Notification/__test__/Notification.test.js +14 -25
  153. package/src/screens/Notification/__test__/NotificationItem.test.js +8 -7
  154. package/src/screens/Notification/components/NotificationItem.js +17 -20
  155. package/src/screens/Notification/index.js +9 -2
  156. package/src/screens/PlayBackCamera/Timer.js +2 -2
  157. package/src/screens/PlayBackCamera/__test__/index.test.js +87 -2
  158. package/src/screens/PlayBackCamera/index.js +22 -6
  159. package/src/screens/ScanChipQR/__test__/ScanChipQR.test.js +7 -20
  160. package/src/screens/ScanChipQR/hooks/index.js +15 -16
  161. package/src/screens/ScanSensorQR/__test__/ScanSensorQR.test.js +8 -24
  162. package/src/screens/ScriptDetail/__test__/index.test.js +17 -86
  163. package/src/screens/ScriptDetail/index.js +16 -11
  164. package/src/screens/SelectUnit/__test__/index.test.js +11 -54
  165. package/src/screens/SelectUnit/index.js +4 -2
  166. package/src/screens/SetSchedule/index.js +9 -9
  167. package/src/screens/SharedUnit/__test__/TabHeader.test.js +0 -2
  168. package/src/screens/Sharing/Components/SensorItem.js +10 -12
  169. package/src/screens/Sharing/InfoMemberUnit.js +1 -1
  170. package/src/screens/Sharing/SelectPermission.js +121 -76
  171. package/src/screens/Sharing/__test__/InfoMemberUnit.test.js +47 -29
  172. package/src/screens/Sharing/__test__/MemberList.test.js +13 -127
  173. package/src/screens/Sharing/__test__/MemberList2.test.js +80 -0
  174. package/src/screens/Sharing/__test__/SelectPermission.test.js +28 -38
  175. package/src/screens/Sharing/__test__/SelectUser.test.js +17 -38
  176. package/src/screens/SideMenuDetail/__test__/index.test.js +12 -23
  177. package/src/screens/SideMenuDetail/index.js +2 -3
  178. package/src/screens/SmartIr/components/GroupButtonByType/GroupButtonByType.js +3 -2
  179. package/src/screens/SubUnit/Detail.js +1 -2
  180. package/src/screens/SubUnit/ManageSubUnit.js +12 -7
  181. package/src/screens/SubUnit/__test__/AddSubUnit.test.js +21 -67
  182. package/src/screens/SubUnit/__test__/Detail.test.js +31 -8
  183. package/src/screens/SubUnit/__test__/EditSubUnit.test.js +21 -89
  184. package/src/screens/SubUnit/__test__/ManageSubUnit.test.js +27 -1
  185. package/src/screens/SubUnit/hooks/__test__/useManageSubUnit.test.js +48 -45
  186. package/src/screens/SubUnit/hooks/useManageSubUnit.js +7 -7
  187. package/src/screens/SyncLGDevice/__test__/AddLGDevice.test.js +14 -90
  188. package/src/screens/Unit/ChooseLocation.js +7 -14
  189. package/src/screens/Unit/Detail.js +30 -75
  190. package/src/screens/Unit/ManageUnit.js +1 -0
  191. package/src/screens/Unit/SelectAddress.js +34 -21
  192. package/src/screens/Unit/SmartAccount.js +4 -4
  193. package/src/screens/Unit/Summaries.js +17 -1
  194. package/src/screens/Unit/__test__/CheckSendEmail.test.js +24 -29
  195. package/src/screens/Unit/__test__/ChooseLocation.test.js +27 -14
  196. package/src/screens/Unit/__test__/Detail.test.js +99 -200
  197. package/src/screens/Unit/__test__/ManageUnit.test.js +18 -42
  198. package/src/screens/Unit/__test__/SelectAddress.test.js +84 -51
  199. package/src/screens/Unit/__test__/SmartAccount.test.js +17 -9
  200. package/src/screens/Unit/__test__/SmartAccountItem.test.js +0 -1
  201. package/src/screens/Unit/__test__/Summaries.test.js +100 -0
  202. package/src/screens/Unit/components/MyAllUnit/__test__/MyAllUnit.test.js +36 -0
  203. package/src/screens/Unit/components/MyAllUnit/__test__/index.test.js +54 -0
  204. package/src/screens/Unit/components/SharedUnit/index.js +1 -0
  205. package/src/screens/Unit/components/__test__/SharedUnit.test.js +31 -34
  206. package/src/screens/Unit/hook/useUnitConnectRemoteDevices.js +50 -0
  207. package/src/screens/Unit/styles.js +4 -0
  208. package/src/screens/UnitSummary/__test__/index.test.js +70 -41
  209. package/src/screens/UnitSummary/components/3PPowerConsumption/__test__/3PPowerConsumption.test.js +31 -2
  210. package/src/screens/UnitSummary/components/PowerConsumeHistoryChart/__test__/index.test.js +7 -4
  211. package/src/screens/UnitSummary/components/PowerConsumeHistoryChart/index.js +2 -13
  212. package/src/screens/UnitSummary/components/PowerConsumption/__test__/ItemPower.test.js +0 -1
  213. package/src/screens/UnitSummary/components/PowerConsumption/__test__/PowerConsumption.test.js +14 -16
  214. package/src/screens/UnitSummary/components/RunningDevices/__test__/index.test.js +11 -2
  215. package/src/screens/UnitSummary/components/RunningDevices/index.js +7 -10
  216. package/src/screens/UnitSummary/components/Temperature/index.js +4 -4
  217. package/src/screens/UnitSummary/components/WaterQuality/Item/index.js +10 -2
  218. package/src/screens/UnitSummary/index.js +15 -1
  219. package/src/utils/Apis/axios.js +46 -31
  220. package/src/utils/Converter/time.js +0 -18
  221. package/src/utils/I18n/translations/en.json +4 -1
  222. package/src/utils/I18n/translations/vi.json +5 -1
  223. package/src/utils/Permission/common.js +67 -0
  224. package/src/utils/Utils.js +11 -7
@@ -5,8 +5,6 @@ import { ToastBottomHelper } from '../../utils/Utils';
5
5
  import { API } from '../../configs';
6
6
  import { axiosPost } from '../../utils/Apis/axios';
7
7
 
8
- let connections = {};
9
-
10
8
  let configMaps = {};
11
9
 
12
10
  let attributeMaps = {};
@@ -89,14 +87,14 @@ async function fetchConnectionEntities(connection) {
89
87
  const entity = states[i];
90
88
  const entityId = entity.entity_id;
91
89
 
92
- if (configMaps.hasOwnProperty(entityId)) {
90
+ if (Object.prototype.hasOwnProperty.call(configMaps, entityId)) {
93
91
  const [configId, type] = configMaps[entityId];
94
92
  const typeConverter = valueTypes[type] || keepValue;
95
93
 
96
94
  configValues[configId] = typeConverter(entity.state, entityId);
97
95
  }
98
96
 
99
- if (attributeMaps.hasOwnProperty(entityId)) {
97
+ if (Object.prototype.hasOwnProperty.call(attributeMaps, entityId)) {
100
98
  const entityAttributeMaps = attributeMaps[entityId];
101
99
  for (const [attributeName, attributeMap] of Object.entries(
102
100
  entityAttributeMaps
@@ -118,14 +116,21 @@ async function fetchConnectionEntities(connection) {
118
116
  setConfigGlobalState('configValues', { ...configValues });
119
117
  }
120
118
 
121
- export const googleHomeConnect = async (options) => {
122
- let isConnected = true;
119
+ export const googleHomeConnect = async (
120
+ oldConnections,
121
+ options,
122
+ onEstablished,
123
+ onDisconnected,
124
+ onReconnected
125
+ ) => {
126
+ let connections = {};
127
+
123
128
  for (let i = 0; i < options.length; i++) {
124
129
  const option = options[i];
125
130
 
126
- if (option.chip_id in connections) {
127
- // skip connected
128
- return isConnected;
131
+ if (option.chip_id in oldConnections && !!oldConnections[option.chip_id]) {
132
+ connections[option.chip_id] = oldConnections[option.chip_id];
133
+ continue;
129
134
  }
130
135
  connections[option.chip_id] = 0; // connecting
131
136
 
@@ -152,43 +157,45 @@ export const googleHomeConnect = async (options) => {
152
157
  }
153
158
  });
154
159
 
155
- let auth = new Auth(option.auth);
156
- const connection = await createConnection({ auth });
157
- await connection.subscribeEvents(stateChangeCallback, 'state_changed');
158
-
159
- connection.addEventListener('disconnected', () => {
160
- ToastBottomHelper.error(t('command_googlehome_lost'));
161
- isConnected = false;
162
- });
163
-
164
- connection.addEventListener('ready', async (conn, eventData) => {
165
- await fetchConnectionEntities(conn);
166
- ToastBottomHelper.success(t('command_googlehome_ready'));
167
- isConnected = true;
168
- });
169
-
170
- connections[option.chip_id] = connection;
171
- await fetchConnectionEntities(connection);
172
- isConnected = true;
160
+ try {
161
+ let auth = new Auth(option.auth);
162
+ const connection = await createConnection({ auth });
163
+ await connection.subscribeEvents(stateChangeCallback, 'state_changed');
164
+
165
+ connection.addEventListener('disconnected', async () => {
166
+ await onDisconnected(option);
167
+ ToastBottomHelper.error(t('command_googlehome_lost'));
168
+ });
169
+
170
+ connection.addEventListener('ready', async (conn, eventData) => {
171
+ await fetchConnectionEntities(conn);
172
+ await onReconnected(option, conn);
173
+ ToastBottomHelper.success(t('command_googlehome_ready'));
174
+ });
175
+
176
+ connections[option.chip_id] = connection;
177
+ await fetchConnectionEntities(connection);
178
+ } catch (err) {
179
+ connections[option.chip_id] = 0;
180
+ }
173
181
  }
174
- return isConnected;
182
+ await onEstablished(connections, options);
183
+ return connections;
175
184
  };
176
185
 
177
- export const googleHomeDisconnect = async (options) => {
186
+ export const googleHomeDisconnect = async (connections, options) => {
178
187
  for (let i = 0; i < options.length; i++) {
179
188
  const option = options[i];
180
-
181
- if (!(option.chip_id in connections)) {
182
- return;
189
+ if (option.chip_id in connections) {
190
+ if (connections[option.chip_id]) {
191
+ await connections[option.chip_id].close();
192
+ }
183
193
  }
184
-
185
- await connections[option.chip_id].close();
186
- delete connections[option.chip_id];
187
194
  }
188
195
  };
189
196
 
190
- function getSensorConnection(sensor) {
191
- return connections[sensor.chip_id];
197
+ function getDeviceConnection(connections, device) {
198
+ return connections[device.chip_id];
192
199
  }
193
200
 
194
201
  function getServiceName(message) {
@@ -209,10 +216,20 @@ async function sendCommandSingleAction(connection, ghAction, data) {
209
216
  message.service_data[name] = data;
210
217
  }
211
218
 
212
- await connection.sendMessagePromise(message);
219
+ try {
220
+ await connection.sendMessagePromise(message);
221
+ return true;
222
+ } catch (err) {
223
+ ToastBottomHelper.error(t('command_send_fail_googlehome'));
224
+ }
213
225
  }
214
226
 
215
- export async function sendCommandOverGoogleHome(sensor, action, data) {
227
+ export async function sendCommandOverGoogleHome(
228
+ connections,
229
+ device,
230
+ action,
231
+ data
232
+ ) {
216
233
  if (
217
234
  !(action.googlehome_actions && action.googlehome_actions.length) &&
218
235
  !action.googlehome_action
@@ -220,30 +237,39 @@ export async function sendCommandOverGoogleHome(sensor, action, data) {
220
237
  return;
221
238
  }
222
239
 
223
- const connection = getSensorConnection(sensor);
240
+ const connection = getDeviceConnection(connections, device);
224
241
  if (!connection) {
225
242
  ToastBottomHelper.error(t('command_send_fail_googlehome'));
226
243
  return;
227
244
  }
228
245
 
246
+ let triggerSuccess;
247
+
229
248
  if (action.googlehome_actions) {
230
249
  for (let i = 0; i < action.googlehome_actions.length; i++) {
231
- await sendCommandSingleAction(
250
+ triggerSuccess = await sendCommandSingleAction(
232
251
  connection,
233
252
  action.googlehome_actions[i],
234
253
  data
235
254
  );
236
255
  }
237
256
  } else if (action.googlehome_action) {
238
- await sendCommandSingleAction(connection, action.googlehome_action, data);
257
+ triggerSuccess = await sendCommandSingleAction(
258
+ connection,
259
+ action.googlehome_action,
260
+ data
261
+ );
239
262
  }
240
263
 
241
- const { success } = await axiosPost(API.SENSOR.ACTIVITY_LOG(), {
242
- action_id: action.id,
243
- message: 'Trigger by user action with google home',
244
- });
245
- if (success) {
246
- ToastBottomHelper.success(t('command_send_success_googlehome'));
264
+ if (triggerSuccess) {
265
+ const { success } = await axiosPost(API.DEVICE.ACTIVITY_LOG(), {
266
+ action_id: action.id,
267
+ message: 'Trigger by user action with google home',
268
+ });
269
+ if (success) {
270
+ ToastBottomHelper.success(t('command_send_success_googlehome'));
271
+ }
272
+ return success;
247
273
  }
248
- return success;
274
+ return false;
249
275
  }
@@ -17,7 +17,7 @@ export const sendCommandOverInternet = async (
17
17
  }
18
18
  }
19
19
  ToastBottomHelper.success(t('Sending command via internet'));
20
- const { success } = await axiosPost(API.SENSOR.QUICK_ACTION(sensor.id), {
20
+ const { success } = await axiosPost(API.DEVICE.TRIGGER_ACTION(sensor.id), {
21
21
  key: action.key,
22
22
  data,
23
23
  source,
@@ -1,6 +1,6 @@
1
1
  import { createConnection, getStates } from 'home-assistant-js-websocket';
2
2
  import Toast from 'react-native-toast-message';
3
- import axios from 'axios';
3
+ import MockAdapter from 'axios-mock-adapter';
4
4
  import {
5
5
  googleHomeConnect,
6
6
  googleHomeDisconnect,
@@ -9,8 +9,9 @@ import {
9
9
  import { getConfigGlobalState, setConfigGlobalState } from '../../states';
10
10
  import { API } from '../../../configs';
11
11
  import { getTranslate } from '../../../utils/I18n';
12
+ import api from '../../../utils/Apis/axios';
12
13
 
13
- jest.mock('axios');
14
+ const mock = new MockAdapter(api.axiosInstance);
14
15
  jest.mock('home-assistant-js-websocket', () => {
15
16
  return {
16
17
  Auth: jest.fn(),
@@ -26,6 +27,10 @@ const connection = {
26
27
  close: jest.fn(),
27
28
  };
28
29
 
30
+ const mockEstablished = jest.fn();
31
+ const mockDisconnected = jest.fn();
32
+ const mockReconnected = jest.fn();
33
+
29
34
  getStates.mockImplementation(() => []);
30
35
  createConnection.mockImplementation(() => connection);
31
36
 
@@ -80,13 +85,7 @@ describe('Remote Control Google Home', () => {
80
85
  },
81
86
  };
82
87
 
83
- const response = {
84
- status: 200,
85
- };
86
-
87
88
  beforeEach(async () => {
88
- axios.post.mockClear();
89
- await googleHomeDisconnect(options);
90
89
  connection.sendMessagePromise.mockClear();
91
90
  createConnection.mockClear();
92
91
  getStates.mockClear();
@@ -112,7 +111,13 @@ describe('Remote Control Google Home', () => {
112
111
  let configValues = getConfigGlobalState('configValues');
113
112
  expect(configValues).toEqual({});
114
113
 
115
- await googleHomeConnect(options);
114
+ await googleHomeConnect(
115
+ {},
116
+ options,
117
+ mockEstablished,
118
+ mockDisconnected,
119
+ mockReconnected
120
+ );
116
121
  expect(createConnection).toBeCalledTimes(1);
117
122
  expect(getStates).toBeCalledTimes(1);
118
123
 
@@ -148,15 +153,33 @@ describe('Remote Control Google Home', () => {
148
153
  }
149
154
  });
150
155
 
151
- await googleHomeConnect(options);
156
+ await googleHomeConnect(
157
+ {},
158
+ options,
159
+ mockEstablished,
160
+ mockDisconnected,
161
+ mockReconnected
162
+ );
152
163
 
153
164
  configValues = getConfigGlobalState('configValues');
154
165
  expect(configValues).toEqual({ 1: true });
155
166
  });
156
167
 
157
168
  it('Connect to same google home will be skipped', async () => {
158
- await googleHomeConnect(options);
159
- await googleHomeConnect(options);
169
+ const connections = await googleHomeConnect(
170
+ {},
171
+ options,
172
+ mockEstablished,
173
+ mockDisconnected,
174
+ mockReconnected
175
+ );
176
+ await googleHomeConnect(
177
+ connections,
178
+ options,
179
+ mockEstablished,
180
+ mockDisconnected,
181
+ mockReconnected
182
+ );
160
183
  expect(createConnection).toBeCalledTimes(1);
161
184
  expect(getStates).toBeCalledTimes(1);
162
185
  });
@@ -167,7 +190,13 @@ describe('Remote Control Google Home', () => {
167
190
  listener();
168
191
  }
169
192
  });
170
- await googleHomeConnect(options);
193
+ await googleHomeConnect(
194
+ {},
195
+ options,
196
+ mockEstablished,
197
+ mockDisconnected,
198
+ mockReconnected
199
+ );
171
200
  expect(createConnection).toBeCalledTimes(1);
172
201
  expect(getStates).toBeCalledTimes(2);
173
202
  });
@@ -178,21 +207,31 @@ describe('Remote Control Google Home', () => {
178
207
  listener();
179
208
  }
180
209
  });
181
- await googleHomeConnect(options);
210
+ await googleHomeConnect(
211
+ {},
212
+ options,
213
+ mockEstablished,
214
+ mockDisconnected,
215
+ mockReconnected
216
+ );
182
217
  expect(Toast.show).toBeCalledWith({
183
- type: 'success',
218
+ type: 'error',
184
219
  position: 'bottom',
185
- text1: getTranslate('en', 'command_googlehome_ready'),
220
+ text1: getTranslate('en', 'command_googlehome_lost'),
186
221
  visibilityTime: 1000,
187
222
  });
188
223
  });
189
224
 
190
225
  it('Send command over google home will send message to chip', async () => {
191
- axios.post.mockImplementation(async () => {
192
- return response;
193
- });
194
- await googleHomeConnect(options);
195
- await sendCommandOverGoogleHome(sensor, action);
226
+ mock.onPost(API.DEVICE.ACTIVITY_LOG()).reply(200);
227
+ const connections = await googleHomeConnect(
228
+ {},
229
+ options,
230
+ mockEstablished,
231
+ mockDisconnected,
232
+ mockReconnected
233
+ );
234
+ await sendCommandOverGoogleHome(connections, sensor, action);
196
235
  expect(connection.sendMessagePromise).toBeCalledWith(
197
236
  action.googlehome_actions[0].message
198
237
  );
@@ -209,21 +248,19 @@ describe('Remote Control Google Home', () => {
209
248
  },
210
249
  id: 20,
211
250
  };
212
-
213
- axios.post.mockImplementation(async () => {
214
- return response;
215
- });
216
- await googleHomeConnect(options);
217
- await sendCommandOverGoogleHome(sensor, action, 19);
218
-
251
+ mock.onPost(API.DEVICE.ACTIVITY_LOG()).reply(200);
252
+ const connections = await googleHomeConnect(
253
+ {},
254
+ options,
255
+ mockEstablished,
256
+ mockDisconnected,
257
+ mockReconnected
258
+ );
259
+ await sendCommandOverGoogleHome(connections, sensor, action, 19);
219
260
  action.googlehome_actions[0].message.service_data.temperature = 19;
220
261
  expect(connection.sendMessagePromise).toBeCalledWith(
221
262
  action.googlehome_actions[0].message
222
263
  );
223
- expect(axios.post).toHaveBeenCalledWith(API.SENSOR.ACTIVITY_LOG(), {
224
- action_id: 1,
225
- message: 'Trigger by user action with google home',
226
- });
227
264
  });
228
265
 
229
266
  it('Send command over google home action googlehome_action one to one', async () => {
@@ -238,38 +275,48 @@ describe('Remote Control Google Home', () => {
238
275
  },
239
276
  id: 20,
240
277
  };
241
-
242
- axios.post.mockImplementation(async () => {
243
- return response;
244
- });
245
- await googleHomeConnect(options);
246
- await sendCommandOverGoogleHome(sensor, action, 19);
247
-
278
+ mock.onPost(API.DEVICE.ACTIVITY_LOG()).reply(200);
279
+ const connections = await googleHomeConnect(
280
+ {},
281
+ options,
282
+ mockEstablished,
283
+ mockDisconnected,
284
+ mockReconnected
285
+ );
286
+ await sendCommandOverGoogleHome(connections, sensor, action, 19);
248
287
  action.googlehome_action.message.service_data.temperature = 19;
249
288
  expect(connection.sendMessagePromise).toBeCalledWith(
250
289
  action.googlehome_action.message
251
290
  );
252
- expect(axios.post).toHaveBeenCalledWith(API.SENSOR.ACTIVITY_LOG(), {
253
- action_id: 1,
254
- message: 'Trigger by user action with google home',
255
- });
256
291
  });
257
292
 
258
293
  it('Send command over google home that not connected', async () => {
259
- await googleHomeConnect(options);
260
- await sendCommandOverGoogleHome({ chip_id: 2 }, action);
294
+ await googleHomeConnect(
295
+ {},
296
+ options,
297
+ mockEstablished,
298
+ mockDisconnected,
299
+ mockReconnected
300
+ );
301
+ await sendCommandOverGoogleHome({}, { chip_id: 2 }, action);
261
302
  expect(connection.sendMessagePromise).not.toBeCalled();
262
303
  });
263
304
 
264
305
  it('Send command over google home that not a google home', async () => {
265
- await googleHomeConnect(options);
266
- await sendCommandOverGoogleHome(sensor, {});
306
+ await googleHomeConnect(
307
+ {},
308
+ options,
309
+ mockEstablished,
310
+ mockDisconnected,
311
+ mockReconnected
312
+ );
313
+ await sendCommandOverGoogleHome({}, sensor, {});
267
314
  expect(connection.sendMessagePromise).not.toBeCalled();
268
315
  });
269
316
 
270
317
  it('Disconnect a not connected gateway', async () => {
271
318
  connection.close.mockClear();
272
- await googleHomeDisconnect(options);
319
+ await googleHomeDisconnect({}, options);
273
320
  expect(connection.close).not.toBeCalled();
274
321
  });
275
322
  });
@@ -1,26 +1,37 @@
1
1
  import Toast from 'react-native-toast-message';
2
- import axios from 'axios';
2
+ import MockAdapter from 'axios-mock-adapter';
3
3
  import { sendCommandOverInternet } from '../Internet';
4
+ import api from '../../../utils/Apis/axios';
5
+ import { API } from '../../../configs';
4
6
 
5
- jest.mock('axios');
7
+ const mock = new MockAdapter(api.axiosInstance);
6
8
 
7
9
  describe('Test IOT Remote Control Internet', () => {
8
10
  beforeEach(() => {
9
11
  Toast.show.mockClear();
10
- axios.post.mockClear();
11
12
  });
12
13
 
13
14
  test('Trigger action via request POST', async () => {
14
- axios.post.mockImplementation(() => ({ status: 200 }));
15
+ mock.onPost(API.DEVICE.TRIGGER_ACTION()).reply(200);
15
16
  await sendCommandOverInternet({}, {}, 'internet');
16
- expect(axios.post).toBeCalled();
17
+ expect(Toast.show).toBeCalledTimes(2);
18
+ });
19
+
20
+ test('Trigger action via request POST with data isInteger', async () => {
21
+ mock.onPost(API.DEVICE.TRIGGER_ACTION()).reply(200);
22
+ await sendCommandOverInternet({}, {}, 1);
23
+ expect(Toast.show).toBeCalledTimes(2);
24
+ });
25
+
26
+ test('Trigger action via request POST with data is null', async () => {
27
+ mock.onPost(API.DEVICE.TRIGGER_ACTION()).reply(200);
28
+ await sendCommandOverInternet({}, {}, null);
17
29
  expect(Toast.show).toBeCalledTimes(2);
18
30
  });
19
31
 
20
32
  test('Trigger action fail show error', async () => {
21
- axios.post.mockImplementation(() => ({ success: false }));
33
+ mock.onPost(API.DEVICE.TRIGGER_ACTION()).reply(400);
22
34
  await sendCommandOverInternet({}, {}, 'internet');
23
- expect(axios.post).toBeCalled();
24
35
  expect(Toast.show).toBeCalled();
25
36
  });
26
37
  });