@@ -19,8 +19,8 @@ package main
1919
2020import (
2121 "encoding/json"
22+ "fmt"
2223 "slices"
23- "strconv"
2424 "strings"
2525 "sync"
2626 "time"
@@ -33,6 +33,9 @@ type serialhub struct {
3333 // Opened serial ports.
3434 ports map [* serport ]bool
3535 mu sync.Mutex
36+
37+ OnRegister func (port * serport )
38+ OnUnregister func (port * serport )
3639}
3740
3841func NewSerialHub () * serialhub {
@@ -45,6 +48,13 @@ func NewSerialHub() *serialhub {
4548type SerialPortList struct {
4649 Ports []* SpPortItem
4750 portsLock sync.Mutex
51+
52+ OnList func ([]byte ) `json:"-"`
53+ OnErr func (string ) `json:"-"`
54+ }
55+
56+ func NewSerialPortList () * SerialPortList {
57+ return & SerialPortList {}
4858}
4959
5060// SpPortItem is the serial port item
@@ -61,14 +71,11 @@ type SpPortItem struct {
6171 ProductID string
6272}
6373
64- // serialPorts contains the ports attached to the machine
65- var serialPorts SerialPortList
66-
6774// Register serial ports from the connections.
6875func (sh * serialhub ) Register (port * serport ) {
6976 sh .mu .Lock ()
7077 //log.Print("Registering a port: ", p.portConf.Name)
71- sh .hub . broadcastSys <- [] byte ( "{ \" Cmd \" : \" Open \" , \" Desc \" : \" Got register/open on port. \" , \" Port \" : \" " + port . portConf . Name + " \" , \" Baud \" :" + strconv . Itoa ( port . portConf . Baud ) + ", \" BufferType \" : \" " + port . BufferType + " \" }" )
78+ sh .OnRegister ( port )
7279 sh .ports [port ] = true
7380 sh .mu .Unlock ()
7481}
@@ -77,7 +84,7 @@ func (sh *serialhub) Register(port *serport) {
7784func (sh * serialhub ) Unregister (port * serport ) {
7885 sh .mu .Lock ()
7986 //log.Print("Unregistering a port: ", p.portConf.Name)
80- sh .hub . broadcastSys <- [] byte ( "{ \" Cmd \" : \" Close \" , \" Desc \" : \" Got unregister/close on port. \" , \" Port \" : \" " + port . portConf . Name + " \" , \" Baud \" :" + strconv . Itoa ( port . portConf . Baud ) + "}" )
87+ sh .OnUnregister ( port )
8188 delete (sh .ports , port )
8289 close (port .sendBuffered )
8390 close (port .sendNoBuf )
@@ -105,11 +112,9 @@ func (sp *SerialPortList) List() {
105112 sp .portsLock .Unlock ()
106113
107114 if err != nil {
108- //log.Println(err)
109- sh .hub .broadcastSys <- []byte ("Error creating json on port list " +
110- err .Error ())
115+ sp .OnErr ("Error creating json on port list " + err .Error ())
111116 } else {
112- sh . hub . broadcastSys <- ls
117+ sp . OnList ( ls )
113118 }
114119}
115120
@@ -196,6 +201,7 @@ func (sp *SerialPortList) add(addedPort *discovery.Port) {
196201
197202 // If the port is already in the list, just update the metadata...
198203 for _ , oldPort := range sp .Ports {
204+ fmt .Println ("oldPort.Name: " , oldPort .Name )
199205 if oldPort .Name == addedPort .Address {
200206 oldPort .SerialNumber = props .Get ("serialNumber" )
201207 oldPort .VendorID = vid
@@ -256,22 +262,22 @@ func (sp *SerialPortList) getPortByName(portname string) *SpPortItem {
256262 return nil
257263}
258264
259- func spErr (err string ) {
265+ func ( h * hub ) spErr (err string ) {
260266 //log.Println("Sending err back: ", err)
261267 //sh.hub.broadcastSys <- []byte(err)
262- sh . hub .broadcastSys <- []byte ("{\" Error\" : \" " + err + "\" }" )
268+ h .broadcastSys <- []byte ("{\" Error\" : \" " + err + "\" }" )
263269}
264270
265- func spClose (portname string ) {
266- if myport , ok := sh .FindPortByName (portname ); ok {
267- sh . hub .broadcastSys <- []byte ("Closing serial port " + portname )
271+ func ( h * hub ) spClose (portname string ) {
272+ if myport , ok := h . serialHub .FindPortByName (portname ); ok {
273+ h .broadcastSys <- []byte ("Closing serial port " + portname )
268274 myport .Close ()
269275 } else {
270- spErr ("We could not find the serial port " + portname + " that you were trying to close." )
276+ h . spErr ("We could not find the serial port " + portname + " that you were trying to close." )
271277 }
272278}
273279
274- func spWrite (arg string ) {
280+ func ( h * hub ) spWrite (arg string ) {
275281 // we will get a string of comXX asdf asdf asdf
276282 //log.Println("Inside spWrite arg: " + arg)
277283 arg = strings .TrimPrefix (arg , " " )
@@ -280,7 +286,7 @@ func spWrite(arg string) {
280286 if len (args ) != 3 {
281287 errstr := "Could not parse send command: " + arg
282288 //log.Println(errstr)
283- spErr (errstr )
289+ h . spErr (errstr )
284290 return
285291 }
286292 bufferingMode := args [0 ]
@@ -291,10 +297,10 @@ func spWrite(arg string) {
291297 //log.Println("The data is:" + data + "---")
292298
293299 // See if we have this port open
294- port , ok := sh .FindPortByName (portname )
300+ port , ok := h . serialHub .FindPortByName (portname )
295301 if ! ok {
296302 // we couldn't find the port, so send err
297- spErr ("We could not find the serial port " + portname + " that you were trying to write to." )
303+ h . spErr ("We could not find the serial port " + portname + " that you were trying to write to." )
298304 return
299305 }
300306
@@ -303,7 +309,7 @@ func spWrite(arg string) {
303309 case "send" , "sendnobuf" , "sendraw" :
304310 // valid buffering mode, go ahead
305311 default :
306- spErr ("Unsupported send command:" + args [0 ] + ". Please specify a valid one" )
312+ h . spErr ("Unsupported send command:" + args [0 ] + ". Please specify a valid one" )
307313 return
308314 }
309315
0 commit comments