@@ -19,8 +19,8 @@ package main
19
19
20
20
import (
21
21
"encoding/json"
22
+ "fmt"
22
23
"slices"
23
- "strconv"
24
24
"strings"
25
25
"sync"
26
26
"time"
@@ -33,6 +33,9 @@ type serialhub struct {
33
33
// Opened serial ports.
34
34
ports map [* serport ]bool
35
35
mu sync.Mutex
36
+
37
+ OnRegister func (port * serport )
38
+ OnUnregister func (port * serport )
36
39
}
37
40
38
41
func NewSerialHub () * serialhub {
@@ -45,6 +48,13 @@ func NewSerialHub() *serialhub {
45
48
type SerialPortList struct {
46
49
Ports []* SpPortItem
47
50
portsLock sync.Mutex
51
+
52
+ OnList func ([]byte ) `json:"-"`
53
+ OnErr func (string ) `json:"-"`
54
+ }
55
+
56
+ func NewSerialPortList () * SerialPortList {
57
+ return & SerialPortList {}
48
58
}
49
59
50
60
// SpPortItem is the serial port item
@@ -61,14 +71,11 @@ type SpPortItem struct {
61
71
ProductID string
62
72
}
63
73
64
- // serialPorts contains the ports attached to the machine
65
- var serialPorts SerialPortList
66
-
67
74
// Register serial ports from the connections.
68
75
func (sh * serialhub ) Register (port * serport ) {
69
76
sh .mu .Lock ()
70
77
//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 )
72
79
sh .ports [port ] = true
73
80
sh .mu .Unlock ()
74
81
}
@@ -77,7 +84,7 @@ func (sh *serialhub) Register(port *serport) {
77
84
func (sh * serialhub ) Unregister (port * serport ) {
78
85
sh .mu .Lock ()
79
86
//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 )
81
88
delete (sh .ports , port )
82
89
close (port .sendBuffered )
83
90
close (port .sendNoBuf )
@@ -105,11 +112,9 @@ func (sp *SerialPortList) List() {
105
112
sp .portsLock .Unlock ()
106
113
107
114
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 ())
111
116
} else {
112
- sh . hub . broadcastSys <- ls
117
+ sp . OnList ( ls )
113
118
}
114
119
}
115
120
@@ -196,6 +201,7 @@ func (sp *SerialPortList) add(addedPort *discovery.Port) {
196
201
197
202
// If the port is already in the list, just update the metadata...
198
203
for _ , oldPort := range sp .Ports {
204
+ fmt .Println ("oldPort.Name: " , oldPort .Name )
199
205
if oldPort .Name == addedPort .Address {
200
206
oldPort .SerialNumber = props .Get ("serialNumber" )
201
207
oldPort .VendorID = vid
@@ -256,22 +262,22 @@ func (sp *SerialPortList) getPortByName(portname string) *SpPortItem {
256
262
return nil
257
263
}
258
264
259
- func spErr (err string ) {
265
+ func ( h * hub ) spErr (err string ) {
260
266
//log.Println("Sending err back: ", err)
261
267
//sh.hub.broadcastSys <- []byte(err)
262
- sh . hub .broadcastSys <- []byte ("{\" Error\" : \" " + err + "\" }" )
268
+ h .broadcastSys <- []byte ("{\" Error\" : \" " + err + "\" }" )
263
269
}
264
270
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 )
268
274
myport .Close ()
269
275
} 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." )
271
277
}
272
278
}
273
279
274
- func spWrite (arg string ) {
280
+ func ( h * hub ) spWrite (arg string ) {
275
281
// we will get a string of comXX asdf asdf asdf
276
282
//log.Println("Inside spWrite arg: " + arg)
277
283
arg = strings .TrimPrefix (arg , " " )
@@ -280,7 +286,7 @@ func spWrite(arg string) {
280
286
if len (args ) != 3 {
281
287
errstr := "Could not parse send command: " + arg
282
288
//log.Println(errstr)
283
- spErr (errstr )
289
+ h . spErr (errstr )
284
290
return
285
291
}
286
292
bufferingMode := args [0 ]
@@ -291,10 +297,10 @@ func spWrite(arg string) {
291
297
//log.Println("The data is:" + data + "---")
292
298
293
299
// See if we have this port open
294
- port , ok := sh .FindPortByName (portname )
300
+ port , ok := h . serialHub .FindPortByName (portname )
295
301
if ! ok {
296
302
// 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." )
298
304
return
299
305
}
300
306
@@ -303,7 +309,7 @@ func spWrite(arg string) {
303
309
case "send" , "sendnobuf" , "sendraw" :
304
310
// valid buffering mode, go ahead
305
311
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" )
307
313
return
308
314
}
309
315
0 commit comments