支持网关上报子设备状态
This commit is contained in:
parent
62ed39104b
commit
d33c449ce1
|
@ -46,6 +46,9 @@ const (
|
|||
FileResponseTopicName string = "FileUploadResultTopic"
|
||||
FileResponseTopic string = "$oc/devices/{device_id}/sys/events/down"
|
||||
|
||||
FILE_ACTION_UPLOAD string = "upload"
|
||||
FILE_ACTION_DOWNLOAD string = "download"
|
||||
FileActionUpload string = "upload"
|
||||
FileActionDownload string = "download"
|
||||
|
||||
// 设备或网关向平台发送请求
|
||||
DeviceToPlatformTopic string = "$oc/devices/{device_id}/sys/events/up"
|
||||
)
|
||||
|
|
|
@ -137,10 +137,10 @@ func CreateFileUploadDownLoadResultResponse(filename, action string, result bool
|
|||
Paras: paras,
|
||||
}
|
||||
serviceEvent.ServiceId = "$file_manager"
|
||||
if action == FILE_ACTION_DOWNLOAD {
|
||||
if action == FileActionDownload {
|
||||
serviceEvent.EventType = "download_result_report"
|
||||
}
|
||||
if action == FILE_ACTION_UPLOAD {
|
||||
if action == FileActionUpload {
|
||||
serviceEvent.EventType = "upload_result_report"
|
||||
}
|
||||
serviceEvent.EventTime = GetEventTimeStamp()
|
||||
|
|
|
@ -2,7 +2,6 @@ package iot
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/golang/glog"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
@ -24,7 +23,7 @@ type httpClient struct {
|
|||
}
|
||||
|
||||
func (client *httpClient) DownloadFile(fileName, downloadUrl string) bool {
|
||||
fmt.Println(downloadUrl)
|
||||
glog.Infof("begin to download file %s, url = %s", fileName, downloadUrl)
|
||||
fileName = SmartFileName(fileName)
|
||||
out, err := os.Create(fileName)
|
||||
if err != nil {
|
||||
|
|
64
iotdevice.go
64
iotdevice.go
|
@ -2,7 +2,6 @@ package iot
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/golang/glog"
|
||||
"github.com/satori/go.uuid"
|
||||
|
@ -10,7 +9,12 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type Gateway interface {
|
||||
UpdateSubDeviceState(subDevicesStatus SubDevicesStatus) bool
|
||||
}
|
||||
|
||||
type Device interface {
|
||||
Gateway
|
||||
Init() bool
|
||||
IsConnected() bool
|
||||
SendMessage(message Message) bool
|
||||
|
@ -39,6 +43,31 @@ type iotDevice struct {
|
|||
fileUrls map[string]string
|
||||
}
|
||||
|
||||
func (device *iotDevice) UpdateSubDeviceState(subDevicesStatus SubDevicesStatus) bool {
|
||||
glog.Infof("begin to update sub-devices status")
|
||||
|
||||
requestEventService := RequestEventService{
|
||||
ServiceId: "$sub_device_manager",
|
||||
EventType: "sub_device_update_status",
|
||||
EventTime: GetEventTimeStamp(),
|
||||
Paras: subDevicesStatus,
|
||||
}
|
||||
|
||||
request := Request{
|
||||
ObjectDeviceId: device.Id,
|
||||
Services: []RequestEventService{requestEventService},
|
||||
}
|
||||
|
||||
if token := device.client.Publish(FormatTopic(DeviceToPlatformTopic, device.Id), 1, false, Interface2JsonString(request));
|
||||
token.Wait() && token.Error() != nil {
|
||||
glog.Warningf("gateway %s update sub devices status failed", device.Id)
|
||||
return false
|
||||
}
|
||||
|
||||
glog.Info("gateway %s update sub devices status failed", device.Id)
|
||||
return true
|
||||
}
|
||||
|
||||
func (device *iotDevice) DownloadFile(filename string) bool {
|
||||
// 构造获取文件上传URL的请求
|
||||
requestParas := FileRequestServiceEventParas{
|
||||
|
@ -68,30 +97,28 @@ func (device *iotDevice) DownloadFile(filename string) bool {
|
|||
for {
|
||||
select {
|
||||
case <-ticker:
|
||||
_, ok := device.fileUrls[filename+FILE_ACTION_DOWNLOAD]
|
||||
_, ok := device.fileUrls[filename+FileActionDownload]
|
||||
if ok {
|
||||
glog.Infof("platform send file upload url success")
|
||||
goto ENDFOR
|
||||
} else {
|
||||
fmt.Println("get url failed")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
ENDFOR:
|
||||
|
||||
if len(device.fileUrls[filename+FILE_ACTION_DOWNLOAD]) == 0 {
|
||||
if len(device.fileUrls[filename+FileActionDownload]) == 0 {
|
||||
glog.Errorf("get file download url failed")
|
||||
return false
|
||||
}
|
||||
|
||||
downloadFlag := CreateHttpClient().DownloadFile(filename, device.fileUrls[filename+FILE_ACTION_DOWNLOAD])
|
||||
downloadFlag := CreateHttpClient().DownloadFile(filename, device.fileUrls[filename+FileActionDownload])
|
||||
if downloadFlag {
|
||||
glog.Errorf("down load file { %s } failed", filename)
|
||||
return false
|
||||
}
|
||||
|
||||
response := CreateFileUploadDownLoadResultResponse(filename, FILE_ACTION_DOWNLOAD, downloadFlag)
|
||||
response := CreateFileUploadDownLoadResultResponse(filename, FileActionDownload, downloadFlag)
|
||||
|
||||
token := device.client.Publish(device.topics[FileResponseTopicName], 1, false, Interface2JsonString(response))
|
||||
if token.Wait() && token.Error() != nil {
|
||||
|
@ -125,40 +152,37 @@ func (device *iotDevice) UploadFile(filename string) bool {
|
|||
token.Wait() && token.Error() != nil {
|
||||
glog.Warningf("publish file upload request url failed")
|
||||
return false
|
||||
} else {
|
||||
fmt.Println("send request success")
|
||||
}
|
||||
glog.Info("publish file upload request url success")
|
||||
|
||||
ticker := time.Tick(time.Second)
|
||||
for {
|
||||
select {
|
||||
case <-ticker:
|
||||
_, ok := device.fileUrls[filename+FILE_ACTION_UPLOAD]
|
||||
_, ok := device.fileUrls[filename+FileActionUpload]
|
||||
if ok {
|
||||
glog.Infof("platform send file upload url success")
|
||||
goto ENDFOR
|
||||
} else {
|
||||
fmt.Println("get url failed")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
ENDFOR:
|
||||
|
||||
if len(device.fileUrls[filename+FILE_ACTION_UPLOAD]) == 0 {
|
||||
if len(device.fileUrls[filename+FileActionUpload]) == 0 {
|
||||
glog.Errorf("get file upload url failed")
|
||||
return false
|
||||
}
|
||||
glog.Infof("file upload url is %s", device.fileUrls[filename+FILE_ACTION_UPLOAD])
|
||||
glog.Infof("file upload url is %s", device.fileUrls[filename+FileActionUpload])
|
||||
|
||||
//filename = SmartFileName(filename)
|
||||
uploadFlag := CreateHttpClient().UploadFile(filename, device.fileUrls[filename+FILE_ACTION_UPLOAD])
|
||||
uploadFlag := CreateHttpClient().UploadFile(filename, device.fileUrls[filename+FileActionUpload])
|
||||
if !uploadFlag {
|
||||
glog.Errorf("upload file failed")
|
||||
return false
|
||||
}
|
||||
|
||||
response := CreateFileUploadDownLoadResultResponse(filename, FILE_ACTION_UPLOAD, uploadFlag)
|
||||
response := CreateFileUploadDownLoadResultResponse(filename, FileActionUpload, uploadFlag)
|
||||
|
||||
token := device.client.Publish(device.topics[FileResponseTopicName], 1, false, Interface2JsonString(response))
|
||||
if token.Wait() && token.Error() != nil {
|
||||
|
@ -248,10 +272,10 @@ func (device *iotDevice) createFileUrlResponseMqttHandler() func(client mqtt.Cli
|
|||
|
||||
fileName := response.Services[0].Paras.ObjectName
|
||||
eventType := response.Services[0].EventType
|
||||
if strings.Contains(eventType, FILE_ACTION_UPLOAD) {
|
||||
device.fileUrls[fileName+FILE_ACTION_UPLOAD] = response.Services[0].Paras.Url
|
||||
if strings.Contains(eventType, FileActionUpload) {
|
||||
device.fileUrls[fileName+FileActionUpload] = response.Services[0].Paras.Url
|
||||
} else {
|
||||
device.fileUrls[fileName+FILE_ACTION_DOWNLOAD] = response.Services[0].Paras.Url
|
||||
device.fileUrls[fileName+FileActionDownload] = response.Services[0].Paras.Url
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -295,8 +319,6 @@ func (device *iotDevice) Init() bool {
|
|||
options.SetClientID(assembleClientId(device))
|
||||
options.SetUsername(device.Id)
|
||||
options.SetPassword(HmacSha256(device.Password, TimeStamp()))
|
||||
fmt.Println(assembleClientId(device))
|
||||
fmt.Println(HmacSha256(device.Password, TimeStamp()))
|
||||
|
||||
device.client = mqtt.NewClient(options)
|
||||
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package iot
|
||||
|
||||
//设备或网关请求的通用消息体
|
||||
type Request struct {
|
||||
ObjectDeviceId string `json:"object_device_id"`
|
||||
Services []RequestEventService `json:"services"`
|
||||
}
|
||||
type RequestEventService struct {
|
||||
ServiceId string `json:"service_id"`
|
||||
EventType string `json:"event_type"`
|
||||
EventTime string `json:"event_time"`
|
||||
Paras interface{} `json:"paras"` // 不同类型的请求paras使用的结构体不同
|
||||
}
|
||||
|
||||
// 1 网关更新子设备状态
|
||||
|
||||
type SubDevicesStatus struct {
|
||||
DeviceStatuses []DeviceStatus `json:"device_statuses"`
|
||||
}
|
||||
|
||||
type DeviceStatus struct {
|
||||
DeviceId string `json:"device_id"`
|
||||
Status string `json:"status"` // 子设备状态。 OFFLINE:设备离线 ONLINE:设备上线
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
iot "github.com/ctlove0523/huaweicloud-iot-device-sdk-go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
device := iot.CreateIotDevice("5fdb75cccbfe2f02ce81d4bf_go-mqtt", "123456789", "tcp://iot-mqtts.cn-north-4.myhuaweicloud.com:1883")
|
||||
device.Init()
|
||||
|
||||
subDevice1 := iot.DeviceStatus{
|
||||
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-1",
|
||||
Status: "ONLINE",
|
||||
}
|
||||
subDevice2 := iot.DeviceStatus{
|
||||
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-2",
|
||||
Status: "ONLINE",
|
||||
}
|
||||
|
||||
subDevice3 := iot.DeviceStatus{
|
||||
DeviceId: "5fdb75cccbfe2f02ce81d4bf_sub-device-3",
|
||||
Status: "ONLINE",
|
||||
}
|
||||
|
||||
devicesStatus := []iot.DeviceStatus{subDevice1, subDevice2, subDevice3}
|
||||
|
||||
ok := device.UpdateSubDeviceState(iot.SubDevicesStatus{
|
||||
DeviceStatuses: devicesStatus,
|
||||
})
|
||||
if ok {
|
||||
fmt.Println("gateway update sub devices status success")
|
||||
} else {
|
||||
fmt.Println("gateway update sub devices status failed")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue