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