Skip to content

Commit

Permalink
[WIP] make c8y bootstrap work
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkberg committed Oct 18, 2019
1 parent dbd9105 commit 0176f0c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 57 deletions.
51 changes: 51 additions & 0 deletions c8y/c8y_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package c8y

import (
"errors"
"fmt"
"math/rand"
"strings"
)
import MQTT "github.com/eclipse/paho.mqtt.golang"

func C8yBootstrap(tenant string, password string) (string, error) {
// configure MQTT client
opts := MQTT.NewClientOptions().AddBroker("tcps://" + tenant + ".cumulocity.com:8883") // scheme://host:port
opts.SetUsername("management/devicebootstrap")
opts.SetUsername(password)
opts.SetClientID(fmt.Sprintf("%s-c8y-mqtt-client-%d", tenant, rand.Uint32()))

c8yError := make(chan error)
c8yAuth := make(chan string)
var receive MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
authorization := string(msg.Payload())
if strings.HasPrefix(authorization, "70") {
c8yAuth <- authorization
}
c8yError <- errors.New(fmt.Sprintf("unknown message received: %v", msg))
}

// configure OnConnect callback: subscribe
opts.OnConnect = func(c MQTT.Client) {
if token := c.Subscribe("s/dcr", 0, receive); token.Wait() && token.Error() != nil {
c8yError <- token.Error()
return
}

pubTopic := "s/ucr"
c.Publish(pubTopic, 0, false, nil)
}

client := MQTT.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
return "", token.Error()
}

defer client.Disconnect(0)
select {
case auth := <-c8yAuth:
return auth, nil
case err := <-c8yError:
return "", err
}
}
3 changes: 1 addition & 2 deletions go.mod → c8y/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module github.com/ubirch/ubirch-go-c8y-client
module github.com/ubirch/ubirch-go-c8y-client/c8y

go 1.13

require (
github.com/eclipse/paho.mqtt.golang v1.2.0
github.com/google/uuid v1.1.1
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582 // indirect
)
49 changes: 0 additions & 49 deletions c8y_client.go

This file was deleted.

7 changes: 7 additions & 0 deletions main/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module main

go 1.13

require github.com/ubirch/ubirch-go-c8y-client/c8y v1.0.0

replace github.com/ubirch/ubirch-go-c8y-client/c8y v1.0.0 => ../c8y
15 changes: 9 additions & 6 deletions main/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package main

import (
// "github.com/google/uuid"
"github.com/ubirch/ubirch-go-c8y-client"
"fmt"
"github.com/ubirch/ubirch-go-c8y-client/c8y"
)
import MQTT "github.com/eclipse/paho.mqtt.golang"

func main() {
tenant := "ubirch"
c8yPassword := "xyz"
handler := MQTT.MessageHandler{}
c8yAuth, err := ubirch_go_c8y_client.C8yBootstrap(tenant, c8yPassword, handler)
c8yPassword := "---"
c8yAuth, err := c8y.C8yBootstrap(tenant, c8yPassword)
if err != nil {
panic(fmt.Sprintf("unable to bootstrap device: %v", err))
}

fmt.Printf(c8yAuth)
}

0 comments on commit 0176f0c

Please sign in to comment.