-
Notifications
You must be signed in to change notification settings - Fork 19
/
deviceList.go
54 lines (46 loc) · 1.36 KB
/
deviceList.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
package main
import (
"golbat/config"
"time"
"github.com/jellydator/ttlcache/v3"
)
type DeviceLocation struct {
Latitude float64
Longitude float64
LastUpdate int64
ScanContext string
}
var deviceLocation *ttlcache.Cache[string, DeviceLocation]
func InitDeviceCache() {
deviceLocation = ttlcache.New[string, DeviceLocation](
ttlcache.WithTTL[string, DeviceLocation](time.Hour * time.Duration(config.Config.Cleanup.DeviceHours)),
)
go deviceLocation.Start()
}
func UpdateDeviceLocation(deviceId string, lat, lon float64, scanContext string) {
deviceLocation.Set(deviceId, DeviceLocation{
Latitude: lat,
Longitude: lon,
LastUpdate: time.Now().Unix(),
ScanContext: scanContext,
}, time.Hour*time.Duration(config.Config.Cleanup.DeviceHours))
}
type ApiDeviceLocation struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
LastUpdate int64 `json:"last_update"`
ScanContext string `json:"scan_context"`
}
func GetAllDevices() map[string]ApiDeviceLocation {
locations := map[string]ApiDeviceLocation{}
for _, key := range deviceLocation.Items() {
deviceLocation := key.Value()
locations[key.Key()] = ApiDeviceLocation{
Latitude: deviceLocation.Latitude,
Longitude: deviceLocation.Longitude,
LastUpdate: deviceLocation.LastUpdate,
ScanContext: deviceLocation.ScanContext,
}
}
return locations
}