Skip to content

Commit 0370591

Browse files
committed
refactor: move spErr, spWrite, and spClose functions from serial.go to hub.go for better organization
1 parent fd3b2d7 commit 0370591

File tree

2 files changed

+54
-58
lines changed

2 files changed

+54
-58
lines changed

hub.go

-58
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,6 @@ func (h *hub) logAction(sl string) {
307307
}
308308
}
309309

310-
func (u *logWriter) Write(p []byte) (n int, err error) {
311-
u.onWrite(p)
312-
return len(p), nil
313-
}
314-
315310
func (h *hub) memoryStats() {
316311
var memStats runtime.MemStats
317312
runtime.ReadMemStats(&memStats)
@@ -339,56 +334,3 @@ func (h *hub) garbageCollection() {
339334
h.broadcastSys <- []byte("{\"gc\":\"done\"}")
340335
h.memoryStats()
341336
}
342-
343-
func (h *hub) spErr(err string) {
344-
h.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}")
345-
}
346-
347-
func (h *hub) spClose(portname string) {
348-
if myport, ok := h.serialHub.FindPortByName(portname); ok {
349-
h.broadcastSys <- []byte("Closing serial port " + portname)
350-
myport.Close()
351-
} else {
352-
h.spErr("We could not find the serial port " + portname + " that you were trying to close.")
353-
}
354-
}
355-
356-
func (h *hub) spWrite(arg string) {
357-
// we will get a string of comXX asdf asdf asdf
358-
//log.Println("Inside spWrite arg: " + arg)
359-
arg = strings.TrimPrefix(arg, " ")
360-
//log.Println("arg after trim: " + arg)
361-
args := strings.SplitN(arg, " ", 3)
362-
if len(args) != 3 {
363-
errstr := "Could not parse send command: " + arg
364-
//log.Println(errstr)
365-
h.spErr(errstr)
366-
return
367-
}
368-
bufferingMode := args[0]
369-
portname := strings.Trim(args[1], " ")
370-
data := args[2]
371-
372-
//log.Println("The port to write to is:" + portname + "---")
373-
//log.Println("The data is:" + data + "---")
374-
375-
// See if we have this port open
376-
port, ok := h.serialHub.FindPortByName(portname)
377-
if !ok {
378-
// we couldn't find the port, so send err
379-
h.spErr("We could not find the serial port " + portname + " that you were trying to write to.")
380-
return
381-
}
382-
383-
// see if bufferingMode is valid
384-
switch bufferingMode {
385-
case "send", "sendnobuf", "sendraw":
386-
// valid buffering mode, go ahead
387-
default:
388-
h.spErr("Unsupported send command:" + args[0] + ". Please specify a valid one")
389-
return
390-
}
391-
392-
// send it to the write channel
393-
port.Write(data, bufferingMode)
394-
}

serial.go

+54
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,57 @@ func (sp *SerialPortList) getPortByName(portname string) *SpPortItem {
266266
}
267267
return nil
268268
}
269+
270+
// FIXME: the spErr, spWrite, spClose should be moved to the 'hub.go' file
271+
func (h *hub) spErr(err string) {
272+
h.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}")
273+
}
274+
275+
func (h *hub) spClose(portname string) {
276+
if myport, ok := h.serialHub.FindPortByName(portname); ok {
277+
h.broadcastSys <- []byte("Closing serial port " + portname)
278+
myport.Close()
279+
} else {
280+
h.spErr("We could not find the serial port " + portname + " that you were trying to close.")
281+
}
282+
}
283+
284+
func (h *hub) spWrite(arg string) {
285+
// we will get a string of comXX asdf asdf asdf
286+
//log.Println("Inside spWrite arg: " + arg)
287+
arg = strings.TrimPrefix(arg, " ")
288+
//log.Println("arg after trim: " + arg)
289+
args := strings.SplitN(arg, " ", 3)
290+
if len(args) != 3 {
291+
errstr := "Could not parse send command: " + arg
292+
//log.Println(errstr)
293+
h.spErr(errstr)
294+
return
295+
}
296+
bufferingMode := args[0]
297+
portname := strings.Trim(args[1], " ")
298+
data := args[2]
299+
300+
//log.Println("The port to write to is:" + portname + "---")
301+
//log.Println("The data is:" + data + "---")
302+
303+
// See if we have this port open
304+
port, ok := h.serialHub.FindPortByName(portname)
305+
if !ok {
306+
// we couldn't find the port, so send err
307+
h.spErr("We could not find the serial port " + portname + " that you were trying to write to.")
308+
return
309+
}
310+
311+
// see if bufferingMode is valid
312+
switch bufferingMode {
313+
case "send", "sendnobuf", "sendraw":
314+
// valid buffering mode, go ahead
315+
default:
316+
h.spErr("Unsupported send command:" + args[0] + ". Please specify a valid one")
317+
return
318+
}
319+
320+
// send it to the write channel
321+
port.Write(data, bufferingMode)
322+
}

0 commit comments

Comments
 (0)