diff --git a/apps/device/api/device.go b/apps/device/api/device.go index 0ad1ba2..60fedb3 100644 --- a/apps/device/api/device.go +++ b/apps/device/api/device.go @@ -93,7 +93,6 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) { biz.ErrIsNil(err, "获取设备失败") template, err := p.ProductTemplateApp.FindList(entity.ProductTemplate{Classify: classify, Pid: device.Pid}) biz.ErrIsNil(err, "查询设备模板失败") - // 从设备影子中读取 res := make([]entity.DeviceStatusVo, 0) for _, tel := range *template { diff --git a/apps/device/api/product_ota.go b/apps/device/api/product_ota.go index d0763e1..459d943 100644 --- a/apps/device/api/product_ota.go +++ b/apps/device/api/product_ota.go @@ -12,10 +12,13 @@ import ( "pandax/apps/device/entity" "pandax/apps/device/services" + "pandax/apps/device/util" + devicerpc "pandax/pkg/device_rpc" ) type ProductOtaApi struct { ProductOtaApp services.ProductOtaModel + DeviceApp services.DeviceModel } const filePath = "uploads/file" @@ -74,3 +77,32 @@ func (p *ProductOtaApi) DeleteProductOta(rc *restfulx.ReqCtx) { ids := strings.Split(id, ",") p.ProductOtaApp.Delete(ids) } + +// DeleteProductOta OTA升级 +func (p *ProductOtaApi) OtaDown(rc *restfulx.ReqCtx) { + // 固件包 + id := restfulx.PathParam(rc, "id") + pid := restfulx.QueryParam(rc, "pid") + ota, err := p.ProductOtaApp.FindOne(id) + biz.ErrIsNil(err, "查询OTA信息失败") + // 1、对比所有设备与OTA固件版本 + devices, err := p.DeviceApp.FindList(entity.Device{Pid: pid, LinkStatus: "online"}) + biz.ErrIsNil(err, "该产品下没有设备存在") + // 2、对低版本的设备进行指令下发升级 + go func() { + rpc := devicerpc.RpcPayload{ + Method: "ota", + Params: map[string]any{ + "verison": ota.Version, + "url": ota.Url, + "Id": ota.Id, + "sign": ota.Check, + }, + } + for _, device := range *devices { + util.BuildRunDeviceRpc(device.Id, "", rpc) + } + + }() + +} diff --git a/apps/device/router/product_ota.go b/apps/device/router/product_ota.go index 6077da1..ad99ca4 100644 --- a/apps/device/router/product_ota.go +++ b/apps/device/router/product_ota.go @@ -63,5 +63,12 @@ func InitProductOtaRouter(container *restful.Container) { Metadata(restfulspec.KeyOpenAPITags, tags). Param(ws.PathParameter("id", "多id 1,2,3").DataType("string"))) + ws.Route(ws.DELETE("/{id}/down").To(func(request *restful.Request, response *restful.Response) { + restfulx.NewReqCtx(request, response).WithLog("Ota升级").Handle(s.OtaDown) + }). + Doc("Ota升级"). + Metadata(restfulspec.KeyOpenAPITags, tags). + Param(ws.QueryParameter("pid", "产品Id").Required(false).DataType("string"))) + container.Add(ws) } diff --git a/apps/system/api/upload.go b/apps/system/api/upload.go index d9256d5..7dfa097 100644 --- a/apps/system/api/upload.go +++ b/apps/system/api/upload.go @@ -17,7 +17,7 @@ type UploadApi struct{} // UploadImage 图片上传 func (up *UploadApi) UploadImage(rc *restfulx.ReqCtx) { _, fileHeader, err := rc.Request.Request.FormFile("file") - fileType := restfulx.QueryParam(rc, "fileType") + fileType := restfulx.PathParam(rc, "path") biz.ErrIsNil(err, "请传入文件") local := &tool.Local{Path: tool.GetFilePath(fileType)} link, fileName, err := local.UploadFile(fileHeader) @@ -41,7 +41,7 @@ func (p *UploadApi) UplaodToOss(rc *restfulx.ReqCtx) { // subpath 是fileName func (up *UploadApi) GetImage(rc *restfulx.ReqCtx) { - fileType := restfulx.QueryParam(rc, "fileType") + fileType := restfulx.PathParam(rc, "path") actual := path.Join(tool.GetFilePath(fileType), restfulx.PathParam(rc, "subpath")) rc.Download(actual) diff --git a/iothub/netbase/hook_base.go b/iothub/netbase/hook_base.go index 29a9928..6341d1c 100644 --- a/iothub/netbase/hook_base.go +++ b/iothub/netbase/hook_base.go @@ -199,7 +199,7 @@ func GetEventFromTopic(reg, topic string) (identifier string) { } // eventType 事件类型 info alarm -func CreateEvent(msgType, eventType, content string, deviceAuth *model.DeviceAuth) *DeviceEventInfo { +func CreateEventInfo(msgType, eventType, content string, deviceAuth *model.DeviceAuth) *DeviceEventInfo { ts := time.Now().Format("2006-01-02 15:04:05.000") ci := &tdengine.Events{ DeviceId: deviceAuth.DeviceId, diff --git a/iothub/server/emqxserver/hook.go b/iothub/server/emqxserver/hook.go index a48aba3..2eac7b2 100644 --- a/iothub/server/emqxserver/hook.go +++ b/iothub/server/emqxserver/hook.go @@ -88,7 +88,7 @@ func (s *HookGrpcService) OnClientConnected(ctx context.Context, in *exhook2.Cli } //添加连接ID mqttclient.Session.Store(etoken.DeviceId, in.Clientinfo.Clientid) - data := netbase.CreateEvent(message.ConnectMes, "info", fmt.Sprintf("设备%s通过MQTT协议连接", etoken.Name), etoken) + data := netbase.CreateEventInfo(message.ConnectMes, "info", fmt.Sprintf("设备%s通过MQTT协议连接", etoken.Name), etoken) go s.HookService.Queue.Queue(data) return &exhook2.EmptySuccess{}, nil } @@ -103,7 +103,7 @@ func (s *HookGrpcService) OnClientDisconnected(ctx context.Context, in *exhook2. } //删除连接ID mqttclient.Session.Delete(etoken.DeviceId) - data := netbase.CreateEvent(message.DisConnectMes, "info", fmt.Sprintf("设备%s断开连接", etoken.Name), etoken) + data := netbase.CreateEventInfo(message.DisConnectMes, "info", fmt.Sprintf("设备%s断开连接", etoken.Name), etoken) go s.HookService.Queue.Queue(data) return &exhook2.EmptySuccess{}, nil } @@ -243,10 +243,10 @@ func (s *HookGrpcService) OnMessagePublish(ctx context.Context, in *exhook2.Mess if in.Message.Topic == ConnectGatewayTopic { if val, ok := value.(string); ok { if val == "online" { - data = netbase.CreateEvent(message.ConnectMes, "info", fmt.Sprintf("子设备%s通过网关连接", etoken.Name), auth) + data = netbase.CreateEventInfo(message.ConnectMes, "info", fmt.Sprintf("子设备%s通过网关连接", etoken.Name), auth) } if val == "offline" { - data = netbase.CreateEvent(message.ConnectMes, "info", fmt.Sprintf("子设备设备%s通过网关连接", etoken.Name), auth) + data = netbase.CreateEventInfo(message.ConnectMes, "info", fmt.Sprintf("子设备设备%s通过网关连接", etoken.Name), auth) } // 子设备发送到队列里 go s.HookService.Queue.Queue(data) diff --git a/iothub/server/httpserver/hook.go b/iothub/server/httpserver/hook.go index 6d56093..b035ef1 100644 --- a/iothub/server/httpserver/hook.go +++ b/iothub/server/httpserver/hook.go @@ -153,12 +153,12 @@ func (hhs *HookHttpService) hook(req *restful.Request, resp *restful.Response) { func (cm *ConnectionManager) AddConnection(addr string, etoken *model.DeviceAuth, service *hook_message_work.HookService) { cm.activeConnections.Store(addr, etoken) - data := netbase.CreateEvent(message.ConnectMes, "info", fmt.Sprintf("设备%s通过HTTP协议连接", etoken.Name), etoken) + data := netbase.CreateEventInfo(message.ConnectMes, "info", fmt.Sprintf("设备%s通过HTTP协议连接", etoken.Name), etoken) go service.Queue.Queue(data) } func (cm *ConnectionManager) RemoveConnection(addr string, etoken *model.DeviceAuth, service *hook_message_work.HookService) { - data := netbase.CreateEvent(message.DisConnectMes, "info", fmt.Sprintf("设备%s断开连接", etoken.Name), etoken) + data := netbase.CreateEventInfo(message.DisConnectMes, "info", fmt.Sprintf("设备%s断开连接", etoken.Name), etoken) cm.activeConnections.Delete(addr) go service.Queue.Queue(data) } diff --git a/iothub/server/tcpserver/hook.go b/iothub/server/tcpserver/hook.go index 3d9eaea..70ed90e 100644 --- a/iothub/server/tcpserver/hook.go +++ b/iothub/server/tcpserver/hook.go @@ -47,7 +47,7 @@ func handleConnection(conn *net.TCPConn, hs *hook_message_work.HookService) { defer func() { _ = conn.Close() if isAuth { - data := netbase.CreateEvent(message.DisConnectMes, "info", fmt.Sprintf("设备%s断开连接", etoken.Name), etoken) + data := netbase.CreateEventInfo(message.DisConnectMes, "info", fmt.Sprintf("设备%s断开连接", etoken.Name), etoken) go hs.Queue.Queue(data) } tcpclient.TcpClient.Delete(etoken.DeviceId) @@ -67,7 +67,7 @@ func handleConnection(conn *net.TCPConn, hs *hook_message_work.HookService) { auth := netbase.Auth(token) if auth { global.Log.Infof("TCP协议 设备%s,认证成功", etoken.DeviceId) - data := netbase.CreateEvent(message.ConnectMes, "info", fmt.Sprintf("设备%s通过TCP协议连接", etoken.Name), etoken) + data := netbase.CreateEventInfo(message.ConnectMes, "info", fmt.Sprintf("设备%s通过TCP协议连接", etoken.Name), etoken) go hs.Queue.Queue(data) isAuth = true tcpclient.TcpClient.Store(etoken.DeviceId, conn) diff --git a/iothub/server/udpserver/hook.go b/iothub/server/udpserver/hook.go index 74c4f0b..69351c0 100644 --- a/iothub/server/udpserver/hook.go +++ b/iothub/server/udpserver/hook.go @@ -44,7 +44,7 @@ func InitUdpHook(addr string, hs *hook_message_work.HookService) { _ = server.listener.Close() if isAuth, ok := authMap[client.AddrPort().String()]; ok && isAuth { - data := netbase.CreateEvent(message.DisConnectMes, "info", fmt.Sprintf("设备%s断开连接", etoken.Name), etoken) + data := netbase.CreateEventInfo(message.DisConnectMes, "info", fmt.Sprintf("设备%s断开连接", etoken.Name), etoken) go hhs.HookService.Queue.Queue(data) } udpclient.UdpClient.Delete(etoken.DeviceId) @@ -72,7 +72,7 @@ func InitUdpHook(addr string, hs *hook_message_work.HookService) { if auth { global.Log.Infof("UDP协议 设备%s,认证成功", etoken.DeviceId) - data := netbase.CreateEvent(message.ConnectMes, "info", fmt.Sprintf("设备%s通过UDP协议连接", etoken.Name), etoken) + data := netbase.CreateEventInfo(message.ConnectMes, "info", fmt.Sprintf("设备%s通过UDP协议连接", etoken.Name), etoken) go hhs.HookService.Queue.Queue(data) authMap[client.AddrPort().String()] = true diff --git a/pkg/tool/file.go b/pkg/tool/file.go index 865d577..9d21964 100644 --- a/pkg/tool/file.go +++ b/pkg/tool/file.go @@ -20,25 +20,19 @@ type Local struct { } const ( - DevicePath = "DevicePath" //设备相关 - RulePath = "RulePath" //规则链截图及json - VisualPath = "VisualPath" //组态截图及json - ThreePath = "ThreePath" //3D组态截图及json - VisualGalleryPath = "VisualGalleryPath" //组态图库 - ThreeModelPath = "ThreeModelPath" //3D模型和缩略图 - ThreeMapPath = "ThreeMapPath" //3d贴图 - OtherPath = "OtherPath" //其他 + ImagePath = "image" // 图片资源,包括贴图 + OtaPath = "ota" // 固件包 + ModelPath = "model" // 模型资源 + DataJsonPath = "data" //组态,规则链json + OtherPath = "other" //其他 ) var PathMap = map[string]string{ - DevicePath: "uploads/device", - RulePath: "uploads/rule", - VisualPath: "uploads/visual", - ThreePath: "uploads/three", - VisualGalleryPath: "uploads/visual/gallery", - ThreeModelPath: "uploads/three/model", - ThreeMapPath: "uploads/three/map", - OtherPath: "uploads/other", + ImagePath: "uploads/image", + OtaPath: "uploads/ota", + ModelPath: "uploads/model", + DataJsonPath: "uploads/json", + OtherPath: "uploads/other", } func init() {