-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotalparameters.go
More file actions
59 lines (49 loc) · 1.16 KB
/
totalparameters.go
File metadata and controls
59 lines (49 loc) · 1.16 KB
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
package main
import (
"fmt"
"sync"
"time"
"github.com/webdevops/myuplink-exporter/myuplink"
)
type (
TotalParameterCache struct {
lock sync.RWMutex
deviceParamMap map[string]*TotalParameterMetric
}
TotalParameterMetric struct {
updated time.Time
value float64
}
)
var (
totalParamCache TotalParameterCache
)
func (c *TotalParameterCache) Init() {
c.deviceParamMap = map[string]*TotalParameterMetric{}
}
func (c *TotalParameterCache) getParameterValue(deviceID string, parameterID string, devicePoint myuplink.SystemDevicePoint) float64 {
key := fmt.Sprintf(
"%s:%s",
deviceID,
parameterID,
)
// exists check (eg. new parameter)
if _, exists := c.deviceParamMap[key]; !exists {
c.lock.Lock()
c.deviceParamMap[key] = &TotalParameterMetric{
updated: devicePoint.Timestamp,
value: *devicePoint.Value,
}
c.lock.Unlock()
}
// increase check
if c.deviceParamMap[key].updated != devicePoint.Timestamp {
c.lock.Lock()
c.deviceParamMap[key].updated = devicePoint.Timestamp
c.deviceParamMap[key].value += *devicePoint.Value
c.lock.Unlock()
}
c.lock.RLock()
defer c.lock.RUnlock()
return c.deviceParamMap[key].value
}