Skip to content
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

Return empty string if no battery is present on the Mac #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

phillipsnick
Copy link

@phillipsnick phillipsnick commented Jan 10, 2023

Return empty string if no battery is present on the Mac (as is the case on the Mac Pro)

Resulting topics on MQTT

Screenshot 2023-01-10 at 20 11 30

Resolves this issue

./mac2mqtt
2023/01/10 19:29:46 Started
2023/01/10 19:29:46 Connected to MQTT
2023/01/10 19:29:46 Sending 'true' to topic: mac2mqtt/Nicks-Mac-Pro/status/alive
cf
panic: runtime error: index out of range [1] with length 0

goroutine 29 [running]:
main.getBatteryChargePercent(0xc000207ef0, 0x131a642)
	/Users/nick/mac2mqtt/mac2mqtt.go:284 +0x115
main.updateBattery(0x137df60, 0xc0001426c0)
	/Users/nick/mac2mqtt/mac2mqtt.go:290 +0x85
main.main.func1(0xc0001a4000, 0x137df60, 0xc0001426c0, 0xc0001a4050)
	/Users/nick/mac2mqtt/mac2mqtt.go:318 +0xe5
created by main.main
	/Users/nick/mac2mqtt/mac2mqtt.go:310 +0x1d0

Because the output of pmset is:

/usr/bin/pmset -g batt
Now drawing from 'AC Power'

@callumgare
Copy link

I can confirm this works. Without this change, when I try to build and run the mac2mqtt binary on my Mac Studio I get the "index out of range" error as reported and immediately crashes. But this change mac2mqtt starts up just fine and functions correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants