-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CloudSync] Base Code to connect to MQTT broker running on AWS
Signed-off-by: Nitu Gupta <[email protected]>
- Loading branch information
1 parent
0af6955
commit 3b39427
Showing
10 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,3 +89,7 @@ config SECURE_MODE | |
default y | ||
---help--- | ||
"Secure mode is enable" | ||
|
||
config CLOUD_SYNC | ||
bool "CloudSync" | ||
default y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package mqtt | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
MQTT "github.com/eclipse/paho.mqtt.golang" | ||
) | ||
|
||
const clientID = "TestHomeEdge" | ||
const mqttPort = 1883 | ||
|
||
// Client is a wrapper on top of `MQTT.Client` | ||
type Client struct { | ||
ID string | ||
Host string | ||
Port uint | ||
Qos byte | ||
sync.RWMutex | ||
ClientOptions *MQTT.ClientOptions | ||
MQTT.Client | ||
} | ||
|
||
//Message is used to wrap the app id and payload into one and publish to broker | ||
type Message struct { | ||
AppID string | ||
Payload string | ||
} | ||
|
||
// Config represents an attribute config setter for the `Client`. | ||
type Config func(*Client) | ||
|
||
// SetClientID sets the mqtt client id. | ||
func SetClientID(id string) Config { | ||
return func(c *Client) { | ||
c.ID = id | ||
} | ||
} | ||
|
||
// SetHost sets the host where to connect. | ||
func SetHost(host string) Config { | ||
return func(c *Client) { | ||
c.Host = host | ||
} | ||
} | ||
|
||
// SetPort sets the port where to connect. | ||
func SetPort(port uint) Config { | ||
return func(c *Client) { | ||
c.Port = port | ||
} | ||
} | ||
|
||
//SetBrokerURL returns the broker url for connection | ||
func (c *Client) SetBrokerURL(protocol string) string { | ||
return fmt.Sprintf("%s://%s:%d", protocol, c.Host, c.Port) | ||
} | ||
|
||
// NewClient returns a configured `Client`. Is mandatory | ||
func NewClient(configs ...Config) (*Client, error) { | ||
client := &Client{ | ||
Qos: byte(0), | ||
} | ||
|
||
for _, config := range configs { | ||
config(client) | ||
} | ||
|
||
copts := MQTT.NewClientOptions() | ||
copts.SetClientID(clientID) | ||
copts.SetAutoReconnect(true) | ||
copts.SetMaxReconnectInterval(1 * time.Second) | ||
copts.SetOnConnectHandler(client.onConnect()) | ||
copts.SetConnectionLostHandler(func(c MQTT.Client, err error) { | ||
log.Warn(logPrefix, " disconnected, reason: "+err.Error()) | ||
}) | ||
|
||
client.ClientOptions = copts | ||
|
||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package mqtt | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
|
||
MQTT "github.com/eclipse/paho.mqtt.golang" | ||
"github.com/lf-edge/edge-home-orchestration-go/internal/common/logmgr" | ||
) | ||
|
||
var ( | ||
log = logmgr.GetInstance() | ||
logPrefix = "[MQTTConnectionMgr]" | ||
) | ||
|
||
// Connect creates a new mqtt client and uses the ClientOptions generated in the NewClient function to connect with the provided host and port. | ||
func (client *Client) Connect() error { | ||
|
||
mqttClient := MQTT.NewClient(client.ClientOptions) | ||
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil { | ||
return token.Error() | ||
} | ||
|
||
log.Info(logPrefix, "MQTT Connected") | ||
|
||
client.Lock() | ||
client.Client = mqttClient | ||
|
||
client.Unlock() | ||
|
||
return nil | ||
} | ||
|
||
//onConnect callback is called on successful connection to broker | ||
func (client *Client) onConnect() MQTT.OnConnectHandler { | ||
log.Info("Running MQTT.OnConnectHandler") | ||
//Adding the subcription if needed for client | ||
return nil | ||
} | ||
|
||
//StartMQTTClient is used to initiate the client and set the configuration | ||
func StartMQTTClient(brokerURL string) { | ||
|
||
clientConfig, err := NewClient( | ||
SetHost(brokerURL), | ||
SetPort(uint(mqttPort)), | ||
) | ||
if err != nil { | ||
log.Warn(logPrefix, err) | ||
} | ||
clientConfig.ClientOptions.SetOnConnectHandler(clientConfig.onConnect()) | ||
URL := clientConfig.SetBrokerURL("tcp") | ||
log.Info(logPrefix, " The broker is", URL) | ||
clientConfig.ClientOptions.AddBroker(URL) | ||
|
||
connectErr := clientConfig.Connect() | ||
if connectErr != nil { | ||
log.Warn(logPrefix, connectErr) | ||
} | ||
} | ||
|
||
//Publish is used to publish the client data to the cloud | ||
func Publish(client *Client, message Message, topic string) error { | ||
|
||
log.Info(logPrefix, "Publishing the data to cloud") | ||
payload, err := json.Marshal(message) | ||
if err != nil { | ||
log.Warn(logPrefix, "Error in Json Marshalling", err) | ||
} | ||
mqttClient := client.Client | ||
for mqttClient == nil { | ||
time.Sleep(time.Second * 2) | ||
} | ||
token := mqttClient.Publish(topic, 0, true, payload) | ||
if token.Wait() && token.Error() != nil { | ||
return token.Error() | ||
} | ||
time.Sleep(time.Second) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mqtt | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
//const Host = "broker.emqx.io" | ||
const Host = "ec2-54-175-241-64.compute-1.amazonaws.com" | ||
const port = "1883" | ||
|
||
func TestStartMQTTClient(t *testing.T) { | ||
t.Run("Success", func(t *testing.T) { | ||
StartMQTTClient(Host) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cloudsyncmgr | ||
|
||
import ( | ||
mqttmgr "github.com/lf-edge/edge-home-orchestration-go/internal/common/mqtt" | ||
) | ||
|
||
const ( | ||
logPrefix = "[cloudsyncmgr]" | ||
) | ||
|
||
// CloudSync is the interface for starting Cloud synchronization | ||
type CloudSync interface { | ||
StartCloudSync(host string) error | ||
} | ||
|
||
//CloudSyncImpl struct | ||
type CloudSyncImpl struct{} | ||
|
||
var ( | ||
cloudsyncIns *CloudSyncImpl | ||
) | ||
|
||
func init() { | ||
cloudsyncIns = &CloudSyncImpl{} | ||
} | ||
|
||
// GetInstance returns cloudSync instaance | ||
func GetInstance() CloudSync { | ||
return cloudsyncIns | ||
} | ||
|
||
// StartCloudSync starts a server in terms of CloudSync | ||
func (c *CloudSyncImpl) StartCloudSync(host string) (err error) { | ||
mqttmgr.StartMQTTClient(host) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters