Write data in database table
This commit is contained in:
		
							
								
								
									
										32
									
								
								mqtt/mqtt.go
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								mqtt/mqtt.go
									
									
									
									
									
								
							@@ -8,7 +8,7 @@ import (
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"mqttListener/Config"
 | 
			
		||||
	"mqttListener/env"
 | 
			
		||||
	"mqttListener/models"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -22,18 +22,11 @@ import (
 | 
			
		||||
 URI = mqtt://<user>:<pass>@<server>:<port>
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/* TODO: This options are just a suggestion
 | 
			
		||||
Options:
 | 
			
		||||
 [-help]                      Display help
 | 
			
		||||
 [-customer]                  Customer- or project number
 | 
			
		||||
 [-device]                    Device number
 | 
			
		||||
 [-uri <uri>]                 Broker URI
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
var client MQTT.Client
 | 
			
		||||
var opts *MQTT.ClientOptions
 | 
			
		||||
var topic string
 | 
			
		||||
var uri url.URL
 | 
			
		||||
var ev *env.Env
 | 
			
		||||
 | 
			
		||||
func Connect() {
 | 
			
		||||
	client = MQTT.NewClient(opts)
 | 
			
		||||
@@ -64,17 +57,19 @@ func Listen() {
 | 
			
		||||
	*/
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Setup() {
 | 
			
		||||
func Setup(environment *env.Env) {
 | 
			
		||||
 | 
			
		||||
	ev = environment
 | 
			
		||||
 | 
			
		||||
	scheme := "tcp://"
 | 
			
		||||
	if Config.EnableTLS {
 | 
			
		||||
	if ev.Config.EnableTLS {
 | 
			
		||||
		scheme = "ssl://"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	urlString := scheme
 | 
			
		||||
	urlString += Config.BrokerUsername + ":" + Config.BrokerPassword + "@"
 | 
			
		||||
	urlString += Config.BrokerAddress + ":"
 | 
			
		||||
	urlString += strconv.FormatInt(Config.BrokerPort, 10)
 | 
			
		||||
	urlString += ev.Config.BrokerUsername + ":" + ev.Config.BrokerPassword + "@"
 | 
			
		||||
	urlString += ev.Config.BrokerAddress + ":"
 | 
			
		||||
	urlString += strconv.FormatInt(ev.Config.BrokerPort, 10)
 | 
			
		||||
 | 
			
		||||
	fmt.Println("broker urlString: ", urlString)
 | 
			
		||||
 | 
			
		||||
@@ -89,7 +84,7 @@ func Setup() {
 | 
			
		||||
 | 
			
		||||
	topic = "/ATB/#"
 | 
			
		||||
 | 
			
		||||
	opts = createClientOptions(Config.BrokerClientId, uri)
 | 
			
		||||
	opts = createClientOptions(ev.Config.BrokerClientID, uri)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -101,7 +96,7 @@ func createClientOptions(ClientID string, uri *url.URL) *MQTT.ClientOptions {
 | 
			
		||||
	opts.SetPassword(password)
 | 
			
		||||
	opts.SetClientID(ClientID)
 | 
			
		||||
	opts.SetDefaultPublishHandler(f)
 | 
			
		||||
	if Config.EnableTLS {
 | 
			
		||||
	if ev.Config.EnableTLS {
 | 
			
		||||
		tlsConfig := &tls.Config{InsecureSkipVerify: true, ClientAuth: tls.NoClientCert}
 | 
			
		||||
		opts.SetTLSConfig(tlsConfig)
 | 
			
		||||
	}
 | 
			
		||||
@@ -180,4 +175,9 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
 | 
			
		||||
	fmt.Printf("Device SN  = %s\n", device.SN)
 | 
			
		||||
 | 
			
		||||
	// TODO: store this device in database
 | 
			
		||||
	err = ev.DB.InsertDevice(&device)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(err.Error())
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user