Skip to content

Commit a462190

Browse files
authored
Another round of code clean-up (#940)
* Removed unused variable * Removed always-true condition * Make Write a method of serport * Removed useless counter itemsInBuffer * Made spCloseRead a method of serialport * Inlined spHandlerClose
1 parent c42fcd7 commit a462190

File tree

3 files changed

+24
-50
lines changed

3 files changed

+24
-50
lines changed

Diff for: conn.go

-4
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ func uploadHandler(c *gin.Context) {
124124
return
125125
}
126126

127-
var filePaths []string
128-
filePaths = append(filePaths, filePath)
129-
130127
tmpdir, err := os.MkdirTemp("", "extrafiles")
131128
if err != nil {
132129
c.String(http.StatusBadRequest, err.Error())
@@ -139,7 +136,6 @@ func uploadHandler(c *gin.Context) {
139136
c.String(http.StatusBadRequest, err.Error())
140137
return
141138
}
142-
filePaths = append(filePaths, path)
143139
log.Printf("Saving %s on %s", extraFile.Filename, path)
144140

145141
err = os.MkdirAll(filepath.Dir(path), 0744)

Diff for: serial.go

+4-25
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,6 @@ func (sh *serialhub) Unregister(port *serport) {
8383
sh.mu.Unlock()
8484
}
8585

86-
// Write data to the serial port.
87-
func (sh *serialhub) Write(port *serport, data string, sendMode string) {
88-
// if user sent in the commands as one text mode line
89-
switch sendMode {
90-
case "send":
91-
port.sendBuffered <- data
92-
case "sendnobuf":
93-
port.sendNoBuf <- []byte(data)
94-
case "sendraw":
95-
port.sendRaw <- data
96-
}
97-
}
98-
9986
func (sh *serialhub) FindPortByName(portname string) (*serport, bool) {
10087
sh.mu.Lock()
10188
defer sh.mu.Unlock()
@@ -275,18 +262,10 @@ func spErr(err string) {
275262
}
276263

277264
func spClose(portname string) {
278-
// look up the registered port by name
279-
// then call the close method inside serialport
280-
// that should cause an unregister channel call back
281-
// to myself
282-
283-
myport, isFound := sh.FindPortByName(portname)
284-
285-
if isFound {
286-
// we found our port
287-
spHandlerClose(myport)
265+
if myport, ok := sh.FindPortByName(portname); ok {
266+
h.broadcastSys <- []byte("Closing serial port " + portname)
267+
myport.Close()
288268
} else {
289-
// we couldn't find the port, so send err
290269
spErr("We could not find the serial port " + portname + " that you were trying to close.")
291270
}
292271
}
@@ -328,5 +307,5 @@ func spWrite(arg string) {
328307
}
329308

330309
// send it to the write channel
331-
sh.Write(port, data, bufferingMode)
310+
port.Write(data, bufferingMode)
332311
}

Diff for: serialport.go

+20-21
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ type serport struct {
4747

4848
isClosingDueToError bool
4949

50-
// counter incremented on queue, decremented on write
51-
itemsInBuffer int
52-
5350
// buffered channel containing up to 25600 outbound messages.
5451
sendBuffered chan string
5552

@@ -161,18 +158,29 @@ func (p *serport) reader(buftype string) {
161158
// Keep track of time difference between two consecutive read with n == 0 and err == nil
162159
// we get here if the port has been disconnected while open (cpu usage will jump to 100%)
163160
// let's close the port only if the events are extremely fast (<1ms)
164-
if err == nil {
165-
diff := time.Since(timeCheckOpen)
166-
if diff.Nanoseconds() < 1000000 {
167-
p.isClosingDueToError = true
168-
break
169-
}
170-
timeCheckOpen = time.Now()
161+
diff := time.Since(timeCheckOpen)
162+
if diff.Nanoseconds() < 1000000 {
163+
p.isClosingDueToError = true
164+
break
171165
}
166+
timeCheckOpen = time.Now()
172167
}
173168
}
174169
if p.isClosingDueToError {
175-
spCloseReal(p)
170+
p.Close()
171+
}
172+
}
173+
174+
// Write data to the serial port.
175+
func (p *serport) Write(data string, sendMode string) {
176+
// if user sent in the commands as one text mode line
177+
switch sendMode {
178+
case "send":
179+
p.sendBuffered <- data
180+
case "sendnobuf":
181+
p.sendNoBuf <- []byte(data)
182+
case "sendraw":
183+
p.sendRaw <- data
176184
}
177185
}
178186

@@ -213,10 +221,6 @@ func (p *serport) writerNoBuf() {
213221
// if we get here, we were able to write successfully
214222
// to the serial port because it blocks until it can write
215223

216-
// decrement counter
217-
p.itemsInBuffer--
218-
log.Printf("itemsInBuffer:%v\n", p.itemsInBuffer)
219-
220224
// FINALLY, OF ALL THE CODE IN THIS PROJECT
221225
// WE TRULY/FINALLY GET TO WRITE TO THE SERIAL PORT!
222226
n2, err := p.portIo.Write(data)
@@ -343,13 +347,8 @@ func spHandlerOpen(portname string, baud int, buftype string) {
343347
serialPorts.List()
344348
}
345349

346-
func spHandlerClose(p *serport) {
350+
func (p *serport) Close() {
347351
p.isClosing = true
348-
h.broadcastSys <- []byte("Closing serial port " + p.portConf.Name)
349-
spCloseReal(p)
350-
}
351-
352-
func spCloseReal(p *serport) {
353352
p.bufferwatcher.Close()
354353
p.portIo.Close()
355354
serialPorts.MarkPortAsClosed(p.portName)

0 commit comments

Comments
 (0)