summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/rempe/Device.java
blob: 92583935ebea3b99440b806527453915ce4ab79f (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package com.javispedro.rempe;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.dsi.ant.plugins.antplus.pcc.AntPlusEnvironmentPcc;
import com.dsi.ant.plugins.antplus.pcc.defines.DeviceState;
import com.dsi.ant.plugins.antplus.pcc.defines.EventFlag;
import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult;
import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc;
import com.dsi.ant.plugins.antplus.pccbase.AntPlusCommonPcc;
import com.dsi.ant.plugins.antplus.pccbase.PccReleaseHandle;

import java.math.BigDecimal;
import java.util.EnumSet;

public class Device {
    private final static String TAG = "Device";

    public static final int INVALID_DEVICE = -1;
    private int mDeviceNumber;

    private PccReleaseHandle<AntPlusEnvironmentPcc> mEnvPccHandle;
    private AntPlusEnvironmentPcc mEnvPcc;

    private String mDeviceName;

    private final Reading mLastReading = new Reading();
    private int mLastRssi;
    private RequestAccessResult mConnectResult;
    private DeviceState mCurState;

    public interface DeviceObserver {
        void onDeviceSearchFinished(RequestAccessResult result);
        void onDeviceInfoChanged();
        void onDeviceStateChanged();
        void onDeviceNewReading();
        void onDeviceRssiChanged();
    }
    private DeviceObserver mObserver;

    /** Creates empty Device object not attached to a specific device number.
     *  Only valid operation is searchForDevice.
     */
    public Device() {
        mDeviceNumber = INVALID_DEVICE;
        mDeviceName = null;
        mConnectResult = RequestAccessResult.SUCCESS;
        mCurState = DeviceState.DEAD;
    }

    /** Creates Device object with deviceNumber. Initial state will be unconnected.
     *  Can use connect() afterwards.  */
    public Device(int deviceNumber, String initialDeviceName) {
        mDeviceNumber = deviceNumber;
        mDeviceName = initialDeviceName;
        mConnectResult = RequestAccessResult.SUCCESS;
        mCurState = DeviceState.DEAD;
    }

    public void searchForDevice(Activity parentActivity) {
        Log.d(TAG, "searchForNewDevice");
        if (isOpen()) {
            Log.e(TAG, "Already open");
            return;
        }

        // Clear old state
        mConnectResult = RequestAccessResult.BAD_PARAMS;
        mCurState = DeviceState.DEAD;
        if (mObserver != null) {
            mObserver.onDeviceStateChanged();
        }

        mEnvPccHandle = AntPlusEnvironmentPcc.requestAccess(parentActivity, parentActivity, mResultReceiver, mDeviceStateChangeReceiver);
    }

    public void connect(Context context) {
        Log.d(TAG, "connect (" + mDeviceNumber + ")");
        if (isOpen()) {
            Log.e(TAG, "Already open");
            return;
        }

        // Clear old connect result / state
        mConnectResult = RequestAccessResult.SUCCESS;
        mCurState = DeviceState.SEARCHING;
        if (mObserver != null) {
            mObserver.onDeviceStateChanged();
        }

        mEnvPccHandle = AntPlusEnvironmentPcc.requestAccess(context, mDeviceNumber, 0, mResultReceiver, mDeviceStateChangeReceiver);
    }

    public void close() {
        Log.d(TAG, "close (" + mDeviceNumber + ")");
        if (mEnvPcc != null) {
            mEnvPcc.subscribeTemperatureDataEvent(null);
            mEnvPcc.subscribeRssiEvent(null);
            mEnvPcc.releaseAccess();
            mEnvPcc = null;
        }
        if (mEnvPccHandle != null) {
            mEnvPccHandle.close();
            mEnvPccHandle = null;
        }
        mConnectResult = RequestAccessResult.SUCCESS;
        mCurState = DeviceState.DEAD;
        if (mObserver != null) {
            mObserver.onDeviceStateChanged();
        }
    }

    public void setObserver(DeviceObserver observer) {
        mObserver = observer;
    }

    public boolean isOpen() {
        return mEnvPccHandle != null;
    }

    @Override
    public String toString() {
        return super.toString() + "(" + mDeviceNumber + ")";
    }

    private void setEnvPcc(AntPlusEnvironmentPcc envPcc) {
        assert mEnvPcc == null;
        mEnvPcc = envPcc;
        final String deviceName = mEnvPcc.getDeviceName();
        final int deviceNumber = mEnvPcc.getAntDeviceNumber();
        Log.d(TAG, "handleConnection deviceName=" + deviceName + " deviceNumber=" + deviceNumber);
        if (mDeviceNumber == INVALID_DEVICE) {
            mDeviceNumber = deviceNumber; // This is the initial search
        } else if (deviceNumber != mDeviceNumber) {
            // We tried to reconnect to a device and now it has a different number?
            Log.e(TAG, "device number mismatch");
        }
        mDeviceName = deviceName;
        mEnvPcc.subscribeTemperatureDataEvent(mTemperatureDataReceiver);
        mEnvPcc.subscribeRssiEvent(mRssiReceiver);
        if (mObserver != null) {
            mObserver.onDeviceInfoChanged();
        }
    }

    public int getDeviceNumber() {
        return mDeviceNumber;
    }

    public String getDeviceName() {
        return mDeviceName;
    }

    public Reading getLastReading() {
        return mLastReading;
    }

    public int getLastRssi() {
        return mLastRssi;
    }

    public DeviceState getCurrentDeviceState() {
        return mCurState;
    }

    public RequestAccessResult getConnectResult() {
        return mConnectResult;
    }

    private final AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc> mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc>() {
        @Override
        public void onResultReceived(AntPlusEnvironmentPcc result, RequestAccessResult resultCode, DeviceState initialDeviceState) {
            Log.d(TAG, "onResultReceived resultCode=" + resultCode + " initialDeviceState=" + initialDeviceState);
            mConnectResult = resultCode;
            mCurState = initialDeviceState;
            if (resultCode == RequestAccessResult.SUCCESS) {
                setEnvPcc(result);
            }
            if (mObserver != null) {
                mObserver.onDeviceSearchFinished(resultCode);
            }
            if (mObserver != null) {
                mObserver.onDeviceStateChanged();
            }
        }
    };

    private final AntPluginPcc.IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new AntPluginPcc.IDeviceStateChangeReceiver() {
        @Override
        public void onDeviceStateChange(DeviceState newDeviceState) {
            Log.d(TAG, "onDeviceStateChange newDeviceState=" + newDeviceState);
            mCurState = newDeviceState;
            if (mObserver != null) {
                mObserver.onDeviceStateChanged();
            }
        }
    };

    private final AntPlusEnvironmentPcc.ITemperatureDataReceiver mTemperatureDataReceiver = new AntPlusEnvironmentPcc.ITemperatureDataReceiver() {
        @Override
        public void onNewTemperatureData(long estTimestamp, EnumSet<EventFlag> eventFlags, BigDecimal currentTemperature, long eventCount, BigDecimal lowLast24Hours, BigDecimal highLast24Hours) {
            Log.d(TAG, "onNewTemperatureData");
            mLastReading.timestamp = estTimestamp;
            mLastReading.temperature = currentTemperature;
            mLastReading.highLast24Hours = highLast24Hours;
            mLastReading.lowLast24Hours = lowLast24Hours;
            if (mObserver != null) {
                mObserver.onDeviceNewReading();
            }
        }
    };

    private final AntPlusCommonPcc.IRssiReceiver mRssiReceiver = new AntPlusCommonPcc.IRssiReceiver() {
        @Override
        public void onRssiData(long estTimestamp, EnumSet<EventFlag> eventFlags, int rssi) {
            Log.d(TAG, "onRssiData rssi=" + rssi);
            mLastRssi = rssi;
            if (mObserver != null) {
                mObserver.onDeviceRssiChanged();
            }
        }
    };

    public static String deviceStateToString(Context context, DeviceState state) {
        switch (state) {
            case DEAD:
                return context.getString(R.string.state_dead);
            case CLOSED:
                return context.getString(R.string.state_closed);
            case SEARCHING:
                return context.getString(R.string.state_searching);
            case TRACKING:
                return context.getString(R.string.state_tracking);
            case PROCESSING_REQUEST:
                return context.getString(R.string.state_processing);
            case UNRECOGNIZED:
                return context.getString(R.string.state_unrecognized);
            default:
                return context.getString(R.string.state_unknown);
        }
    }

    public static String connectionRequestAccessResultToString(Context context, RequestAccessResult result) {
        switch (result) {
            case SUCCESS:
                return context.getString(R.string.connection_result_success);
            case USER_CANCELLED:
                return context.getString(R.string.connection_result_user_cancelled);
            case CHANNEL_NOT_AVAILABLE:
                return context.getString(R.string.connection_result_channel_not_available);
            case OTHER_FAILURE:
                return context.getString(R.string.connection_result_other_failure);
            case DEPENDENCY_NOT_INSTALLED:
                return context.getString(R.string.connection_result_dependency_not_installed);
            case DEVICE_ALREADY_IN_USE:
                return context.getString(R.string.connection_result_device_already_in_use);
            case SEARCH_TIMEOUT:
                return context.getString(R.string.connection_result_search_timeout);
            case ALREADY_SUBSCRIBED:
                return context.getString(R.string.connection_result_already_subscribed);
            case BAD_PARAMS:
                return context.getString(R.string.connection_result_bad_params);
            case ADAPTER_NOT_DETECTED:
                return context.getString(R.string.connection_result_adapter_not_detected);
            case UNRECOGNIZED:
                return context.getString(R.string.connection_result_unrecognized);
            default:
                return context.getString(R.string.connection_result_unknown);
        }
    }
}