@react-native-ohos/react-native-sensors 7.2.2-rc.2 → 7.2.3-rc.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.
- package/LICENSE +21 -21
- package/README.OpenSource +11 -0
- package/README.md +13 -13
- package/harmony/sensors/Index.ets +18 -16
- package/harmony/sensors/build-profile.json5 +12 -12
- package/harmony/sensors/hvigorfile.ts +6 -6
- package/harmony/sensors/oh-package.json5 +11 -11
- package/harmony/sensors/src/main/cpp/CMakeLists.txt +8 -8
- package/harmony/sensors/src/main/cpp/SensorsPackage.h +25 -25
- package/harmony/sensors/src/main/cpp/generated/RNOH/generated/BaseReactNativeSensorsPackage.h +30 -30
- package/harmony/sensors/src/main/cpp/generated/RNOH/generated/turbo_modules/RTNSensors.cpp +25 -25
- package/harmony/sensors/src/main/cpp/generated/RNOH/generated/turbo_modules/RTNSensors.h +19 -19
- package/harmony/sensors/src/main/ets/RTNSensorsTurboModule.ts +275 -275
- package/harmony/sensors/src/main/ets/{SensorsPackage.ts → SensorsPackage.ets} +38 -37
- package/harmony/sensors/src/main/ets/generated/components/ts.ts +5 -5
- package/harmony/sensors/src/main/ets/generated/index.ets +5 -5
- package/harmony/sensors/src/main/ets/generated/ts.ts +6 -6
- package/harmony/sensors/src/main/ets/generated/turboModules/RTNSensors.ts +30 -30
- package/harmony/sensors/src/main/ets/generated/turboModules/ts.ts +5 -5
- package/harmony/sensors/src/main/module.json5 +9 -9
- package/harmony/sensors/src/main/resources/base/element/string.json +8 -8
- package/harmony/sensors/src/main/resources/en_US/element/string.json +8 -8
- package/harmony/sensors/src/main/resources/zh_CN/element/string.json +8 -8
- package/harmony/sensors/{ts.ts → ts.ets} +17 -17
- package/harmony/sensors.har +0 -0
- package/index.d.ts +69 -69
- package/index.js +13 -13
- package/package.json +90 -88
- package/src/NativeSensors.ts +30 -30
- package/src/rnsensors.js +49 -49
- package/src/sensors.js +75 -75
|
@@ -1,276 +1,276 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
3
|
-
* Licensed under the MIT License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License.
|
|
5
|
-
* You may obtain a copy of the License at
|
|
6
|
-
*
|
|
7
|
-
* https://github.com/react-native-sensors/react-native-sensors/blob/master/LICENSE
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
* See the License for the specific language governing permissions and
|
|
13
|
-
* limitations under the License.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { TurboModule, RNOHLogger, TurboModuleContext } from "@rnoh/react-native-openharmony/ts";
|
|
17
|
-
import sensor from "@ohos.sensor";
|
|
18
|
-
import BusinessError from "@ohos.base";
|
|
19
|
-
|
|
20
|
-
import { RTNSensors } from "./generated/turboModules/RTNSensors";
|
|
21
|
-
|
|
22
|
-
export class RTNSensorsTurboModule extends TurboModule implements RTNSensors.Spec {
|
|
23
|
-
private logger: RNOHLogger;
|
|
24
|
-
private sensorsInterval = new Map<string, number>([
|
|
25
|
-
["accelerometer", 0],
|
|
26
|
-
["gyroscope", 0],
|
|
27
|
-
["magnetometer", 0],
|
|
28
|
-
["barometer", 0],
|
|
29
|
-
["orientation", 0],
|
|
30
|
-
["gravity", 0],
|
|
31
|
-
])
|
|
32
|
-
private sensorsLogLevel = new Map<string, number>([
|
|
33
|
-
["accelerometer", 0],
|
|
34
|
-
["gyroscope", 0],
|
|
35
|
-
["magnetometer", 0],
|
|
36
|
-
["barometer", 0],
|
|
37
|
-
["orientation", 0],
|
|
38
|
-
["gravity", 0],
|
|
39
|
-
])
|
|
40
|
-
private sensorsId = new Map<string, sensor.SensorId>([
|
|
41
|
-
["accelerometer", sensor.SensorId.ACCELEROMETER],
|
|
42
|
-
["gyroscope", sensor.SensorId.GYROSCOPE],
|
|
43
|
-
["magnetometer", sensor.SensorId.MAGNETIC_FIELD],
|
|
44
|
-
["barometer", sensor.SensorId.BAROMETER],
|
|
45
|
-
["orientation", sensor.SensorId.ORIENTATION],
|
|
46
|
-
["gravity", sensor.SensorId.GRAVITY],
|
|
47
|
-
])
|
|
48
|
-
|
|
49
|
-
constructor(ctx: TurboModuleContext) {
|
|
50
|
-
super(ctx);
|
|
51
|
-
this.logger = ctx.logger.clone("Sensors");
|
|
52
|
-
}
|
|
53
|
-
accelerometer(interval: number): void {
|
|
54
|
-
try {
|
|
55
|
-
interval = this.sensorsInterval.get('accelerometer')!;
|
|
56
|
-
const logLevel: number = this.sensorsLogLevel.get("accelerometer")!;
|
|
57
|
-
sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => {
|
|
58
|
-
if (logLevel > 0) {
|
|
59
|
-
this.logger.clone('accelerometer').info(`accelerometer onCreate ${interval}`)
|
|
60
|
-
}
|
|
61
|
-
if (logLevel > 1) {
|
|
62
|
-
this.logger.clone('accelerometer').info(`accelerometer onCreate ${JSON.stringify({
|
|
63
|
-
x: data?.x,
|
|
64
|
-
y: data?.y,
|
|
65
|
-
z: data?.z,
|
|
66
|
-
timestamp: data?.timestamp
|
|
67
|
-
})}`)
|
|
68
|
-
}
|
|
69
|
-
this.ctx.rnInstance.emitDeviceEvent('
|
|
70
|
-
x: data?.x,
|
|
71
|
-
y: data?.y,
|
|
72
|
-
z: data?.z,
|
|
73
|
-
timestamp: data?.timestamp
|
|
74
|
-
});
|
|
75
|
-
}, { interval: interval });
|
|
76
|
-
} catch (error) {
|
|
77
|
-
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
78
|
-
this.logger.clone('accelerometer').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
gyroscope(interval: number): void {
|
|
83
|
-
try {
|
|
84
|
-
interval = this.sensorsInterval.get('gyroscope')!;
|
|
85
|
-
const logLevel: number = this.sensorsLogLevel.get("gyroscope")!;
|
|
86
|
-
sensor.on(sensor.SensorId.GYROSCOPE, (data: sensor.GyroscopeResponse) => {
|
|
87
|
-
if (logLevel > 0) {
|
|
88
|
-
this.logger.clone('gyroscope').info(`gyroscope onCreate ${interval}`)
|
|
89
|
-
}
|
|
90
|
-
if (logLevel > 1) {
|
|
91
|
-
this.logger.clone('gyroscope').info(`gyroscope onCreate ${JSON.stringify({
|
|
92
|
-
x: data?.x,
|
|
93
|
-
y: data?.y,
|
|
94
|
-
z: data?.z,
|
|
95
|
-
timestamp: data?.timestamp
|
|
96
|
-
})}`)
|
|
97
|
-
}
|
|
98
|
-
this.ctx.rnInstance.emitDeviceEvent('
|
|
99
|
-
x: data?.x,
|
|
100
|
-
y: data?.y,
|
|
101
|
-
z: data?.z,
|
|
102
|
-
timestamp: data?.timestamp
|
|
103
|
-
});
|
|
104
|
-
}, { interval: interval });
|
|
105
|
-
} catch (error) {
|
|
106
|
-
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
107
|
-
this.logger.clone('gyroscope').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
magnetometer(interval: number): void {
|
|
112
|
-
try {
|
|
113
|
-
interval = this.sensorsInterval.get('magnetometer')!;
|
|
114
|
-
const logLevel: number = this.sensorsLogLevel.get("magnetometer")!;
|
|
115
|
-
sensor.on(sensor.SensorId.MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => {
|
|
116
|
-
if (logLevel > 0) {
|
|
117
|
-
this.logger.clone('magnetometer').info(`magnetometer onCreate ${interval}`)
|
|
118
|
-
}
|
|
119
|
-
if (logLevel > 1) {
|
|
120
|
-
this.logger.clone('magnetometer').info(`magnetometer onCreate ${JSON.stringify({
|
|
121
|
-
x: data?.x,
|
|
122
|
-
y: data?.y,
|
|
123
|
-
z: data?.z,
|
|
124
|
-
timestamp: data?.timestamp
|
|
125
|
-
})}`)
|
|
126
|
-
}
|
|
127
|
-
this.ctx.rnInstance.emitDeviceEvent('
|
|
128
|
-
x: data?.x,
|
|
129
|
-
y: data?.y,
|
|
130
|
-
z: data?.z,
|
|
131
|
-
timestamp: data?.timestamp
|
|
132
|
-
});
|
|
133
|
-
}, { interval: interval });
|
|
134
|
-
} catch (error) {
|
|
135
|
-
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
136
|
-
this.logger.clone('magnetometer').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
barometer(interval: number): void {
|
|
141
|
-
try {
|
|
142
|
-
interval = this.sensorsInterval.get('barometer')!;
|
|
143
|
-
const logLevel: number = this.sensorsLogLevel.get("barometer")!;
|
|
144
|
-
sensor.on(sensor.SensorId.BAROMETER, (data: sensor.BarometerResponse) => {
|
|
145
|
-
if (logLevel > 0) {
|
|
146
|
-
this.logger.clone('barometer').info(`barometer onCreate ${interval}`)
|
|
147
|
-
}
|
|
148
|
-
if (logLevel > 1) {
|
|
149
|
-
this.logger.clone('barometer').info(`barometer onCreate ${JSON.stringify({
|
|
150
|
-
pressure: data?.pressure
|
|
151
|
-
})}`)
|
|
152
|
-
}
|
|
153
|
-
this.ctx.rnInstance.emitDeviceEvent('
|
|
154
|
-
pressure: data?.pressure
|
|
155
|
-
});
|
|
156
|
-
}, { interval: interval });
|
|
157
|
-
} catch (error) {
|
|
158
|
-
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
159
|
-
this.logger.clone('barometer').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
orientation(interval: number): void {
|
|
164
|
-
try {
|
|
165
|
-
interval = this.sensorsInterval.get('orientation')!;
|
|
166
|
-
const logLevel: number = this.sensorsLogLevel.get("orientation")!;
|
|
167
|
-
sensor.on(sensor.SensorId.ORIENTATION, (data: sensor.OrientationResponse) => {
|
|
168
|
-
if (logLevel > 0) {
|
|
169
|
-
this.logger.clone('orientation').info(`orientation onCreate ${interval}`)
|
|
170
|
-
}
|
|
171
|
-
if (logLevel > 1) {
|
|
172
|
-
this.logger.clone('orientation').info(`orientation onCreate ${JSON.stringify({
|
|
173
|
-
x: data?.beta,
|
|
174
|
-
y: data?.gamma,
|
|
175
|
-
z: data?.alpha,
|
|
176
|
-
timestamp: data?.timestamp
|
|
177
|
-
})}`)
|
|
178
|
-
}
|
|
179
|
-
this.ctx.rnInstance.emitDeviceEvent('
|
|
180
|
-
x: data?.beta,
|
|
181
|
-
y: data?.gamma,
|
|
182
|
-
z: data?.alpha,
|
|
183
|
-
timestamp: data?.timestamp
|
|
184
|
-
});
|
|
185
|
-
}, { interval: interval });
|
|
186
|
-
} catch (error) {
|
|
187
|
-
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
188
|
-
this.logger.clone('orientation').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
gravity(interval: number): void {
|
|
193
|
-
try {
|
|
194
|
-
interval = this.sensorsInterval.get('gravity')!;
|
|
195
|
-
const logLevel: number = this.sensorsLogLevel.get("gravity")!;
|
|
196
|
-
sensor.on(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => {
|
|
197
|
-
if (logLevel > 0) {
|
|
198
|
-
this.logger.clone('gravity').info(`gravity onCreate ${interval}`)
|
|
199
|
-
}
|
|
200
|
-
if (logLevel > 1) {
|
|
201
|
-
this.logger.clone('gravity').info(`gravity onCreate ${JSON.stringify({
|
|
202
|
-
x: data?.x,
|
|
203
|
-
y: data?.y,
|
|
204
|
-
z: data?.z,
|
|
205
|
-
timestamp: data?.timestamp
|
|
206
|
-
})}`)
|
|
207
|
-
}
|
|
208
|
-
this.ctx.rnInstance.emitDeviceEvent('gravityClick', {
|
|
209
|
-
x: data?.x,
|
|
210
|
-
y: data?.y,
|
|
211
|
-
z: data?.z,
|
|
212
|
-
timestamp: data?.timestamp
|
|
213
|
-
});
|
|
214
|
-
}, { interval: interval });
|
|
215
|
-
} catch (error) {
|
|
216
|
-
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
217
|
-
this.logger.clone('gravity').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
off(sensorType: string): void {
|
|
222
|
-
try {
|
|
223
|
-
switch (sensorType) {
|
|
224
|
-
case 'accelerometer':
|
|
225
|
-
sensor.off(sensor.SensorId.ACCELEROMETER);
|
|
226
|
-
break;
|
|
227
|
-
case 'gyroscope':
|
|
228
|
-
sensor.off(sensor.SensorId.GYROSCOPE);
|
|
229
|
-
break;
|
|
230
|
-
case 'magnetometer':
|
|
231
|
-
sensor.off(sensor.SensorId.MAGNETIC_FIELD);
|
|
232
|
-
break;
|
|
233
|
-
case 'barometer':
|
|
234
|
-
sensor.off(sensor.SensorId.BAROMETER);
|
|
235
|
-
break;
|
|
236
|
-
case 'orientation':
|
|
237
|
-
sensor.off(sensor.SensorId.ORIENTATION);
|
|
238
|
-
break;
|
|
239
|
-
case 'gravity':
|
|
240
|
-
sensor.off(sensor.SensorId.GRAVITY);
|
|
241
|
-
break;
|
|
242
|
-
default:
|
|
243
|
-
}
|
|
244
|
-
} catch (error) {
|
|
245
|
-
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
246
|
-
this.logger.clone(`${sensorType} off}`).error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
setUpdateInterval(sensorType: string, updateInterval: number): void {
|
|
251
|
-
updateInterval = updateInterval * 1000000;
|
|
252
|
-
this.sensorsInterval.set(
|
|
253
|
-
sensorType,
|
|
254
|
-
updateInterval
|
|
255
|
-
)
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
async isAvailable(sensorType: string): Promise<boolean> {
|
|
259
|
-
try {
|
|
260
|
-
const sensorsTypeId: sensor.SensorId = this.sensorsId.get(sensorType);
|
|
261
|
-
const sensorsTypeDetails: sensor.Sensor = await sensor.getSingleSensor(sensorsTypeId);
|
|
262
|
-
return new Promise((resolve) => resolve(!!sensorsTypeDetails?.sensorId))
|
|
263
|
-
} catch (error) {
|
|
264
|
-
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
265
|
-
this.logger.clone(`${sensorType} isAvailable}`).error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
266
|
-
return new Promise((resolve) => resolve(false))
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
setLogLevel(sensorType: string, logLevel: number): void {
|
|
271
|
-
this.sensorsLogLevel.set(
|
|
272
|
-
sensorType,
|
|
273
|
-
logLevel
|
|
274
|
-
)
|
|
275
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
3
|
+
* Licensed under the MIT License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* https://github.com/react-native-sensors/react-native-sensors/blob/master/LICENSE
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { TurboModule, RNOHLogger, TurboModuleContext } from "@rnoh/react-native-openharmony/ts";
|
|
17
|
+
import sensor from "@ohos.sensor";
|
|
18
|
+
import BusinessError from "@ohos.base";
|
|
19
|
+
|
|
20
|
+
import { RTNSensors } from "./generated/turboModules/RTNSensors";
|
|
21
|
+
|
|
22
|
+
export class RTNSensorsTurboModule extends TurboModule implements RTNSensors.Spec {
|
|
23
|
+
private logger: RNOHLogger;
|
|
24
|
+
private sensorsInterval = new Map<string, number>([
|
|
25
|
+
["accelerometer", 0],
|
|
26
|
+
["gyroscope", 0],
|
|
27
|
+
["magnetometer", 0],
|
|
28
|
+
["barometer", 0],
|
|
29
|
+
["orientation", 0],
|
|
30
|
+
["gravity", 0],
|
|
31
|
+
])
|
|
32
|
+
private sensorsLogLevel = new Map<string, number>([
|
|
33
|
+
["accelerometer", 0],
|
|
34
|
+
["gyroscope", 0],
|
|
35
|
+
["magnetometer", 0],
|
|
36
|
+
["barometer", 0],
|
|
37
|
+
["orientation", 0],
|
|
38
|
+
["gravity", 0],
|
|
39
|
+
])
|
|
40
|
+
private sensorsId = new Map<string, sensor.SensorId>([
|
|
41
|
+
["accelerometer", sensor.SensorId.ACCELEROMETER],
|
|
42
|
+
["gyroscope", sensor.SensorId.GYROSCOPE],
|
|
43
|
+
["magnetometer", sensor.SensorId.MAGNETIC_FIELD],
|
|
44
|
+
["barometer", sensor.SensorId.BAROMETER],
|
|
45
|
+
["orientation", sensor.SensorId.ORIENTATION],
|
|
46
|
+
["gravity", sensor.SensorId.GRAVITY],
|
|
47
|
+
])
|
|
48
|
+
|
|
49
|
+
constructor(ctx: TurboModuleContext) {
|
|
50
|
+
super(ctx);
|
|
51
|
+
this.logger = ctx.logger.clone("Sensors");
|
|
52
|
+
}
|
|
53
|
+
accelerometer(interval: number): void {
|
|
54
|
+
try {
|
|
55
|
+
interval = this.sensorsInterval.get('accelerometer')!;
|
|
56
|
+
const logLevel: number = this.sensorsLogLevel.get("accelerometer")!;
|
|
57
|
+
sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => {
|
|
58
|
+
if (logLevel > 0) {
|
|
59
|
+
this.logger.clone('accelerometer').info(`accelerometer onCreate ${interval}`)
|
|
60
|
+
}
|
|
61
|
+
if (logLevel > 1) {
|
|
62
|
+
this.logger.clone('accelerometer').info(`accelerometer onCreate ${JSON.stringify({
|
|
63
|
+
x: data?.x,
|
|
64
|
+
y: data?.y,
|
|
65
|
+
z: data?.z,
|
|
66
|
+
timestamp: data?.timestamp
|
|
67
|
+
})}`)
|
|
68
|
+
}
|
|
69
|
+
this.ctx.rnInstance.emitDeviceEvent('Accelerometer', {
|
|
70
|
+
x: data?.x,
|
|
71
|
+
y: data?.y,
|
|
72
|
+
z: data?.z,
|
|
73
|
+
timestamp: data?.timestamp
|
|
74
|
+
});
|
|
75
|
+
}, { interval: interval });
|
|
76
|
+
} catch (error) {
|
|
77
|
+
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
78
|
+
this.logger.clone('accelerometer').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
gyroscope(interval: number): void {
|
|
83
|
+
try {
|
|
84
|
+
interval = this.sensorsInterval.get('gyroscope')!;
|
|
85
|
+
const logLevel: number = this.sensorsLogLevel.get("gyroscope")!;
|
|
86
|
+
sensor.on(sensor.SensorId.GYROSCOPE, (data: sensor.GyroscopeResponse) => {
|
|
87
|
+
if (logLevel > 0) {
|
|
88
|
+
this.logger.clone('gyroscope').info(`gyroscope onCreate ${interval}`)
|
|
89
|
+
}
|
|
90
|
+
if (logLevel > 1) {
|
|
91
|
+
this.logger.clone('gyroscope').info(`gyroscope onCreate ${JSON.stringify({
|
|
92
|
+
x: data?.x,
|
|
93
|
+
y: data?.y,
|
|
94
|
+
z: data?.z,
|
|
95
|
+
timestamp: data?.timestamp
|
|
96
|
+
})}`)
|
|
97
|
+
}
|
|
98
|
+
this.ctx.rnInstance.emitDeviceEvent('Gyroscope', {
|
|
99
|
+
x: data?.x,
|
|
100
|
+
y: data?.y,
|
|
101
|
+
z: data?.z,
|
|
102
|
+
timestamp: data?.timestamp
|
|
103
|
+
});
|
|
104
|
+
}, { interval: interval });
|
|
105
|
+
} catch (error) {
|
|
106
|
+
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
107
|
+
this.logger.clone('gyroscope').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
magnetometer(interval: number): void {
|
|
112
|
+
try {
|
|
113
|
+
interval = this.sensorsInterval.get('magnetometer')!;
|
|
114
|
+
const logLevel: number = this.sensorsLogLevel.get("magnetometer")!;
|
|
115
|
+
sensor.on(sensor.SensorId.MAGNETIC_FIELD, (data: sensor.MagneticFieldResponse) => {
|
|
116
|
+
if (logLevel > 0) {
|
|
117
|
+
this.logger.clone('magnetometer').info(`magnetometer onCreate ${interval}`)
|
|
118
|
+
}
|
|
119
|
+
if (logLevel > 1) {
|
|
120
|
+
this.logger.clone('magnetometer').info(`magnetometer onCreate ${JSON.stringify({
|
|
121
|
+
x: data?.x,
|
|
122
|
+
y: data?.y,
|
|
123
|
+
z: data?.z,
|
|
124
|
+
timestamp: data?.timestamp
|
|
125
|
+
})}`)
|
|
126
|
+
}
|
|
127
|
+
this.ctx.rnInstance.emitDeviceEvent('Magnetometer', {
|
|
128
|
+
x: data?.x,
|
|
129
|
+
y: data?.y,
|
|
130
|
+
z: data?.z,
|
|
131
|
+
timestamp: data?.timestamp
|
|
132
|
+
});
|
|
133
|
+
}, { interval: interval });
|
|
134
|
+
} catch (error) {
|
|
135
|
+
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
136
|
+
this.logger.clone('magnetometer').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
barometer(interval: number): void {
|
|
141
|
+
try {
|
|
142
|
+
interval = this.sensorsInterval.get('barometer')!;
|
|
143
|
+
const logLevel: number = this.sensorsLogLevel.get("barometer")!;
|
|
144
|
+
sensor.on(sensor.SensorId.BAROMETER, (data: sensor.BarometerResponse) => {
|
|
145
|
+
if (logLevel > 0) {
|
|
146
|
+
this.logger.clone('barometer').info(`barometer onCreate ${interval}`)
|
|
147
|
+
}
|
|
148
|
+
if (logLevel > 1) {
|
|
149
|
+
this.logger.clone('barometer').info(`barometer onCreate ${JSON.stringify({
|
|
150
|
+
pressure: data?.pressure
|
|
151
|
+
})}`)
|
|
152
|
+
}
|
|
153
|
+
this.ctx.rnInstance.emitDeviceEvent('Barometer', {
|
|
154
|
+
pressure: data?.pressure
|
|
155
|
+
});
|
|
156
|
+
}, { interval: interval });
|
|
157
|
+
} catch (error) {
|
|
158
|
+
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
159
|
+
this.logger.clone('barometer').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
orientation(interval: number): void {
|
|
164
|
+
try {
|
|
165
|
+
interval = this.sensorsInterval.get('orientation')!;
|
|
166
|
+
const logLevel: number = this.sensorsLogLevel.get("orientation")!;
|
|
167
|
+
sensor.on(sensor.SensorId.ORIENTATION, (data: sensor.OrientationResponse) => {
|
|
168
|
+
if (logLevel > 0) {
|
|
169
|
+
this.logger.clone('orientation').info(`orientation onCreate ${interval}`)
|
|
170
|
+
}
|
|
171
|
+
if (logLevel > 1) {
|
|
172
|
+
this.logger.clone('orientation').info(`orientation onCreate ${JSON.stringify({
|
|
173
|
+
x: data?.beta,
|
|
174
|
+
y: data?.gamma,
|
|
175
|
+
z: data?.alpha,
|
|
176
|
+
timestamp: data?.timestamp
|
|
177
|
+
})}`)
|
|
178
|
+
}
|
|
179
|
+
this.ctx.rnInstance.emitDeviceEvent('Orientation', {
|
|
180
|
+
x: data?.beta,
|
|
181
|
+
y: data?.gamma,
|
|
182
|
+
z: data?.alpha,
|
|
183
|
+
timestamp: data?.timestamp
|
|
184
|
+
});
|
|
185
|
+
}, { interval: interval });
|
|
186
|
+
} catch (error) {
|
|
187
|
+
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
188
|
+
this.logger.clone('orientation').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
gravity(interval: number): void {
|
|
193
|
+
try {
|
|
194
|
+
interval = this.sensorsInterval.get('gravity')!;
|
|
195
|
+
const logLevel: number = this.sensorsLogLevel.get("gravity")!;
|
|
196
|
+
sensor.on(sensor.SensorId.GRAVITY, (data: sensor.GravityResponse) => {
|
|
197
|
+
if (logLevel > 0) {
|
|
198
|
+
this.logger.clone('gravity').info(`gravity onCreate ${interval}`)
|
|
199
|
+
}
|
|
200
|
+
if (logLevel > 1) {
|
|
201
|
+
this.logger.clone('gravity').info(`gravity onCreate ${JSON.stringify({
|
|
202
|
+
x: data?.x,
|
|
203
|
+
y: data?.y,
|
|
204
|
+
z: data?.z,
|
|
205
|
+
timestamp: data?.timestamp
|
|
206
|
+
})}`)
|
|
207
|
+
}
|
|
208
|
+
this.ctx.rnInstance.emitDeviceEvent('gravityClick', {
|
|
209
|
+
x: data?.x,
|
|
210
|
+
y: data?.y,
|
|
211
|
+
z: data?.z,
|
|
212
|
+
timestamp: data?.timestamp
|
|
213
|
+
});
|
|
214
|
+
}, { interval: interval });
|
|
215
|
+
} catch (error) {
|
|
216
|
+
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
217
|
+
this.logger.clone('gravity').error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
off(sensorType: string): void {
|
|
222
|
+
try {
|
|
223
|
+
switch (sensorType) {
|
|
224
|
+
case 'accelerometer':
|
|
225
|
+
sensor.off(sensor.SensorId.ACCELEROMETER);
|
|
226
|
+
break;
|
|
227
|
+
case 'gyroscope':
|
|
228
|
+
sensor.off(sensor.SensorId.GYROSCOPE);
|
|
229
|
+
break;
|
|
230
|
+
case 'magnetometer':
|
|
231
|
+
sensor.off(sensor.SensorId.MAGNETIC_FIELD);
|
|
232
|
+
break;
|
|
233
|
+
case 'barometer':
|
|
234
|
+
sensor.off(sensor.SensorId.BAROMETER);
|
|
235
|
+
break;
|
|
236
|
+
case 'orientation':
|
|
237
|
+
sensor.off(sensor.SensorId.ORIENTATION);
|
|
238
|
+
break;
|
|
239
|
+
case 'gravity':
|
|
240
|
+
sensor.off(sensor.SensorId.GRAVITY);
|
|
241
|
+
break;
|
|
242
|
+
default:
|
|
243
|
+
}
|
|
244
|
+
} catch (error) {
|
|
245
|
+
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
246
|
+
this.logger.clone(`${sensorType} off}`).error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
setUpdateInterval(sensorType: string, updateInterval: number): void {
|
|
251
|
+
updateInterval = updateInterval * 1000000;
|
|
252
|
+
this.sensorsInterval.set(
|
|
253
|
+
sensorType,
|
|
254
|
+
updateInterval
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async isAvailable(sensorType: string): Promise<boolean> {
|
|
259
|
+
try {
|
|
260
|
+
const sensorsTypeId: sensor.SensorId = this.sensorsId.get(sensorType);
|
|
261
|
+
const sensorsTypeDetails: sensor.Sensor = await sensor.getSingleSensor(sensorsTypeId);
|
|
262
|
+
return new Promise((resolve) => resolve(!!sensorsTypeDetails?.sensorId))
|
|
263
|
+
} catch (error) {
|
|
264
|
+
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
|
|
265
|
+
this.logger.clone(`${sensorType} isAvailable}`).error(`Failed to invoke on. Code: ${e.code}, message: ${e.message}`);
|
|
266
|
+
return new Promise((resolve) => resolve(false))
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
setLogLevel(sensorType: string, logLevel: number): void {
|
|
271
|
+
this.sensorsLogLevel.set(
|
|
272
|
+
sensorType,
|
|
273
|
+
logLevel
|
|
274
|
+
)
|
|
275
|
+
}
|
|
276
276
|
}
|
|
@@ -1,37 +1,38 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
3
|
-
* Licensed under the MIT License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License.
|
|
5
|
-
* You may obtain a copy of the License at
|
|
6
|
-
*
|
|
7
|
-
* https://github.com/react-native-sensors/react-native-sensors/blob/master/LICENSE
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
* See the License for the specific language governing permissions and
|
|
13
|
-
* limitations under the License.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { RNPackage, TurboModulesFactory } from '@rnoh/react-native-openharmony/ts';
|
|
17
|
-
import type { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
|
|
18
|
-
import { RTNSensorsTurboModule } from './RTNSensorsTurboModule';
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
3
|
+
* Licensed under the MIT License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* https://github.com/react-native-sensors/react-native-sensors/blob/master/LICENSE
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { RNPackage, TurboModulesFactory } from '@rnoh/react-native-openharmony/ts';
|
|
17
|
+
import type { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
|
|
18
|
+
import { RTNSensorsTurboModule } from './RTNSensorsTurboModule';
|
|
19
|
+
import { RNOHPackage } from '@rnoh/react-native-openharmony';
|
|
20
|
+
|
|
21
|
+
class SensorsModulesFactory extends TurboModulesFactory {
|
|
22
|
+
createTurboModule(name: string): TurboModule | null {
|
|
23
|
+
if (name === 'RTNSensors') {
|
|
24
|
+
return new RTNSensorsTurboModule(this.ctx)
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
hasTurboModule(name: string): boolean {
|
|
30
|
+
return name === 'RTNSensors';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class SensorsPackage extends RNOHPackage {
|
|
35
|
+
createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory {
|
|
36
|
+
return new SensorsModulesFactory(ctx);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export {}
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export * from "./ts"
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export * from "./ts"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export * as RNC from "./components/ts"
|
|
6
|
-
export * as TM from "./turboModules/ts"
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export * as RNC from "./components/ts"
|
|
6
|
+
export * as TM from "./turboModules/ts"
|