Skip to content

Another round of code clean-up #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ func uploadHandler(c *gin.Context) {
return
}

var filePaths []string
filePaths = append(filePaths, filePath)

tmpdir, err := os.MkdirTemp("", "extrafiles")
if err != nil {
c.String(http.StatusBadRequest, err.Error())
Expand All @@ -139,7 +136,6 @@ func uploadHandler(c *gin.Context) {
c.String(http.StatusBadRequest, err.Error())
return
}
filePaths = append(filePaths, path)
log.Printf("Saving %s on %s", extraFile.Filename, path)

err = os.MkdirAll(filepath.Dir(path), 0744)
Expand Down
29 changes: 4 additions & 25 deletions serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,6 @@ func (sh *serialhub) Unregister(port *serport) {
sh.mu.Unlock()
}

// Write data to the serial port.
func (sh *serialhub) Write(port *serport, data string, sendMode string) {
// if user sent in the commands as one text mode line
switch sendMode {
case "send":
port.sendBuffered <- data
case "sendnobuf":
port.sendNoBuf <- []byte(data)
case "sendraw":
port.sendRaw <- data
}
}

func (sh *serialhub) FindPortByName(portname string) (*serport, bool) {
sh.mu.Lock()
defer sh.mu.Unlock()
Expand Down Expand Up @@ -275,18 +262,10 @@ func spErr(err string) {
}

func spClose(portname string) {
// look up the registered port by name
// then call the close method inside serialport
// that should cause an unregister channel call back
// to myself

myport, isFound := sh.FindPortByName(portname)

if isFound {
// we found our port
spHandlerClose(myport)
if myport, ok := sh.FindPortByName(portname); ok {
h.broadcastSys <- []byte("Closing serial port " + portname)
myport.Close()
} else {
// we couldn't find the port, so send err
spErr("We could not find the serial port " + portname + " that you were trying to close.")
}
}
Expand Down Expand Up @@ -328,5 +307,5 @@ func spWrite(arg string) {
}

// send it to the write channel
sh.Write(port, data, bufferingMode)
port.Write(data, bufferingMode)
}
41 changes: 20 additions & 21 deletions serialport.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ type serport struct {

isClosingDueToError bool

// counter incremented on queue, decremented on write
itemsInBuffer int

// buffered channel containing up to 25600 outbound messages.
sendBuffered chan string

Expand Down Expand Up @@ -161,18 +158,29 @@ func (p *serport) reader(buftype string) {
// Keep track of time difference between two consecutive read with n == 0 and err == nil
// we get here if the port has been disconnected while open (cpu usage will jump to 100%)
// let's close the port only if the events are extremely fast (<1ms)
if err == nil {
diff := time.Since(timeCheckOpen)
if diff.Nanoseconds() < 1000000 {
p.isClosingDueToError = true
break
}
timeCheckOpen = time.Now()
diff := time.Since(timeCheckOpen)
if diff.Nanoseconds() < 1000000 {
p.isClosingDueToError = true
break
}
timeCheckOpen = time.Now()
}
}
if p.isClosingDueToError {
spCloseReal(p)
p.Close()
}
}

// Write data to the serial port.
func (p *serport) Write(data string, sendMode string) {
// if user sent in the commands as one text mode line
switch sendMode {
case "send":
p.sendBuffered <- data
case "sendnobuf":
p.sendNoBuf <- []byte(data)
case "sendraw":
p.sendRaw <- data
}
}

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

// decrement counter
p.itemsInBuffer--
log.Printf("itemsInBuffer:%v\n", p.itemsInBuffer)

// FINALLY, OF ALL THE CODE IN THIS PROJECT
// WE TRULY/FINALLY GET TO WRITE TO THE SERIAL PORT!
n2, err := p.portIo.Write(data)
Expand Down Expand Up @@ -343,13 +347,8 @@ func spHandlerOpen(portname string, baud int, buftype string) {
serialPorts.List()
}

func spHandlerClose(p *serport) {
func (p *serport) Close() {
p.isClosing = true
h.broadcastSys <- []byte("Closing serial port " + p.portConf.Name)
spCloseReal(p)
}

func spCloseReal(p *serport) {
p.bufferwatcher.Close()
p.portIo.Close()
serialPorts.MarkPortAsClosed(p.portName)
Expand Down
Loading