@eohjsc/react-native-smart-city 0.7.3-rc9 → 0.7.4

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 (166) hide show
  1. package/assets/images/Map/LocationPin.svg +10 -0
  2. package/package.json +4 -2
  3. package/src/commons/Action/__test__/ItemQuickAction.test.js +2 -2
  4. package/src/commons/ActionGroup/NumberUpDownActionTemplate.js +3 -3
  5. package/src/commons/ActionGroup/OnOffSmartLock/AutoLock/ButtonWrapper.js +3 -3
  6. package/src/commons/ActionGroup/OnOffSmartLock/PasscodeList/index.js +3 -3
  7. package/src/commons/ActionGroup/OnOffSmartLock/SetupGeneratePasscode/ButtonWrapper.js +2 -2
  8. package/src/commons/ActionGroup/OnOffTemplate/OnOffSimpleTemplate.js +2 -2
  9. package/src/commons/ActionGroup/OptionsDropdownActionTemplate.js +2 -2
  10. package/src/commons/ActionGroup/SliderRangeTemplate.js +7 -7
  11. package/src/commons/ActionGroup/SmartTiviActionTemplate/SmartTiviActionTemplate.js +2 -2
  12. package/src/commons/ActionGroup/SmartTiviActionTemplate/component/CircleButton.js +5 -5
  13. package/src/commons/ActionGroup/SmartTiviActionTemplate/component/ControlPlay.js +5 -5
  14. package/src/commons/ActionGroup/TerminalBoxTemplate.js +2 -2
  15. package/src/commons/ActionGroup/TextBoxTemplate.js +2 -2
  16. package/src/commons/ActionGroup/TimerActionTemplate.js +6 -3
  17. package/src/commons/ActionGroup/TwoButtonTemplate/index.js +2 -2
  18. package/src/commons/ActionGroup/__test__/index.test.js +2 -2
  19. package/src/commons/ActionGroup/index.js +2 -2
  20. package/src/commons/Automate/ItemAutomate.js +9 -41
  21. package/src/commons/Automate/ItemConditionScriptDetail.js +109 -0
  22. package/src/commons/Automate/ItemConditionScriptDetailStyles.js +46 -0
  23. package/src/commons/Automate/__test__/ItemAutomate.test.js +0 -21
  24. package/src/commons/BackDefault/index.js +2 -2
  25. package/src/commons/BottomButtonView/index.js +1 -0
  26. package/src/commons/ButtonPopup/index.js +3 -2
  27. package/src/commons/DateTimeRangeChange/DateTimeButton.js +3 -3
  28. package/src/commons/Device/ConnectedViewHeader.js +2 -2
  29. package/src/commons/Device/Emergency/EmergencyDetail.js +3 -3
  30. package/src/commons/Device/FlatListItems.js +3 -3
  31. package/src/commons/Device/Hanet/ItemHanetDevice.js +3 -3
  32. package/src/commons/Device/HorizontalBarChart.js +54 -34
  33. package/src/commons/Device/ItemAddNew/index.js +2 -2
  34. package/src/commons/Device/ItemDevice.js +2 -2
  35. package/src/commons/Device/SonosSpeaker/index.js +3 -3
  36. package/src/commons/Device/WaterPurifierStatus/AlertStatusMachine.js +2 -2
  37. package/src/commons/Device/WaterQualitySensor/QualityIndicatorsItem.js +6 -2
  38. package/src/commons/DisplayChecking/index.js +2 -2
  39. package/src/commons/EmergencyButton/AlertSendConfirm.js +2 -2
  40. package/src/commons/FieldTemplate/ChooseUserField/index.js +2 -2
  41. package/src/commons/FlatListDnD/index.js +12 -9
  42. package/src/commons/Grid/GridItem.js +10 -2
  43. package/src/commons/Header/HeaderCustom.js +2 -2
  44. package/src/commons/HeaderAni/index.js +2 -2
  45. package/src/commons/IconComponent/index.js +4 -4
  46. package/src/commons/NavBar/index.js +2 -2
  47. package/src/commons/OneTapTemplate/NumberUpDownActionTemplate.js +3 -3
  48. package/src/commons/OneTapTemplate/__test__/StatesGridActionTemplate.test.js +4 -4
  49. package/src/commons/SearchLocation/index.js +3 -3
  50. package/src/commons/SelectActionCard/index.js +2 -2
  51. package/src/commons/SelectSubUnit/index.js +6 -2
  52. package/src/commons/Sharing/BtnRemoveMember.js +2 -2
  53. package/src/commons/Sharing/StationDevicePermissions.js +2 -2
  54. package/src/commons/SubUnit/DeviceTemplate/ConfigAndEvaluation/ConfigAndEvaluation.js +2 -2
  55. package/src/commons/SubUnit/DeviceTemplate/ConfigValue/ConfigValue.js +2 -2
  56. package/src/commons/SubUnit/DeviceTemplate/EvaluationOverConfig/EvaluationOverConfig.js +2 -2
  57. package/src/commons/SubUnit/OneTap/ItemOneTap.js +3 -4
  58. package/src/commons/SubUnit/OneTap/__test__/SubUnitAutomate.test.js +0 -2
  59. package/src/commons/SubUnit/OneTap/index.js +9 -8
  60. package/src/commons/Unit/HeaderUnit/index.js +3 -3
  61. package/src/commons/Unit/SharedUnit.js +7 -7
  62. package/src/commons/Widgets/IFrameWithConfig/IFrameWithConfig.js +4 -15
  63. package/src/commons/Widgets/IFrameWithConfig/__tests__/IFrameWithConfig.test.js +29 -18
  64. package/src/configs/API.js +1 -1
  65. package/src/configs/AccessibilityLabel.js +1 -0
  66. package/src/configs/Theme.js +1 -1
  67. package/src/hooks/IoT/__test__/useRemoteControl.test.js +14 -7
  68. package/src/hooks/IoT/useRemoteControl.js +18 -7
  69. package/src/hooks/useMqtt.js +2 -2
  70. package/src/iot/RemoteControl/Bluetooth.js +19 -22
  71. package/src/iot/RemoteControl/Internet.js +11 -3
  72. package/src/navigations/EmergencyContactsStack.js +4 -3
  73. package/src/navigations/SharedStack.js +2 -0
  74. package/src/navigations/UnitStack.js +56 -57
  75. package/src/screens/AQIGuide/index.js +0 -2
  76. package/src/screens/ActivityLog/index.js +2 -2
  77. package/src/screens/AddLocationMaps/index.js +16 -19
  78. package/src/screens/AddLocationMaps/indexStyle.js +1 -0
  79. package/src/screens/AddNewGateway/RenameNewDevices.js +2 -2
  80. package/src/screens/AddNewGateway/SelectDeviceType.js +1 -1
  81. package/src/screens/AllGateway/GatewayInfo/__test__/index.test.js +25 -9
  82. package/src/screens/Automate/AddNewAction/ChooseAction.js +17 -3
  83. package/src/screens/Automate/AddNewAction/ChooseConfig.js +7 -6
  84. package/src/screens/Automate/AddNewAction/NewActionWrapper.js +8 -8
  85. package/src/screens/Automate/AddNewAction/SelectControlDevices.js +30 -20
  86. package/src/screens/Automate/AddNewAction/SelectMonitorDevices.js +17 -10
  87. package/src/screens/Automate/AddNewAction/SetupConfigCondition.js +58 -44
  88. package/src/screens/Automate/AddNewAction/SetupScriptDelay.js +1 -1
  89. package/src/screens/Automate/AddNewAction/SetupScriptNotify.js +3 -3
  90. package/src/screens/Automate/AddNewAction/Styles/SetupSensorStyles.js +20 -4
  91. package/src/screens/Automate/AddNewAction/__test__/ChooseAction.test.js +1 -1
  92. package/src/screens/Automate/AddNewAction/__test__/SelectControlDevices.test.js +14 -8
  93. package/src/screens/Automate/AddNewAction/__test__/SelectMonitorDevices.test.js +13 -7
  94. package/src/screens/Automate/AddNewAction/__test__/SetupConfigCondition.test.js +54 -15
  95. package/src/screens/Automate/AddNewAction/__test__/SetupScriptNotify.test.js +29 -0
  96. package/src/screens/Automate/AddNewAutoSmart/AddTypeSmart.js +10 -10
  97. package/src/screens/Automate/AddNewAutoSmart/__test__/AddNewAutoSmart.test.js +2 -0
  98. package/src/screens/Automate/Components/InputName.js +10 -7
  99. package/src/screens/Automate/EditActionsList/index.js +19 -17
  100. package/src/screens/Automate/MultiUnits.js +19 -22
  101. package/src/screens/Automate/OneTap/__test__/AddNewOneTap.test.js +12 -2
  102. package/src/screens/Automate/Scenario/__test__/AddNewOneTap.test.js +9 -1
  103. package/src/screens/Automate/ScriptDetail/Components/AddActionScript.js +14 -6
  104. package/src/screens/Automate/ScriptDetail/Components/RenameScript.js +4 -7
  105. package/src/screens/Automate/ScriptDetail/__test__/index.test.js +42 -17
  106. package/src/screens/Automate/ScriptDetail/index.js +33 -22
  107. package/src/screens/Automate/SetSchedule/__test__/index.test.js +21 -10
  108. package/src/screens/Automate/SetSchedule/components/RowItem.js +5 -5
  109. package/src/screens/Automate/SetSchedule/index.js +8 -7
  110. package/src/screens/Automate/Styles/indexStyles.js +1 -1
  111. package/src/screens/Automate/__test__/MultiUnits.test.js +1 -4
  112. package/src/screens/Automate/__test__/index.test.js +0 -1
  113. package/src/screens/Automate/index.js +36 -34
  114. package/src/screens/ChangePosition/index.js +2 -1
  115. package/src/screens/Device/EditDevice/__test__/EditDevice.test.js +4 -4
  116. package/src/screens/Device/EditDevice/index.js +4 -4
  117. package/src/screens/Device/__test__/BluetoothDevice.test.js +300 -0
  118. package/src/screens/Device/__test__/detail.test.js +3 -3
  119. package/src/screens/Device/components/BluetoothDevice.js +135 -0
  120. package/src/screens/Device/components/SensorDisplayItem.js +4 -3
  121. package/src/screens/Device/detail.js +70 -62
  122. package/src/screens/Device/hooks/useEvaluateValue.js +1 -1
  123. package/src/screens/Drawer/index.js +3 -3
  124. package/src/screens/EmergencyContacts/EmergencyContactsList.js +8 -4
  125. package/src/screens/EmergencyContacts/EmergencyContactsSelectContacts.js +3 -3
  126. package/src/screens/EmergencySetting/components/DropDownItem.js +2 -2
  127. package/src/screens/GuestInfo/components/RowGuestInfo.js +2 -2
  128. package/src/screens/GuestInfo/index.js +2 -2
  129. package/src/screens/HanetCamera/ManageAccess.js +3 -3
  130. package/src/screens/HanetCamera/MemberInfo.js +2 -2
  131. package/src/screens/HanetCamera/components/RequestFaceIDPopup.js +2 -2
  132. package/src/screens/ManageAccess/index.js +3 -3
  133. package/src/screens/Notification/components/NotificationItem.js +3 -3
  134. package/src/screens/Notification/index.js +3 -3
  135. package/src/screens/ScanChipQR/components/QRScan/index.js +3 -3
  136. package/src/screens/SelectUnit/__test__/index.test.js +1 -1
  137. package/src/screens/SelectUnit/index.js +4 -4
  138. package/src/screens/SharedUnit/TabHeader.js +2 -2
  139. package/src/screens/Sharing/Components/EndDevice.js +3 -3
  140. package/src/screens/Sharing/UnitMemberList.js +2 -2
  141. package/src/screens/SmartAccount/SuccessfullyConnected/index.js +2 -2
  142. package/src/screens/SmartIr/__test__/GroupButtonByType.test.js +3 -2
  143. package/src/screens/SmartIr/components/GroupButtonByType/GroupButtonByType.js +2 -2
  144. package/src/screens/SmartIr/components/SelectBrand.js +2 -2
  145. package/src/screens/SubUnit/AddSubUnit.js +28 -24
  146. package/src/screens/SubUnit/AddSubUnitStyles.js +10 -4
  147. package/src/screens/SubUnit/EditSubUnit.js +12 -11
  148. package/src/screens/SubUnit/ManageSubUnit.js +3 -3
  149. package/src/screens/SubUnit/__test__/AddSubUnit.test.js +13 -13
  150. package/src/screens/SubUnit/__test__/EditSubUnit.test.js +3 -3
  151. package/src/screens/Template/EditTemplate.js +6 -4
  152. package/src/screens/UVIndexGuide/index.js +2 -4
  153. package/src/screens/Unit/SelectAddToFavorites.js +2 -2
  154. package/src/screens/Unit/SelectAddress.js +16 -12
  155. package/src/screens/Unit/components/AutomateScript/index.js +2 -2
  156. package/src/screens/Unit/components/ButtonWrapper/index.js +3 -3
  157. package/src/screens/Unit/components/Header/index.js +2 -2
  158. package/src/screens/Unit/components/SharedUnit/index.js +5 -5
  159. package/src/screens/UnitSummary/components/AirQuality/index.js +6 -11
  160. package/src/screens/UnitSummary/components/WaterQuality/Item/index.js +6 -6
  161. package/src/screens/UnitSummary/index.js +3 -3
  162. package/src/utils/Apis/axios.js +5 -5
  163. package/src/utils/Functions/ShortEmail.js +1 -0
  164. package/src/utils/I18n/translations/en.js +6 -3
  165. package/src/utils/I18n/translations/vi.js +8 -4
  166. package/src/utils/bluetooth.js +3 -0
@@ -63,7 +63,7 @@ describe('Test EditDevice', () => {
63
63
  await act(async () => {
64
64
  textInput.props.onChange('new_name');
65
65
  });
66
- mock.onPatch(API.DEVICE.SENSOR_DETAIL(1)).reply(200, { name: 'new_name' });
66
+ mock.onPatch(API.DEVICE.DEVICE_DETAIL(1)).reply(200, { name: 'new_name' });
67
67
  await act(async () => {
68
68
  await alertAction.props.rightButtonClick();
69
69
  });
@@ -92,7 +92,7 @@ describe('Test EditDevice', () => {
92
92
  await act(async () => {
93
93
  textInput.props.onChange('new_name');
94
94
  });
95
- mock.onPatch(API.DEVICE.SENSOR_DETAIL(1)).reply(400);
95
+ mock.onPatch(API.DEVICE.DEVICE_DETAIL(1)).reply(400);
96
96
  await act(async () => {
97
97
  await alertAction.props.rightButtonClick();
98
98
  });
@@ -120,7 +120,7 @@ describe('Test EditDevice', () => {
120
120
  await buttonDelete[0].props.onPress();
121
121
  });
122
122
  expect(alertAction.props.visible).toBeTruthy();
123
- mock.onDelete(API.DEVICE.SENSOR_DETAIL(1)).reply(204);
123
+ mock.onDelete(API.DEVICE.DEVICE_DETAIL(1)).reply(204);
124
124
  await act(async () => {
125
125
  await alertAction.props.rightButtonClick();
126
126
  });
@@ -144,7 +144,7 @@ describe('Test EditDevice', () => {
144
144
  await buttonDelete[0].props.onPress();
145
145
  });
146
146
  expect(alertAction.props.visible).toBeTruthy();
147
- mock.onDelete(API.DEVICE.SENSOR_DETAIL(1)).reply(400);
147
+ mock.onDelete(API.DEVICE.DEVICE_DETAIL(1)).reply(400);
148
148
  await act(async () => {
149
149
  await alertAction.props.rightButtonClick();
150
150
  });
@@ -2,7 +2,7 @@ import React, { memo, useState, useCallback } from 'react';
2
2
  import { View, TouchableOpacity } from 'react-native';
3
3
  import { useTranslations } from '../../../hooks/Common/useTranslations';
4
4
  import { HeaderCustom } from '../../../commons/Header';
5
- import AntDesign from 'react-native-vector-icons/AntDesign';
5
+ import { IconOutline } from '@ant-design/icons-react-native';
6
6
  import { Colors } from '../../../configs';
7
7
  import Text from '../../../commons/Text';
8
8
  import { useRoute } from '@react-navigation/native';
@@ -30,7 +30,7 @@ const EditDevice = memo(() => {
30
30
  useEditDevice();
31
31
  const renameSensor = useCallback(async () => {
32
32
  const { success, data } = await axiosPatch(
33
- API.DEVICE.SENSOR_DETAIL(sensor?.id),
33
+ API.DEVICE.DEVICE_DETAIL(sensor?.id),
34
34
  {
35
35
  name: inputName,
36
36
  }
@@ -55,7 +55,7 @@ const EditDevice = memo(() => {
55
55
 
56
56
  const deleteSensor = useCallback(async () => {
57
57
  hideAlertAction();
58
- const { success } = await axiosDelete(API.DEVICE.SENSOR_DETAIL(sensor?.id));
58
+ const { success } = await axiosDelete(API.DEVICE.DEVICE_DETAIL(sensor?.id));
59
59
 
60
60
  if (success) {
61
61
  navigation.pop(2);
@@ -92,7 +92,7 @@ const EditDevice = memo(() => {
92
92
  <Text type="Body" Reugular color={Colors.Grey7}>
93
93
  {sensorName}
94
94
  </Text>
95
- <AntDesign
95
+ <IconOutline
96
96
  name="right"
97
97
  size={18}
98
98
  color={Colors.Gray7}
@@ -0,0 +1,300 @@
1
+ import React from 'react';
2
+ import { Alert } from 'react-native';
3
+ import { act } from 'react-test-renderer';
4
+ import MockAdapter from 'axios-mock-adapter';
5
+ import { NavigationContext } from '@react-navigation/native';
6
+
7
+ import DeviceDetail from '../detail';
8
+ import { API } from '../../../configs';
9
+ import { AccessibilityLabel } from '../../../configs/Constants';
10
+ import { SCProvider } from '../../../context';
11
+ import { mockSCStore } from '../../../context/mockStore';
12
+ import api from '../../../utils/Apis/axios';
13
+ import { useWatchConfigs } from '../../../hooks/IoT';
14
+ import { render } from '../../../../jest/render';
15
+ import { BleManager } from 'react-native-ble-plx';
16
+ import { v4 as uuidv4 } from 'uuid';
17
+ import base64 from 'react-native-base64';
18
+
19
+ const mock = new MockAdapter(api.axiosInstance);
20
+
21
+ jest.mock('../../../hooks/IoT', () => {
22
+ return {
23
+ ...jest.requireActual('../../../hooks/IoT'),
24
+ useWatchConfigs: jest.fn(),
25
+ };
26
+ });
27
+
28
+ jest.mock('../../../hooks/Common', () => {
29
+ return {
30
+ ...jest.requireActual('../../../hooks/Common'),
31
+ useHomeAssistantDeviceConnected: () => ({ isConnected: true }),
32
+ };
33
+ });
34
+
35
+ let store = mockSCStore({});
36
+
37
+ const navContextValue = {
38
+ isFocused: () => false,
39
+ addListener: jest.fn(() => jest.fn()),
40
+ };
41
+
42
+ const wrapComponent = (state, account, route) => (
43
+ <SCProvider initState={state}>
44
+ <NavigationContext.Provider
45
+ value={{
46
+ ...navContextValue,
47
+ isFocused: () => true,
48
+ }}
49
+ >
50
+ <DeviceDetail account={account} route={route} />
51
+ </NavigationContext.Provider>
52
+ </SCProvider>
53
+ );
54
+
55
+ const mockAlertShow = jest.fn();
56
+ Alert.alert = mockAlertShow;
57
+
58
+ describe('test DeviceDetail', () => {
59
+ let route;
60
+ let account;
61
+ const address = uuidv4();
62
+ const password = uuidv4();
63
+ const device = {
64
+ connect: jest.fn(() => Promise.resolve(device)),
65
+ discoverAllServicesAndCharacteristics: jest.fn(() =>
66
+ Promise.resolve(device)
67
+ ),
68
+ writeCharacteristicWithoutResponseForService: jest.fn(),
69
+ writeCharacteristicWithResponseForService: jest.fn(),
70
+ cancelConnection: jest.fn(),
71
+ readCharacteristicForService: jest.fn((service, characteristic) => ({
72
+ value: base64.encode('confirmed'),
73
+ })),
74
+ };
75
+ const bleManager = new BleManager();
76
+ bleManager.startDeviceScan.mockImplementation((uuids, options, handler) =>
77
+ handler(null, device)
78
+ );
79
+
80
+ beforeEach(() => {
81
+ device.connect.mockClear();
82
+ device.cancelConnection.mockClear();
83
+ device.discoverAllServicesAndCharacteristics.mockClear();
84
+ device.writeCharacteristicWithoutResponseForService.mockClear();
85
+ device.writeCharacteristicWithResponseForService.mockClear();
86
+ device.readCharacteristicForService.mockClear();
87
+
88
+ mock.onGet(API.DEVICE.DEVICE_DETAIL(1)).reply(200, {
89
+ id: 1,
90
+ is_enable_bluetooth: true,
91
+ remote_control_options: {
92
+ bluetooth: {
93
+ address,
94
+ password,
95
+ },
96
+ },
97
+ });
98
+
99
+ mock.onGet(API.DEVICE.DISPLAY(1)).reply(200, {
100
+ items: [
101
+ {
102
+ configuration: {
103
+ action1: '2b949045-8e03-4c07-a855-7794ade2e69c',
104
+ action1_data: {
105
+ color: '#00979D',
106
+ command_prefer_over_bluetooth: true,
107
+ command_prefer_over_googlehome: false,
108
+ command_prefer_over_internet: false,
109
+ googlehome_actions: [],
110
+ icon: 'caretup',
111
+ id: 9,
112
+ key: '2b949045-8e03-4c07-a855-7794ade2e69c',
113
+ arduino_action: {
114
+ value: 1,
115
+ pin: {
116
+ pin_number: 2,
117
+ },
118
+ },
119
+ },
120
+ action2: '38347d5e-4418-4ab0-978c-c82f4c034897',
121
+ action2_data: {
122
+ color: '#00979D',
123
+ command_prefer_over_bluetooth: false,
124
+ command_prefer_over_googlehome: false,
125
+ command_prefer_over_internet: true,
126
+ googlehome_actions: [],
127
+ icon: 'stop',
128
+ id: 11,
129
+ key: '38347d5e-4418-4ab0-978c-c82f4c034897',
130
+ },
131
+ action3: 'a492e08c-8cb1-44ee-8ea0-46aaca4e5141',
132
+ action3_data: {
133
+ color: '#00979D',
134
+ command_prefer_over_bluetooth: false,
135
+ command_prefer_over_googlehome: false,
136
+ command_prefer_over_internet: true,
137
+ googlehome_actions: [],
138
+ icon: 'caretdown',
139
+ id: 10,
140
+ key: 'a492e08c-8cb1-44ee-8ea0-46aaca4e5141',
141
+ },
142
+ icon1: 'caretup',
143
+ icon2: 'stop',
144
+ icon3: 'caretdown',
145
+ text1: 'UP',
146
+ text2: 'STOP/UNLOCK',
147
+ text3: 'DOWN',
148
+ },
149
+ id: 19,
150
+ order: 2,
151
+ template: 'three_button_action_template',
152
+ type: 'action',
153
+ },
154
+ ],
155
+ side_menu_items: [{ id: 1, order: 1, name: 'Setup generate passcode' }],
156
+ });
157
+ route = {
158
+ params: {
159
+ unitData: {
160
+ id: 1,
161
+ name: 'Unit name',
162
+ address: '298 Dien Bien Phu',
163
+ remote_control_options: {
164
+ googlehome: [
165
+ {
166
+ config_maps: [],
167
+ auth: {},
168
+ chip_id: 1,
169
+ },
170
+ ],
171
+ },
172
+ },
173
+ station: {
174
+ id: 2,
175
+ name: 'Station name',
176
+ },
177
+ sensorData: {
178
+ id: 1,
179
+ is_managed_by_backend: true,
180
+ station: { id: 2, name: 'Station name' },
181
+ name: 'Sensor name',
182
+ chip_id: 1,
183
+ },
184
+ title: 'Button',
185
+ },
186
+ };
187
+ account = {
188
+ token: 'abc',
189
+ };
190
+ jest.useFakeTimers();
191
+
192
+ const setState = jest.fn();
193
+ const useLayoutEffectSpy = jest.spyOn(React, 'useLayoutEffect');
194
+ useLayoutEffectSpy.mockImplementation(() => setState);
195
+ useWatchConfigs.mockReset();
196
+ });
197
+
198
+ it('scan bluetooth if available', async () => {
199
+ await render(wrapComponent(store, account, route));
200
+ jest.runAllTimers();
201
+ expect(bleManager.stopDeviceScan).toHaveBeenCalledTimes(2);
202
+ expect(bleManager.startDeviceScan).toHaveBeenCalledTimes(1);
203
+ expect(bleManager.startDeviceScan.mock.calls[0][0]).toEqual([address]);
204
+ });
205
+
206
+ it('connect to first device', async () => {
207
+ await render(wrapComponent(store, account, route));
208
+ jest.runAllTimers();
209
+ expect(bleManager.stopDeviceScan).toHaveBeenCalledTimes(2);
210
+ expect(device.connect).toHaveBeenCalledTimes(1);
211
+ expect(device.discoverAllServicesAndCharacteristics).toHaveBeenCalledTimes(
212
+ 1
213
+ );
214
+ expect(
215
+ device.writeCharacteristicWithoutResponseForService
216
+ ).toHaveBeenCalledTimes(1);
217
+ expect(
218
+ device.writeCharacteristicWithResponseForService
219
+ ).toHaveBeenCalledTimes(1);
220
+ expect(
221
+ base64.decode(
222
+ device.writeCharacteristicWithoutResponseForService.mock.calls[0][2]
223
+ )
224
+ ).toEqual('start');
225
+ const confirmMessage =
226
+ device.writeCharacteristicWithResponseForService.mock.calls[0][2];
227
+ expect(base64.decode(confirmMessage).substring(0, 7)).toEqual('confirm');
228
+ });
229
+
230
+ it('disconnect when leave screen', async () => {
231
+ const { unmount } = await render(wrapComponent(store, account, route));
232
+ jest.runAllTimers();
233
+ await act(async () => {
234
+ unmount();
235
+ });
236
+ expect(device.cancelConnection).toHaveBeenCalledTimes(1);
237
+ });
238
+
239
+ it('fail scan', async () => {
240
+ bleManager.startDeviceScan.mockImplementationOnce(
241
+ (uuids, options, handler) => handler('error', device)
242
+ );
243
+ await render(wrapComponent(store, account, route));
244
+ jest.runAllTimers();
245
+ expect(bleManager.stopDeviceScan).toHaveBeenCalledTimes(1);
246
+ expect(device.connect).toHaveBeenCalledTimes(0);
247
+ });
248
+
249
+ it('control arduino pin device', async () => {
250
+ const { root } = await render(wrapComponent(store, account, route));
251
+ jest.runAllTimers();
252
+ const button = root.findByProps({
253
+ accessibilityLabel: AccessibilityLabel.BUTTON_TEMPLATE_1,
254
+ });
255
+ device.writeCharacteristicWithResponseForService.mockClear();
256
+ device.writeCharacteristicWithoutResponseForService.mockClear();
257
+ await act(async () => {
258
+ await button.props.onPress();
259
+ });
260
+ expect(
261
+ device.writeCharacteristicWithResponseForService
262
+ ).toHaveBeenCalledTimes(1);
263
+ });
264
+
265
+ it('control arduino pin fail', async () => {
266
+ device.readCharacteristicForService.mockReturnValue({ value: 'wrong' });
267
+ const { root } = await render(wrapComponent(store, account, route));
268
+ jest.runAllTimers();
269
+ const button = root.findByProps({
270
+ accessibilityLabel: AccessibilityLabel.BUTTON_TEMPLATE_1,
271
+ });
272
+ device.writeCharacteristicWithResponseForService.mockClear();
273
+ device.writeCharacteristicWithoutResponseForService.mockClear();
274
+ await act(async () => {
275
+ await button.props.onPress();
276
+ });
277
+ expect(
278
+ device.writeCharacteristicWithResponseForService
279
+ ).toHaveBeenCalledTimes(0);
280
+ });
281
+
282
+ it('control after fail scan', async () => {
283
+ bleManager.startDeviceScan.mockImplementationOnce(
284
+ (uuids, options, handler) => handler('error', device)
285
+ );
286
+ const { root } = await render(wrapComponent(store, account, route));
287
+ jest.runAllTimers();
288
+ const button = root.findByProps({
289
+ accessibilityLabel: AccessibilityLabel.BUTTON_TEMPLATE_1,
290
+ });
291
+ device.writeCharacteristicWithResponseForService.mockClear();
292
+ device.writeCharacteristicWithoutResponseForService.mockClear();
293
+ await act(async () => {
294
+ await button.props.onPress();
295
+ });
296
+ expect(
297
+ device.writeCharacteristicWithResponseForService
298
+ ).toHaveBeenCalledTimes(0);
299
+ });
300
+ });
@@ -3,8 +3,6 @@ import MockAdapter from 'axios-mock-adapter';
3
3
  import React from 'react';
4
4
  import { Alert, ScrollView } from 'react-native';
5
5
  import { act, create } from 'react-test-renderer';
6
-
7
- import AntDesign from 'react-native-vector-icons/AntDesign';
8
6
  import { AlertAction, ButtonPopup, MenuActionMore } from '../../../commons';
9
7
  import CurrentRainSensor from '../../../commons/Device/RainningSensor/CurrentRainSensor';
10
8
  import { AlertSendConfirm } from '../../../commons/EmergencyButton/AlertSendConfirm';
@@ -19,6 +17,7 @@ import api from '../../../utils/Apis/axios';
19
17
  import { getTranslate } from '../../../utils/I18n';
20
18
  import Routes from '../../../utils/Route';
21
19
  import DeviceDetail from '../detail';
20
+ import { IconOutline } from '@ant-design/icons-react-native';
22
21
 
23
22
  const mock = new MockAdapter(api.axiosInstance);
24
23
 
@@ -352,7 +351,8 @@ describe('test DeviceDetail', () => {
352
351
  const buttonPopupIcon = instance.find(
353
352
  (el) =>
354
353
  el.props.accessibilityLabel ===
355
- AccessibilityLabel.BUTTON_POPUP_RESOLVED_ICON && el.type === AntDesign
354
+ AccessibilityLabel.BUTTON_POPUP_RESOLVED_ICON &&
355
+ el.type === IconOutline
356
356
  );
357
357
  expect(buttonPopupTitle.props.children).toEqual([
358
358
  'Unit name',
@@ -0,0 +1,135 @@
1
+ /* eslint-disable no-console */
2
+ // console log is required for debug, this feature has a lot of debugging
3
+ import { useCallback, useEffect, useRef, useState } from 'react';
4
+ import base64 from 'react-native-base64';
5
+ import { ScanMode } from 'react-native-ble-plx';
6
+ import md5 from 'md5';
7
+ import { bleManager } from '../../../utils/bluetooth';
8
+
9
+ const CHARACTERISTIC_CONFIRM_CODE_UUID = '6ee26cfb-b42f-43ae-9ecf-f70d84bbd64f';
10
+ const CHARACTERISTIC_CONTROL_UUID = 'f0e7aa38-e466-42af-b4c7-5e6ec5c9a614';
11
+
12
+ export const useBluetoothDevice = (device) => {
13
+ const serviceUuid = device?.remote_control_options?.bluetooth?.address;
14
+ const password = device?.remote_control_options?.bluetooth?.password;
15
+ const deviceRef = useRef(null);
16
+ const [isConnected, setIsConnected] = useState(false);
17
+
18
+ useEffect(() => {
19
+ // on unmount, disconnect bluetooth, so it can be reused again
20
+ return () => {
21
+ if (isConnected) {
22
+ deviceRef.current.cancelConnection();
23
+ }
24
+ };
25
+ }, [isConnected]);
26
+
27
+ const control = useCallback(
28
+ (action, data) => {
29
+ if (!isConnected) {
30
+ return;
31
+ }
32
+
33
+ const value =
34
+ data?.value === undefined ? action.arduino_action.value : data?.value;
35
+ const message = `${action.arduino_action.pin.pin_number};${value}`;
36
+
37
+ console.log('control', message);
38
+
39
+ deviceRef.current.writeCharacteristicWithResponseForService(
40
+ serviceUuid,
41
+ CHARACTERISTIC_CONTROL_UUID,
42
+ base64.encode(message)
43
+ );
44
+ },
45
+ [isConnected, serviceUuid]
46
+ );
47
+
48
+ const sendConfirmCode = useCallback(
49
+ async (instanceCode) => {
50
+ console.log(`sendConfirmCode ${instanceCode}`);
51
+ const confirmCode = md5(`${password}${instanceCode}`);
52
+
53
+ await deviceRef.current.writeCharacteristicWithResponseForService(
54
+ serviceUuid,
55
+ CHARACTERISTIC_CONFIRM_CODE_UUID,
56
+ base64.encode('confirm' + confirmCode)
57
+ );
58
+ const result = await deviceRef.current.readCharacteristicForService(
59
+ serviceUuid,
60
+ CHARACTERISTIC_CONFIRM_CODE_UUID
61
+ );
62
+ const resultText = base64.decode(result.value);
63
+ console.log(resultText);
64
+ if (resultText.includes('confirmed')) {
65
+ setIsConnected(true);
66
+ return true;
67
+ }
68
+ return false;
69
+ },
70
+ [password, serviceUuid]
71
+ );
72
+
73
+ const askInstanceCode = useCallback(async () => {
74
+ console.log('askInstanceCode');
75
+
76
+ await deviceRef.current.writeCharacteristicWithoutResponseForService(
77
+ serviceUuid,
78
+ CHARACTERISTIC_CONFIRM_CODE_UUID,
79
+ base64.encode('start')
80
+ );
81
+ const response = await deviceRef.current.readCharacteristicForService(
82
+ serviceUuid,
83
+ CHARACTERISTIC_CONFIRM_CODE_UUID
84
+ );
85
+ return base64.decode(response.value);
86
+ }, [serviceUuid]);
87
+
88
+ const realConnect = useCallback(async () => {
89
+ // return await new Promise((resolve) => {
90
+ if (!serviceUuid || !password) {
91
+ console.log('Bluetooth is not enabled for this device');
92
+ return false;
93
+ }
94
+ console.log('start real connect');
95
+ try {
96
+ bleManager.stopDeviceScan();
97
+ } catch (e) {
98
+ /* istanbul ignore next */
99
+ console.log('Failed to stop scan');
100
+ }
101
+ bleManager.startDeviceScan(
102
+ [serviceUuid],
103
+ { scanMode: ScanMode?.LowLatency },
104
+ // eslint-disable-next-line promise/prefer-await-to-callbacks
105
+ async (error, bleDevice) => {
106
+ if (error) {
107
+ console.log(error);
108
+ return false;
109
+ }
110
+ console.log(bleDevice?.name, bleDevice?.localName);
111
+
112
+ console.log('bleManager.stopDeviceScan');
113
+ bleManager.stopDeviceScan();
114
+ console.log('device.connect');
115
+ const connectedDevice = await bleDevice.connect({ timeout: 5000 });
116
+
117
+ console.log('device.discoverAllServicesAndCharacteristics');
118
+ const fullInfoDevice =
119
+ await connectedDevice.discoverAllServicesAndCharacteristics();
120
+ console.log('device.publicCodeCharacteristic');
121
+ deviceRef.current = fullInfoDevice;
122
+
123
+ const instanceCode = await askInstanceCode();
124
+ return await sendConfirmCode(instanceCode);
125
+ }
126
+ );
127
+ // });
128
+ }, [askInstanceCode, password, sendConfirmCode, serviceUuid]);
129
+
130
+ useEffect(() => {
131
+ realConnect();
132
+ }, [realConnect]);
133
+
134
+ return { control, isConnected };
135
+ };
@@ -33,6 +33,7 @@ const { standardizeWidth, standardizeHeight } = standardizeCameraScreenSize(
33
33
  export const SensorDisplayItem = ({
34
34
  item = {},
35
35
  sensor,
36
+ bluetoothDevice,
36
37
  emergency,
37
38
  offsetTitle,
38
39
  setOffsetTitle,
@@ -46,7 +47,7 @@ export const SensorDisplayItem = ({
46
47
  const { type, template, uri, id, name, title, value_evaluation } =
47
48
  configuration;
48
49
 
49
- const sendRemoteCommand = useRemoteControl();
50
+ const sendRemoteCommand = useRemoteControl(bluetoothDevice);
50
51
  const [processing, setProcessing] = useState(false);
51
52
  // eslint-disable-next-line no-unused-vars
52
53
  const [configValues, setConfigValues] = useConfigGlobalState('configValues');
@@ -114,12 +115,12 @@ export const SensorDisplayItem = ({
114
115
  }, [configValues, evaluate, evaluateValue, configuration, value_evaluation]);
115
116
 
116
117
  const doAction = useCallback(
117
- async (action, data, interrupted = false) => {
118
+ async (action, data, interrupted = false, silent = false) => {
118
119
  if (processing && !interrupted) {
119
120
  return;
120
121
  }
121
122
  setProcessing(true);
122
- await sendRemoteCommand(sensor, action, data, userId);
123
+ await sendRemoteCommand(sensor, action, data, userId, silent);
123
124
  setProcessing(false);
124
125
  },
125
126
  [processing, sendRemoteCommand, sensor, userId]