[优化]文件上传

This commit is contained in:
tfl
2024-08-22 17:43:38 +08:00
parent dd5b38b4e3
commit 96e2500fad
10 changed files with 62 additions and 30 deletions

View File

@@ -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 {

View File

@@ -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)
}
}()
}

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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,

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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

View File

@@ -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() {