wangzhibo
2026-08-30 af6b816e29ebe329c59cfdd368bf41d2a4940946
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.jsbogang.womp.health;
 
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
 
import com.jsbogang.womp.ble.AlarmPrefs;
import com.jsbogang.womp.ble.DevicePrefs;
import com.jsbogang.womp.ble.VeepooBleManager;
import com.veepoo.protocol.VPOperateManager;
import com.veepoo.protocol.listener.base.IBleWriteResponse;
import com.veepoo.protocol.listener.data.AbsBloodGlucoseChangeListener;
import com.veepoo.protocol.listener.data.IBPDetectDataListener;
import com.veepoo.protocol.listener.data.IHeartDataListener;
import com.veepoo.protocol.listener.data.ISpo2hDataListener;
import com.veepoo.protocol.model.datas.BpData;
import com.veepoo.protocol.model.datas.HeartData;
import com.veepoo.protocol.model.datas.Spo2hData;
import com.veepoo.protocol.model.enums.EBPDetectModel;
import com.veepoo.protocol.model.enums.EBPDetectStatus;
import com.veepoo.protocol.model.enums.EBloodGlucoseRiskLevel;
import com.veepoo.protocol.model.enums.EBloodGlucoseStatus;
import com.veepoo.protocol.model.enums.EHeartStatus;
 
/**
 * 每 30 分钟主动触发心率/血压/血氧/血糖测量。手表端用户自行测量时设备会 busy,此时跳过本轮。
 */
public final class ActiveMeasureRunner {
    private static final String TAG = "ActiveMeasure";
    private static final ActiveMeasureRunner INSTANCE = new ActiveMeasureRunner();
    private static final IBleWriteResponse WR = code -> {};
 
    private Context app;
    private final Handler handler = new Handler(Looper.getMainLooper());
    private volatile boolean measuring;
    private int lastHr;
    private int lastBpHigh;
    private int lastBpLow;
    private float lastSpo2;
    private float lastGlucose;
    private Runnable timeoutTask;
 
    private ActiveMeasureRunner() {}
 
    public static ActiveMeasureRunner get() {
        return INSTANCE;
    }
 
    public void attach(Context context) {
        app = context.getApplicationContext();
    }
 
    public boolean isMeasuring() {
        return measuring;
    }
 
    public void startCycle() {
        if (app == null || measuring || !VeepooBleManager.get().isReady()) {
            return;
        }
        if (HealthSyncer.get().isSyncing()) {
            handler.postDelayed(this::startCycle, 15_000);
            return;
        }
        measuring = true;
        lastHr = 0;
        lastBpHigh = 0;
        lastBpLow = 0;
        lastSpo2 = 0;
        lastGlucose = 0;
        startHeart();
    }
 
    private void startHeart() {
        HealthSyncer.get().notifyUi("正在测量心率…");
        try {
            VPOperateManager.getInstance().startDetectHeart(WR, new IHeartDataListener() {
                @Override
                public void onDataChange(HeartData heart) {
                    if (heart == null) {
                        return;
                    }
                    if (heart.getHeartStatus() == EHeartStatus.STATE_HEART_BUSY) {
                        abortBusy();
                        return;
                    }
                    int v = heart.getData();
                    if (v >= 30 && v <= 220) {
                        lastHr = v;
                    }
                }
            });
        } catch (Exception e) {
            Log.w(TAG, "start heart fail", e);
            handler.postDelayed(this::startBp, 800);
            return;
        }
        armTimeout(() -> {
            stopHeart();
            if (lastHr > 0) {
                saveMetric(lastHr, 0, 0, 0, 0);
            }
            startBp();
        }, 25_000);
    }
 
    private void stopHeart() {
        try {
            VPOperateManager.getInstance().stopDetectHeart(WR);
        } catch (Exception ignored) {
        }
    }
 
    private void startBp() {
        if (!stillOn()) {
            return;
        }
        HealthSyncer.get().notifyUi("正在测量血压…");
        try {
            VPOperateManager.getInstance().startDetectBP(WR, new IBPDetectDataListener() {
                @Override
                public void onDataChange(BpData bpData) {
                    if (bpData == null) {
                        return;
                    }
                    if (bpData.getStatus() == EBPDetectStatus.STATE_BP_BUSY) {
                        abortBusy();
                        return;
                    }
                    if (bpData.getHighPressure() > 0 && bpData.getLowPressure() > 0) {
                        lastBpHigh = bpData.getHighPressure();
                        lastBpLow = bpData.getLowPressure();
                    }
                    if (bpData.getProgress() >= 100 && lastBpHigh > 0) {
                        finishBp();
                    }
                }
            }, EBPDetectModel.DETECT_MODEL_PUBLIC);
        } catch (Exception e) {
            Log.w(TAG, "start bp fail", e);
            handler.postDelayed(this::startSpo2, 800);
            return;
        }
        armTimeout(this::finishBp, 55_000);
    }
 
    private void finishBp() {
        clearTimeout();
        try {
            VPOperateManager.getInstance().stopDetectBP(WR, EBPDetectModel.DETECT_MODEL_PUBLIC);
        } catch (Exception ignored) {
        }
        if (lastBpHigh > 0) {
            saveMetric(0, lastBpHigh, lastBpLow, 0, 0);
        }
        handler.postDelayed(this::startSpo2, 1200);
    }
 
    private void startSpo2() {
        if (!stillOn()) {
            return;
        }
        HealthSyncer.get().notifyUi("正在测量血氧…");
        try {
            VPOperateManager.getInstance().startDetectSPO2H(WR, new ISpo2hDataListener() {
                @Override
                public void onSpO2HADataChange(Spo2hData data) {
                    if (data == null) {
                        return;
                    }
                    int v = data.getValue();
                    if (v >= 70 && v <= 100) {
                        lastSpo2 = v;
                    }
                    if (data.getCheckingProgress() >= 100 && lastSpo2 > 0) {
                        finishSpo2();
                    }
                }
            });
        } catch (Exception e) {
            Log.w(TAG, "start spo2 fail", e);
            handler.postDelayed(this::startGlucose, 800);
            return;
        }
        armTimeout(this::finishSpo2, 50_000);
    }
 
    private void finishSpo2() {
        clearTimeout();
        try {
            VPOperateManager.getInstance().stopDetectSPO2H(WR, data -> {});
        } catch (Exception ignored) {
        }
        if (lastSpo2 > 0) {
            saveMetric(0, 0, 0, lastSpo2, 0);
        }
        handler.postDelayed(this::startGlucose, 1200);
    }
 
    private void startGlucose() {
        if (!stillOn()) {
            return;
        }
        HealthSyncer.get().notifyUi("正在测量血糖…");
        try {
            VPOperateManager.getInstance().startBloodGlucoseDetect(WR, new AbsBloodGlucoseChangeListener() {
                @Override
                public void onDetectError(int opt, EBloodGlucoseStatus status) {
                    finishGlucose();
                }
 
                @Override
                public void onBloodGlucoseDetect(int progress, float bloodGlucose, EBloodGlucoseRiskLevel level) {
                    if (bloodGlucose > 0) {
                        lastGlucose = bloodGlucose;
                    }
                    if (progress >= 100) {
                        finishGlucose();
                    }
                }
 
                @Override
                public void onBloodGlucoseStopDetect() {
                    finishGlucose();
                }
            });
        } catch (Exception e) {
            Log.w(TAG, "start glucose fail", e);
            done();
            return;
        }
        armTimeout(this::finishGlucose, 45_000);
    }
 
    private void finishGlucose() {
        clearTimeout();
        try {
            VPOperateManager.getInstance().stopBloodGlucoseDetect(WR, new AbsBloodGlucoseChangeListener() {});
        } catch (Exception ignored) {
        }
        if (lastGlucose > 0) {
            saveMetric(0, 0, 0, 0, lastGlucose);
        }
        done();
    }
 
    private void abortBusy() {
        clearTimeout();
        stopHeart();
        try {
            VPOperateManager.getInstance().stopDetectBP(WR, EBPDetectModel.DETECT_MODEL_PUBLIC);
        } catch (Exception ignored) {
        }
        try {
            VPOperateManager.getInstance().stopDetectSPO2H(WR, data -> {});
        } catch (Exception ignored) {
        }
        measuring = false;
        HealthSyncer.get().syncSleep();
        HealthSyncer.get().notifyUi("手表正忙,本次测量已跳过");
    }
 
    private void done() {
        clearTimeout();
        measuring = false;
        HealthUploader.get().flush();
        HealthSyncer.get().syncSleep();
        HealthSyncer.get().notifyUi("测量完成");
    }
 
    private boolean stillOn() {
        if (!measuring) {
            return false;
        }
        if (!VeepooBleManager.get().isReady()) {
            measuring = false;
            return false;
        }
        return true;
    }
 
    private void armTimeout(Runnable r, long ms) {
        clearTimeout();
        timeoutTask = r;
        handler.postDelayed(timeoutTask, ms);
    }
 
    private void clearTimeout() {
        if (timeoutTask != null) {
            handler.removeCallbacks(timeoutTask);
            timeoutTask = null;
        }
    }
 
    private void saveMetric(int hr, int bpHigh, int bpLow, float spo2, float glucose) {
        if (app == null || AppDatabase.get() == null || !DevicePrefs.isBound(app)) {
            return;
        }
        HealthSample s = new HealthSample();
        s.timestamp = System.currentTimeMillis();
        s.deviceMac = DevicePrefs.getMac(app);
        s.dataType = "周期";
        s.uploaded = 0;
        s.stepValue = 0;
        s.heartRate = hr;
        s.bpHigh = bpHigh;
        s.bpLow = bpLow;
        s.spo2 = spo2;
        s.glucose = glucose;
        if (AlarmPrefs.isAbnormal(app, hr, bpHigh, bpLow, spo2)) {
            s.dataType = "异常";
        }
        AppDatabase.dao().upsertSample(s);
        HealthSyncer.get().notifyUi("测量数据已更新");
    }
}