Compare commits
17 Commits
Author | SHA1 | Date |
---|---|---|
|
8a80c8a609 | |
|
94940f66c9 | |
|
88ddf0d9c0 | |
|
95d3db9196 | |
|
b52c585528 | |
|
7810772cb5 | |
|
a693a92115 | |
|
7b85cb03bc | |
|
1de06f272c | |
|
6816ebdb73 | |
|
2cc64731f1 | |
|
901972c7b8 | |
|
cf518b3829 | |
|
56bf4a9523 | |
|
83e2486d6a | |
|
e92ee83802 | |
|
beca61081b |
70
messages.go
70
messages.go
|
@ -28,7 +28,6 @@ package messages
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"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 {
|
func (t *ThrottleEntry) ForAttempt(attempt float64, minTime time.Duration, maxTime time.Duration) time.Duration {
|
||||||
// Zero-values are nonsensical, so we use
|
// Zero-values are nonsensical, so we use
|
||||||
// them to apply defaults
|
// them to apply defaults
|
||||||
min := t.Min
|
minValue := t.Min
|
||||||
if min <= 0 {
|
if minValue <= 0 {
|
||||||
min = minTime
|
minValue = minTime
|
||||||
}
|
}
|
||||||
max := t.Max
|
maxValue := t.Max
|
||||||
if max <= 0 {
|
if maxValue <= 0 {
|
||||||
max = maxTime
|
maxValue = maxTime
|
||||||
}
|
}
|
||||||
if min >= max {
|
if minValue >= maxValue {
|
||||||
// short-circuit
|
// short-circuit
|
||||||
return max
|
return maxValue
|
||||||
}
|
}
|
||||||
factor := t.Factor
|
factor := t.Factor
|
||||||
if factor <= 0 {
|
if factor <= 0 {
|
||||||
factor = 2
|
factor = 2
|
||||||
}
|
}
|
||||||
//calculate this duration
|
//calculate this duration
|
||||||
minFloat := float64(min)
|
minFloat := float64(minValue)
|
||||||
durationFloat := minFloat * math.Pow(factor, attempt)
|
durationFloat := minFloat * math.Pow(factor, attempt)
|
||||||
if t.Jitter {
|
if t.Jitter {
|
||||||
durationFloat = rand.Float64()*(durationFloat-minFloat) + minFloat
|
durationFloat = rand.Float64()*(durationFloat-minFloat) + minFloat
|
||||||
}
|
}
|
||||||
//ensure float64 won't overflow int64
|
//ensure float64 won't overflow int64
|
||||||
if durationFloat > maxInt64 {
|
if durationFloat > maxInt64 {
|
||||||
return max
|
return maxValue
|
||||||
}
|
}
|
||||||
dur := time.Duration(durationFloat)
|
dur := time.Duration(durationFloat)
|
||||||
//keep within bounds
|
//keep within bounds
|
||||||
if dur < min {
|
if dur < minValue {
|
||||||
return min
|
return minValue
|
||||||
}
|
}
|
||||||
if dur > max {
|
if dur > maxValue {
|
||||||
return max
|
return maxValue
|
||||||
}
|
}
|
||||||
return dur
|
return dur
|
||||||
}
|
}
|
||||||
|
@ -966,53 +965,38 @@ type StatusMessage struct {
|
||||||
States map[string]bool `json:"states,omitempty"`
|
States map[string]bool `json:"states,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ManufacturerDataElement struct {
|
type BleServiceData struct {
|
||||||
// The company ID, which must be one of the assigned company IDs.
|
UUID string `json:"uuid,omitempty"`
|
||||||
// The full list is in here:
|
Name string `json:"name,omitempty"`
|
||||||
// https://www.bluetooth.com/specifications/assigned-numbers/
|
|
||||||
// The list can also be viewed here:
|
|
||||||
// https://bitbucket.org/bluetooth-SIG/public/src/main/assigned_numbers/company_identifiers/company_identifiers.yaml
|
|
||||||
// The value 0xffff can also be used for testing.
|
|
||||||
CompanyID uint16 `json:"company-id,omitempty"`
|
|
||||||
|
|
||||||
// The value, which can be any value but can't be very large.
|
|
||||||
Data []byte `json:"data,omitempty"`
|
Data []byte `json:"data,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BleAdvertisementPayload struct {
|
type BleManufacturerData struct {
|
||||||
LocalName string `json:"localName,omitempty"`
|
CompanyId uint16 `json:"company_id,omitempty"`
|
||||||
Bytes []byte `json:"bytes,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
ManufacturerData []ManufacturerDataElement `json:"manufacturerData,omitempty"`
|
Data []byte `json:"data,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BleDevice struct {
|
type BleDevice struct {
|
||||||
Address string `json:"address,omitempty"`
|
Address string `json:"address,omitempty"`
|
||||||
RSSI int16 `json:"rssi,omitempty"`
|
RSSI int16 `json:"rssi,omitempty"`
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
Advertisement BleAdvertisementPayload `json:"advertisement,omitempty"`
|
ManufacturerData []BleManufacturerData `json:"companies,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BleDevicesMap map[string]BleDevice
|
|
||||||
|
|
||||||
type BleDevices []BleDevice
|
|
||||||
|
|
||||||
type BleAdvertisementMessage struct {
|
type BleAdvertisementMessage struct {
|
||||||
Header MessageHeader `json:"header,omitempty"`
|
Header MessageHeader `json:"header,omitempty"`
|
||||||
Devices BleDevices `json:"devices,omitempty"`
|
Device BleDevice `json:"device,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// noinspection GoUnusedExportedFunction
|
// noinspection GoUnusedExportedFunction
|
||||||
func CreateHeader(status int, location string) MessageHeader {
|
func CreateHeader(status int, location string) MessageHeader {
|
||||||
|
|
||||||
// Do we have a build version?
|
// Do we have a build version?
|
||||||
//
|
//
|
||||||
var build BuildVersion
|
var build BuildVersion
|
||||||
buildBytes, err := os.ReadFile("/opt/build_version.json")
|
buildBytes, err := os.ReadFile("/opt/build_version.json")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = json.Unmarshal(buildBytes, &build)
|
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
|
// Build Message Header
|
||||||
|
|
Loading…
Reference in New Issue