diff --git a/c8y/c8y_client.go b/c8y/c8y_client.go index 4a01e11..5e85b68 100644 --- a/c8y/c8y_client.go +++ b/c8y/c8y_client.go @@ -9,7 +9,7 @@ import ( ) import MQTT "github.com/eclipse/paho.mqtt.golang" -func Bootstrap(tenant string, uuid string, password string) (string, error) { +func Bootstrap(uuid string, tenant string, password string) (string, error) { // configure MQTT client address := "tcps://" + tenant + ".cumulocity.com:8883/" // scheme://host:port log.Println(address) @@ -17,12 +17,14 @@ func Bootstrap(tenant string, uuid string, password string) (string, error) { opts.SetUsername("management/devicebootstrap") opts.SetPassword(password) opts.SetClientID(uuid) + c8yError := make(chan error) c8yAuth := make(chan string) answerReceived := false + // Answer receive-callback - var authReceive MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) { + authReceive := func(client MQTT.Client, msg MQTT.Message) { answerReceived = true answer := string(msg.Payload()) if strings.HasPrefix(answer, "70") { @@ -33,12 +35,13 @@ func Bootstrap(tenant string, uuid string, password string) (string, error) { c8yError <- errors.New(fmt.Sprintf("unknown message received: %v", msg)) } } + // Error receive-callback - var errReceive MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) { + errReceive := func(client MQTT.Client, msg MQTT.Message) { answerReceived = true answer := string(msg.Payload()) log.Println("received error message:" + answer) - c8yError <- errors.New(fmt.Sprintf("unknown message received: %v", msg)) + c8yError <- errors.New(fmt.Sprintf("error message received: %v", msg)) } // configure OnConnect callback: subscribe @@ -60,8 +63,8 @@ func Bootstrap(tenant string, uuid string, password string) (string, error) { // publish until answer received for !answerReceived { log.Println("publishing...") - c.Publish("s/ucr", 0, false, nil) - time.Sleep(10 * time.Second) + c.Publish("s/ucr", 2, false, nil) + time.Sleep(5 * time.Second) } } @@ -82,7 +85,7 @@ func Bootstrap(tenant string, uuid string, password string) (string, error) { func GetClient(uuid string, tenant string, user string, password string) (MQTT.Client, error) { address := "tcps://" + tenant + ".cumulocity.com:8883/" - opts := MQTT.NewClientOptions().AddBroker(address) // scheme://host:port + opts := MQTT.NewClientOptions().AddBroker(address) opts.SetClientID(uuid) opts.SetUsername(tenant + "/" + user) opts.SetPassword(password) diff --git a/main/main.go b/main/main.go index f154e1d..9f79106 100644 --- a/main/main.go +++ b/main/main.go @@ -3,7 +3,6 @@ package main import ( "github.com/ubirch/ubirch-go-c8y-client/c8y" "log" - "time" ) const ConfigFile = "config.json" @@ -16,34 +15,33 @@ func main() { conf.Load(ConfigFile) UUID := conf.UUID - //c8yBootstrap := conf.Bootstrap + c8yBootstrap := conf.Bootstrap c8yTenant := conf.Tenant - c8yUser := conf.User - c8yPassword := conf.Password + //c8yUser := conf.User + //c8yPassword := conf.Password log.Println("UUID: " + UUID) - //// bootstrap - //c8yAuth, err := c8y.Bootstrap(c8yTenant, UUID, c8yBootstrap) - //if err != nil { - // log.Printf("tenant: %s, password: %s\n", c8yTenant, c8yBootstrap) - // log.Fatalf("unable to bootstrap device: %v", err) - //} - //log.Println(c8yAuth) - - client, err := c8y.GetClient(UUID, c8yTenant, c8yUser, c8yPassword) + // bootstrap + c8yAuth, err := c8y.Bootstrap(UUID, c8yTenant, c8yBootstrap) if err != nil { - log.Fatalf("error: %v", err) - } - defer client.Disconnect(0) - - value := true - for { - err = c8y.Send(client, value) - if err != nil { - log.Fatalf("error: %v", err) - } - value = !value - time.Sleep(2 * time.Second) + log.Fatalf("unable to bootstrap device: %v", err) } + log.Println(c8yAuth) + + //client, err := c8y.GetClient(UUID, c8yTenant, c8yUser, c8yPassword) + //if err != nil { + // log.Fatalf("error: %v", err) + //} + //defer client.Disconnect(0) + // + //value := true + //for { + // err = c8y.Send(client, value) + // if err != nil { + // log.Fatalf("error: %v", err) + // } + // value = !value + // time.Sleep(2 * time.Second) + //} }