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