Repository navigation
Populating Devices
Ohlin Arellano edited this page May 25, 2025
·
1 revision
You can populate your device list in two ways:
- Discovery – find devices on the local network in real-time
- Loading from JSON – load previously discovered devices from a file
Use the Discover() function to scan your network for LIFX devices.
package main
import (
"fmt"
"time"
"github.com/murphy28/lifxlan"
)
func main() {
lifx, err := lifxlan.NewClient()
if err != nil {
fmt.Println("Error creating LIFX client:", err)
return
}
defer lifx.Close()
err = lifx.Discover(5 * time.Second)
if err != nil {
fmt.Println("Error discovering LIFX devices:", err)
return
}
devices := lifx.GetDevices()
if len(devices) == 0 {
fmt.Println("No LIFX devices found.")
return
}
fmt.Printf("Found %d LIFX devices:\n", len(devices))
for _, device := range devices {
label, err := device.GetLabel()
if err != nil {
fmt.Println("Error getting device label:", err)
continue
}
fmt.Println(label)
}
}Example Output:
Discovered device: d073d5xxxxxx at 192.168.1.10
Discovered device: d073d5yyyyyy at 192.168.1.11
Discovered device: d073d5zzzzzz at 192.168.1.12
Found 3 LIFX devices:
Living Room
Bedroom
Desk Lamp
To skip discovery next time, you can export devices to a devices.json file:
// Export devices to a file
err = os.WriteFile("devices.json", []byte(lifx.ExportJSON()), 0644)
if err != nil {
fmt.Println("Error writing devices to file:", err)
return
}This JSON includes:
- MAC address
- IP address
- Label
- Product information
Once you’ve exported device info, you can load it later without running discovery:
package main
import (
"fmt"
"os"
"github.com/murphy28/lifxlan"
)
func main() {
lifx, err := lifxlan.NewClient()
if err != nil {
fmt.Println("Error creating LIFX client:", err)
return
}
defer lifx.Close()
content, err := os.ReadFile("devices.json")
if err != nil {
fmt.Println("Error reading devices.json:", err)
return
}
err = lifx.LoadDevices(string(content))
if err != nil {
fmt.Println("Error loading devices:", err)
return
}
devices := lifx.GetDevices()
if len(devices) == 0 {
fmt.Println("No LIFX devices found.")
return
}
fmt.Printf("Found %d LIFX devices:\n", len(devices))
for _, device := range devices {
label, err := device.GetLabel()
if err != nil {
fmt.Println("Error getting device label:", err)
continue
}
fmt.Println(label)
}
}Example Output:
Loaded device: d073d5aaaaaa at 192.168.1.10
Loaded device: d073d5bbbbbb at 192.168.1.11
Loaded device: d073d5cccccc at 192.168.1.12
Found 3 LIFX devices:
Living Room
Bedroom
Desk Lamp
When loaded, devices are automatically pinged and refreshed to ensure they're still reachable.