Skip to content

Remove global clients #1014

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

Open
wants to merge 54 commits into
base: main
Choose a base branch
from
Open

Remove global clients #1014

wants to merge 54 commits into from

Conversation

dido18
Copy link
Contributor

@dido18 dido18 commented Jan 27, 2025

Please check if the PR fulfills these requirements

  • The PR has no duplicates (please search among the Pull Requests
    before creating one)
  • Tests for the changes have been added (for bug fixes / features)
  • What kind of change does this PR introduce?
    The purpose of this PR is to improve the maintainability, testability, and clarity of the codebase by removing global variables and explicitly passing dependencies as arguments to functions or methods. Global variables can lead to tightly coupled code, making it harder to understand, test, and maintain.
  • What is the current behavior?
  • What is the new behavior?
  1. remove global variables, they are now created in the main function and passed explicitly to the components or functions that need them.

    • Tools *tools.Tools
    • Systray systray.Systray
    • Index *index.Resource
    • h = hub
    • loggerWs logWriter
    • serialPorts SerialPortList
    • sh = serialhub
  2. move the go hub.serialPortList.Run() from the maingo into the hub.run() because the hub is in charge of connecting serial events to websocket clients.

  • Does this PR introduce a breaking change?
    No.
  • Other information:

@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels Jan 29, 2025
@dido18 dido18 changed the base branch from main to datarace-on-config-access January 29, 2025 10:34
@dido18 dido18 changed the base branch from datarace-on-config-access to main March 31, 2025 14:05
@codecov-commenter
Copy link

codecov-commenter commented Mar 31, 2025

Codecov Report

Attention: Patch coverage is 18.87755% with 159 lines in your changes missing coverage. Please review.

Project coverage is 20.81%. Comparing base (e7ea376) to head (852f4eb).

Files with missing lines Patch % Lines
hub.go 26.08% 47 Missing and 4 partials ⚠️
serialport.go 0.00% 28 Missing ⚠️
main.go 0.00% 23 Missing ⚠️
serial.go 34.28% 23 Missing ⚠️
update.go 0.00% 13 Missing ⚠️
info.go 0.00% 10 Missing ⚠️
conn.go 10.00% 9 Missing ⚠️
tools/download.go 71.42% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1014      +/-   ##
==========================================
+ Coverage   20.24%   20.81%   +0.57%     
==========================================
  Files          42       42              
  Lines        3241     3291      +50     
==========================================
+ Hits          656      685      +29     
- Misses       2498     2515      +17     
- Partials       87       91       +4     
Flag Coverage Δ
unit 20.81% <18.87%> (+0.57%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dido18 dido18 marked this pull request as ready for review April 1, 2025 13:45
@dido18 dido18 requested a review from lucarin91 April 3, 2025 08:37
@dido18
Copy link
Contributor Author

dido18 commented Apr 3, 2025

Tested on linux with MKR 1010

  • upload the echo-skecth and test that the serial monitor works as expected
  • provision an IOT device + update NINA
    • downgrade the NINA with ~/.arduino-create/arduino/arduino-fwuploader/2.4.1/arduino-fwuploader firmware flash -b arduino:samd:mkrwifi1010 -a /dev/ttyACM0 --module [email protected]
    • follow the create a device with iot provisioning

hub.go Outdated
Comment on lines 389 to 392
p.OnClose = func(port *serport) {
hub.serialPortList.MarkPortAsClosed(p.portName)
hub.serialPortList.List()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove the OnClose callback and move those two lines of code in the spClose method below after calling p.Close(). AFAIK this struct is the only one closing ports, so moving the code there should cover all the cases. The following should be the updated spClose function:

func (hub *hub) spClose(portname string) {
	if myport, ok := hub.serialHub.FindPortByName(portname); ok {
		hub.broadcastSys <- []byte("Closing serial port " + portname)
		myport.Close()
		hub.serialPortList.MarkPortAsClosed(myport.portName)
		hub.serialPortList.List()
	} else {
		hub.spErr("We could not find the serial port " + portname + " that you were trying to close.")
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhm I think it is also called here

@dido18 dido18 requested review from lucarin91 and cmaglie April 17, 2025 10:21
@dido18
Copy link
Contributor Author

dido18 commented Apr 18, 2025

Tested again like #1014 (comment)

@dido18
Copy link
Contributor Author

dido18 commented Apr 18, 2025

Test: It can download a tool.

This hte was logs

downloadtool avrdude 6.0.1-arduino5

{
  "DownloadStatus": "Pending",
  "Msg": "Ensure that the files are executable"
}

{
  "DownloadStatus": "Pending",
  "Msg": "Updating map with location /home/dido/.arduino-create/arduino/avrdude/6.0.1-arduino5"
}

{
  "DownloadStatus": "Success",
  "Msg": "Map Updated"
}

@dido18 dido18 requested a review from alessio-perugini April 18, 2025 13:04
Copy link
Contributor

@lucarin91 lucarin91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would try to reduce callbacks if possible. They are usually difficult to follow.

hub.go Outdated
Comment on lines 64 to 78
onRegister := func(port *serport) {
broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}")
}
onUnregister := func(port *serport) {
broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}")
}
serialHubub := newSerialHub(onRegister, onUnregister)

onList := func(data []byte) {
broadcastSys <- data
}
onErr := func(err string) {
broadcastSys <- []byte("{\"Error\":\"" + err + "\"}")
}
serialPortList := newSerialPortList(tools, onList, onErr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would simplify both of this two with the ChanWriter

Suggested change
onRegister := func(port *serport) {
broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}")
}
onUnregister := func(port *serport) {
broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}")
}
serialHubub := newSerialHub(onRegister, onUnregister)
onList := func(data []byte) {
broadcastSys <- data
}
onErr := func(err string) {
broadcastSys <- []byte("{\"Error\":\"" + err + "\"}")
}
serialPortList := newSerialPortList(tools, onList, onErr)
serialHubub := newSerialHub(ChanWriter{Ch:broadcastSys})
serialPortList := newSerialPortList(tools, ChanWriter{Ch:broadcastSys})

Comment on lines +56 to +57
OnList func([]byte) `json:"-"`
OnErr func(string) `json:"-"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
OnList func([]byte) `json:"-"`
OnErr func(string) `json:"-"`
writer io.Writer `json:"-"`

serial.go Outdated
// Register serial ports from the connections.
func (sh *serialhub) Register(port *serport) {
sh.mu.Lock()
//log.Print("Registering a port: ", p.portConf.Name)
h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}")
sh.onRegister(port)
Copy link
Contributor

@lucarin91 lucarin91 Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sh.onRegister(port)
fmt.Fprintf(sh.writer, `{"Cmd":"Open","Desc":"Got register/open on port.","Port":%q,"Baud":%d,"BufferType":%q}`, port.portConf.Name , port.portConf.Baud, port.BufferType)

sh.ports[port] = true
sh.mu.Unlock()
}

// Unregister requests from connections.
func (sh *serialhub) Unregister(port *serport) {
sh.mu.Lock()
//log.Print("Unregistering a port: ", p.portConf.Name)
h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}")
sh.onUnregister(port)
Copy link
Contributor

@lucarin91 lucarin91 Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sh.onUnregister(port)
fmt.Fprintf(sh.writer, `{"Cmd":"Close","Desc":"Got unregister/close on port.","Port":%q,"Baud":%d}`, port.portConf.Name, port.portConf.Baud)

Comment on lines +108 to +109
OnList: onList,
OnErr: onErr,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
OnList: onList,
OnErr: onErr,
writer: io.Writer,

//log.Println(err)
h.broadcastSys <- []byte("Error creating json on port list " +
err.Error())
sp.OnErr("Error creating json on port list " + err.Error())
Copy link
Contributor

@lucarin91 lucarin91 Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sp.OnErr("Error creating json on port list " + err.Error())
fmt.Fprintf(sp.writer,"Error creating json on port list %s", err.Error())

} else {
h.broadcastSys <- ls
sp.OnList(ls)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sp.OnList(ls)
sp.Write(ls)

@lucarin91 lucarin91 dismissed their stale review April 18, 2025 14:42

I added some comments but I don't want to block the PR

@cmaglie
Copy link
Member

cmaglie commented Apr 24, 2025

I've merged #1032 here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants