@react-native-ohos/react-native-sensors 7.2.2-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.
Files changed (30) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -0
  3. package/harmony/sensors/Index.ets +16 -0
  4. package/harmony/sensors/build-profile.json5 +12 -0
  5. package/harmony/sensors/hvigorfile.ts +6 -0
  6. package/harmony/sensors/oh-package.json5 +11 -0
  7. package/harmony/sensors/src/main/cpp/CMakeLists.txt +9 -0
  8. package/harmony/sensors/src/main/cpp/SensorsPackage.h +25 -0
  9. package/harmony/sensors/src/main/cpp/generated/RNOH/generated/BaseReactNativeSensorsPackage.h +30 -0
  10. package/harmony/sensors/src/main/cpp/generated/RNOH/generated/turbo_modules/RTNSensors.cpp +25 -0
  11. package/harmony/sensors/src/main/cpp/generated/RNOH/generated/turbo_modules/RTNSensors.h +20 -0
  12. package/harmony/sensors/src/main/ets/RTNSensorsTurboModule.ts +276 -0
  13. package/harmony/sensors/src/main/ets/SensorsPackage.ts +37 -0
  14. package/harmony/sensors/src/main/ets/generated/components/ts.ts +5 -0
  15. package/harmony/sensors/src/main/ets/generated/index.ets +5 -0
  16. package/harmony/sensors/src/main/ets/generated/ts.ts +6 -0
  17. package/harmony/sensors/src/main/ets/generated/turboModules/RTNSensors.ts +30 -0
  18. package/harmony/sensors/src/main/ets/generated/turboModules/ts.ts +5 -0
  19. package/harmony/sensors/src/main/module.json5 +9 -0
  20. package/harmony/sensors/src/main/resources/base/element/string.json +8 -0
  21. package/harmony/sensors/src/main/resources/en_US/element/string.json +8 -0
  22. package/harmony/sensors/src/main/resources/zh_CN/element/string.json +8 -0
  23. package/harmony/sensors/ts.ts +17 -0
  24. package/harmony/sensors.har +0 -0
  25. package/index.d.ts +69 -0
  26. package/index.js +14 -0
  27. package/package.json +88 -0
  28. package/src/NativeSensors.ts +31 -0
  29. package/src/rnsensors.js +49 -0
  30. package/src/sensors.js +76 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Daniel Schmidt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @react-native-ohos/react-native-sensors
2
+
3
+ This project is based on [react-native-sensors](https://github.com/react-native-sensors/react-native-sensors/tree/v7.2.1-rc.2)
4
+
5
+ ## Documentation
6
+
7
+ - [中文](https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/react-native-sensors.md)
8
+
9
+ - [English](https://gitee.com/react-native-oh-library/usage-docs/blob/master/en/react-native-sensors.md)
10
+
11
+ ## License
12
+
13
+ This project is licensed under [The MIT License (MIT)](https://github.com/react-native-sensors/react-native-sensors/blob/master/LICENSE), Please enjoy and participate
@@ -0,0 +1,16 @@
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
+ export * from './ts'
@@ -0,0 +1,12 @@
1
+ {
2
+ "apiType": "stageMode",
3
+ "buildOption": {
4
+ },
5
+ "buildOptionSet": [
6
+ ],
7
+ "targets": [
8
+ {
9
+ "name": "default"
10
+ }
11
+ ]
12
+ }
@@ -0,0 +1,6 @@
1
+ import { harTasks } from '@ohos/hvigor-ohos-plugin';
2
+
3
+ export default {
4
+ system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
5
+ plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
6
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ name: "@react-native-ohos/react-native-sensors",
3
+ version: "7.2.2-rc.2",
4
+ description: "Please describe the basic information.",
5
+ main: "Index.ets",
6
+ author: "",
7
+ license: "MIT License",
8
+ dependencies: {
9
+ "@rnoh/react-native-openharmony": "^0.72.38"
10
+ },
11
+ }
@@ -0,0 +1,9 @@
1
+ cmake_minimum_required(VERSION 3.13)
2
+ set(CMAKE_VERBOSE_MAKEFILE on)
3
+
4
+ set(rnoh_sensors_generated_dir "${CMAKE_CURRENT_SOURCE_DIR}/generated")
5
+ file(GLOB_RECURSE rnoh_sensors_generated_SRC "${rnoh_sensors_generated_dir}/**/*.cpp")
6
+ file(GLOB rnoh_sensors_SRC CONFIGURE_DEPENDS *.cpp)
7
+ add_library(rnoh_sensors SHARED ${rnoh_sensors_SRC} ${rnoh_sensors_generated_SRC})
8
+ target_include_directories(rnoh_sensors PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${rnoh_sensors_generated_dir})
9
+ target_link_libraries(rnoh_sensors PUBLIC rnoh)
@@ -0,0 +1,25 @@
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
+ #pragma once
17
+
18
+ #include "generated/RNOH/generated/BaseReactNativeSensorsPackage.h"
19
+
20
+ namespace rnoh {
21
+ class SensorsPackage : public BaseReactNativeSensorsPackage {
22
+ using Super = BaseReactNativeSensorsPackage;
23
+ using Super::Super;
24
+ };
25
+ } // namespace rnoh
@@ -0,0 +1,30 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-lib-harmony"
3
+ */
4
+
5
+ #pragma once
6
+
7
+ #include "RNOH/Package.h"
8
+ #include "RNOH/ArkTSTurboModule.h"
9
+ #include "./turbo_modules/RTNSensors.h"
10
+
11
+ namespace rnoh {
12
+ class BaseReactNativeSensorsPackageTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
13
+ public:
14
+ SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
15
+ if (name == "RTNSensors") {
16
+ return std::make_shared<RTNSensors>(ctx, name);
17
+ }
18
+ return nullptr;
19
+ };
20
+ };
21
+
22
+ class BaseReactNativeSensorsPackage : public Package {
23
+ public:
24
+ BaseReactNativeSensorsPackage(Package::Context ctx) : Package(ctx) {
25
+ };
26
+ std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
27
+ return std::make_unique<BaseReactNativeSensorsPackageTurboModuleFactoryDelegate>();
28
+ }
29
+ };
30
+ } // namespace rnoh
@@ -0,0 +1,25 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-lib-harmony"
3
+ */
4
+
5
+ #include "RTNSensors.h"
6
+
7
+ namespace rnoh {
8
+ using namespace facebook;
9
+
10
+ RTNSensors::RTNSensors(const ArkTSTurboModule::Context ctx, const std::string name) : ArkTSTurboModule(ctx, name) {
11
+ methodMap_ = {
12
+ ARK_METHOD_METADATA(accelerometer, 0),
13
+ ARK_METHOD_METADATA(gyroscope, 0),
14
+ ARK_METHOD_METADATA(magnetometer, 0),
15
+ ARK_METHOD_METADATA(barometer, 0),
16
+ ARK_METHOD_METADATA(orientation, 0),
17
+ ARK_METHOD_METADATA(gravity, 0),
18
+ ARK_METHOD_METADATA(off, 1),
19
+ ARK_METHOD_METADATA(setUpdateInterval, 2),
20
+ ARK_ASYNC_METHOD_METADATA(isAvailable, 1),
21
+ ARK_METHOD_METADATA(setLogLevel, 2),
22
+ };
23
+ }
24
+
25
+ } // namespace rnoh
@@ -0,0 +1,20 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-lib-harmony"
3
+ */
4
+
5
+ #ifndef SENSORMODULE_H
6
+ #define SENSORMODULE_H
7
+
8
+ #pragma once
9
+
10
+ #include "RNOH/ArkTSTurboModule.h"
11
+
12
+ namespace rnoh {
13
+
14
+ class JSI_EXPORT RTNSensors : public ArkTSTurboModule {
15
+ public:
16
+ RTNSensors(const ArkTSTurboModule::Context ctx, const std::string name);
17
+ };
18
+
19
+ } // namespace rnoh
20
+ #endif
@@ -0,0 +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('accelerometerClick', {
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('gyroscopeClick', {
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('magnetometerClick', {
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('barometerClick', {
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('orientationClick', {
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
+ }
@@ -0,0 +1,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
+
20
+ class SensorsModulesFactory extends TurboModulesFactory {
21
+ createTurboModule(name: string): TurboModule | null {
22
+ if (name === 'RTNSensors') {
23
+ return new RTNSensorsTurboModule(this.ctx)
24
+ }
25
+ return null;
26
+ }
27
+
28
+ hasTurboModule(name: string): boolean {
29
+ return name === 'RTNSensors';
30
+ }
31
+ }
32
+
33
+ export class SensorsPackage extends RNPackage {
34
+ createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory {
35
+ return new SensorsModulesFactory(ctx);
36
+ }
37
+ }
@@ -0,0 +1,5 @@
1
+
2
+ /**
3
+ */
4
+
5
+ export {}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-lib-harmony"
3
+ */
4
+
5
+ export * from "./ts"
@@ -0,0 +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"
@@ -0,0 +1,30 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-lib-harmony"
3
+ */
4
+
5
+ export namespace RTNSensors {
6
+ export const NAME = 'RTNSensors' as const
7
+
8
+ export interface Spec {
9
+ accelerometer(interval: number): void;
10
+
11
+ gyroscope(interval: number): void;
12
+
13
+ magnetometer(interval: number): void;
14
+
15
+ barometer(interval: number): void;
16
+
17
+ orientation(interval: number): void;
18
+
19
+ gravity(interval: number): void;
20
+
21
+ off(sensorType: string): void;
22
+
23
+ setUpdateInterval(sensorType: string, updateInterval: number): void;
24
+
25
+ isAvailable(sensorType: string): Promise<boolean>;
26
+
27
+ setLogLevel(sensorType: string, logLevel: number): void;
28
+
29
+ }
30
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * This code was generated by "react-native codegen-lib-harmony"
3
+ */
4
+
5
+ export * from "./RTNSensors"
@@ -0,0 +1,9 @@
1
+ {
2
+ "module": {
3
+ "name": "sensors",
4
+ "type": "har",
5
+ "deviceTypes": [
6
+ "default",
7
+ ]
8
+ }
9
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "string": [
3
+ {
4
+ "name": "page_show",
5
+ "value": "page from package"
6
+ }
7
+ ]
8
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "string": [
3
+ {
4
+ "name": "page_show",
5
+ "value": "page from package"
6
+ }
7
+ ]
8
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "string": [
3
+ {
4
+ "name": "page_show",
5
+ "value": "page from package"
6
+ }
7
+ ]
8
+ }
@@ -0,0 +1,17 @@
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
+ export * from './src/main/ets/RTNSensorsTurboModule'
17
+ export * from './src/main/ets/SensorsPackage'
Binary file
package/index.d.ts ADDED
@@ -0,0 +1,69 @@
1
+ declare module "react-native-sensors" {
2
+ import { Observable } from "rxjs";
3
+
4
+ type Sensors = {
5
+ accelerometer: "accelerometer";
6
+ gyroscope: "gyroscope";
7
+ magnetometer: "magnetometer";
8
+ barometer: "barometer";
9
+ orientation: "orientation";
10
+ gravity: "gravity";
11
+ };
12
+
13
+ export const SensorTypes: Sensors;
14
+
15
+ export const setUpdateIntervalForType: (
16
+ type: keyof Sensors,
17
+ updateInterval: number
18
+ ) => void;
19
+
20
+ export const setLogLevelForType: (
21
+ type: keyof Sensors,
22
+ logLevel: 0 | 1 | 2
23
+ ) => void;
24
+
25
+ export interface SensorData {
26
+ x: number;
27
+ y: number;
28
+ z: number;
29
+ timestamp: number;
30
+ }
31
+
32
+ export interface BarometerData {
33
+ pressure: number;
34
+ timestamp: number;
35
+ }
36
+
37
+ export interface OrientationData {
38
+ qx: number;
39
+ qy: number;
40
+ qz: number;
41
+ qw: number;
42
+ pitch: number;
43
+ roll: number;
44
+ yaw: number;
45
+ timestamp: number;
46
+ }
47
+
48
+ type SensorsBase = {
49
+ accelerometer: Observable<SensorData>;
50
+ gyroscope: Observable<SensorData>;
51
+ magnetometer: Observable<SensorData>;
52
+ barometer: Observable<BarometerData>;
53
+ orientation: Observable<OrientationData>;
54
+ gravity: Observable<SensorData>;
55
+ };
56
+
57
+ export const {
58
+ accelerometer,
59
+ gyroscope,
60
+ magnetometer,
61
+ barometer,
62
+ orientation,
63
+ gravity,
64
+ }: SensorsBase;
65
+
66
+ const sensors: SensorsBase;
67
+
68
+ export default sensors;
69
+ }
package/index.js ADDED
@@ -0,0 +1,14 @@
1
+ import sensors from "./src/sensors";
2
+ export { setUpdateInterval as setUpdateIntervalForType, setLogLevelForType } from "./src/rnsensors";
3
+
4
+ export const SensorTypes = {
5
+ accelerometer: "accelerometer",
6
+ gyroscope: "gyroscope",
7
+ magnetometer: "magnetometer",
8
+ barometer: "barometer",
9
+ orientation: "orientation",
10
+ gravity: "gravity"
11
+ };
12
+
13
+ export const { accelerometer, gyroscope, magnetometer, barometer, orientation, gravity } = sensors;
14
+ export default sensors;
package/package.json ADDED
@@ -0,0 +1,88 @@
1
+ {
2
+ "name": "@react-native-ohos/react-native-sensors",
3
+ "version": "7.2.2-rc.2",
4
+ "description": "A developer friendly approach for sensors in react-native",
5
+ "scripts": {
6
+ "contributor:add": "all-contributors add",
7
+ "postcontributor:add": "npm run contributor:generate",
8
+ "contributor:generate": "all-contributors generate",
9
+ "postcontributor:generate": "npm run format",
10
+ "format": "prettier --write ./src/*",
11
+ "test": "prettier --check ./src/*",
12
+ "semantic-release": "semantic-release",
13
+ "codegen-lib": "react-native codegen-lib-harmony --no-safety-check --npm-package-name react-native-sensors --cpp-output-path ./harmony/sensors/src/main/cpp/generated --ets-output-path ./harmony/sensors/src/main/ets/generated --turbo-modules-spec-paths ./src/NativeSensors.ts",
14
+ "travis-deploy-once": "travis-deploy-once"
15
+ },
16
+ "main": "index.js",
17
+ "types": "index.d.ts",
18
+ "repository": "git@github.com:react-native-sensors/react-native-sensors.git",
19
+ "homepage": "https://github.com/react-native-sensors/react-native-sensors",
20
+ "keywords": [
21
+ "react-native",
22
+ "harmony"
23
+ ],
24
+ "author": {
25
+ "name": "Daniel Schmidt",
26
+ "email": "danielmschmidt92@gmail.com",
27
+ "url": "http://danielmschmidt.de"
28
+ },
29
+ "husky": {
30
+ "hooks": {
31
+ "pre-commit": "pretty-quick --staged"
32
+ }
33
+ },
34
+ "jest": {
35
+ "timers": "fake",
36
+ "projects": [
37
+ {
38
+ "displayName": "Unit",
39
+ "testEnvironment": "node",
40
+ "testPathIgnorePatterns": [
41
+ "tests/*"
42
+ ],
43
+ "timers": "fake"
44
+ }
45
+ ]
46
+ },
47
+ "babel": {
48
+ "presets": [
49
+ "react-native"
50
+ ]
51
+ },
52
+ "license": "MIT",
53
+ "dependencies": {
54
+ "rxjs": ">= 6"
55
+ },
56
+ "devDependencies": {
57
+ "all-contributors-cli": "6.1.2",
58
+ "babel-core": "6.26.3",
59
+ "babel-jest": "24.1.0",
60
+ "babel-preset-react-native": "4.0.0",
61
+ "husky": "4.2.3",
62
+ "jest": "24.1.0",
63
+ "metro-config": "^0.67.0",
64
+ "prettier": "2.2.1",
65
+ "pretty-quick": "3.1.0",
66
+ "react": "^17.0.2",
67
+ "react-native": "^0.68.0",
68
+ "regenerator-runtime": "0.13.1",
69
+ "rxjs-marbles": "5.0.0",
70
+ "semantic-release": "17.3.9",
71
+ "react-native-harmony-cli": "npm:@react-native-oh/react-native-harmony-cli@^0.0.27",
72
+ "travis-deploy-once": "5.0.11"
73
+ },
74
+ "harmony": {
75
+ "alias": "react-native-sensors"
76
+ },
77
+ "files": [
78
+ "src",
79
+ "harmony",
80
+ "LICENSE",
81
+ "README.md",
82
+ "index.*"
83
+ ],
84
+ "publishConfig": {
85
+ "registry": "https://registry.npmjs.org/",
86
+ "access": "public"
87
+ }
88
+ }
@@ -0,0 +1,31 @@
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 type { TurboModule } from "react-native/Libraries/TurboModule/RCTExport";
17
+ import { TurboModuleRegistry } from "react-native";
18
+
19
+ export interface Spec extends TurboModule {
20
+ accelerometer(interval?: number): void;
21
+ gyroscope(interval?: number): void;
22
+ magnetometer(interval?: number): void;
23
+ barometer(interval?: number): void;
24
+ orientation(interval?: number): void;
25
+ gravity(interval?: number): void;
26
+ off(sensorType: string): void;
27
+ setUpdateInterval(sensorType: string, updateInterval: number): void;
28
+ isAvailable(sensorType: string): Promise<boolean>;
29
+ setLogLevel(sensorType: string, logLevel: number): void;
30
+ }
31
+ export default TurboModuleRegistry.get<Spec>("RTNSensors") as Spec | null;
@@ -0,0 +1,49 @@
1
+ import RTNSensors from "./NativeSensors";
2
+ // Cache the availability of sensors
3
+ const availableSensors = {};
4
+
5
+ export function start(type) {
6
+ switch (type) {
7
+ case 'accelerometer':
8
+ RTNSensors.accelerometer();
9
+ break;
10
+ case 'gyroscope':
11
+ RTNSensors.gyroscope();
12
+ break;
13
+ case 'magnetometer':
14
+ RTNSensors.magnetometer();
15
+ break;
16
+ case 'barometer':
17
+ RTNSensors.barometer();
18
+ break;
19
+ case 'orientation':
20
+ RTNSensors.orientation();
21
+ break;
22
+ case 'gravity':
23
+ RTNSensors.gravity();
24
+ break;
25
+ default:
26
+ }
27
+ }
28
+
29
+ export function isAvailable(type) {
30
+ if (availableSensors[type]) {
31
+ return availableSensors[type];
32
+ }
33
+ const promise = RTNSensors.isAvailable(type);
34
+ availableSensors[type] = promise;
35
+
36
+ return promise;
37
+ }
38
+
39
+ export function stop(type) {
40
+ RTNSensors.off(type);
41
+ }
42
+
43
+ export function setUpdateInterval(type, updateInterval) {
44
+ RTNSensors.setUpdateInterval(type, updateInterval);
45
+ }
46
+
47
+ export function setLogLevelForType(type, level) {
48
+ RTNSensors.setLogLevel(type, level);
49
+ }
package/src/sensors.js ADDED
@@ -0,0 +1,76 @@
1
+ import { DeviceEventEmitter } from "react-native";
2
+ import { Observable } from "rxjs";
3
+ import { publish, refCount } from "rxjs/operators";
4
+ import * as RNSensors from "./rnsensors";
5
+
6
+ const listenerKeys = new Map([
7
+ ["accelerometer", "Accelerometer"],
8
+ ["gyroscope", "Gyroscope"],
9
+ ["magnetometer", "Magnetometer"],
10
+ ["barometer", "Barometer"],
11
+ ["orientation", "Orientation"],
12
+ ["gravity", "gravityClick"],
13
+ ]);
14
+
15
+ const eventEmitterSubscription = new Map([
16
+ ["accelerometer", null],
17
+ ["gyroscope", null],
18
+ ["magnetometer", null],
19
+ ["barometer", null],
20
+ ["orientation", null],
21
+ ["gravity", null],
22
+ ]);
23
+
24
+ function createSensorObservable(sensorType) {
25
+ return Observable.create(function subscribe(observer) {
26
+ this.isSensorAvailable = false;
27
+ this.unsubscribeCallback = () => {
28
+ if (!this.isSensorAvailable) return;
29
+ if (eventEmitterSubscription.get(sensorType))
30
+ eventEmitterSubscription.get(sensorType).remove();
31
+ RNSensors.stop(sensorType);
32
+ RNSensors.setUpdateInterval(sensorType, 0);
33
+ RNSensors.setLogLevelForType(sensorType, 0);
34
+ };
35
+ RNSensors.isAvailable(sensorType).then((res) => {
36
+ this.isSensorAvailable = true;
37
+ if (!res) {
38
+ observer.error(`Sensors${sensorType} is not available`);
39
+ return;
40
+ } else {
41
+ RNSensors.start(sensorType);
42
+ eventEmitterSubscription.set(
43
+ sensorType,
44
+ DeviceEventEmitter.addListener(
45
+ listenerKeys.get(sensorType),
46
+ (date) => {
47
+ observer.next(date);
48
+ }
49
+ )
50
+ );
51
+ }
52
+ });
53
+ return this.unsubscribeCallback;
54
+ }).pipe(makeSingleton());
55
+ }
56
+
57
+ // As we only have one sensor we need to share it between the different consumers
58
+ function makeSingleton() {
59
+ return (source) => source.pipe(publish(), refCount());
60
+ }
61
+
62
+ const accelerometer = createSensorObservable("accelerometer");
63
+ const gyroscope = createSensorObservable("gyroscope");
64
+ const magnetometer = createSensorObservable("magnetometer");
65
+ const barometer = createSensorObservable("barometer");
66
+ const orientation = createSensorObservable("orientation");
67
+ const gravity = createSensorObservable("gravity");
68
+
69
+ export default {
70
+ gyroscope,
71
+ accelerometer,
72
+ magnetometer,
73
+ barometer,
74
+ orientation,
75
+ gravity,
76
+ };