1# Stationary Development
2
3
4## When to Use
5
6An application can call the **Stationary** module to obtain the device status, for example, whether the device is absolutely or relatively still.
7
8For details about the APIs, see [Stationary](../../reference/apis-multimodalawareness-kit/js-apis-stationary.md).
9
10## Device Status Type Parameters
11
12| Name| Description|
13| -------- | -------- |
14| still | Absolutely still.|
15| relativeStill | Relatively still.|
16
17## Parameters for Subscribing to Device Status events
18
19| Name                            | Value   | Description                                      |
20| ------------------------------ | ---- | ---------------------------------------- |
21| ENTER         | 1    | Event indicating entering device status.  |
22| EXIT | 2   | Event indicating exiting device status.|
23| ENTER_EXIT | 3   | Event indicating entering and exiting device status.|
24
25## Returned Device Status Parameters
26
27| Name                            | Value   | Description                                      |
28| ------------------------------ | ---- | ---------------------------------------- |
29| ENTER         | 1    | Entering device status.  |
30| EXIT | 2   | Exiting device status.|
31
32## Available APIs
33
34| Name                                                      | Description                                                        |
35| ------------------------------------------------------------ | ------------------------------------------------------------ |
36| on(activity: ActivityType, event: ActivityEvent, reportLatencyNs: number, callback: Callback<ActivityResponse>): void | Subscribes to the device status. This API uses an asynchronous callback to return the result.|
37| once(activity: ActivityType, callback: Callback<ActivityResponse>): void | Obtains the device status. This API uses an asynchronous callback to return the result.|
38| off(activity: ActivityType, event: ActivityEvent, callback?: Callback<ActivityResponse>): void | Unsubscribes from the device status.                                |
39
40## Constraints
41
42The device must support the acceleration sensor.
43Currently, only the algorithm framework is provided. The API test framework returns the following result: data={"type":3,"value":-1}.
44If the relative stationary and absolute stationary capabilities are required, you must implement them in **device_status/libs/src/algorithm**. The following code snippet is for reference:
45
46   ```ts
47   algoPara_.resultantAcc =
48      sqrt((algoPara_.x * algoPara_.x) + (algoPara_.y * algoPara_.y) + (algoPara_.z * algoPara_.z));
49   if ((algoPara_.resultantAcc > RESULTANT_ACC_LOW_THRHD) && (algoPara_.resultantAcc < RESULTANT_ACC_UP_THRHD)) {
50      if (state_ == STILL) {
51         return;
52      }
53      counter_--;
54      if (counter_ == 0) {
55         counter_ = COUNTER_THRESHOLD;
56         UpdateStateAndReport(VALUE_ENTER, STILL, TYPE_ABSOLUTE_STILL);
57      }
58   } else {
59      counter_ = COUNTER_THRESHOLD;
60      if (state_ == UNSTILL) {
61         return;
62      }
63      UpdateStateAndReport(VALUE_EXIT, UNSTILL, TYPE_ABSOLUTE_STILL);
64   }
65   ```
66
67## How to Develop
68
691. Subscribe to the event indicating entering the absolute still state, and the event is reported every 1 second.
70
71   ```ts
72   import { stationary } from '@kit.MultimodalAwarenessKit';
73   import { BusinessError } from '@kit.BasicServicesKit';
74   let reportLatencyNs = 1000000000;
75   try {
76      stationary.on('still', stationary.ActivityEvent.ENTER, reportLatencyNs, (data) => {
77         console.log('data='+ JSON.stringify(data));
78      })
79   } catch (error) {
80      let message = (error as BusinessError).message;
81      console.error('stationary on failed:' + message);
82   }
83   ```
84
852. Obtain the event indicating entering the absolute still state.
86
87   ```ts
88   import { stationary } from '@kit.MultimodalAwarenessKit';
89   import { BusinessError } from '@kit.BasicServicesKit';
90   try {
91      stationary.once('still', (data) => {
92         console.log('data='+ JSON.stringify(data));
93      })
94   } catch (error) {
95      let message = (error as BusinessError).message;
96      console.error('stationary once failed:' + message);
97   }
98   ```
99
1003. Unsubscribe from the event indicating entering the absolute still state.
101
102   ```ts
103   import { stationary } from '@kit.MultimodalAwarenessKit';
104   import { BusinessError } from '@kit.BasicServicesKit';
105   try {
106      stationary.off('still', stationary.ActivityEvent.ENTER, (data) => {
107         console.log('data='+ JSON.stringify(data));
108      })
109   } catch (error) {
110      let message = (error as BusinessError).message;
111      console.error('stationary off failed:' + message);
112   }
113   ```
114