Skip to content

Commit

Permalink
minor changes, bootstrapping still not working
Browse files Browse the repository at this point in the history
  • Loading branch information
leroxyl committed Oct 22, 2019
1 parent ffa2d75 commit 777de51
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
17 changes: 10 additions & 7 deletions c8y/c8y_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ 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)
opts := MQTT.NewClientOptions().AddBroker(address)
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") {
Expand All @@ -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
Expand All @@ -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)
}
}

Expand All @@ -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)
Expand Down
48 changes: 23 additions & 25 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"github.com/ubirch/ubirch-go-c8y-client/c8y"
"log"
"time"
)

const ConfigFile = "config.json"
Expand All @@ -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)
//}
}

0 comments on commit 777de51

Please sign in to comment.