-
Notifications
You must be signed in to change notification settings - Fork 0
/
audioExposure.go
65 lines (60 loc) · 2.03 KB
/
audioExposure.go
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
package export
import (
"github.com/opendroid/hk/logger"
"go.uber.org/zap"
"sort"
"strconv"
"time"
)
// Exposure elements for exposure
type Exposure struct {
CreationDate int64 `json:"creation_timestamp_sec"`
EndDate int64 `json:"end_date"`
SourceName string `json:"source"`
Unit string `json:"unit,omitempty"`
Value float32 `json:"value"`
}
// AudioExposure slice of all data
type AudioExposure struct {
Exposure []Exposure `json:"exposure"`
}
// AudioExposure processes and returns Audio exposure data
func (h *HealthData) AudioExposure() *AudioExposure {
if h == nil {
return nil
}
var d AudioExposure
d.Exposure = make([]Exposure, 0)
for _, r := range h.Records {
if RecordType(r.Type) == EnvironmentalAudioExposure || RecordType(r.Type) == HeadphoneAudioExposure {
num, err := strconv.ParseFloat(r.Value, 32)
if err != nil {
logger.Error("appendData: strconv error",
zap.String("method", "AudioExposure"),
zap.String("value", r.Value), zap.String("info", err.Error()))
}
n := float32(num)
e1 := Exposure{recordTime(r.CreationDate), recordTime(r.EndDate),
r.SourceName, r.Unit, n}
d.Exposure = append(d.Exposure, e1)
}
}
// Sort data first
sort.Slice(d.Exposure, func(i, j int) bool { return d.Exposure[i].CreationDate < d.Exposure[j].CreationDate })
return &d
}
// recordTime converts a Record format to time object.
// Safer to convert timestamp to Unix MS, so it can be manipulated by various browsers.
// See waring in https://developers.google.com/chart/interactive/docs/datesandtimes
// eg: https://stackoverflow.com/questions/60308048/why-chart-js-charts-are-not-plotting-data-in-safari-works-in-chrome
func recordTime(ts string) int64 {
// Sample"creation_date": "2019-10-23 19:10:11 -0800" from Health Export
format := "2006-01-02 15:04:05 -0700"
if t, err := time.Parse(format, ts); err != nil {
logger.Error("Bad timestamp", zap.String("method", "recordTime"),
zap.String("value", ts), zap.String("info", err.Error()))
return 0
} else {
return t.Unix()
}
}