-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* clean up the Raw HTTP route, move code to raw_decoder. * move device location tracking to its own package/struct * create a map of method -> decode function, minimum level required * we don't need level 30 for everything * map will be more efficient than big switch/case * decode functions take in the proto and its metadata like device uuid, account, etc. This will allow us to pass information deeper for things like shiny stats which will require tracking accounts.
- Loading branch information
Showing
6 changed files
with
562 additions
and
499 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package device_tracker | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/jellydator/ttlcache/v3" | ||
) | ||
|
||
type DeviceTracker interface { | ||
UpdateDeviceLocation(deviceId string, lat, lon float64, scanContext string) | ||
GetAllDevices() map[string]ApiDeviceLocation | ||
} | ||
|
||
type ApiDeviceLocation struct { | ||
Latitude float64 `json:"latitude"` | ||
Longitude float64 `json:"longitude"` | ||
LastUpdate int64 `json:"last_update"` | ||
ScanContext string `json:"scan_context"` | ||
} | ||
|
||
type DeviceLocation struct { | ||
Latitude float64 | ||
Longitude float64 | ||
LastUpdate int64 | ||
ScanContext string | ||
} | ||
|
||
type deviceTracker struct { | ||
maxDeviceTTL time.Duration | ||
deviceLocation *ttlcache.Cache[string, DeviceLocation] | ||
} | ||
|
||
func (tracker *deviceTracker) UpdateDeviceLocation(deviceId string, lat, lon float64, scanContext string) { | ||
if lat == 0 || lon == 0 || deviceId == "" { | ||
return | ||
} | ||
tracker.deviceLocation.Set(deviceId, DeviceLocation{ | ||
Latitude: lat, | ||
Longitude: lon, | ||
LastUpdate: time.Now().Unix(), | ||
ScanContext: scanContext, | ||
}, tracker.maxDeviceTTL) | ||
} | ||
|
||
func (tracker *deviceTracker) GetAllDevices() map[string]ApiDeviceLocation { | ||
locations := map[string]ApiDeviceLocation{} | ||
for _, key := range tracker.deviceLocation.Items() { | ||
deviceLocation := key.Value() | ||
locations[key.Key()] = ApiDeviceLocation(deviceLocation) | ||
} | ||
return locations | ||
} | ||
|
||
func NewDeviceTracker(maxDeviceTTLHours int) DeviceTracker { | ||
maxDeviceTTL := time.Hour * time.Duration(maxDeviceTTLHours) | ||
tracker := &deviceTracker{ | ||
maxDeviceTTL: maxDeviceTTL, | ||
deviceLocation: ttlcache.New[string, DeviceLocation]( | ||
ttlcache.WithTTL[string, DeviceLocation](maxDeviceTTL), | ||
), | ||
} | ||
go tracker.deviceLocation.Start() | ||
return tracker | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.