• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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  }
24  const LinearAccelerometerResponse = {
25    x: "[PC preview] unknown x",
26    y: "[PC preview] unknown y",
27    z: "[PC preview] unknown z"
28  }
29  const AccelerometerUncalibratedResponse = {
30    x: "[PC preview] unknown x",
31    y: "[PC preview] unknown y",
32    z: "[PC preview] unknown z",
33    biasX: "[PC preview] unknown biasX",
34    biasY: "[PC preview] unknown biasY",
35    biasZ: "[PC preview] unknown biasZ"
36  }
37  const GravityResponse = {
38    x: "[PC preview] unknown x",
39    y: "[PC preview] unknown y",
40    z: "[PC preview] unknown z"
41  }
42  const OrientationResponse = {
43    alpha: "[PC preview] unknown alpha",
44    beta: "[PC preview] unknown beta",
45    gamma: "[PC preview] unknown gamma"
46  }
47  const RotationVectorResponse = {
48    x: "[PC preview] unknown x",
49    y: "[PC preview] unknown y",
50    z: "[PC preview] unknown z"
51  }
52  const GyroscopeResponse = {
53    x: "[PC preview] unknown x",
54    y: "[PC preview] unknown y",
55    z: "[PC preview] unknown z"
56  }
57  const GyroscopeUncalibratedResponse = {
58    x: "[PC preview] unknown x",
59    y: "[PC preview] unknown y",
60    z: "[PC preview] unknown z",
61    biasX: "[PC preview] unknown biasX",
62    biasY: "[PC preview] unknown biasY",
63    biasZ: "[PC preview] unknown biasZ"
64  }
65  const SignificantMotionResponse = {
66    scalar: "[PC preview] unknown scalar"
67  }
68  const ProximityResponse = {
69    distance: "[PC preview] unknown distance"
70  }
71  const LightResponse = {
72    intensity: "[PC preview] unknown intensity"
73  }
74  const HallResponse = {
75    status: "[PC preview] unknown status"
76  }
77  const MagneticFieldResponse = {
78    x: "[PC preview] unknown x",
79    y: "[PC preview] unknown y",
80    z: "[PC preview] unknown z"
81  }
82  const MagneticFieldUncalibratedResponse = {
83    x: "[PC preview] unknown x",
84    y: "[PC preview] unknown y",
85    z: "[PC preview] unknown z",
86    biasX: "[PC preview] unknown biasX",
87    biasY: "[PC preview] unknown biasY",
88    biasZ: "[PC preview] unknown biasZ"
89  }
90  const PedometerResponse = {
91    steps: "[PC preview] unknown steps"
92  }
93  const HumidityResponse = {
94    humidity: "[PC preview] unknown humidity"
95  }
96  const PedometerDetectResponse = {
97    scalar: "[PC preview] unknown scalar"
98  }
99  const AmbientTemperatureResponse = {
100    temperature: "[PC preview] unknown temperature"
101  }
102  const BarometerResponse = {
103    pressure: "[PC preview] unknown pressure"
104  }
105  const HeartRateResponse = {
106    heartRate: "[PC preview] unknown heartRate"
107  }
108  const WearDetectionResponse = {
109    value: "[PC preview] unknown value"
110  }
111  const Options = {
112    interval: "[PC preview] unknown value"
113  }
114  const GeomagneticResponse = {
115    x: "[PC preview] unknown x",
116    y: "[PC preview] unknown y",
117    z: "[PC preview] unknown z",
118    biasX: "[PC preview] unknown biasX",
119    biasY: "[PC preview] unknown biasY",
120    biasZ: "[PC preview] unknown biasZ"
121  }
122  const LocationOptions = {
123    latitude: "[PC preview] unknown latitude",
124    longitude: "[PC preview] unknown longitude",
125    altitude: "[PC preview] unknown altitude",
126  }
127  const CoordinatesOptions = {
128    x: "[PC preview] unknown x",
129    y: "[PC preview] unknown y"
130  }
131  const SensorType = {
132    SENSOR_TYPE_ID_ACCELEROMETER: 1,
133    SENSOR_TYPE_ID_GYROSCOPE: 2,
134    SENSOR_TYPE_ID_AMBIENT_LIGHT: 5,
135    SENSOR_TYPE_ID_MAGNETIC_FIELD: 6,
136    SENSOR_TYPE_ID_BAROMETER: 8,
137    SENSOR_TYPE_ID_HALL: 10,
138    SENSOR_TYPE_ID_PROXIMITY: 12,
139    SENSOR_TYPE_ID_HUMIDITY: 13,
140    SENSOR_TYPE_ID_ORIENTATION: 256,
141    SENSOR_TYPE_ID_GRAVITY: 257,
142    SENSOR_TYPE_ID_LINEAR_ACCELERATION: 258,
143    SENSOR_TYPE_ID_ROTATION_VECTOR: 259,
144    SENSOR_TYPE_ID_AMBIENT_TEMPERATURE: 260,
145    SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED: 261,
146    SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED: 263,
147    SENSOR_TYPE_ID_SIGNIFICANT_MOTION: 264,
148    SENSOR_TYPE_ID_PEDOMETER_DETECTION: 265,
149    SENSOR_TYPE_ID_PEDOMETER: 266,
150    SENSOR_TYPE_ID_HEART_RATE: 278,
151    SENSOR_TYPE_ID_WEAR_DETECTION: 280,
152    SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED: 281
153  }
154  const sensor = {
155    on: function (...args) {
156      console.warn("sensor.on interface mocked in the Previewer. How this interface works on the" +
157        " Previewer may be different from that on a real device.")
158      const len = args.length;
159      const callback = typeof args[len - 1] == 'function' ? args[len - 1] : args[len - 2];
160      if (args[0] == 1) {
161        callback.call(this, paramMock.businessErrorMock, AccelerometerResponse);
162      } else if (args[0] == 2) {
163        callback.call(this, paramMock.businessErrorMock, GyroscopeResponse);
164      } else if (args[0] == 5) {
165        callback.call(this, paramMock.businessErrorMock, LightResponse);
166      } else if (args[0] == 6) {
167        callback.call(this, paramMock.businessErrorMock, MagneticFieldResponse);
168      } else if (args[0] == 8) {
169        callback.call(this, paramMock.businessErrorMock, BarometerResponse);
170      } else if (args[0] == 10) {
171        callback.call(this, paramMock.businessErrorMock, HallResponse);
172      } else if (args[0] == 12) {
173        callback.call(this, paramMock.businessErrorMock, ProximityResponse);
174      } else if (args[0] == 13) {
175        callback.call(this, paramMock.businessErrorMock, HumidityResponse);
176      } else if (args[0] == 256) {
177        callback.call(this, paramMock.businessErrorMock, OrientationResponse);
178      } else if (args[0] == 257) {
179        callback.call(this, paramMock.businessErrorMock, GravityResponse);
180      } else if (args[0] == 258) {
181        callback.call(this, paramMock.businessErrorMock, LinearAccelerometerResponse);
182      } else if (args[0] == 259) {
183        callback.call(this, paramMock.businessErrorMock, RotationVectorResponse);
184      } else if (args[0] == 260) {
185        callback.call(this, paramMock.businessErrorMock, AmbientTemperatureResponse);
186      } else if (args[0] == 261) {
187        callback.call(this, paramMock.businessErrorMock, MagneticFieldUncalibratedResponse);
188      } else if (args[0] == 263) {
189        callback.call(this, paramMock.businessErrorMock, GyroscopeUncalibratedResponse);
190      } else if (args[0] == 264) {
191        callback.call(this, paramMock.businessErrorMock, SignificantMotionResponse);
192      } else if (args[0] == 265) {
193        callback.call(this, paramMock.businessErrorMock, PedometerDetectResponse);
194      } else if (args[0] == 266) {
195        callback.call(this, paramMock.businessErrorMock, PedometerResponse);
196      } else if (args[0] == 278) {
197        callback.call(this, paramMock.businessErrorMock, HeartRateResponse);
198      } else if (args[0] == 280) {
199        callback.call(this, paramMock.businessErrorMock, WearDetectionResponse);
200      } else if (args[0] == 281) {
201        callback.call(this, paramMock.businessErrorMock, AccelerometerUncalibratedResponse);
202      }
203    },
204    once: function (...args) {
205      console.warn("sensor.once interface mocked in the Previewer. How this interface works on the" +
206        " Previewer may be different from that on a real device.")
207      const len = args.length
208      if (args[0] == 1) {
209        args[len - 1].call(this, paramMock.businessErrorMock, AccelerometerResponse);
210      } else if (args[0] == 2) {
211        args[len - 1].call(this, paramMock.businessErrorMock, GyroscopeResponse);
212      } else if (args[0] == 5) {
213        args[len - 1].call(this, paramMock.businessErrorMock, LightResponse);
214      } else if (args[0] == 6) {
215        args[len - 1].call(this, paramMock.businessErrorMock, MagneticFieldResponse);
216      } else if (args[0] == 8) {
217        args[len - 1].call(this, paramMock.businessErrorMock, BarometerResponse);
218      } else if (args[0] == 10) {
219        args[len - 1].call(this, paramMock.businessErrorMock, HallResponse);
220      } else if (args[0] == 12) {
221        args[len - 1].call(this, paramMock.businessErrorMock, ProximityResponse);
222      } else if (args[0] == 13) {
223        args[len - 1].call(this, paramMock.businessErrorMock, HumidityResponse);
224      } else if (args[0] == 256) {
225        args[len - 1].call(this, paramMock.businessErrorMock, OrientationResponse);
226      } else if (args[0] == 257) {
227        args[len - 1].call(this, paramMock.businessErrorMock, GravityResponse);
228      } else if (args[0] == 258) {
229        args[len - 1].call(this, paramMock.businessErrorMock, LinearAccelerometerResponse);
230      } else if (args[0] == 259) {
231        args[len - 1].call(this, paramMock.businessErrorMock, RotationVectorResponse);
232      } else if (args[0] == 260) {
233        args[len - 1].call(this, paramMock.businessErrorMock, AmbientTemperatureResponse);
234      } else if (args[0] == 261) {
235        args[len - 1].call(this, paramMock.businessErrorMock, MagneticFieldUncalibratedResponse);
236      } else if (args[0] == 263) {
237        args[len - 1].call(this, paramMock.businessErrorMock, GyroscopeUncalibratedResponse);
238      } else if (args[0] == 264) {
239        args[len - 1].call(this, paramMock.businessErrorMock, SignificantMotionResponse);
240      } else if (args[0] == 265) {
241        args[len - 1].call(this, paramMock.businessErrorMock, PedometerDetectResponse);
242      } else if (args[0] == 266) {
243        args[len - 1].call(this, paramMock.businessErrorMock, PedometerResponse);
244      } else if (args[0] == 278) {
245        args[len - 1].call(this, paramMock.businessErrorMock, HeartRateResponse);
246      } else if (args[0] == 280) {
247        args[len - 1].call(this, paramMock.businessErrorMock, WearDetectionResponse);
248      } else if (args[0] == 281) {
249        args[len - 1].call(this, paramMock.businessErrorMock, AccelerometerUncalibratedResponse);
250      }
251    },
252    off: function (...args) {
253      console.warn("sensor.off 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      args[len - 1].call(this, paramMock.businessErrorMock);
257    },
258    getGeomagneticField: function (...args) {
259      console.warn("sensor.getGeomagneticField interface mocked in the Previewer. How this interface works on the" +
260        " Previewer may be different from that on a real device.")
261      const len = args.length
262      if (typeof args[len - 1] === 'function') {
263        args[len - 1].call(this, paramMock.businessErrorMoc, GeomagneticResponse);
264      } else {
265        return new Promise((resolve, reject) => {
266          resolve(GeomagneticResponse);
267        })
268      }
269    },
270    getAltitude: function (...args) {
271      console.warn("sensor.getAltitude interface mocked in the Previewer. How this interface works on the" +
272        " Previewer may be different from that on a real device.")
273      const len = args.length
274      if (typeof args[len - 1] === 'function') {
275        args[len - 1].call(this, paramMock.businessErrorMoc, paramMock.paramNumberMock);
276      } else {
277        return new Promise((resolve, reject) => {
278          resolve(paramMock.paramNumberMock);
279        })
280      }
281    },
282    getGeomagneticDip: function (...args) {
283      console.warn("sensor.getGeomagneticDip interface mocked in the Previewer. How this interface works on the" +
284        " Previewer may be different from that on a real device.")
285      const len = args.length
286      if (typeof args[len - 1] === 'function') {
287        args[len - 1].call(this, paramMock.businessErrorMoc, paramMock.paramNumberMock);
288      } else {
289        return new Promise((resolve, reject) => {
290          resolve(paramMock.paramNumberMock);
291        })
292      }
293    },
294    getAngleModifiy: function (...args) {
295      console.warn("sensor.getAngleModifiy interface mocked in the Previewer. How this interface works on the" +
296        " Previewer may be different from that on a real device.")
297      const len = args.length
298      if (typeof args[len - 1] === 'function') {
299        args[len - 1].call(this, paramMock.businessErrorMoc, paramMock.paramArrayMock);
300      } else {
301        return new Promise((resolve, reject) => {
302          resolve(paramMock.paramArrayMock);
303        })
304      }
305    },
306    createRotationMatrix: function (...args) {
307      console.warn("sensor.createRotationMatrix interface mocked in the Previewer. How this interface works on the" +
308        " Previewer may be different from that on a real device.")
309      const len = args.length
310      if (typeof args[len - 1] === 'function') {
311        args[len - 1].call(this, paramMock.businessErrorMoc, paramMock.paramArrayMock);
312      } else {
313        return new Promise((resolve, reject) => {
314          resolve(paramMock.paramArrayMock);
315        })
316      }
317    },
318    transformCoordinateSystem: function (...args) {
319      console.warn("sensor.transformCoordinateSystem interface mocked in the Previewer. How this interface works on the" +
320        " Previewer may be different from that on a real device.")
321      const len = args.length
322      if (typeof args[len - 1] === 'function') {
323        args[len - 1].call(this, paramMock.businessErrorMoc, paramMock.paramArrayMock);
324      } else {
325        return new Promise((resolve, reject) => {
326          resolve(paramMock.paramArrayMock);
327        })
328      }
329    },
330    createQuaternion: function (...args) {
331      console.warn("sensor.createQuaternion interface mocked in the Previewer. How this interface works on the" +
332        " Previewer may be different from that on a real device.")
333      const len = args.length
334      if (typeof args[len - 1] === 'function') {
335        args[len - 1].call(this, paramMock.businessErrorMoc, paramMock.paramArrayMock);
336      } else {
337        return new Promise((resolve, reject) => {
338          resolve(paramMock.paramArrayMock);
339        })
340      }
341    },
342    getDirection: function (...args) {
343      console.warn("sensor.getDirection interface mocked in the Previewer. How this interface works on the" +
344        " Previewer may be different from that on a real device.")
345      const len = args.length
346      if (typeof args[len - 1] === 'function') {
347        args[len - 1].call(this, paramMock.businessErrorMoc, paramMock.paramArrayMock);
348      } else {
349        return new Promise((resolve, reject) => {
350          resolve(paramMock.paramArrayMock);
351        })
352      }
353    }
354  }
355  return sensor
356}
357