refactor handling of types cross package and add documentation

This commit is contained in:
kayos@tcp.direct 2021-08-17 08:28:24 -07:00
parent ed222d181d
commit df3dfabe22
5 changed files with 35 additions and 23 deletions

@ -116,7 +116,6 @@ func init() {
dust.PrintBanner() dust.PrintBanner()
protogen.ScanMgr = &protogen.Meta{ protogen.ScanMgr = &protogen.Meta{
Count: 0,
Scans: make(map[int]*protogen.Scan), Scans: make(map[int]*protogen.Scan),
Mailbox: make(chan protogen.Postcard), Mailbox: make(chan protogen.Postcard),
} }

@ -14,12 +14,12 @@ import (
"bufio" "bufio"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"os" "os"
projVars "protomolecule/src/vars"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
projVars "protomolecule/src/vars"
bluetooth "git.tcp.direct/kayos/prototooth" bluetooth "git.tcp.direct/kayos/prototooth"
"github.com/prologic/bitcask" "github.com/prologic/bitcask"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -62,8 +62,14 @@ func (manuf *Manufacturer) IngestUUID(uuid bluetooth.UUID) bool {
manuf.UUIDs = append(manuf.UUIDs, uuid) manuf.UUIDs = append(manuf.UUIDs, uuid)
return true return true
} }
func FinalizeDevice(bigidea Device) *Device {
bigidea.mu = &sync.RWMutex{}
return &bigidea
}
func (d *Device) ConnectHandler(target bluetooth.Addresser, c bool) { func (d *Device) ConnectHandler(target bluetooth.Addresser, c bool) {
d.mu.Lock()
defer d.mu.Unlock()
if c { if c {
d.Connected = true d.Connected = true
} else { } else {
@ -191,7 +197,7 @@ func Exists(Addr string) bool {
return false return false
} }
// Remember - store device details into the database // Remember stores device details into the database
func Remember(dev Device) error { func Remember(dev Device) error {
var err error var err error
var rhist map[time.Time]int16 var rhist map[time.Time]int16

@ -2,7 +2,7 @@ package eros
import ( import (
"time" "time"
"sync"
bluetooth "git.tcp.direct/kayos/prototooth" bluetooth "git.tcp.direct/kayos/prototooth"
) )
@ -82,4 +82,7 @@ type Device struct {
Connected bool Connected bool
Conn *bluetooth.Device Conn *bluetooth.Device
// The most at-risk type we have so far for concurrency, needs to be mutexed
mu *sync.RWMutex
} }

@ -41,21 +41,19 @@ func (m *Meta) NewScan() *Scan {
scan := m.Scans[newid] scan := m.Scans[newid]
scan.Device = make(map[int]*eros.Device) scan.Device = make(map[int]*eros.Device)
m.Count = len(m.Scans)
return scan return scan
} }
func (s *Scan) NewDevice(name string, addr string, manuf string, rssi int16) *eros.Device { func (s *Scan) NewDevice(name string, addr string, manuf string, rssi int16) *eros.Device {
newid := len(s.Device) s.mu.Lock()
s.Device[newid] = &eros.Device{ defer s.mu.Unlock()
s.Device[len(s.Device)] = eros.FinalizeDevice(eros.Device{
Name: name, Name: name,
Addr: addr, Addr: addr,
Manufacturer: manuf, Manufacturer: manuf,
RSSIlast: rssi, RSSIlast: rssi,
} })
return s.Device[len(s.Device)]
s.Count = len(s.Device)
return s.Device[newid]
} }
/* /*
@ -94,6 +92,7 @@ func (s *Scan) resultHandler(scanAdapter *bluetooth.Adapter, result bluetooth.Sc
payload := result.AdvertisementPayload payload := result.AdvertisementPayload
addr := result.Address.String() addr := result.Address.String()
lname := result.LocalName() lname := result.LocalName()
//adbytes := payload.Bytes() //adbytes := payload.Bytes()
rssi := result.RSSI rssi := result.RSSI

@ -4,6 +4,7 @@ import (
bluetooth "git.tcp.direct/kayos/prototooth" bluetooth "git.tcp.direct/kayos/prototooth"
"protomolecule/src/eros" "protomolecule/src/eros"
"time" "time"
"sync"
) )
/* /*
@ -14,45 +15,49 @@ Why the Meta struct?
I've mocked up some commented out examples in the source code below. I've mocked up some commented out examples in the source code below.
*/ */
type Meta struct { type Meta struct {
Count int
Scans map[int]*Scan Scans map[int]*Scan
Mailbox chan Postcard Mailbox chan Postcard
// // // Future Concepts // // // // // // Future Concepts // // //
// BLEScans map[int]*BLEScan // BLEScans map[int]*BLEScan
// LoraScans map[int]*LoraScan // LoraScans map[int]*LoraScan
// ZigScans map[int]*ZigScan // ZigScans map[int]*ZigScan
// WiFiScans map[int]*WiFiScan // WiFiScans map[int]*WiFiScan
// we definitely need this to be safe for concurrency
mu *sync.RWMutex
} }
// Postcard will encapsulate interprocess communication messages that we send back to the main thread // Postcard will encapsulate interprocess communication messages that we send back to the main thread
type Postcard struct { type Postcard struct {
// From - the ScanID the message originated from // From - the ScanID the message originated from
From int From int
// Stamp - the time the IPC message was created // Stamp - the time the IPC message was created
Stamp time.Time Stamp time.Time
// Device ID - relevant DeviceID (if applicable) - Remember to use Scan.Devices[id] for easy referral // Device ID - relevant DeviceID (if applicable) - Remember to use Scan.Devices[id] for easy referral
DeviceID int DeviceID int
// Command - the actual IPC command // Command - the actual IPC command
Command string Command string
// Arguments - augmenting arguments to the command // Arguments - augmenting arguments to the command
Arguments []string Arguments []string
} }
// Instance of a BLE scan // TODO: Form profiles on devices
// Scan represents an instance of a BLE scan
type Scan struct { type Scan struct {
Count int Count int
// The ID is how we will refer back to running scans during IPC to react or cancel a scan
ID int ID int
// Started the scan at this time
Started time.Time Started time.Time
// Activity was last seen at this time
Activity time.Time Activity time.Time
// Device represents a bluetooth endpoints
Device map[int]*eros.Device Device map[int]*eros.Device
// Connection is an active connection to a bluetooth endpoint
Connection map[int]*bluetooth.Connection Connection map[int]*bluetooth.Connection
/* Ignore is a map of devices to disregard during a scan,
by using the hardware address as a key. (needs disclaimer?) */
Ignore map[string]bool
// Gonna plan on mutexing scan operations just to be safe
mu *sync.RWMutex
} }