diff --git a/cmd/start.go b/cmd/start.go index b7f19e0..09f63ee 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -10,10 +10,12 @@ import ( "syscall" "github.com/danesparza/appliance-monitor/api" + "github.com/danesparza/appliance-monitor/data" "github.com/danesparza/appliance-monitor/sensordata" "github.com/danesparza/appliance-monitor/zeroconf" "github.com/gorilla/mux" "github.com/rs/cors" + "github.com/rs/xid" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -41,6 +43,32 @@ func start(cmd *cobra.Command, args []string) { log.Println("[INFO] Using config file:", viper.ConfigFileUsed()) } + // Get a reference to the config database + configDB := data.ConfigDB{ + Database: viper.GetString("datastore.config")} + + // Get the configured deviceId + deviceID, err := configDB.Get("deviceID") + if err != nil { + log.Printf("[ERROR] Problem getting deviceId: %v", err) + return + } + + // If we don't have a deviceId yet, configure one: + if deviceID.Value == "" { + + // Generate a deviceId: + guid := xid.New() + deviceID.Name = "deviceID" + deviceID.Value = guid.String() + + // Save it: + configDB.Set(deviceID) + } + + // Emit our deviceID: + log.Printf("[INFO] Using deviceID: %s\n", deviceID.Value) + // Create a router and setup our REST endpoints... Router := mux.NewRouter() diff --git a/config.db b/config.db index 38ca615..0eafd6c 100644 Binary files a/config.db and b/config.db differ diff --git a/zeroconf/server.go b/zeroconf/server.go index be217cd..c3960da 100644 --- a/zeroconf/server.go +++ b/zeroconf/server.go @@ -27,13 +27,22 @@ func Serve(ctx context.Context, restart chan bool) { return } + // Get the configured deviceId + deviceID, err := configDB.Get("deviceID") + if err != nil { + log.Printf("[ERROR] Problem getting deviceId: %v", err) + return + } + // Get our machine id: guid := xid.New() machineID := guid.Machine() // Create the zeroconf server server, err := zeroconf.Register(appName.Value, "_appliance-monitor._tcp", "local.", 3030, []string{ - "txtv=1", fmt.Sprintf("id=%x", machineID)}, nil) + "txtv=1", + fmt.Sprintf("machineID=%x", machineID), + fmt.Sprintf("deviceID=%s", deviceID.Value)}, nil) if err != nil { log.Printf("[ERROR] Problem starting zeroconf server: %v", err) @@ -60,7 +69,7 @@ func Serve(ctx context.Context, restart chan bool) { // Start a new server with the new name server, err = zeroconf.Register(appName.Value, "_appliance-monitor._tcp", "local.", 3030, []string{ - "txtv=1", fmt.Sprintf("id=%x", machineID)}, nil) + "txtv=1", fmt.Sprintf("machineid=%x", machineID)}, nil) if err != nil { log.Printf("[ERROR] Problem restarting zeroconf server: %v", err)