Browse Source

增加四寸屏、新风控制器查询

liuxiulin 1 week ago
parent
commit
b057a4f309

+ 25 - 7
pkg/deviceStatus/deviceStatus.go

@@ -5,9 +5,13 @@ import (
 )
 
 const (
-	StatusKeyPrefix = "device:split:status:"
-	InfoKeyPrefix   = "device:ffx:info:"
-	dataExpires     = 7200
+	splitStatusKeyPrefix  = "device:split:status:"
+	mainStatusKeyPrefix   = "device:main:status:"
+	ffxStatusKeyPrefix    = "device:ffx:status:"
+	newfanStatusKeyPrefix = "device:newfan:status:"
+
+	ffxInfoKeyPrefix = "device:ffx:info:"
+	dataExpires      = 7200
 )
 
 type SplitStatus struct {
@@ -42,8 +46,18 @@ func NewDevStatusManager(host string, port int) *DevStatusManager {
 	return mgr
 }
 
-func (mgr *DevStatusManager) GetDeviceStatus(id string) (string, error) {
-	key := StatusKeyPrefix + id
+func (mgr *DevStatusManager) GetDeviceStatus(id, productId string) (string, error) {
+	var key string
+	switch productId {
+	case "1pw4umsiq2hd4w7p7o68k69bb0bnlzw0":
+		key = splitStatusKeyPrefix + id
+	case "1pw4umsgxzlda7g4p1zcd6v2m0ukxftv":
+		key = newfanStatusKeyPrefix + id
+	case "1pw4ums6ty0d5pyua33f65h100yf45z2":
+		key = ffxStatusKeyPrefix + id
+	default:
+		key = mainStatusKeyPrefix + id
+	}
 	// get status from redis
 	result, err := mgr.redisClient.DoVar("GET", key)
 	if err != nil {
@@ -52,8 +66,12 @@ func (mgr *DevStatusManager) GetDeviceStatus(id string) (string, error) {
 	return result.String(), nil
 }
 
-func (mgr *DevStatusManager) GetDeviceInfo(id string) (string, error) {
-	key := InfoKeyPrefix + id
+func (mgr *DevStatusManager) GetDeviceInfo(id, productId string) (string, error) {
+	var key string
+	switch productId {
+	case "1pw4ums6ty0d5pyua33f65h100yf45z2":
+		key = ffxInfoKeyPrefix + id
+	}
 	// get status from redis
 	result, err := mgr.redisClient.DoVar("GET", key)
 	if err != nil {

+ 17 - 1
pkg/models/device.go

@@ -88,8 +88,9 @@ const (
 	OtaUpgrade      Command = "devUpgrade"      // ota升级
 )
 
-type SendCommandParams struct {
+type SendSplitCommandParams struct {
 	DeviceId     string `json:"device_id"`
+	ProductId    string `json:"product_id"`
 	Enable       int    `json:"enable"`         // 是否启用(0:禁用,1:启用)
 	Internal     int    `json:"internal"`       // 间隔时间,单位:秒,范围:30~180,默认:30秒
 	Module       int    `json:"module"`         // 对应模块(1:新风模块2:加湿模块)
@@ -103,6 +104,21 @@ type SendCommandParams struct {
 	HeatModeTemp int    `json:"heat_mode_temp"` // 制热模式出水温度,0:代表不设置
 }
 
+// Validate 验证
+func (a *SendSplitCommandParams) Validate() error {
+	if a.DeviceId == "" {
+		return errors.New("非法参数[DeviceIDs, Label]")
+	}
+	return nil
+}
+
+type SendCommandParams struct {
+	DeviceId    string                 `json:"device_id"`
+	SunDeviceId string                 `json:"sun_device_id"`
+	Cmd         string                 `json:"cmd"`
+	Params      map[string]interface{} `json:"params"`
+}
+
 // Validate 验证
 func (a *SendCommandParams) Validate() error {
 	if a.DeviceId == "" {

+ 1 - 0
pkg/rpcs/access.go

@@ -12,6 +12,7 @@ type ReplySetStatus ReplyEmptyResult
 
 type ArgsGetStatus struct {
 	Id          string
+	ProductId   string
 	VendorId    string
 	SubDeviceId string
 }

+ 2 - 2
services/devicemanager/manager.go

@@ -124,7 +124,7 @@ func (dm *DeviceManager) GetProgress(args rpcs.ArgsOtaProgress, reply *rpcs.Repl
 }
 
 func (dm *DeviceManager) GetDeviceStatus(args rpcs.ArgsGetStatus, reply *rpcs.ReplyStatus) error {
-	status, err := dm.statusManager.GetDeviceStatus(args.Id)
+	status, err := dm.statusManager.GetDeviceStatus(args.Id, args.ProductId)
 	if err != nil {
 		return err
 	}
@@ -135,7 +135,7 @@ func (dm *DeviceManager) GetDeviceStatus(args rpcs.ArgsGetStatus, reply *rpcs.Re
 }
 
 func (dm *DeviceManager) GetDeviceInfo(args rpcs.ArgsGetStatus, reply *rpcs.ReplayInfo) error {
-	info, err := dm.statusManager.GetDeviceInfo(args.Id)
+	info, err := dm.statusManager.GetDeviceInfo(args.Id, args.ProductId)
 	if err != nil {
 		return err
 	}

+ 1 - 5
services/emqx-agent/agent.go

@@ -185,7 +185,6 @@ func processReportStatus(topicInfo *protocol.TopicInfo, vendorId string, message
 func (a *Access) processDeviceUpgrade(deviceId string, message *gjson.Json) error {
 	var reply rpcs.ReplyEmptyResult
 	data := gjson.New(message.GetJson("data").MustToJson())
-	server.Log.Infof("收到指令:%s", data.MustToJsonString())
 	switch data.GetString("cmd") {
 	case "download":
 		params := gjson.New(data.GetJson("params").MustToJson())
@@ -218,10 +217,7 @@ func (a *Access) processDeviceUpgrade(deviceId string, message *gjson.Json) erro
 		device := a.GetLockDevice(deviceId)
 		device.Mutex.Lock()
 		defer device.Mutex.Unlock()
-		if device != nil {
-			device.Locked = false
-		}
-
+		device.Locked = false
 		server.Log.Infof("OTA升级完成;%s", deviceId)
 	}
 

+ 38 - 14
services/knowoapi/controllers/device.go

@@ -261,10 +261,11 @@ func (a *DeviceController) GetOtaProgress() {
 }
 
 // GetStatus 获取设备信息
-// GET /device/status?device_id=
+// GET /device/status?device_id=&product_id=
 func (a *DeviceController) GetStatus() {
 	deviceId := a.Ctx.URLParam("device_id")
-	data, err := a.Service.GetDeviceStatus(deviceId)
+	productId := a.Ctx.URLParam("product_id")
+	data, err := a.Service.GetDeviceStatus(deviceId, productId)
 	if err != nil {
 		responseError(a.Ctx, ErrNormal, err.Error())
 		return
@@ -277,7 +278,7 @@ func (a *DeviceController) GetStatus() {
 // PostReport  获取设备状态
 // POST /device/report
 func (a *DeviceController) PostReport() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -294,7 +295,7 @@ func (a *DeviceController) PostReport() {
 // PostRestart 重启设备
 // POST /device/restart
 func (a *DeviceController) PostRestart() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -311,7 +312,7 @@ func (a *DeviceController) PostRestart() {
 // PostClear 清除设备配置数据
 // POST /device/clear
 func (a *DeviceController) PostClear() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -328,7 +329,7 @@ func (a *DeviceController) PostClear() {
 // PostDatatrans 设备端自动上报配置
 // POST /device/datarrans
 func (a *DeviceController) PostDatatrans() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -345,7 +346,7 @@ func (a *DeviceController) PostDatatrans() {
 // PostInfo
 // POST /device/info
 func (a *DeviceController) PostInfo() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -362,7 +363,7 @@ func (a *DeviceController) PostInfo() {
 // PostForcerun 远程控制某个模块强制运行
 // POST /device/forcerun
 func (a *DeviceController) PostForcerun() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -379,7 +380,7 @@ func (a *DeviceController) PostForcerun() {
 // PostId  写入设备 ID
 // POST /device/id
 func (a *DeviceController) PostId() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -396,7 +397,7 @@ func (a *DeviceController) PostId() {
 // PostFjsq 智能分集水器控制
 // POST /device/fjsq
 func (a *DeviceController) PostFjsq() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -413,7 +414,7 @@ func (a *DeviceController) PostFjsq() {
 // PostOutdoorpower 设置水系统外机电源状态
 // POST /device/outdoorpower
 func (a *DeviceController) PostOutdoorpower() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -430,7 +431,7 @@ func (a *DeviceController) PostOutdoorpower() {
 // PostOutdoortemp 设置水系统外机出水温度
 // POST /device/outdoortemp
 func (a *DeviceController) PostOutdoortemp() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -447,7 +448,7 @@ func (a *DeviceController) PostOutdoortemp() {
 // PostOutdoorlinkage 开启/关闭水系统联动
 // POST /device/outdoorlinkage
 func (a *DeviceController) PostOutdoorlinkage() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -464,7 +465,7 @@ func (a *DeviceController) PostOutdoorlinkage() {
 // PostMixedwaterlinkage 开启/关闭调温中心联动
 // POST /device/mixedwaterlinkage
 func (a *DeviceController) PostMixedwaterlinkage() {
-	params := new(models.SendCommandParams)
+	params := new(models.SendSplitCommandParams)
 	if err := parseBody(a.Ctx, params); err != nil {
 		badRequest(a.Ctx, err)
 		return
@@ -492,3 +493,26 @@ func (a *DeviceController) Put() {
 	}
 	done(a.Ctx, "已保存")
 }
+
+// PostSendCommand 下发指令
+// POST /device/sendcommand
+func (a *DeviceController) PostSendCommand() {
+	params := new(models.SendCommandParams)
+	if err := parseBody(a.Ctx, params); err != nil {
+		badRequest(a.Ctx, err)
+		return
+	}
+
+	err := a.Service.SendCommand(rpcs.ArgsSendCommand{
+		DeviceId:  params.DeviceId,
+		SubDevice: params.SunDeviceId,
+		Cmd:       params.Cmd,
+		Params:    params.Params,
+	})
+
+	if err != nil {
+		responseError(a.Ctx, ErrNormal, err.Error())
+		return
+	}
+	done(a.Ctx, params.DeviceId)
+}

+ 43 - 28
services/knowoapi/services/device.go

@@ -32,31 +32,33 @@ type DeviceService interface {
 	// GetUpgradeProgress 获取ota升级进度
 	GetUpgradeProgress(deviceId string) (rpcs.ReplyOtaProgress, error)
 	// GetDeviceStatus 获取设备状态数据
-	GetDeviceStatus(deviceId string) (*gjson.Json, error)
+	GetDeviceStatus(deviceId, productId string) (*gjson.Json, error)
 	// SetReport 获取设备状态
-	SetReport(params models.SendCommandParams) (*gjson.Json, error)
+	SetReport(params models.SendSplitCommandParams) (*gjson.Json, error)
 	// Restart 重启设备
-	Restart(params models.SendCommandParams) error
+	Restart(params models.SendSplitCommandParams) error
 	// ClearData 清除设备配置数据
-	ClearData(params models.SendCommandParams) error
+	ClearData(params models.SendSplitCommandParams) error
 	// SetDataTrans 设备端自动上报配置
-	SetDataTrans(params models.SendCommandParams) error
+	SetDataTrans(params models.SendSplitCommandParams) error
 	// GetInfo 获取网关信息
-	GetInfo(params models.SendCommandParams) (*gjson.Json, error)
+	GetInfo(params models.SendSplitCommandParams) (*gjson.Json, error)
 	// ForceRun 远程控制某个模块强制运行
-	ForceRun(params models.SendCommandParams) error
+	ForceRun(params models.SendSplitCommandParams) error
 	// SetDeviceId 写入设备id
-	SetDeviceId(params models.SendCommandParams) error
+	SetDeviceId(params models.SendSplitCommandParams) error
 	// SetFjsqStatus 智能分集水器控制
-	SetFjsqStatus(params models.SendCommandParams) error
+	SetFjsqStatus(params models.SendSplitCommandParams) error
 	// SetOutdoorPower 设置水系统外机电源状态
-	SetOutdoorPower(params models.SendCommandParams) error
+	SetOutdoorPower(params models.SendSplitCommandParams) error
 	// SetOutdoorTemp 设置水系统外机出水温度
-	SetOutdoorTemp(params models.SendCommandParams) error
+	SetOutdoorTemp(params models.SendSplitCommandParams) error
 	// SetOutdoorLinkage 开启/关闭水系统联动
-	SetOutdoorLinkage(params models.SendCommandParams) error
+	SetOutdoorLinkage(params models.SendSplitCommandParams) error
 	// SetMixedWaterLinkage 开启/关闭调温中心联动
-	SetMixedWaterLinkage(params models.SendCommandParams) error
+	SetMixedWaterLinkage(params models.SendSplitCommandParams) error
+	// SendCommand 下发指令
+	SendCommand(args rpcs.ArgsSendCommand) error
 }
 
 type deviceservice struct {
@@ -206,11 +208,12 @@ func (a deviceservice) GetUpgradeProgress(deviceId string) (rpcs.ReplyOtaProgres
 	return reply, nil
 }
 
-func (a deviceservice) GetDeviceStatus(deviceId string) (*gjson.Json, error) {
+func (a deviceservice) GetDeviceStatus(deviceId, productId string) (*gjson.Json, error) {
 	var args rpcs.ArgsGetStatus
 	args.Id = deviceId
-
+	args.ProductId = productId
 	var reply rpcs.ReplyStatus
+
 	err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceStatus", args, &reply)
 	if err != nil {
 		server.Log.Errorf("设备状态数据获取失败:%v", err)
@@ -220,7 +223,7 @@ func (a deviceservice) GetDeviceStatus(deviceId string) (*gjson.Json, error) {
 }
 
 // SetReport 获取设备状态
-func (a deviceservice) SetReport(params models.SendCommandParams) (*gjson.Json, error) {
+func (a deviceservice) SetReport(params models.SendSplitCommandParams) (*gjson.Json, error) {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.Report)
@@ -228,11 +231,11 @@ func (a deviceservice) SetReport(params models.SendCommandParams) (*gjson.Json,
 	if err != nil {
 		return nil, err
 	}
-	return a.GetDeviceStatus(params.DeviceId)
+	return a.GetDeviceStatus(params.DeviceId, params.ProductId)
 }
 
 // Restart 重启设备
-func (a deviceservice) Restart(params models.SendCommandParams) error {
+func (a deviceservice) Restart(params models.SendSplitCommandParams) error {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.Restart)
@@ -240,7 +243,7 @@ func (a deviceservice) Restart(params models.SendCommandParams) error {
 }
 
 // ClearData 清除设备配置数据
-func (a deviceservice) ClearData(params models.SendCommandParams) error {
+func (a deviceservice) ClearData(params models.SendSplitCommandParams) error {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.ClearData)
@@ -248,7 +251,7 @@ func (a deviceservice) ClearData(params models.SendCommandParams) error {
 }
 
 // SetDataTrans 设备端自动上报配置
-func (a deviceservice) SetDataTrans(params models.SendCommandParams) error {
+func (a deviceservice) SetDataTrans(params models.SendSplitCommandParams) error {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Params = map[string]interface{}{
@@ -260,7 +263,7 @@ func (a deviceservice) SetDataTrans(params models.SendCommandParams) error {
 }
 
 // GetInfo 获取网关信息
-func (a deviceservice) GetInfo(params models.SendCommandParams) (*gjson.Json, error) {
+func (a deviceservice) GetInfo(params models.SendSplitCommandParams) (*gjson.Json, error) {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.GetInfo)
@@ -271,6 +274,7 @@ func (a deviceservice) GetInfo(params models.SendCommandParams) (*gjson.Json, er
 
 	var newArgs rpcs.ArgsGetStatus
 	newArgs.Id = params.DeviceId
+	newArgs.ProductId = params.ProductId
 	var reply rpcs.ReplayInfo
 
 	err = server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceInfo", newArgs, &reply)
@@ -282,7 +286,7 @@ func (a deviceservice) GetInfo(params models.SendCommandParams) (*gjson.Json, er
 }
 
 // ForceRun 远程控制某个模块强制运行
-func (a deviceservice) ForceRun(params models.SendCommandParams) error {
+func (a deviceservice) ForceRun(params models.SendSplitCommandParams) error {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.ForceRun)
@@ -294,7 +298,7 @@ func (a deviceservice) ForceRun(params models.SendCommandParams) error {
 }
 
 // SetDeviceId 写入设备id
-func (a deviceservice) SetDeviceId(params models.SendCommandParams) error {
+func (a deviceservice) SetDeviceId(params models.SendSplitCommandParams) error {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.SetDeviceId)
@@ -306,7 +310,7 @@ func (a deviceservice) SetDeviceId(params models.SendCommandParams) error {
 }
 
 // SetFjsqStatus 智能分集水器控制
-func (a deviceservice) SetFjsqStatus(params models.SendCommandParams) error {
+func (a deviceservice) SetFjsqStatus(params models.SendSplitCommandParams) error {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.SetFjsqStatus)
@@ -318,7 +322,7 @@ func (a deviceservice) SetFjsqStatus(params models.SendCommandParams) error {
 }
 
 // SetOutdoorPower 设置水系统外机电源状态
-func (a deviceservice) SetOutdoorPower(params models.SendCommandParams) error {
+func (a deviceservice) SetOutdoorPower(params models.SendSplitCommandParams) error {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.SetOutdoorPower)
@@ -329,7 +333,7 @@ func (a deviceservice) SetOutdoorPower(params models.SendCommandParams) error {
 }
 
 // SetOutdoorTemp 设置水系统外机出水温度
-func (a deviceservice) SetOutdoorTemp(params models.SendCommandParams) error {
+func (a deviceservice) SetOutdoorTemp(params models.SendSplitCommandParams) error {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.SetOutdoorTemp)
@@ -341,7 +345,7 @@ func (a deviceservice) SetOutdoorTemp(params models.SendCommandParams) error {
 }
 
 // SetOutdoorLinkage 开启/关闭水系统联动
-func (a deviceservice) SetOutdoorLinkage(params models.SendCommandParams) error {
+func (a deviceservice) SetOutdoorLinkage(params models.SendSplitCommandParams) error {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.SetOutdoorTemp)
@@ -352,7 +356,7 @@ func (a deviceservice) SetOutdoorLinkage(params models.SendCommandParams) error
 }
 
 // SetMixedWaterLinkage 开启/关闭调温中心联动
-func (a deviceservice) SetMixedWaterLinkage(params models.SendCommandParams) error {
+func (a deviceservice) SetMixedWaterLinkage(params models.SendSplitCommandParams) error {
 	var args rpcs.ArgsSendCommand
 	args.DeviceId = params.DeviceId
 	args.Cmd = string(models.SetOutdoorTemp)
@@ -372,3 +376,14 @@ func (a deviceservice) sendCommand(args rpcs.ArgsSendCommand) error {
 	}
 	return nil
 }
+
+// SendCommand 下发指令
+func (a deviceservice) SendCommand(args rpcs.ArgsSendCommand) error {
+	var reply rpcs.ReplySendCommand
+	err := server.RPCCallByName(nil, rpcs.ControllerName, "Controller.SendCommand", args, &reply)
+	if err != nil {
+		server.Log.Errorf("指令下发失败:%v", err)
+		return err
+	}
+	return nil
+}