mac2mqtt icon indicating copy to clipboard operation
mac2mqtt copied to clipboard

Return empty string if no battery is present on the Mac

Open phillipsnick opened this issue 2 years ago • 1 comments

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'

phillipsnick avatar Jan 10 '23 20:01 phillipsnick

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.

callumgare avatar Apr 07 '23 05:04 callumgare