summaryrefslogtreecommitdiff
path: root/libs/ihealth-sdk/android/src/main/java/com/ihealth/ihealthlibrary/BTMModule.java
blob: 32e71068b07a9261c2828f6d452aabd3a8445628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.ihealth.ihealthlibrary;

import android.text.TextUtils;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.ihealth.communication.control.BtmControl;
import com.ihealth.communication.manager.iHealthDevicesManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lixuesong on 2016/11/21.
 */
@ReactModule(name = "BTMModule")
public class BTMModule extends iHealthBaseModule {
    private static final String modelName = "BTMModule";
    private static final String TAG = "BTMModule";

    private static final String EVENT_NOTIFY = "event_notify_btm";

    public BTMModule(ReactApplicationContext reactContext) {
        super(TAG, reactContext);
    }

    @Override
    public String getName() {
        return modelName;
    }

    @Override
    public Map<String, Object> getConstants() {
        Map<String, Object> map = new HashMap<>();
        map.put("Event_Notify", EVENT_NOTIFY);
        return map;
    }

    private static BtmControl getControl(String mac) {
        return iHealthDevicesManager.getInstance().getBtmControl(mac);
    }

    @ReactMethod
    public void getBattery(String mac) {
        BtmControl btmControl = getControl(mac);
        if (btmControl != null) {
            btmControl.getBattery();
        } else {
            Log.e(TAG, "Can not find BTM Control mac:" + mac);
        }
    }

    @ReactMethod
    public void setStandbyTime(String mac, int hour, int minute, int second) {
        BtmControl btmControl = getControl(mac);
        if (btmControl != null) {
            btmControl.setStandbyTime(hour, minute, second);
        } else {
            Log.e(TAG, "Can not find BTM Control mac:" + mac);
        }
    }

    @ReactMethod
    public void setTemperatureUnit(String mac, int unit) {
        BtmControl btmControl = getControl(mac);
        if (btmControl != null) {
            btmControl.setTemperatureUnit((byte) unit);
        } else {
            Log.e(TAG, "Can not find BTM Control mac:" + mac);
        }
    }

    @ReactMethod
    public void setMeasuringTarget(String mac, int target) {
        BtmControl btmControl = getControl(mac);
        if (btmControl != null) {
            btmControl.setMeasuringTarget((byte) target);
        } else {
            Log.e(TAG, "Can not find BTM Control mac:" + mac);
        }
    }

    @ReactMethod
    public void setOfflineTarget(String mac, int target) {
        BtmControl btmControl = getControl(mac);
        if (btmControl != null) {
            btmControl.setOfflineTarget((byte) target);
        } else {
            Log.e(TAG, "Can not find BTM Control mac:" + mac);
        }
    }

    @ReactMethod
    public void getMemoryData(String mac) {
        BtmControl btmControl = getControl(mac);
        if (btmControl != null) {
            btmControl.getMemoryData();
        } else {
            Log.e(TAG, "Can not find BTM Control mac:" + mac);
        }
    }

    @ReactMethod
    public void disconnect(String mac) {
        BtmControl btmControl = getControl(mac);
        if (btmControl != null) {
            btmControl.disconnect();
        } else {
            Log.e(TAG, "Can not find BTM Control mac:" + mac);
        }
    }

    @Override
    public void handleNotify(String mac, String deviceType, String action, String message) {
        WritableMap params = Arguments.createMap();
        params.putString("action", action);
        params.putString("mac", mac);
        params.putString("type", deviceType);
        if (!TextUtils.isEmpty(message)) {
            Utils.jsonToMap(message, params);
        }
        sendEvent(EVENT_NOTIFY, params);
    }

    @ReactMethod
    public void getAllConnectedDevices() {
        List<String> devices = iHealthDevicesManager.getInstance().getBTMDevices();
        WritableMap params = Arguments.createMap();
        if (devices.size() > 0) {
            WritableArray array = Arguments.createArray();
            for (String device : devices) {
                array.pushString(device);
            }
            params.putArray("devices", array);
            params.putString("action", ACTION_GET_ALL_CONNECTED_DEVICES);
        }
        sendEvent(EVENT_NOTIFY, params);
    }
}