• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache 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 *     http://www.apache.org/licenses/LICENSE-2.0
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
16import { paramMock } from "../utils"
17
18export function mockSensor() {
19    const AccelerometerResponse = {
20        x: '[PC preview] unknown x',
21        y: '[PC preview] unknown y',
22        z: '[PC preview] unknown z',
23        timestamp: '[PC preview] unknown timestamp'
24    }
25    const LinearAccelerometerResponse = {
26        x: '[PC preview] unknown x',
27        y: '[PC preview] unknown y',
28        z: '[PC preview] unknown z',
29        timestamp: '[PC preview] unknown timestamp'
30    }
31    const AccelerometerUncalibratedResponse = {
32        x: '[PC preview] unknown x',
33        y: '[PC preview] unknown y',
34        z: '[PC preview] unknown z',
35        biasX: '[PC preview] unknown biasX',
36        biasY: '[PC preview] unknown biasY',
37        biasZ: '[PC preview] unknown biasZ',
38        timestamp: '[PC preview] unknown timestamp'
39    }
40    const GravityResponse = {
41        x: '[PC preview] unknown x',
42        y: '[PC preview] unknown y',
43        z: '[PC preview] unknown z',
44        timestamp: '[PC preview] unknown timestamp'
45    }
46    const OrientationResponse = {
47        alpha: '[PC preview] unknown alpha',
48        beta: '[PC preview] unknown beta',
49        gamma: '[PC preview] unknown gamma',
50        timestamp: '[PC preview] unknown timestamp'
51    }
52    const RotationVectorResponse = {
53        x: '[PC preview] unknown x',
54        y: '[PC preview] unknown y',
55        z: '[PC preview] unknown z',
56        w: '[PC preview] unknown w',
57        timestamp: '[PC preview] unknown timestamp'
58    }
59    const GyroscopeResponse = {
60        x: '[PC preview] unknown x',
61        y: '[PC preview] unknown y',
62        z: '[PC preview] unknown z',
63        timestamp: '[PC preview] unknown timestamp'
64    }
65    const GyroscopeUncalibratedResponse = {
66        x: '[PC preview] unknown x',
67        y: '[PC preview] unknown y',
68        z: '[PC preview] unknown z',
69        biasX: '[PC preview] unknown biasX',
70        biasY: '[PC preview] unknown biasY',
71        biasZ: '[PC preview] unknown biasZ',
72        timestamp: '[PC preview] unknown timestamp'
73    }
74    const SignificantMotionResponse = {
75        scalar: '[PC preview] unknown scalar',
76        timestamp: '[PC preview] unknown timestamp'
77    }
78    const ProximityResponse = {
79        distance: '[PC preview] unknown distance',
80        timestamp: '[PC preview] unknown timestamp'
81    }
82    const LightResponse = {
83        intensity: '[PC preview] unknown intensity',
84        timestamp: '[PC preview] unknown timestamp'
85    }
86    const HallResponse = {
87        status: '[PC preview] unknown status',
88        timestamp: '[PC preview] unknown timestamp'
89    }
90    const MagneticFieldResponse = {
91        x: '[PC preview] unknown x',
92        y: '[PC preview] unknown y',
93        z: '[PC preview] unknown z',
94        timestamp: '[PC preview] unknown timestamp'
95    }
96    const MagneticFieldUncalibratedResponse = {
97        x: '[PC preview] unknown x',
98        y: '[PC preview] unknown y',
99        z: '[PC preview] unknown z',
100        biasX: '[PC preview] unknown biasX',
101        biasY: '[PC preview] unknown biasY',
102        biasZ: '[PC preview] unknown biasZ',
103        timestamp: '[PC preview] unknown timestamp'
104    }
105    const PedometerResponse = {
106        steps: '[PC preview] unknown steps',
107        timestamp: '[PC preview] unknown timestamp'
108    }
109    const HumidityResponse = {
110        humidity: '[PC preview] unknown humidity',
111        timestamp: '[PC preview] unknown timestamp'
112    }
113    const PedometerDetectResponse = {
114        scalar: '[PC preview] unknown scalar',
115        timestamp: '[PC preview] unknown timestamp'
116    }
117    const AmbientTemperatureResponse = {
118        temperature: '[PC preview] unknown temperature',
119        timestamp: '[PC preview] unknown timestamp'
120    }
121    const BarometerResponse = {
122        pressure: '[PC preview] unknown pressure',
123        timestamp: '[PC preview] unknown timestamp'
124    }
125    const HeartRateResponse = {
126        heartRate: '[PC preview] unknown heartRate',
127        timestamp: '[PC preview] unknown timestamp'
128    }
129    const WearDetectionResponse = {
130        value: '[PC preview] unknown value',
131        timestamp: '[PC preview] unknown timestamp'
132    }
133    const Options = {
134        interval: '[PC preview] unknown value'
135    }
136    const GeomagneticResponse = {
137        x: '[PC preview] unknown x',
138        y: '[PC preview] unknown y',
139        z: '[PC preview] unknown z',
140        geomagneticDip: '[PC preview] unknown geomagneticDip',
141        deflectionAngle: '[PC preview] unknown deflectionAngle',
142        levelIntensity: '[PC preview] unknown levelIntensity',
143        totalIntensity: '[PC preview] unknown totalIntensity'
144    }
145    const PedometerDetectionResponse = {
146        scalar: '[PC preview] unknown scalar',
147        timestamp: '[PC preview] unknown timestamp'
148    }
149    const Response  = {
150        timestamp: '[PC preview] unknown timestamp'
151    }
152    const LocationOptions = {
153        latitude: '[PC preview] unknown latitude',
154        longitude: '[PC preview] unknown longitude',
155        altitude: '[PC preview] unknown altitude'
156    }
157    const CoordinatesOptions = {
158        x: '[PC preview] unknown x',
159        y: '[PC preview] unknown y'
160    }
161    const RotationMatrixResponse = {
162        rotation: '[PC preview] unknown rotation',
163        inclination: '[PC preview] unknown inclination'
164    }
165    const Sensor = {
166        sensorName: '[PC preview] unknown sensorName',
167        venderName: '[PC preview] unknown venderName',
168        firmwareVersion: '[PC preview] unknown firmwareVersion',
169        hardwareVersion: '[PC preview] unknown hardwareVersion',
170        sensorTypeId: '[PC preview] unknown sensorTypeId',
171        maxRange: '[PC preview] unknown maxRange',
172        precision: '[PC preview] unknown precision',
173        power: '[PC preview] unknown power'
174    }
175    const SensorType = {
176        SENSOR_TYPE_ID_ACCELEROMETER: 1,
177        SENSOR_TYPE_ID_GYROSCOPE: 2,
178        SENSOR_TYPE_ID_AMBIENT_LIGHT: 5,
179        SENSOR_TYPE_ID_MAGNETIC_FIELD: 6,
180        SENSOR_TYPE_ID_BAROMETER: 8,
181        SENSOR_TYPE_ID_HALL: 10,
182        SENSOR_TYPE_ID_PROXIMITY: 12,
183        SENSOR_TYPE_ID_HUMIDITY: 13,
184        SENSOR_TYPE_ID_ORIENTATION: 256,
185        SENSOR_TYPE_ID_GRAVITY: 257,
186        SENSOR_TYPE_ID_LINEAR_ACCELERATION: 258,
187        SENSOR_TYPE_ID_LINEAR_ACCELEROMETER: 258,
188        SENSOR_TYPE_ID_ROTATION_VECTOR: 259,
189        SENSOR_TYPE_ID_AMBIENT_TEMPERATURE: 260,
190        SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED: 261,
191        SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED: 263,
192        SENSOR_TYPE_ID_SIGNIFICANT_MOTION: 264,
193        SENSOR_TYPE_ID_PEDOMETER_DETECTION: 265,
194        SENSOR_TYPE_ID_PEDOMETER: 266,
195        SENSOR_TYPE_ID_HEART_RATE: 278,
196        SENSOR_TYPE_ID_HEART_BEAT_RATE: 278,
197        SENSOR_TYPE_ID_WEAR_DETECTION: 280,
198        SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED: 281
199    }
200    const sensor = {
201        Sensor,
202        SensorType,
203        on: function (...args) {
204            console.warn('sensor.on interface mocked in the Previewer. How this interface works on the'
205                + 'Previewer may be different from that on a real device.')
206            const len = args.length;
207            const callback = typeof args[len - 1] === 'function' ? args[len - 1] : args[len - 2];
208            if (args[0] == 1) {
209                callback.call(this, AccelerometerResponse);
210            } else if (args[0] == 2) {
211                callback.call(this, GyroscopeResponse);
212            } else if (args[0] == 5) {
213                callback.call(this, LightResponse);
214            } else if (args[0] == 6) {
215                callback.call(this, MagneticFieldResponse);
216            } else if (args[0] == 8) {
217                callback.call(this, BarometerResponse);
218            } else if (args[0] == 10) {
219                callback.call(this, HallResponse);
220            } else if (args[0] == 12) {
221                callback.call(this, ProximityResponse);
222            } else if (args[0] == 13) {
223                callback.call(this, HumidityResponse);
224            } else if (args[0] == 256) {
225                callback.call(this, OrientationResponse);
226            } else if (args[0] == 257) {
227                callback.call(this, GravityResponse);
228            } else if (args[0] == 258) {
229                callback.call(this, LinearAccelerometerResponse);
230            } else if (args[0] == 259) {
231                callback.call(this, RotationVectorResponse);
232            } else if (args[0] == 260) {
233                callback.call(this, AmbientTemperatureResponse);
234            } else if (args[0] == 261) {
235                callback.call(this, MagneticFieldUncalibratedResponse);
236            } else if (args[0] == 263) {
237                callback.call(this, GyroscopeUncalibratedResponse);
238            } else if (args[0] == 264) {
239                callback.call(this, SignificantMotionResponse);
240            } else if (args[0] == 265) {
241                callback.call(this, PedometerDetectResponse);
242            } else if (args[0] == 266) {
243                callback.call(this, PedometerResponse);
244            } else if (args[0] == 278) {
245                callback.call(this, HeartRateResponse);
246            } else if (args[0] == 280) {
247                callback.call(this, WearDetectionResponse);
248            } else if (args[0] == 281) {
249                callback.call(this, AccelerometerUncalibratedResponse);
250            }
251        },
252        once: function (...args) {
253            console.warn('sensor.once interface mocked in the Previewer. How this interface works on the'
254                + 'Previewer may be different from that on a real device.')
255            const len = args.length
256            if (args[0] == 1) {
257                args[len - 1].call(this, AccelerometerResponse);
258            } else if (args[0] == 2) {
259                args[len - 1].call(this, GyroscopeResponse);
260            } else if (args[0] == 5) {
261                args[len - 1].call(this, LightResponse);
262            } else if (args[0] == 6) {
263                args[len - 1].call(this, MagneticFieldResponse);
264            } else if (args[0] == 8) {
265                args[len - 1].call(this, BarometerResponse);
266            } else if (args[0] == 10) {
267                args[len - 1].call(this, HallResponse);
268            } else if (args[0] == 12) {
269                args[len - 1].call(this, ProximityResponse);
270            } else if (args[0] == 13) {
271                args[len - 1].call(this, HumidityResponse);
272            } else if (args[0] == 256) {
273                args[len - 1].call(this, OrientationResponse);
274            } else if (args[0] == 257) {
275                args[len - 1].call(this, GravityResponse);
276            } else if (args[0] == 258) {
277                args[len - 1].call(this, LinearAccelerometerResponse);
278            } else if (args[0] == 259) {
279                args[len - 1].call(this, RotationVectorResponse);
280            } else if (args[0] == 260) {
281                args[len - 1].call(this, AmbientTemperatureResponse);
282            } else if (args[0] == 261) {
283                args[len - 1].call(this, MagneticFieldUncalibratedResponse);
284            } else if (args[0] == 263) {
285                args[len - 1].call(this, GyroscopeUncalibratedResponse);
286            } else if (args[0] == 264) {
287                args[len - 1].call(this, SignificantMotionResponse);
288            } else if (args[0] == 265) {
289                args[len - 1].call(this, PedometerDetectResponse);
290            } else if (args[0] == 266) {
291                args[len - 1].call(this, PedometerResponse);
292            } else if (args[0] == 278) {
293                args[len - 1].call(this, HeartRateResponse);
294            } else if (args[0] == 280) {
295                args[len - 1].call(this, WearDetectionResponse);
296            } else if (args[0] == 281) {
297                args[len - 1].call(this, AccelerometerUncalibratedResponse);
298            }
299        },
300        off: function (...args) {
301            console.warn('sensor.off interface mocked in the Previewer. How this interface works on the'
302                + 'Previewer may be different from that on a real device.')
303        },
304        getGeomagneticField: function (...args) {
305            console.warn('sensor.getGeomagneticField interface mocked in the Previewer. How this interface works on the'
306                + 'Previewer may be different from that on a real device.')
307            const len = args.length
308            if (typeof args[len - 1] === 'function') {
309                args[len - 1].call(this, paramMock.businessErrorMock, GeomagneticResponse);
310            } else {
311                return new Promise((resolve, reject) => {
312                    resolve(GeomagneticResponse);
313                });
314            }
315        },
316        getAltitude: function (...args) {
317            console.warn('sensor.getAltitude interface mocked in the Previewer. How this interface works on the'
318                + 'Previewer may be different from that on a real device.')
319            const len = args.length
320            if (typeof args[len - 1] === 'function') {
321                args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock);
322            } else {
323                return new Promise((resolve, reject) => {
324                    resolve(paramMock.paramNumberMock);
325                });
326            }
327        },
328        getGeomagneticDip: function (...args) {
329            console.warn('sensor.getGeomagneticDip interface mocked in the Previewer. How this interface works on the'
330                + 'Previewer may be different from that on a real device.')
331            const len = args.length
332            if (typeof args[len - 1] === 'function') {
333                args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramNumberMock);
334            } else {
335                return new Promise((resolve, reject) => {
336                    resolve(paramMock.paramNumberMock);
337                });
338            }
339        },
340        getAngleModify: function (...args) {
341            console.warn('sensor.getAngleModifiy interface mocked in the Previewer. How this interface works on the'
342                + 'Previewer may be different from that on a real device.')
343            const len = args.length
344            if (typeof args[len - 1] === 'function') {
345                args[len - 1].call(this, paramMock.businessErrorMock, [paramMock.paraNumberMock]);
346            } else {
347                return new Promise((resolve, reject) => {
348                    resolve([paramMock.paraNumberMock]);
349                });
350            }
351        },
352        createRotationMatrix: function (...args) {
353            console.warn('sensor.createRotationMatrix interface mocked in the Previewer. How this interface works on the'
354                + 'Previewer may be different from that on a real device.')
355            const len = args.length
356            if (len == 1) {
357                return new Promise((resolve, reject) => {
358                    resolve(paramMock.paramArrayMock);
359                });
360            } else if (len == 2) {
361                if (typeof args[len - 1] == 'function') {
362                    args[len - 1].call(this, paramMock.businessErrorMock, paramMock.paramArrayMock);
363                } else {
364                    return new Promise((resolve, reject) => {
365                        resolve(RotationMatrixResponse);
366                    });
367                }
368            } else if (len == 3) {
369                args[len - 1].call(this, paramMock.businessErrorMock, paramMock.RotationMatrixResponse);
370            }
371
372        },
373        transformCoordinateSystem: function (...args) {
374            console.warn('sensor.transformCoordinateSystem interface mocked in the Previewer. How this interface works on the'
375                + 'Previewer may be different from that on a real device.')
376            const len = args.length
377            if (typeof args[len - 1] === 'function') {
378                args[len - 1].call(this, paramMock.businessErrorMock, [paramMock.paraNumberMock]);
379            } else {
380                return new Promise((resolve, reject) => {
381                    resolve([paramMock.paraNumberMock]);
382                });
383            }
384        },
385        createQuaternion: function (...args) {
386            console.warn('sensor.createQuaternion interface mocked in the Previewer. How this interface works on the'
387                + 'Previewer may be different from that on a real device.')
388            const len = args.length
389            if (typeof args[len - 1] === 'function') {
390                args[len - 1].call(this, paramMock.businessErrorMock, [paramMock.paraNumberMock]);
391          } else {
392                return new Promise((resolve, reject) => {
393                    resolve([paramMock.paraNumberMock]);
394                });
395            }
396        },
397        getDirection: function (...args) {
398            console.warn('sensor.getDirection interface mocked in the Previewer. How this interface works on the'
399                + 'Previewer may be different from that on a real device.')
400            const len = args.length
401            if (typeof args[len - 1] === 'function') {
402                args[len - 1].call(this, paramMock.businessErrorMock, [paramMock.paraNumberMock]);
403            } else {
404                return new Promise((resolve, reject) => {
405                    resolve([paramMock.paraNumberMock]);
406                });
407            }
408        },
409        getSingleSensor: function (...args) {
410            console.warn('sensor.getSingleSensor interface mocked in the Previewer. How this interface works on the'
411                + 'Previewer may be different from that on a real device.')
412            const len = args.length
413            if (typeof args[len - 1] === 'function') {
414                args[len - 1].call(this, paramMock.businessErrorMock, Sensor);
415            } else {
416                return new Promise((resolve, reject) => {
417                    resolve(Sensor);
418                });
419            }
420        },
421        getSensorLists: function (...args) {
422            console.warn('sensor.getSensorLists interface mocked in the Previewer. How this interface works on the'
423                + 'Previewer may be different from that on a real device.')
424            const len = args.length
425            if (typeof args[len - 1] === 'function') {
426                args[len - 1].call(this, paramMock.businessErrorMock,new Array(Sensor));
427            } else {
428                return new Promise((resolve, reject) => {
429                    resolve(Array(Sensor));
430                });
431            }
432        }
433    };
434    return sensor
435}