wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
/*  Copyright (C) 2019-2024 José Rebelo, Petr Vaněk, Q-er
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.charts;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
 
public class SleepAnalysis {
 
    public static final long MIN_SESSION_LENGTH = 5 * 60;
    public static final long MAX_WAKE_PHASE_LENGTH = 2 * 60 * 60;
 
    public List<SleepSession> calculateSleepSessions(List<? extends ActivitySample> samples) {
        List<SleepSession> result = new ArrayList<>();
 
        ActivitySample previousSample = null;
        Date sleepStart = null;
        Date sleepEnd = null;
        long lightSleepDuration = 0;
        long deepSleepDuration = 0;
        long remSleepDuration = 0;
        long awakeSleepDuration = 0;
        long durationSinceLastSleep = 0;
 
        for (ActivitySample sample : samples) {
            if (isSleep(sample)) {
                if (sleepStart == null)
                    sleepStart = getDateFromSample(sample);
                sleepEnd = getDateFromSample(sample);
 
                durationSinceLastSleep = 0;
            } else {
                //exclude "not worn" times from sleep sessions as this makes a discrepancy with the charts
                if (lightSleepDuration + deepSleepDuration + remSleepDuration + awakeSleepDuration > MIN_SESSION_LENGTH)
                    result.add(new SleepSession(sleepStart, sleepEnd, lightSleepDuration, deepSleepDuration, remSleepDuration, awakeSleepDuration));
                sleepStart = null;
                sleepEnd = null;
                lightSleepDuration = 0;
                deepSleepDuration = 0;
                remSleepDuration = 0;
                awakeSleepDuration = 0;
            }
 
            if (previousSample != null) {
                long durationSinceLastSample = sample.getTimestamp() - previousSample.getTimestamp();
                if (sample.getKind() == ActivityKind.LIGHT_SLEEP) {
                    lightSleepDuration += durationSinceLastSample;
                } else if (sample.getKind() == ActivityKind.DEEP_SLEEP) {
                    deepSleepDuration += durationSinceLastSample;
                } else if (sample.getKind() == ActivityKind.REM_SLEEP) {
                    remSleepDuration += durationSinceLastSample;
                } else if (sample.getKind() == ActivityKind.AWAKE_SLEEP) {
                    awakeSleepDuration += durationSinceLastSample;
                } else {
                    durationSinceLastSleep += durationSinceLastSample;
                    if (sleepStart != null && durationSinceLastSleep > MAX_WAKE_PHASE_LENGTH) {
                        if (lightSleepDuration + deepSleepDuration + remSleepDuration + awakeSleepDuration > MIN_SESSION_LENGTH)
                            result.add(new SleepSession(sleepStart, sleepEnd, lightSleepDuration, deepSleepDuration, remSleepDuration, awakeSleepDuration));
                        sleepStart = null;
                        sleepEnd = null;
                        lightSleepDuration = 0;
                        deepSleepDuration = 0;
                        remSleepDuration = 0;
                        awakeSleepDuration = 0;
                    }
                }
            }
 
            previousSample = sample;
        }
        if (lightSleepDuration + deepSleepDuration + remSleepDuration + awakeSleepDuration > MIN_SESSION_LENGTH) {
            result.add(new SleepSession(sleepStart, sleepEnd, lightSleepDuration, deepSleepDuration, remSleepDuration, awakeSleepDuration));
        }
        return result;
    }
 
    private boolean isSleep(ActivitySample sample) {
        return sample.getKind() == ActivityKind.DEEP_SLEEP ||
                sample.getKind() == ActivityKind.LIGHT_SLEEP ||
                sample.getKind() == ActivityKind.REM_SLEEP ||
                sample.getKind() == ActivityKind.AWAKE_SLEEP;
    }
 
    private Date getDateFromSample(ActivitySample sample) {
        return new Date(sample.getTimestamp() * 1000L);
    }
 
 
    public static class SleepSession {
        private final Date sleepStart;
        private final Date sleepEnd;
        private final long lightSleepDuration;
        private final long deepSleepDuration;
        private final long remSleepDuration;
        private final long awakeSleepDuration;
 
        private SleepSession(Date sleepStart,
                             Date sleepEnd,
                             long lightSleepDuration,
                             long deepSleepDuration,
                             long remSleepDuration,
                             long awakeSleepDuration) {
            this.sleepStart = sleepStart;
            this.sleepEnd = sleepEnd;
            this.lightSleepDuration = lightSleepDuration;
            this.deepSleepDuration = deepSleepDuration;
            this.remSleepDuration = remSleepDuration;
            this.awakeSleepDuration = awakeSleepDuration;
        }
 
        public Date getSleepStart() {
            return sleepStart;
        }
 
        public Date getSleepEnd() {
            return sleepEnd;
        }
 
        public long getLightSleepDuration() {
            return lightSleepDuration;
        }
 
        public long getDeepSleepDuration() {
            return deepSleepDuration;
        }
 
        public long getRemSleepDuration() {
            return remSleepDuration;
        }
 
        public long getAwakeSleepDuration() {
            return awakeSleepDuration;
        }
    }
}