Skip to content

Commit

Permalink
[WIP] flow for bootstrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
leroxyl committed Oct 18, 2019
1 parent 0c48a4d commit dbd9105
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
26 changes: 21 additions & 5 deletions c8y_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,43 @@ import (
)
import MQTT "github.com/eclipse/paho.mqtt.golang"

func C8yBootstrap(tenant string, password string, handler MQTT.MessageHandler) (MQTT.Client, error) {
opts := MQTT.NewClientOptions().AddBroker(tenant + ".cumulocity.com:8883") // scheme://host:port
func C8yBootstrap(tenant string, password string, handler MQTT.MessageHandler) (string, error) {
// configure MQTT client
opts := MQTT.NewClientOptions().AddBroker("tcp://" + tenant + ".cumulocity.com:8883") // scheme://host:port
opts.SetUsername("management/devicebootstrap")
opts.SetUsername(password)
opts.SetClientID(tenant + "-c8y-mqtt-client")
if handler != nil {
opts.SetDefaultPublishHandler(handler)
}
topic := "s/dcr"

// configure OnConnect callback: subscribe
subTopic := "s/dcr"
opts.OnConnect = func(c MQTT.Client) {
if token := c.Subscribe(topic, 0, handler); token.Wait() && token.Error() != nil {
if token := c.Subscribe(subTopic, 0, handler); token.Wait() && token.Error() != nil {
panic(token.Error())
}
}

// configure OnMessage callback -> if message.payload starts with b'70': authorization = message.payload -> publish
c8yAuth := "TODO"

// enable SSL/TLS support

client := MQTT.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
return nil, token.Error()
} else {
log.Printf("connected to mqtt server: %v", client)
}

return client, nil
// wait for answer (OnMessage callback)

// publish
pubTopic := "s/ucr"
client.Publish(pubTopic, 0, false, nil) // TODO check if that's right

client.Disconnect(999)

return c8yAuth, nil
}
9 changes: 7 additions & 2 deletions main/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package main

import (
// "github.com/google/uuid"
// "github.com/google/uuid"
"github.com/ubirch/ubirch-go-c8y-client"
)
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)
}

0 comments on commit dbd9105

Please sign in to comment.