支持软固件升级

This commit is contained in:
ctlove0523 2020-12-29 23:04:59 +08:00
parent 791ff9347b
commit afb3ccf861
1 changed files with 7 additions and 13 deletions

View File

@ -1,8 +1,8 @@
package iot package iot
import ( import (
"crypto/tls"
"encoding/json" "encoding/json"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang" mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/satori/go.uuid" "github.com/satori/go.uuid"
@ -88,13 +88,9 @@ func (device *iotDevice) SyncAllVersionSubDevices() {
Services: dataEntries, Services: dataEntries,
} }
topic := FormatTopic(DeviceToPlatformTopic, device.Id)
fmt.Printf("topic = %s\n", topic)
if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), 1, false, Interface2JsonString(data)); if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), 1, false, Interface2JsonString(data));
token.Wait() && token.Error() != nil { token.Wait() && token.Error() != nil {
fmt.Println("send sync sub device request failed") glog.Errorf("send sub device sync request failed")
} else {
fmt.Printf("send syc sub device request success")
} }
} }
@ -121,7 +117,7 @@ func (device *iotDevice) SyncSubDevices(version int) {
if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), 1, false, Interface2JsonString(data)); if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), 1, false, Interface2JsonString(data));
token.Wait() && token.Error() != nil { token.Wait() && token.Error() != nil {
fmt.Println("send sync sub device reqeust failed") glog.Errorf("send sync sub device request failed")
} }
} }
@ -159,8 +155,6 @@ func (device *iotDevice) AddSubDevices(deviceInfos []DeviceInfo) bool {
Services: []DataEntry{requestEventService}, Services: []DataEntry{requestEventService},
} }
fmt.Printf("topic = %s\n", FormatTopic(DeviceToPlatformTopic, device.Id))
fmt.Printf("request = %s\n", Interface2JsonString(request))
if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), 1, false, Interface2JsonString(request)); if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), 1, false, Interface2JsonString(request));
token.Wait() && token.Error() != nil { token.Wait() && token.Error() != nil {
glog.Warningf("gateway %s add sub devices request send failed", device.Id) glog.Warningf("gateway %s add sub devices request send failed", device.Id)
@ -436,11 +430,10 @@ func (device *iotDevice) createPropertiesSetMqttHandler() func(client mqtt.Clien
// 平台向设备下发的事件callback // 平台向设备下发的事件callback
func (device *iotDevice) handlePlatformToDeviceData() func(client mqtt.Client, message mqtt.Message) { func (device *iotDevice) handlePlatformToDeviceData() func(client mqtt.Client, message mqtt.Message) {
fmt.Println("begin to handle data from platform to device")
handler := func(client mqtt.Client, message mqtt.Message) { handler := func(client mqtt.Client, message mqtt.Message) {
data := &Data{} data := &Data{}
if json.Unmarshal(message.Payload(), data) != nil { if json.Unmarshal(message.Payload(), data) != nil {
fmt.Println("unmarshal data failed") return
} }
for _, entry := range data.Services { for _, entry := range data.Services {
@ -450,14 +443,12 @@ func (device *iotDevice) handlePlatformToDeviceData() func(client mqtt.Client, m
// 子设备添加 // 子设备添加
subDeviceInfo := &SubDeviceInfo{} subDeviceInfo := &SubDeviceInfo{}
if json.Unmarshal([]byte(Interface2JsonString(entry.Paras)), subDeviceInfo) != nil { if json.Unmarshal([]byte(Interface2JsonString(entry.Paras)), subDeviceInfo) != nil {
fmt.Println("begin to invoke sub device add")
continue continue
} }
device.subDevicesAddHandler(*subDeviceInfo) device.subDevicesAddHandler(*subDeviceInfo)
case "delete_sub_device_notify": case "delete_sub_device_notify":
subDeviceInfo := &SubDeviceInfo{} subDeviceInfo := &SubDeviceInfo{}
if json.Unmarshal([]byte(Interface2JsonString(entry.Paras)), subDeviceInfo) != nil { if json.Unmarshal([]byte(Interface2JsonString(entry.Paras)), subDeviceInfo) != nil {
fmt.Println("begin to invoke sub device delete")
continue continue
} }
device.subDevicesDeleteHandler(*subDeviceInfo) device.subDevicesDeleteHandler(*subDeviceInfo)
@ -577,6 +568,9 @@ func (device *iotDevice) Init() bool {
options.SetClientID(assembleClientId(device)) options.SetClientID(assembleClientId(device))
options.SetUsername(device.Id) options.SetUsername(device.Id)
options.SetPassword(HmacSha256(device.Password, TimeStamp())) options.SetPassword(HmacSha256(device.Password, TimeStamp()))
options.SetTLSConfig(&tls.Config{
InsecureSkipVerify: true,
})
device.client = mqtt.NewClient(options) device.client = mqtt.NewClient(options)