Expand database and device

This commit is contained in:
2020-09-13 18:37:22 +02:00
parent 3f0fa81f18
commit f21e4ff1d6
4 changed files with 39 additions and 15 deletions

View File

@@ -138,13 +138,16 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
log.Printf("ERROR: matchedDefault: %s", err.Error())
}
var customerID string
var deviceID string
if matchedWithType {
fmt.Printf("Topic matched regexpWithType\n")
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4]
customerID = topicSlice[3]
deviceID = topicSlice[4]
} else if matchedDefault {
fmt.Printf("Topic matched regexpDefault\n")
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4] + "_" + topicSlice[5]
customerID = topicSlice[2]
deviceID = topicSlice[5]
} else {
log.Printf("ERROR: no matching topic: %s", msg.Topic())
return
@@ -153,7 +156,11 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
fmt.Printf("Generated deviceID = %s\n", deviceID)
var device models.Device
device.ID = deviceID
device.TOPIC = msg.Topic()
device.CustomerID = customerID
device.DeviceID = deviceID
device.ProjectName = "projectName t.b.d."
device.MAC = "t.b.d."
device.SN = "t.b.d."
device.LastMsg = "t.b.d"
@@ -174,7 +181,7 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
fmt.Printf("Device MAC = %s\n", device.MAC)
fmt.Printf("Device SN = %s\n", device.SN)
// TODO: store this device in database
// store this device in database
err = ev.DB.InsertDevice(&device)
if err != nil {
fmt.Println(err.Error())