Compare commits
18 Commits
Author | SHA1 | Date |
---|---|---|
|
8a80c8a609 | |
|
94940f66c9 | |
|
88ddf0d9c0 | |
|
95d3db9196 | |
|
b52c585528 | |
|
7810772cb5 | |
|
a693a92115 | |
|
7b85cb03bc | |
|
1de06f272c | |
|
6816ebdb73 | |
|
2cc64731f1 | |
|
901972c7b8 | |
|
cf518b3829 | |
|
56bf4a9523 | |
|
83e2486d6a | |
|
e92ee83802 | |
|
beca61081b | |
|
c7e4bfa581 |
63
messages.go
63
messages.go
|
@ -28,7 +28,6 @@ package messages
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"math"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
@ -237,39 +236,39 @@ const maxInt64 = float64(math.MaxInt64 - 512)
|
|||
func (t *ThrottleEntry) ForAttempt(attempt float64, minTime time.Duration, maxTime time.Duration) time.Duration {
|
||||
// Zero-values are nonsensical, so we use
|
||||
// them to apply defaults
|
||||
min := t.Min
|
||||
if min <= 0 {
|
||||
min = minTime
|
||||
minValue := t.Min
|
||||
if minValue <= 0 {
|
||||
minValue = minTime
|
||||
}
|
||||
max := t.Max
|
||||
if max <= 0 {
|
||||
max = maxTime
|
||||
maxValue := t.Max
|
||||
if maxValue <= 0 {
|
||||
maxValue = maxTime
|
||||
}
|
||||
if min >= max {
|
||||
if minValue >= maxValue {
|
||||
// short-circuit
|
||||
return max
|
||||
return maxValue
|
||||
}
|
||||
factor := t.Factor
|
||||
if factor <= 0 {
|
||||
factor = 2
|
||||
}
|
||||
//calculate this duration
|
||||
minFloat := float64(min)
|
||||
minFloat := float64(minValue)
|
||||
durationFloat := minFloat * math.Pow(factor, attempt)
|
||||
if t.Jitter {
|
||||
durationFloat = rand.Float64()*(durationFloat-minFloat) + minFloat
|
||||
}
|
||||
//ensure float64 won't overflow int64
|
||||
if durationFloat > maxInt64 {
|
||||
return max
|
||||
return maxValue
|
||||
}
|
||||
dur := time.Duration(durationFloat)
|
||||
//keep within bounds
|
||||
if dur < min {
|
||||
return min
|
||||
if dur < minValue {
|
||||
return minValue
|
||||
}
|
||||
if dur > max {
|
||||
return max
|
||||
if dur > maxValue {
|
||||
return maxValue
|
||||
}
|
||||
return dur
|
||||
}
|
||||
|
@ -966,40 +965,38 @@ type StatusMessage struct {
|
|||
States map[string]bool `json:"states,omitempty"`
|
||||
}
|
||||
|
||||
type BleAdvertisementPayload struct {
|
||||
LocalName string `json:"localName,omitempty"`
|
||||
Bytes []byte `json:"bytes,omitempty"`
|
||||
ManufacturerData map[uint16][]byte `json:"manufacturerData,omitempty"`
|
||||
type BleServiceData struct {
|
||||
UUID string `json:"uuid,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Data []byte `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
type BleManufacturerData struct {
|
||||
CompanyId uint16 `json:"company_id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Data []byte `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
type BleDevice struct {
|
||||
Address string `json:"address,omitempty"`
|
||||
RSSI int16 `json:"rssi,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Advertisement BleAdvertisementPayload `json:"advertisement,omitempty"`
|
||||
Address string `json:"address,omitempty"`
|
||||
RSSI int16 `json:"rssi,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
ManufacturerData []BleManufacturerData `json:"companies,omitempty"`
|
||||
}
|
||||
|
||||
type BleDevicesMap map[string]BleDevice
|
||||
|
||||
type BleDevices []BleDevice
|
||||
|
||||
type BleAdvertisementMessage struct {
|
||||
Header MessageHeader `json:"header,omitempty"`
|
||||
Devices BleDevices `json:"devices,omitempty"`
|
||||
Header MessageHeader `json:"header,omitempty"`
|
||||
Device BleDevice `json:"device,omitempty"`
|
||||
}
|
||||
|
||||
// noinspection GoUnusedExportedFunction
|
||||
func CreateHeader(status int, location string) MessageHeader {
|
||||
|
||||
// Do we have a build version?
|
||||
//
|
||||
var build BuildVersion
|
||||
buildBytes, err := os.ReadFile("/opt/build_version.json")
|
||||
if err == nil {
|
||||
err = json.Unmarshal(buildBytes, &build)
|
||||
log.Println("[INFO] Reading version: ", string(buildBytes))
|
||||
} else {
|
||||
log.Println("[ERROR] Unable to read /opt/build_version.json file:", err)
|
||||
}
|
||||
|
||||
// Build Message Header
|
||||
|
|
Loading…
Reference in New Issue