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()
protogen.ScanMgr = &protogen.Meta{
Count: 0,
Scans: make(map[int]*protogen.Scan),
Mailbox: make(chan protogen.Postcard),
}

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

@ -2,7 +2,7 @@ package eros
import (
"time"
"sync"
bluetooth "git.tcp.direct/kayos/prototooth"
)
@ -82,4 +82,7 @@ type Device struct {
Connected bool
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.Device = make(map[int]*eros.Device)
m.Count = len(m.Scans)
return scan
}
func (s *Scan) NewDevice(name string, addr string, manuf string, rssi int16) *eros.Device {
newid := len(s.Device)
s.Device[newid] = &eros.Device{
s.mu.Lock()
defer s.mu.Unlock()
s.Device[len(s.Device)] = eros.FinalizeDevice(eros.Device{
Name: name,
Addr: addr,
Manufacturer: manuf,
RSSIlast: rssi,
}
s.Count = len(s.Device)
return s.Device[newid]
})
return s.Device[len(s.Device)]
}
/*
@ -94,6 +92,7 @@ func (s *Scan) resultHandler(scanAdapter *bluetooth.Adapter, result bluetooth.Sc
payload := result.AdvertisementPayload
addr := result.Address.String()
lname := result.LocalName()
//adbytes := payload.Bytes()
rssi := result.RSSI

@ -4,6 +4,7 @@ import (
bluetooth "git.tcp.direct/kayos/prototooth"
"protomolecule/src/eros"
"time"
"sync"
)
/*
@ -14,45 +15,49 @@ Why the Meta struct?
I've mocked up some commented out examples in the source code below.
*/
type Meta struct {
Count int
Scans map[int]*Scan
Mailbox chan Postcard
// // // Future Concepts // // //
// BLEScans map[int]*BLEScan
// LoraScans map[int]*LoraScan
// ZigScans map[int]*ZigScan
// 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
type Postcard struct {
// From - the ScanID the message originated from
From int
// Stamp - the time the IPC message was created
Stamp time.Time
// Device ID - relevant DeviceID (if applicable) - Remember to use Scan.Devices[id] for easy referral
DeviceID int
// Command - the actual IPC command
Command string
// Arguments - augmenting arguments to the command
Arguments []string
}
// Instance of a BLE scan
// TODO: Form profiles on devices
// Scan represents an instance of a BLE scan
type Scan struct {
Count int
// The ID is how we will refer back to running scans during IPC to react or cancel a scan
ID int
// Started the scan at this time
Started time.Time
// Activity was last seen at this time
Activity time.Time
// Device represents a bluetooth endpoints
Device map[int]*eros.Device
// Connection is an active connection to a bluetooth endpoint
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
}