-
Notifications
You must be signed in to change notification settings - Fork 156
/
handlelocation.go
273 lines (250 loc) · 8.88 KB
/
handlelocation.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
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
// Copyright (c) 2017-2022 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package zedagent
import (
"bytes"
"fmt"
"net/http"
"strings"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/lf-edge/eve-api/go/info"
"github.com/lf-edge/eve/pkg/pillar/flextimer"
"github.com/lf-edge/eve/pkg/pillar/types"
"github.com/lf-edge/eve/pkg/pillar/zedcloud"
"google.golang.org/protobuf/proto"
)
const (
// URL path at which the location information is published to the Local profile server.
lpsLocationURLPath = "/api/v1/location"
// Local profile server may decrease the rate of location reporting to 1 per 5 minutes
// (at most) by returning 404 to a Location POST request coming from EVE.
lpsLocationThrottledInterval = 5 * time.Minute
)
// 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) {
locInfo := getLocationInfo(ctx)
if locInfo == nil {
// Not available.
return
}
if dest&(ControllerDest|LOCDest) != 0 {
*iter++
start := time.Now()
publishLocationToDest(ctx, locInfo, *iter, dest)
ctx.ps.CheckMaxTimeTopic(wdName, "publishLocationToDest", start,
warningTime, errorTime)
}
if dest&LPSDest != 0 {
start := time.Now()
publishLocationToLocalServer(ctx.getconfigCtx, locInfo)
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: ptypes.TimestampNow(),
}
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")
}
size := int64(proto.Size(infoMsg))
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, size, bailOnHTTPErr, withNetTrace,
forcePeriodic, info.ZInfoTypes_ZiLocation)
}
func publishLocationToLocalServer(ctx *getconfigContext, locInfo *info.ZInfoLocation) {
if ctx.sideController.lpsThrottledLocation {
if time.Since(ctx.sideController.lpsLastPublishedLocation) < lpsLocationThrottledInterval {
return
}
}
localProfileServer := ctx.sideController.localProfileServer
if localProfileServer == "" {
return
}
localServerURL, err := makeLocalServerBaseURL(localProfileServer)
if err != nil {
log.Errorf("publishLocationToLocalServer: makeLocalServerBaseURL: %v", err)
return
}
if !ctx.sideController.localServerMap.upToDate {
err := updateLocalServerMap(ctx, localServerURL)
if err != nil {
log.Errorf("publishLocationToLocalServer: updateLocalServerMap: %v", err)
return
}
// Make sure HasLocalServer is set correctly for the AppInstanceConfig
updateHasLocalServer(ctx)
}
srvMap := ctx.sideController.localServerMap.servers
if len(srvMap) == 0 {
log.Functionf("publishLocationToLocalServer: cannot find any configured "+
"apps for localServerURL: %s", localServerURL)
return
}
var errList []string
for bridgeName, servers := range srvMap {
for _, srv := range servers {
fullURL := srv.localServerAddr + lpsLocationURLPath
resp, err := zedcloud.SendLocalProto(
zedcloudCtx, fullURL, bridgeName, srv.bridgeIP, locInfo, nil)
ctx.sideController.lpsLastPublishedLocation = time.Now()
if err != nil {
errList = append(errList, fmt.Sprintf("SendLocalProto: %v", err))
if resp == nil {
continue
}
}
switch resp.StatusCode {
case http.StatusNotFound:
ctx.sideController.lpsThrottledLocation = true
return
case http.StatusOK, http.StatusCreated, http.StatusNoContent:
ctx.sideController.lpsThrottledLocation = false
return
default:
if err == nil {
errList = append(errList,
fmt.Sprintf("SendLocalProto: wrong response status code: %d",
resp.StatusCode))
}
continue
}
}
}
log.Errorf("publishLocationToLocalServer: all attempts failed: %s",
strings.Join(errList, ";"))
return
}
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)
timestampProto, _ := ptypes.TimestampProto(timestamp)
return &info.ZInfoLocation{
Logicallabel: locInfo.LogicalLabel,
Latitude: locInfo.Latitude,
Longitude: locInfo.Longitude,
Altitude: locInfo.Altitude,
UtcTimestamp: timestampProto,
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
}
}