-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathhandlelocation.go
More file actions
192 lines (173 loc) · 6.41 KB
/
handlelocation.go
File metadata and controls
192 lines (173 loc) · 6.41 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
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
// Copyright (c) 2017-2022 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package zedagent
import (
"bytes"
"time"
"github.com/lf-edge/eve-api/go/info"
"github.com/lf-edge/eve/pkg/pillar/flextimer"
"github.com/lf-edge/eve/pkg/pillar/types"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
// Run a periodic post of the location information.
func locationTimerTask(ctx *zedagentContext, handleChannel chan interface{},
triggerLocationInfo chan destinationBitset) {
var cloudIteration int
// Ticker for periodic publishing to the controller.
cloudInterval := ctx.globalConfig.GlobalValueInt(types.LocationCloudInterval)
interval := time.Duration(cloudInterval) * time.Second
max := float64(interval)
min := max * 0.3
cloudTicker := flextimer.NewRangeTicker(time.Duration(min), time.Duration(max))
// Ticker for periodic publishing to the Local profile server.
appInterval := ctx.globalConfig.GlobalValueInt(types.LocationAppInterval)
interval = time.Duration(appInterval) * time.Second
max = float64(interval)
min = max * 0.3
appTicker := flextimer.NewRangeTicker(time.Duration(min), time.Duration(max))
// Return handles to the caller.
handleChannel <- cloudTicker
handleChannel <- appTicker
wdName := agentName + "-location"
// Run a periodic timer so we always update StillRunning
stillRunning := time.NewTicker(25 * time.Second)
ctx.ps.StillRunning(wdName, warningTime, errorTime)
ctx.ps.RegisterFileWatchdog(wdName)
for {
select {
case <-cloudTicker.C:
publishLocation(ctx, &cloudIteration, wdName, ControllerDest)
case dest := <-triggerLocationInfo:
publishLocation(ctx, &cloudIteration, wdName, dest)
case <-appTicker.C:
publishLocation(ctx, &cloudIteration, wdName, LPSDest)
case <-stillRunning.C:
}
ctx.ps.StillRunning(wdName, warningTime, errorTime)
}
}
// Called when globalConfig changes.
// Assumes that the caller has verifier that the interval has changed.
func updateLocationCloudTimer(ctx *getconfigContext, cloudInterval uint32) {
if ctx.locationCloudTickerHandle == nil {
log.Warnf("updateLocationCloudTimer: locationCloudTickerHandle is still nil")
return
}
interval := time.Duration(cloudInterval) * time.Second
log.Functionf("updateLocationCloudTimer: cloudInterval change to %v", interval)
max := float64(interval)
min := max * 0.3
flextimer.UpdateRangeTicker(ctx.locationCloudTickerHandle,
time.Duration(min), time.Duration(max))
// Force an immediate timeout since timer could have decreased.
flextimer.TickNow(ctx.locationCloudTickerHandle)
}
// Called when globalConfig changes.
// Assumes that the caller has verifier that the interval has changed.
func updateLocationAppTimer(ctx *getconfigContext, appInterval uint32) {
if ctx.locationAppTickerHandle == nil {
log.Warnf("updateLocationAppTimer: locationAppTickerHandle is still nil")
return
}
interval := time.Duration(appInterval) * time.Second
log.Functionf("updateLocationAppTimer: appInterval change to %v", interval)
max := float64(interval)
min := max * 0.3
flextimer.UpdateRangeTicker(ctx.locationAppTickerHandle,
time.Duration(min), time.Duration(max))
// Force an immediate timeout since timer could have decreased.
flextimer.TickNow(ctx.locationAppTickerHandle)
}
func publishLocation(ctx *zedagentContext, iter *int, wdName string,
dest destinationBitset) {
locationInfo := getLocationInfo(ctx)
if locationInfo == nil {
// Not available.
return
}
if dest&(ControllerDest|LOCDest) != 0 {
*iter++
start := time.Now()
publishLocationToDest(ctx, locationInfo, *iter, dest)
ctx.ps.CheckMaxTimeTopic(wdName, "publishLocationToDest", start,
warningTime, errorTime)
}
if dest&LPSDest != 0 {
start := time.Now()
ctx.getconfigCtx.localCmdAgent.PublishLocationToLps(locationInfo)
ctx.ps.CheckMaxTimeTopic(wdName, "publishLocationToLocalServer", start,
warningTime, errorTime)
}
}
func publishLocationToDest(ctx *zedagentContext, locInfo *info.ZInfoLocation,
iteration int, dest destinationBitset) {
log.Functionf("publishLocationToDest: iteration %d", iteration)
infoMsg := &info.ZInfoMsg{
Ztype: info.ZInfoTypes_ZiLocation,
DevId: devUUID.String(),
InfoContent: &info.ZInfoMsg_Locinfo{
Locinfo: locInfo,
},
AtTimeStamp: timestamppb.Now(),
}
log.Functionf("publishLocationToDest: sending %v", infoMsg)
data, err := proto.Marshal(infoMsg)
if err != nil {
log.Fatal("publishLocationToDest: proto marshaling error: ", err)
}
buf := bytes.NewBuffer(data)
if buf == nil {
log.Fatal("malloc error")
}
const bailOnHTTPErr = false
const withNetTrace = false
key := "location:" + devUUID.String()
// Even for the controller destination we can't stall the queue on error,
// because this is recurring call, so set @forcePeriodic to true
forcePeriodic := true
queueInfoToDest(ctx, dest, key, buf, bailOnHTTPErr, withNetTrace,
forcePeriodic, info.ZInfoTypes_ZiLocation)
}
func getLocationInfo(ctx *zedagentContext) *info.ZInfoLocation {
m, err := ctx.subLocationInfo.Get("global")
if err != nil {
// Location info is not available.
return nil
}
locInfo, ok := m.(types.WwanLocationInfo)
if !ok {
log.Error("unexpected type of wwan location info")
return nil
}
unixSec := int64(locInfo.UTCTimestamp / 1000)
unixNano := int64((locInfo.UTCTimestamp % 1000) * 1000000)
timestamp := time.Unix(unixSec, unixNano)
return &info.ZInfoLocation{
Logicallabel: locInfo.LogicalLabel,
Latitude: locInfo.Latitude,
Longitude: locInfo.Longitude,
Altitude: locInfo.Altitude,
UtcTimestamp: timestamppb.New(timestamp),
HorizontalReliability: locationReliabilityToProto(locInfo.HorizontalReliability),
VerticalReliability: locationReliabilityToProto(locInfo.VerticalReliability),
HorizontalUncertainty: locInfo.HorizontalUncertainty,
VerticalUncertainty: locInfo.VerticalUncertainty,
}
}
func locationReliabilityToProto(reliability types.LocReliability) info.LocReliability {
switch reliability {
case types.LocReliabilityUnspecified:
return info.LocReliability_LOC_RELIABILITY_UNSPECIFIED
case types.LocReliabilityVeryLow:
return info.LocReliability_LOC_RELIABILITY_VERY_LOW
case types.LocReliabilityLow:
return info.LocReliability_LOC_RELIABILITY_LOW
case types.LocReliabilityMedium:
return info.LocReliability_LOC_RELIABILITY_MEDIUM
case types.LocReliabilityHigh:
return info.LocReliability_LOC_RELIABILITY_HIGH
default:
return info.LocReliability_LOC_RELIABILITY_UNSPECIFIED
}
}