@@ -53,7 +53,7 @@ type hub struct {
53
53
// Serial hub to communicate with serial ports
54
54
serialHub * serialhub
55
55
56
- serialPortList * serialPortList
56
+ serialPortList * SerialPortList
57
57
58
58
tools * tools.Tools
59
59
@@ -69,7 +69,7 @@ func newHub(tools *tools.Tools, systray *systray.Systray) *hub {
69
69
onUnregister := func (port * serport ) {
70
70
broadcastSys <- []byte ("{\" Cmd\" :\" Close\" ,\" Desc\" :\" Got unregister/close on port.\" ,\" Port\" :\" " + port .portConf .Name + "\" ,\" Baud\" :" + strconv .Itoa (port .portConf .Baud ) + "}" )
71
71
}
72
- serialHub := newSerialHub (onRegister , onUnregister )
72
+ serialHubub := newSerialHub (onRegister , onUnregister )
73
73
74
74
onList := func (data []byte ) {
75
75
broadcastSys <- data
@@ -85,7 +85,7 @@ func newHub(tools *tools.Tools, systray *systray.Systray) *hub {
85
85
register : make (chan * connection ),
86
86
unregister : make (chan * connection ),
87
87
connections : make (map [* connection ]bool ),
88
- serialHub : serialHub ,
88
+ serialHub : serialHubub ,
89
89
serialPortList : serialPortList ,
90
90
tools : tools ,
91
91
systray : systray ,
@@ -110,53 +110,53 @@ const commands = `{
110
110
]
111
111
}`
112
112
113
- func (hub * hub ) unregisterConnection (c * connection ) {
114
- if _ , contains := hub .connections [c ]; ! contains {
113
+ func (h * hub ) unregisterConnection (c * connection ) {
114
+ if _ , contains := h .connections [c ]; ! contains {
115
115
return
116
116
}
117
- delete (hub .connections , c )
117
+ delete (h .connections , c )
118
118
close (c .send )
119
119
}
120
120
121
- func (hub * hub ) sendToRegisteredConnections (data []byte ) {
122
- for c := range hub .connections {
121
+ func (h * hub ) sendToRegisteredConnections (data []byte ) {
122
+ for c := range h .connections {
123
123
select {
124
124
case c .send <- data :
125
125
//log.Print("did broadcast to ")
126
126
//log.Print(c.ws.RemoteAddr())
127
127
//c.send <- []byte("hello world")
128
128
default :
129
- hub .unregisterConnection (c )
129
+ h .unregisterConnection (c )
130
130
}
131
131
}
132
132
}
133
133
134
- func (hub * hub ) run () {
135
- go hub .serialPortList .Run ()
134
+ func (h * hub ) run () {
135
+ go h .serialPortList .Run ()
136
136
137
137
for {
138
138
select {
139
- case c := <- hub .register :
140
- hub .connections [c ] = true
139
+ case c := <- h .register :
140
+ h .connections [c ] = true
141
141
// send supported commands
142
142
c .send <- []byte (fmt .Sprintf (`{"Version" : "%s"} ` , version ))
143
143
c .send <- []byte (html .EscapeString (commands ))
144
144
c .send <- []byte (fmt .Sprintf (`{"Hostname" : "%s"} ` , * hostname ))
145
145
c .send <- []byte (fmt .Sprintf (`{"OS" : "%s"} ` , runtime .GOOS ))
146
- case c := <- hub .unregister :
147
- hub .unregisterConnection (c )
148
- case m := <- hub .broadcast :
146
+ case c := <- h .unregister :
147
+ h .unregisterConnection (c )
148
+ case m := <- h .broadcast :
149
149
if len (m ) > 0 {
150
- hub .checkCmd (m )
151
- hub .sendToRegisteredConnections (m )
150
+ h .checkCmd (m )
151
+ h .sendToRegisteredConnections (m )
152
152
}
153
- case m := <- hub .broadcastSys :
154
- hub .sendToRegisteredConnections (m )
153
+ case m := <- h .broadcastSys :
154
+ h .sendToRegisteredConnections (m )
155
155
}
156
156
}
157
157
}
158
158
159
- func (hub * hub ) checkCmd (m []byte ) {
159
+ func (h * hub ) checkCmd (m []byte ) {
160
160
//log.Print("Inside checkCmd")
161
161
s := string (m [:])
162
162
@@ -171,18 +171,18 @@ func (hub *hub) checkCmd(m []byte) {
171
171
172
172
args := strings .Split (s , " " )
173
173
if len (args ) < 3 {
174
- go hub .spErr ("You did not specify a port and baud rate in your open cmd" )
174
+ go h .spErr ("You did not specify a port and baud rate in your open cmd" )
175
175
return
176
176
}
177
177
if len (args [1 ]) < 1 {
178
- go hub .spErr ("You did not specify a serial port" )
178
+ go h .spErr ("You did not specify a serial port" )
179
179
return
180
180
}
181
181
182
182
baudStr := strings .Replace (args [2 ], "\n " , "" , - 1 )
183
183
baud , err := strconv .Atoi (baudStr )
184
184
if err != nil {
185
- go hub .spErr ("Problem converting baud rate " + args [2 ])
185
+ go h .spErr ("Problem converting baud rate " + args [2 ])
186
186
return
187
187
}
188
188
// pass in buffer type now as string. if user does not
@@ -193,30 +193,30 @@ func (hub *hub) checkCmd(m []byte) {
193
193
buftype := strings .Replace (args [3 ], "\n " , "" , - 1 )
194
194
bufferAlgorithm = buftype
195
195
}
196
- go hub .spHandlerOpen (args [1 ], baud , bufferAlgorithm )
196
+ go h .spHandlerOpen (args [1 ], baud , bufferAlgorithm )
197
197
198
198
} else if strings .HasPrefix (sl , "close" ) {
199
199
200
200
args := strings .Split (s , " " )
201
201
if len (args ) > 1 {
202
- go hub .spClose (args [1 ])
202
+ go h .spClose (args [1 ])
203
203
} else {
204
- go hub .spErr ("You did not specify a port to close" )
204
+ go h .spErr ("You did not specify a port to close" )
205
205
}
206
206
207
207
} else if strings .HasPrefix (sl , "killupload" ) {
208
208
// kill the running process (assumes singleton for now)
209
209
go func () {
210
210
upload .Kill ()
211
- hub .broadcastSys <- []byte ("{\" uploadStatus\" : \" Killed\" }" )
211
+ h .broadcastSys <- []byte ("{\" uploadStatus\" : \" Killed\" }" )
212
212
log .Println ("{\" uploadStatus\" : \" Killed\" }" )
213
213
}()
214
214
215
215
} else if strings .HasPrefix (sl , "send" ) {
216
216
// will catch send and sendnobuf and sendraw
217
- go hub .spWrite (s )
217
+ go h .spWrite (s )
218
218
} else if strings .HasPrefix (sl , "list" ) {
219
- go hub .serialPortList .List ()
219
+ go h .serialPortList .List ()
220
220
} else if strings .HasPrefix (sl , "downloadtool" ) {
221
221
go func () {
222
222
args := strings .Split (s , " " )
@@ -227,7 +227,7 @@ func (hub *hub) checkCmd(m []byte) {
227
227
if len (args ) <= 1 {
228
228
mapD := map [string ]string {"DownloadStatus" : "Error" , "Msg" : "Not enough arguments" }
229
229
mapB , _ := json .Marshal (mapD )
230
- hub .broadcastSys <- mapB
230
+ h .broadcastSys <- mapB
231
231
return
232
232
}
233
233
if len (args ) > 1 {
@@ -250,51 +250,51 @@ func (hub *hub) checkCmd(m []byte) {
250
250
reportPendingProgress := func (msg string ) {
251
251
mapD := map [string ]string {"DownloadStatus" : "Pending" , "Msg" : msg }
252
252
mapB , _ := json .Marshal (mapD )
253
- hub .broadcastSys <- mapB
253
+ h .broadcastSys <- mapB
254
254
}
255
- err := hub .tools .Download (pack , tool , toolVersion , behaviour , reportPendingProgress )
255
+ err := h .tools .Download (pack , tool , toolVersion , behaviour , reportPendingProgress )
256
256
if err != nil {
257
257
mapD := map [string ]string {"DownloadStatus" : "Error" , "Msg" : err .Error ()}
258
258
mapB , _ := json .Marshal (mapD )
259
- hub .broadcastSys <- mapB
259
+ h .broadcastSys <- mapB
260
260
} else {
261
261
mapD := map [string ]string {"DownloadStatus" : "Success" , "Msg" : "Map Updated" }
262
262
mapB , _ := json .Marshal (mapD )
263
- hub .broadcastSys <- mapB
263
+ h .broadcastSys <- mapB
264
264
}
265
265
}()
266
266
} else if strings .HasPrefix (sl , "log" ) {
267
- go hub .logAction (sl )
267
+ go h .logAction (sl )
268
268
} else if strings .HasPrefix (sl , "restart" ) {
269
- // potentially, the sysStray dependencies can be removed https://github .com/arduino/arduino-create-agent/issues/1013
269
+ // potentially, the sysStray dependencies can be removed https://gith .com/arduino/arduino-create-agent/issues/1013
270
270
log .Println ("Received restart from the daemon. Why? Boh" )
271
- hub .systray .Restart ()
271
+ h .systray .Restart ()
272
272
} else if strings .HasPrefix (sl , "exit" ) {
273
- hub .systray .Quit ()
273
+ h .systray .Quit ()
274
274
} else if strings .HasPrefix (sl , "memstats" ) {
275
- hub .memoryStats ()
275
+ h .memoryStats ()
276
276
} else if strings .HasPrefix (sl , "gc" ) {
277
- hub .garbageCollection ()
277
+ h .garbageCollection ()
278
278
} else if strings .HasPrefix (sl , "hostname" ) {
279
- hub .getHostname ()
279
+ h .getHostname ()
280
280
} else if strings .HasPrefix (sl , "version" ) {
281
- hub .getVersion ()
281
+ h .getVersion ()
282
282
} else {
283
- go hub .spErr ("Could not understand command." )
283
+ go h .spErr ("Could not understand command." )
284
284
}
285
285
}
286
286
287
287
type logWriter struct {
288
288
onWrite func ([]byte )
289
289
}
290
290
291
- func (hub * hub ) logAction (sl string ) {
291
+ func (h * hub ) logAction (sl string ) {
292
292
if strings .HasPrefix (sl , "log on" ) {
293
293
* logDump = "on"
294
294
295
295
logWriter := logWriter {}
296
296
logWriter .onWrite = func (p []byte ) {
297
- hub .broadcastSys <- p
297
+ h .broadcastSys <- p
298
298
}
299
299
300
300
multiWriter := io .MultiWriter (& logWriter , os .Stderr )
@@ -304,7 +304,7 @@ func (hub *hub) logAction(sl string) {
304
304
log .SetOutput (os .Stderr )
305
305
// } else if strings.HasPrefix(sl, "log show") {
306
306
// TODO: send all the saved log to websocket
307
- //hub .broadcastSys <- []byte("{\"BufFlowDebug\" : \"" + *logDump + "\"}")
307
+ //h .broadcastSys <- []byte("{\"BufFlowDebug\" : \"" + *logDump + "\"}")
308
308
}
309
309
}
310
310
@@ -313,35 +313,35 @@ func (u *logWriter) Write(p []byte) (n int, err error) {
313
313
return len (p ), nil
314
314
}
315
315
316
- func (hub * hub ) memoryStats () {
316
+ func (h * hub ) memoryStats () {
317
317
var memStats runtime.MemStats
318
318
runtime .ReadMemStats (& memStats )
319
319
json , _ := json .Marshal (memStats )
320
320
log .Printf ("memStats:%v\n " , string (json ))
321
- hub .broadcastSys <- json
321
+ h .broadcastSys <- json
322
322
}
323
323
324
- func (hub * hub ) getHostname () {
325
- hub .broadcastSys <- []byte ("{\" Hostname\" : \" " + * hostname + "\" }" )
324
+ func (h * hub ) getHostname () {
325
+ h .broadcastSys <- []byte ("{\" Hostname\" : \" " + * hostname + "\" }" )
326
326
}
327
327
328
- func (hub * hub ) getVersion () {
329
- hub .broadcastSys <- []byte ("{\" Version\" : \" " + version + "\" }" )
328
+ func (h * hub ) getVersion () {
329
+ h .broadcastSys <- []byte ("{\" Version\" : \" " + version + "\" }" )
330
330
}
331
331
332
- func (hub * hub ) garbageCollection () {
332
+ func (h * hub ) garbageCollection () {
333
333
log .Printf ("Starting garbageCollection()\n " )
334
- hub .broadcastSys <- []byte ("{\" gc\" :\" starting\" }" )
335
- hub .memoryStats ()
334
+ h .broadcastSys <- []byte ("{\" gc\" :\" starting\" }" )
335
+ h .memoryStats ()
336
336
debug .SetGCPercent (100 )
337
337
debug .FreeOSMemory ()
338
338
debug .SetGCPercent (- 1 )
339
339
log .Printf ("Done with garbageCollection()\n " )
340
- hub .broadcastSys <- []byte ("{\" gc\" :\" done\" }" )
341
- hub .memoryStats ()
340
+ h .broadcastSys <- []byte ("{\" gc\" :\" done\" }" )
341
+ h .memoryStats ()
342
342
}
343
343
344
- func (hub * hub ) spHandlerOpen (portname string , baud int , buftype string ) {
344
+ func (h * hub ) spHandlerOpen (portname string , baud int , buftype string ) {
345
345
346
346
log .Print ("Inside spHandler" )
347
347
@@ -365,8 +365,8 @@ func (hub *hub) spHandlerOpen(portname string, baud int, buftype string) {
365
365
if err != nil {
366
366
//log.Fatal(err)
367
367
log .Print ("Error opening port " + err .Error ())
368
- //hub .broadcastSys <- []byte("Error opening port. " + err.Error())
369
- hub .broadcastSys <- []byte ("{\" Cmd\" :\" OpenFail\" ,\" Desc\" :\" Error opening port. " + err .Error () + "\" ,\" Port\" :\" " + conf .Name + "\" ,\" Baud\" :" + strconv .Itoa (conf .Baud ) + "}" )
368
+ //h .broadcastSys <- []byte("Error opening port. " + err.Error())
369
+ h .broadcastSys <- []byte ("{\" Cmd\" :\" OpenFail\" ,\" Desc\" :\" Error opening port. " + err .Error () + "\" ,\" Port\" :\" " + conf .Name + "\" ,\" Baud\" :" + strconv .Itoa (conf .Baud ) + "}" )
370
370
371
371
return
372
372
}
@@ -384,34 +384,34 @@ func (hub *hub) spHandlerOpen(portname string, baud int, buftype string) {
384
384
}
385
385
386
386
p .OnMessage = func (msg []byte ) {
387
- hub .broadcastSys <- msg
387
+ h .broadcastSys <- msg
388
388
}
389
389
p .OnClose = func (port * serport ) {
390
- hub .serialPortList .MarkPortAsClosed (p .portName )
391
- hub .serialPortList .List ()
390
+ h .serialPortList .MarkPortAsClosed (p .portName )
391
+ h .serialPortList .List ()
392
392
}
393
393
394
394
var bw Bufferflow
395
395
396
396
switch buftype {
397
397
case "timed" :
398
- bw = NewBufferflowTimed (portname , hub .broadcastSys )
398
+ bw = NewBufferflowTimed (portname , h .broadcastSys )
399
399
case "timedraw" :
400
- bw = NewBufferflowTimedRaw (portname , hub .broadcastSys )
400
+ bw = NewBufferflowTimedRaw (portname , h .broadcastSys )
401
401
case "default" :
402
- bw = NewBufferflowDefault (portname , hub .broadcastSys )
402
+ bw = NewBufferflowDefault (portname , h .broadcastSys )
403
403
default :
404
404
log .Panicf ("unknown buffer type: %s" , buftype )
405
405
}
406
406
407
407
bw .Init ()
408
408
p .bufferwatcher = bw
409
409
410
- hub .serialHub .Register (p )
411
- defer hub .serialHub .Unregister (p )
410
+ h .serialHub .Register (p )
411
+ defer h .serialHub .Unregister (p )
412
412
413
- hub .serialPortList .MarkPortAsOpened (portname )
414
- hub .serialPortList .List ()
413
+ h .serialPortList .MarkPortAsOpened (portname )
414
+ h .serialPortList .List ()
415
415
416
416
// this is internally buffered thread to not send to serial port if blocked
417
417
go p .writerBuffered ()
@@ -422,19 +422,23 @@ func (hub *hub) spHandlerOpen(portname string, baud int, buftype string) {
422
422
423
423
p .reader (buftype )
424
424
425
- hub .serialPortList .List ()
425
+ h .serialPortList .List ()
426
426
}
427
427
428
- func (hub * hub ) spClose (portname string ) {
429
- if myport , ok := hub .serialHub .FindPortByName (portname ); ok {
430
- hub .broadcastSys <- []byte ("Closing serial port " + portname )
428
+ func (h * hub ) spErr (err string ) {
429
+ h .broadcastSys <- []byte ("{\" Error\" : \" " + err + "\" }" )
430
+ }
431
+
432
+ func (h * hub ) spClose (portname string ) {
433
+ if myport , ok := h .serialHub .FindPortByName (portname ); ok {
434
+ h .broadcastSys <- []byte ("Closing serial port " + portname )
431
435
myport .Close ()
432
436
} else {
433
- hub .spErr ("We could not find the serial port " + portname + " that you were trying to close." )
437
+ h .spErr ("We could not find the serial port " + portname + " that you were trying to close." )
434
438
}
435
439
}
436
440
437
- func (hub * hub ) spWrite (arg string ) {
441
+ func (h * hub ) spWrite (arg string ) {
438
442
// we will get a string of comXX asdf asdf asdf
439
443
//log.Println("Inside spWrite arg: " + arg)
440
444
arg = strings .TrimPrefix (arg , " " )
@@ -443,7 +447,7 @@ func (hub *hub) spWrite(arg string) {
443
447
if len (args ) != 3 {
444
448
errstr := "Could not parse send command: " + arg
445
449
//log.Println(errstr)
446
- hub .spErr (errstr )
450
+ h .spErr (errstr )
447
451
return
448
452
}
449
453
bufferingMode := args [0 ]
@@ -454,10 +458,10 @@ func (hub *hub) spWrite(arg string) {
454
458
//log.Println("The data is:" + data + "---")
455
459
456
460
// See if we have this port open
457
- port , ok := hub .serialHub .FindPortByName (portname )
461
+ port , ok := h .serialHub .FindPortByName (portname )
458
462
if ! ok {
459
463
// we couldn't find the port, so send err
460
- hub .spErr ("We could not find the serial port " + portname + " that you were trying to write to." )
464
+ h .spErr ("We could not find the serial port " + portname + " that you were trying to write to." )
461
465
return
462
466
}
463
467
@@ -466,15 +470,10 @@ func (hub *hub) spWrite(arg string) {
466
470
case "send" , "sendnobuf" , "sendraw" :
467
471
// valid buffering mode, go ahead
468
472
default :
469
- hub .spErr ("Unsupported send command:" + args [0 ] + ". Please specify a valid one" )
473
+ h .spErr ("Unsupported send command:" + args [0 ] + ". Please specify a valid one" )
470
474
return
471
475
}
472
476
473
477
// send it to the write channel
474
478
port .Write (data , bufferingMode )
475
479
}
476
-
477
- func (hub * hub ) spErr (err string ) {
478
- //log.Println("Sending err back: ", err)
479
- hub .broadcastSys <- []byte ("{\" Error\" : \" " + err + "\" }" )
480
- }
0 commit comments