-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHBSService.java
359 lines (297 loc) · 15.3 KB
/
HBSService.java
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package uma.hbs;
import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.Vibrator;
import android.text.format.Time;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import uma.hbs.db.TableDataSource;
import uma.hbs.model.Activity;
import uma.hbs.model.HBSensor;
/**
* Created by Administrator on 4/02/2016.
*/
public class HBSService extends Service implements SensorEventListener{
private List<Integer> hbsList = new ArrayList<>();
private List<Integer> hbsListFinal = new ArrayList<>();
SensorManager mSensorManager;
Sensor mHeartRateSensor;
Sensor mStepCountSensor;
Time mTime;
long lastSensorSecond, lastSensorSecond2 = -1;
PowerManager.WakeLock wl;
int lastMinSection = -1;
double zFactor = 2.0;
TableDataSource dataSource;
int iniStepCount = 0, endStepCount = 0, lastStepCount = 0, lastKnownSteps = 0, sensorStep = 0, firstSteps = 0;
long iniActTime = 0, lastActTime = 0, endActTime = 0, sensorTime = 0;
long id2Global = 0, it = 0, StepNumberTeste = 0, sensorTest = 0, myTimeReference = 0, sensorTimeReference = 0;;
long todayStepsInserted = 0, stepsGlobal= 0, finishGlobal = 0, startGlobal = 0,lastMinStepCount=0;
boolean firstReadSensor = false, firstTimeSensorAfterReset = false, canInsertRow =false;
/*public HBSService() {
// Used to name the worker thread, important only for debugging.
super("hbs-service");
}*/
/*@Override
protected void onHandleIntent(Intent intent) {*/
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
dataSource = new TableDataSource(getApplicationContext());
dataSource.open();
mTime = new Time();
/*Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
long[] vibrationPattern = {0, 500, 50, 300};
//-1 - don't repeat
final int indexInPatternToRepeat = -1;
vibrator.vibrate(vibrationPattern, indexInPatternToRepeat);*/
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
wl.acquire();
getSensors();
}
private void getSensors() {
mSensorManager = ((SensorManager)getSystemService(SENSOR_SERVICE));
mStepCountSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mStepCountSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d("sensor", "onAccuracyChanged - accuracy: " + accuracy);
}
public void onSensorChanged(SensorEvent event) {
mTime.setToNow();
long currentSecond = mTime.second;
long currentTime = mTime.toMillis(false);
if(event.sensor.getType() == Sensor.TYPE_STEP_COUNTER){
Log.d("sensor", "Sensor step counter");
}
if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
if(currentSecond != lastSensorSecond){
int hbsVal = (int)event.values[0];
//see what section is atm
int currentMinSection = (int) (mTime.minute/5); //TODO meter 5
if(lastMinSection == -1){
Log.d("section", "1st TIME IN SENSOR, SECTION - " + lastMinSection);
lastMinSection = currentMinSection;
}
else if(lastMinSection == currentMinSection){
Log.d("section", "CURRENT SECTION - " + lastMinSection);
}else{
//calculate stuff with the list untoched
//Log.d("section", "sum1 = " + getSum(hbsList));
double avg = getAverage(hbsList);
//Log.d("section", "average1 = " + avg);
double stdDev = getStandardDeviation(hbsList);
Log.d("section", "deviation1 = " + stdDev);
//create new list
for (int i = 0; i < hbsList.size(); i++) {
if (Math.abs(hbsList.get(i)-avg) < (zFactor * stdDev)){
hbsListFinal.add(hbsList.get(i));
}
}
/*for (int i = 0; i<hbsListFinal.size(); i++){
Log.i("sectionNEWLIST: ", "Value " + i + ": " + hbsListFinal.get(i));
}*/
double stdDev2 = getStandardDeviation(hbsListFinal);
Log.d("section", "deviation2 = " + stdDev2); // TODO change colour based on this value
int lastminute = mTime.minute - 5; // had -1
if(lastminute < 0)
lastminute = 59;
long section = (long)(lastminute/5);
Log.d("section", "time = " + (currentTime - 300000L) + "deviation = " + (float) stdDev2 + "section = " + section);
long sectionEnd = (long)(mTime.minute/5);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(currentTime);
cal.set(Calendar.MINUTE, (((int)sectionEnd))*5);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
sectionEnd = cal.getTimeInMillis();
long sectionStart = sectionEnd - (5*60000);
long totalTimeActive = 0;
totalTimeActive = dataSource.findAllActivitiesSection(sectionStart, sectionEnd, currentTime);//currentTime
Log.d("teste", "TOTALTIMEACTIVE - " + totalTimeActive);
insertHBSensorRow(currentTime - 300000L, (float) stdDev2, section, totalTimeActive);
// clean list and new section start
hbsList.clear();
lastMinSection = currentMinSection;
Log.d("section", "START NEW SECTION - " + lastMinSection);
}
//Log.d("sensor", "hb sensor: " + (int)event.values[0] + "at" + currentSecond);
Log.d("section", "SENSOR VALUE: " + (int)event.values[0] + " AT " + mTime.hour + ":" + mTime.minute + ":" + mTime.second);
if(hbsVal >= 60 && hbsVal<=120){ // drop values too small or big /**/
hbsList.add(hbsVal);
}
Log.d("section", "Array list size is: " + hbsList.size());
lastSensorSecond = currentSecond;
}
}
else if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
myTimeReference = System.currentTimeMillis();
// set event timestamp to current time in milliseconds
event.timestamp = myTimeReference +
Math.round((event.timestamp - sensorTimeReference) / 1000000.0);
sensorTime = event.timestamp;
sensorTime = currentTime;
StepNumberTeste = (int)event.values[0];
//Log.d("tick", "steps: " + StepNumberTeste);
sensorTest++;
int stepsValue = (int) event.values[0];
//Log.d("teste3", "stepsValue: " + stepsValue + " lastKnownSteps:" + lastKnownSteps);
if (firstReadSensor) {// if sensor was initialized already and we have the initial values set
if(firstSteps != stepsValue){
if(lastKnownSteps != stepsValue) {
if (iniActTime == 0 && iniStepCount == 0) {
it = iniActTime = lastActTime = endActTime = sensorTime;
iniStepCount = firstSteps;
lastStepCount = endStepCount = sensorStep = lastKnownSteps = stepsValue;
canInsertRow = true;
//Log.d("teste", "IF - iniT-lastT-endT: " + iniActTime + "/" + lastActTime + "/" + endActTime);
//Log.d("teste", "IF - iniS-lastS-endS: " + iniStepCount + "/" + lastStepCount + "/" + endStepCount);
firstTimeSensorAfterReset = true;
id2Global = insertRowActivity(iniActTime, iniStepCount, endActTime, endStepCount, 1L);
} else {//if there was increment on step count and activity was already initialized
if(sensorTime > (lastActTime + (long) 59000)){
//canInsertRow = false;
//firstTimeSensorAfterReset = true;
//insert activity row on wear db
dataSource.updateActtivity(id2Global, 0L);
//Log.d("teste3", "CALL INSERTROWACTIVITY on Draw" + iniActTime + "/" + endActTime + "/ steps: " + endStepCount + "-" + iniStepCount);
//change set to 0
it = iniActTime = lastActTime = endActTime = sensorTime;
iniStepCount = endStepCount;
lastStepCount = endStepCount = sensorStep = lastKnownSteps = stepsValue;
canInsertRow = true;
//Log.d("teste3", "IF - iniT-lastT-endT: " + iniActTime + "/" + lastActTime + "/" + endActTime);
//Log.d("teste3", "IF - iniS-lastS-endS: " + iniStepCount + "/" + lastStepCount + "/" + endStepCount);
firstTimeSensorAfterReset = true;
id2Global = insertRowActivity(iniActTime, iniStepCount, endActTime, endStepCount, 1L);
}else{
if(currentSecond != lastSensorSecond2){
lastActTime = endActTime = sensorTime;
lastStepCount = endStepCount = lastKnownSteps = stepsValue;
//Log.d("teste", "ELSE - iniT-lastT-endT: " + iniActTime + "/" + lastActTime + "/" + endActTime);
//Log.d("teste", "ELSE - iniS-lastS-endS: " + iniStepCount + "/" + lastStepCount + "/" + endStepCount);
firstTimeSensorAfterReset = true;
dataSource.updateActtivity(id2Global, endStepCount-iniStepCount, iniActTime, endActTime, 1L);
}
}
}
}
}else{//case none activity was started and there was no increment of step count
Log.d("teste", "firststeps equal current sensor steps");
}
} else {//get initial values when the sensor is initialized
firstReadSensor = true;
Log.d("teste", "--------------------------1st Time on Sensor-------------------------------");
firstSteps = lastKnownSteps = (int) event.values[0];
lastMinStepCount = todayStepsInserted = StepNumberTeste;
}
lastSensorSecond2 = currentSecond;
}
else{
Log.d("sensor", "Unknown sensor type");
}
}
@Override
public void onDestroy() {
Log.d("sensor", "destroy");
mSensorManager.unregisterListener(this);
}
@Override
public void onStart(Intent intent, int startid) {
Log.d("sensor", "start");
}
//---------------------------------------------sensors methods close
//----------------------------------------------math funstions needed
public double getAverage(List<Integer> lista) {
double sum = getSum(lista);
double count = lista.size();
double avg = sum / count;
return avg;
}
public double getSum(List<Integer> lista) {
double sum = 0d;
for (double d : lista) {
sum += d;
}
return sum;
}
public double getStandardDeviation(List<Integer> lista) {
double sum = getSum(lista);
double avg = getAverage(lista);
double calc1 = 0;
double calc2 = 0;
double count = lista.size();
double stdDeviation = 0;
for (int i = 0; i < count; i++) {
calc1 = lista.get(i) - avg;
calc1 = Math.pow(calc1, 2);
calc2 = calc2 + calc1;
}
calc2 = calc2 / (count-1);
stdDeviation = Math.sqrt(calc2);
return stdDeviation;
}
private long insertHBSensorRow(long t, float sdnn, long section, long totalTimeActive){
HBSensor hb = new HBSensor();
hb.setTime(t);
hb.setSdnn(sdnn);
hb.setSection(section);
hb.setActiveTime(totalTimeActive);
hb.setSet(0l);
hb = dataSource.createHBSensor(hb);
Log.d("InsertHBSensor2", "HBSensor Row Created with id " + hb.getId() + " Time: " + t + " SDNN: " + sdnn + " SECTION: " + section);
return hb.getId();
}
public long insertRowActivity(long iniT, int iniS, long endT, int endS, long set){
Activity a = new Activity();
long s = endS - iniS;
a.setStep(s);
a.setStart(iniT);
a.setFinish(endT);
a.setSet(set);
a = dataSource.createActivity(a);
Date d1 = new Date(iniT);
Date d2 = new Date(endT);
Log.d("teste", "Activity Row Created with id " + a.getId2() + " InitialTime: " + d1 + " - EndTime: " + d2 + " - Steps: " + s);
return a.getId2();
}
public void listAllRows(String tName){
if(tName.equals("hbsensor")){
List<HBSensor> q = dataSource.findAllHBSensor();
if(q.size() != 0) {
long id;
long time;
float sdnn;
long section;
long totalTimeActive;
for (HBSensor h : q) {
id = h.getId();
time = h.getTime();
sdnn = h.getSdnn();
totalTimeActive = h.getActiveTime();
section = h.getSection();
Log.d("ListHBSensor", "Id: " + id +" Time: " + time + " - SDNN: " + sdnn + " - SDNN: " + totalTimeActive + " - SECTION: " + section);
}
}else
Log.d("ListHBSensor2", "table empty");
}
}
}