Skip to content

Commit 062bca0

Browse files
committed
refactor: move spHandlerOpen function from serialport.go to hub.go for better organization
1 parent 0699e93 commit 062bca0

File tree

2 files changed

+86
-86
lines changed

2 files changed

+86
-86
lines changed

hub.go

-86
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package main
1717

1818
import (
19-
"bytes"
2019
"encoding/json"
2120
"fmt"
2221
"html"
@@ -31,7 +30,6 @@ import (
3130
"github.com/arduino/arduino-create-agent/tools"
3231
"github.com/arduino/arduino-create-agent/upload"
3332
log "github.com/sirupsen/logrus"
34-
"go.bug.st/serial"
3533
)
3634

3735
type hub struct {
@@ -341,90 +339,6 @@ func (h *hub) garbageCollection() {
341339
h.memoryStats()
342340
}
343341

344-
func (h *hub) spHandlerOpen(portname string, baud int, buftype string) {
345-
346-
log.Print("Inside spHandler")
347-
348-
var out bytes.Buffer
349-
350-
out.WriteString("Opening serial port ")
351-
out.WriteString(portname)
352-
out.WriteString(" at ")
353-
out.WriteString(strconv.Itoa(baud))
354-
out.WriteString(" baud")
355-
log.Print(out.String())
356-
357-
conf := &SerialConfig{Name: portname, Baud: baud, RtsOn: true}
358-
359-
mode := &serial.Mode{
360-
BaudRate: baud,
361-
}
362-
363-
sp, err := serial.Open(portname, mode)
364-
log.Print("Just tried to open port")
365-
if err != nil {
366-
//log.Fatal(err)
367-
log.Print("Error opening port " + err.Error())
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-
371-
return
372-
}
373-
log.Print("Opened port successfully")
374-
//p := &serport{send: make(chan []byte, 256), portConf: conf, portIo: sp}
375-
// we can go up to 256,000 lines of gcode in the buffer
376-
p := &serport{
377-
sendBuffered: make(chan string, 256000),
378-
sendNoBuf: make(chan []byte),
379-
sendRaw: make(chan string),
380-
portConf: conf,
381-
portIo: sp,
382-
portName: portname,
383-
BufferType: buftype,
384-
}
385-
386-
p.OnMessage = func(msg []byte) {
387-
h.broadcastSys <- msg
388-
}
389-
p.OnClose = func(port *serport) {
390-
h.serialPortList.MarkPortAsClosed(p.portName)
391-
h.serialPortList.List()
392-
}
393-
394-
var bw Bufferflow
395-
396-
switch buftype {
397-
case "timed":
398-
bw = NewBufferflowTimed(portname, h.broadcastSys)
399-
case "timedraw":
400-
bw = NewBufferflowTimedRaw(portname, h.broadcastSys)
401-
case "default":
402-
bw = NewBufferflowDefault(portname, h.broadcastSys)
403-
default:
404-
log.Panicf("unknown buffer type: %s", buftype)
405-
}
406-
407-
bw.Init()
408-
p.bufferwatcher = bw
409-
410-
h.serialHub.Register(p)
411-
defer h.serialHub.Unregister(p)
412-
413-
h.serialPortList.MarkPortAsOpened(portname)
414-
h.serialPortList.List()
415-
416-
// this is internally buffered thread to not send to serial port if blocked
417-
go p.writerBuffered()
418-
// this is thread to send to serial port regardless of block
419-
go p.writerNoBuf()
420-
// this is thread to send to serial port but with base64 decoding
421-
go p.writerRaw()
422-
423-
p.reader(buftype)
424-
425-
h.serialPortList.List()
426-
}
427-
428342
func (h *hub) spErr(err string) {
429343
h.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}")
430344
}

serialport.go

+86
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"unicode/utf8"
2626

2727
log "github.com/sirupsen/logrus"
28+
"go.bug.st/serial"
2829
)
2930

3031
// SerialConfig is the serial port configuration
@@ -281,6 +282,91 @@ func (p *serport) writerRaw() {
281282
p.OnMessage([]byte(msgstr))
282283
}
283284

285+
// FIXME: move this into the `hub.go` file
286+
func (h *hub) spHandlerOpen(portname string, baud int, buftype string) {
287+
288+
log.Print("Inside spHandler")
289+
290+
var out bytes.Buffer
291+
292+
out.WriteString("Opening serial port ")
293+
out.WriteString(portname)
294+
out.WriteString(" at ")
295+
out.WriteString(strconv.Itoa(baud))
296+
out.WriteString(" baud")
297+
log.Print(out.String())
298+
299+
conf := &SerialConfig{Name: portname, Baud: baud, RtsOn: true}
300+
301+
mode := &serial.Mode{
302+
BaudRate: baud,
303+
}
304+
305+
sp, err := serial.Open(portname, mode)
306+
log.Print("Just tried to open port")
307+
if err != nil {
308+
//log.Fatal(err)
309+
log.Print("Error opening port " + err.Error())
310+
//h.broadcastSys <- []byte("Error opening port. " + err.Error())
311+
h.broadcastSys <- []byte("{\"Cmd\":\"OpenFail\",\"Desc\":\"Error opening port. " + err.Error() + "\",\"Port\":\"" + conf.Name + "\",\"Baud\":" + strconv.Itoa(conf.Baud) + "}")
312+
313+
return
314+
}
315+
log.Print("Opened port successfully")
316+
//p := &serport{send: make(chan []byte, 256), portConf: conf, portIo: sp}
317+
// we can go up to 256,000 lines of gcode in the buffer
318+
p := &serport{
319+
sendBuffered: make(chan string, 256000),
320+
sendNoBuf: make(chan []byte),
321+
sendRaw: make(chan string),
322+
portConf: conf,
323+
portIo: sp,
324+
portName: portname,
325+
BufferType: buftype,
326+
}
327+
328+
p.OnMessage = func(msg []byte) {
329+
h.broadcastSys <- msg
330+
}
331+
p.OnClose = func(port *serport) {
332+
h.serialPortList.MarkPortAsClosed(p.portName)
333+
h.serialPortList.List()
334+
}
335+
336+
var bw Bufferflow
337+
338+
switch buftype {
339+
case "timed":
340+
bw = NewBufferflowTimed(portname, h.broadcastSys)
341+
case "timedraw":
342+
bw = NewBufferflowTimedRaw(portname, h.broadcastSys)
343+
case "default":
344+
bw = NewBufferflowDefault(portname, h.broadcastSys)
345+
default:
346+
log.Panicf("unknown buffer type: %s", buftype)
347+
}
348+
349+
bw.Init()
350+
p.bufferwatcher = bw
351+
352+
h.serialHub.Register(p)
353+
defer h.serialHub.Unregister(p)
354+
355+
h.serialPortList.MarkPortAsOpened(portname)
356+
h.serialPortList.List()
357+
358+
// this is internally buffered thread to not send to serial port if blocked
359+
go p.writerBuffered()
360+
// this is thread to send to serial port regardless of block
361+
go p.writerNoBuf()
362+
// this is thread to send to serial port but with base64 decoding
363+
go p.writerRaw()
364+
365+
p.reader(buftype)
366+
367+
h.serialPortList.List()
368+
}
369+
284370
func (p *serport) Close() {
285371
p.isClosing.Store(true)
286372

0 commit comments

Comments
 (0)