Skip to content

Commit

Permalink
Add deviceId, rename id to machineId
Browse files Browse the repository at this point in the history
  • Loading branch information
danesparza committed Jul 27, 2017
1 parent 504ef46 commit bcbef33
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
28 changes: 28 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()

Expand Down
Binary file modified config.db
Binary file not shown.
13 changes: 11 additions & 2 deletions zeroconf/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit bcbef33

Please sign in to comment.