Compare commits

..

No commits in common. "main" and "v.1.1" have entirely different histories.
main ... v.1.1

2 changed files with 43 additions and 30 deletions

3
go.mod
View File

@ -1,3 +0,0 @@
module git.bellaerba.dev/henry/farm-messages
go 1.23

View File

@ -28,6 +28,7 @@ package messages
import (
"encoding/json"
"log"
"math"
"math/rand"
"os"
@ -236,39 +237,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
minValue := t.Min
if minValue <= 0 {
minValue = minTime
min := t.Min
if min <= 0 {
min = minTime
}
maxValue := t.Max
if maxValue <= 0 {
maxValue = maxTime
max := t.Max
if max <= 0 {
max = maxTime
}
if minValue >= maxValue {
if min >= max {
// short-circuit
return maxValue
return max
}
factor := t.Factor
if factor <= 0 {
factor = 2
}
//calculate this duration
minFloat := float64(minValue)
minFloat := float64(min)
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 maxValue
return max
}
dur := time.Duration(durationFloat)
//keep within bounds
if dur < minValue {
return minValue
if dur < min {
return min
}
if dur > maxValue {
return maxValue
if dur > max {
return max
}
return dur
}
@ -965,38 +966,53 @@ type StatusMessage struct {
States map[string]bool `json:"states,omitempty"`
}
type BleServiceData struct {
UUID string `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
type ManufacturerDataElement struct {
// The company ID, which must be one of the assigned company IDs.
// The full list is in here:
// 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"`
}
type BleManufacturerData struct {
CompanyId uint16 `json:"company_id,omitempty"`
Name string `json:"name,omitempty"`
Data []byte `json:"data,omitempty"`
type BleAdvertisementPayload struct {
LocalName string `json:"localName,omitempty"`
Bytes []byte `json:"bytes,omitempty"`
ManufacturerData []ManufacturerDataElement `json:"manufacturerData,omitempty"`
}
type BleDevice struct {
Address string `json:"address,omitempty"`
RSSI int16 `json:"rssi,omitempty"`
Name string `json:"name,omitempty"`
ManufacturerData []BleManufacturerData `json:"companies,omitempty"`
Address string `json:"address,omitempty"`
RSSI int16 `json:"rssi,omitempty"`
Name string `json:"name,omitempty"`
Advertisement BleAdvertisementPayload `json:"advertisement,omitempty"`
}
type BleDevicesMap map[string]BleDevice
type BleDevices []BleDevice
type BleAdvertisementMessage struct {
Header MessageHeader `json:"header,omitempty"`
Device BleDevice `json:"device,omitempty"`
Header MessageHeader `json:"header,omitempty"`
Devices BleDevices `json:"devices,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