Skip to content

Commit a152b02

Browse files
committed
feat: move spErr spClose spWrite into hub (from serialhub)
1 parent 7ea21c0 commit a152b02

File tree

2 files changed

+54
-55
lines changed

2 files changed

+54
-55
lines changed

hub.go

+54
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,57 @@ func (h *hub) garbageCollection() {
336336
h.broadcastSys <- []byte("{\"gc\":\"done\"}")
337337
h.memoryStats()
338338
}
339+
340+
func (h *hub) spErr(err string) {
341+
//log.Println("Sending err back: ", err)
342+
h.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}")
343+
}
344+
345+
func (h *hub) spClose(portname string) {
346+
if myport, ok := h.serialHub.FindPortByName(portname); ok {
347+
h.broadcastSys <- []byte("Closing serial port " + portname)
348+
myport.Close()
349+
} else {
350+
h.spErr("We could not find the serial port " + portname + " that you were trying to close.")
351+
}
352+
}
353+
354+
func (h *hub) spWrite(arg string) {
355+
// we will get a string of comXX asdf asdf asdf
356+
//log.Println("Inside spWrite arg: " + arg)
357+
arg = strings.TrimPrefix(arg, " ")
358+
//log.Println("arg after trim: " + arg)
359+
args := strings.SplitN(arg, " ", 3)
360+
if len(args) != 3 {
361+
errstr := "Could not parse send command: " + arg
362+
//log.Println(errstr)
363+
h.spErr(errstr)
364+
return
365+
}
366+
bufferingMode := args[0]
367+
portname := strings.Trim(args[1], " ")
368+
data := args[2]
369+
370+
//log.Println("The port to write to is:" + portname + "---")
371+
//log.Println("The data is:" + data + "---")
372+
373+
// See if we have this port open
374+
port, ok := h.serialHub.FindPortByName(portname)
375+
if !ok {
376+
// we couldn't find the port, so send err
377+
h.spErr("We could not find the serial port " + portname + " that you were trying to write to.")
378+
return
379+
}
380+
381+
// see if bufferingMode is valid
382+
switch bufferingMode {
383+
case "send", "sendnobuf", "sendraw":
384+
// valid buffering mode, go ahead
385+
default:
386+
h.spErr("Unsupported send command:" + args[0] + ". Please specify a valid one")
387+
return
388+
}
389+
390+
// send it to the write channel
391+
port.Write(data, bufferingMode)
392+
}

serialhub.go

-55
Original file line numberDiff line numberDiff line change
@@ -70,58 +70,3 @@ func (sh *serialhub) FindPortByName(portname string) (*serport, bool) {
7070
}
7171
return nil, false
7272
}
73-
74-
func (h *hub) spErr(err string) {
75-
//log.Println("Sending err back: ", err)
76-
//sh.hub.broadcastSys <- []byte(err)
77-
h.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}")
78-
}
79-
80-
func (h *hub) spClose(portname string) {
81-
if myport, ok := h.serialHub.FindPortByName(portname); ok {
82-
h.broadcastSys <- []byte("Closing serial port " + portname)
83-
myport.Close()
84-
} else {
85-
h.spErr("We could not find the serial port " + portname + " that you were trying to close.")
86-
}
87-
}
88-
89-
func (h *hub) spWrite(arg string) {
90-
// we will get a string of comXX asdf asdf asdf
91-
//log.Println("Inside spWrite arg: " + arg)
92-
arg = strings.TrimPrefix(arg, " ")
93-
//log.Println("arg after trim: " + arg)
94-
args := strings.SplitN(arg, " ", 3)
95-
if len(args) != 3 {
96-
errstr := "Could not parse send command: " + arg
97-
//log.Println(errstr)
98-
h.spErr(errstr)
99-
return
100-
}
101-
bufferingMode := args[0]
102-
portname := strings.Trim(args[1], " ")
103-
data := args[2]
104-
105-
//log.Println("The port to write to is:" + portname + "---")
106-
//log.Println("The data is:" + data + "---")
107-
108-
// See if we have this port open
109-
port, ok := h.serialHub.FindPortByName(portname)
110-
if !ok {
111-
// we couldn't find the port, so send err
112-
h.spErr("We could not find the serial port " + portname + " that you were trying to write to.")
113-
return
114-
}
115-
116-
// see if bufferingMode is valid
117-
switch bufferingMode {
118-
case "send", "sendnobuf", "sendraw":
119-
// valid buffering mode, go ahead
120-
default:
121-
h.spErr("Unsupported send command:" + args[0] + ". Please specify a valid one")
122-
return
123-
}
124-
125-
// send it to the write channel
126-
port.Write(data, bufferingMode)
127-
}

0 commit comments

Comments
 (0)