Compare commits

..

17 Commits
v.1.1 ... main

Author SHA1 Message Date
henry 8a80c8a609 Don't do this:
log.Println("[INFO] Reading version: ", string(buildBytes))
	} else {
		log.Println("[ERROR] Unable to read /opt/build_version.json file:", err)
Generates too much spam
2025-03-02 21:52:30 -08:00
henry 94940f66c9 syncing up BLE between Go and Python 2024-12-29 16:33:06 -08:00
Henry Seurer 88ddf0d9c0 Merge remote-tracking branch 'origin/main' 2024-12-17 20:36:55 -08:00
Henry Seurer 95d3db9196 Cleaning up BLE messages 2024-12-17 20:36:50 -08:00
henry b52c585528 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	messages.go
2024-12-08 08:51:38 -08:00
henry 7810772cb5 Added Services 2024-12-08 08:50:20 -08:00
Henry Seurer a693a92115 Added Company Name 2024-11-21 09:42:31 -08:00
Henry Seurer 7b85cb03bc Typo Devices => Device 2024-11-21 09:25:13 -08:00
Henry Seurer 1de06f272c Manufacture Data Update 2024-11-21 09:17:20 -08:00
henry 6816ebdb73 Send one device at a time instead of an array. 2024-11-21 07:58:42 -08:00
Henry Seurer 2cc64731f1 change how we handle ble bluetooth messages. 2024-11-19 08:31:47 -08:00
henry 901972c7b8 Added BleCompany 2024-11-19 06:54:43 -08:00
henry cf518b3829 Added BleCompany 2024-11-19 06:50:02 -08:00
henry 56bf4a9523 tweak to message structure 2024-11-19 06:26:18 -08:00
henry 83e2486d6a updated ble message to support company. 2024-11-19 06:20:53 -08:00
henry e92ee83802 upgraded module and moved to git.bellaerba.dev/henry/farm-messages 2024-11-19 05:59:43 -08:00
Henry Seurer beca61081b Added go.mod 2024-11-18 13:58:04 -08:00
2 changed files with 30 additions and 43 deletions

3
go.mod Normal file
View File

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

View File

@ -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