Ver código fonte

增加查询设备事件接口

liuxiulin 12 horas atrás
pai
commit
5b9f1cdace

+ 51 - 23
pkg/deviceStatus/deviceStatus.go

@@ -4,14 +4,47 @@ import (
 	"github.com/gogf/gf/database/gredis"
 )
 
-const (
-	splitStatusKeyPrefix  = "device:split:status:"
-	mainStatusKeyPrefix   = "device:main:status:"
-	ffxStatusKeyPrefix    = "device:ffx:status:"
-	newfanStatusKeyPrefix = "device:newfan:status:"
+var StatusKey = map[string]string{
+	"1pw4umsiq2hd4w7p7o68k69bb0bnlzw0": "device:split:status:",
+	"1umcgk0i6m0ch45z0hbgrf8900k4s576": "device:main:status:",
+	"1pw4umsgxzld8kwvsm28ndz2f0dktty8": "device:main:status:",
+	"1pw4ums6ty0d5pyua33f65h100yf45z2": "device:ffx:status:",
+	"1pw4umsgxzlda7g4p1zcd6v2m0ukxftv": "device:newfan:status:",
+	"1pw4umsimu4dcvsvph9iepl300r7d7v9": "device:wifi:status:",
+	"1pw4umsgxzldbd8wyvknk623f0e2obdb": "device:fp:status:",
+	"1pw4umsgxzld9eikeie2o822g0hy0yyn": "device:th4G:status:",
+	"1pw4umsiq2hd4hplv2mhp9mba0l1bv87": "device:twb:status:",
+	"1pw4umsgxzldbd6c46r0qpn3e0jekjgj": "device:wj:status:",
+}
+
+var InfoKey = map[string]string{
+	"1pw4umsiq2hd4w7p7o68k69bb0bnlzw0": "device:split:info:",
+	"1umcgk0i6m0ch45z0hbgrf8900k4s576": "device:main:info:",
+	"1pw4umsgxzld8kwvsm28ndz2f0dktty8": "device:main:info:",
+	"1pw4ums6ty0d5pyua33f65h100yf45z2": "device:ffx:info:",
+	"1pw4umsgxzlda7g4p1zcd6v2m0ukxftv": "device:newfan:info:",
+	"1pw4umsimu4dcvsvph9iepl300r7d7v9": "device:wifi:info:",
+	"1pw4umsgxzldbd8wyvknk623f0e2obdb": "device:fp:info:",
+	"1pw4umsgxzld9eikeie2o822g0hy0yyn": "device:th4G:info:",
+	"1pw4umsiq2hd4hplv2mhp9mba0l1bv87": "device:twb:info:",
+	"1pw4umsgxzldbd6c46r0qpn3e0jekjgj": "device:wj:info:",
+}
 
-	ffxInfoKeyPrefix = "device:ffx:info:"
-	dataExpires      = 7200
+var EventKey = map[string]string{
+	"1pw4umsiq2hd4w7p7o68k69bb0bnlzw0": "device:split:event:",
+	"1umcgk0i6m0ch45z0hbgrf8900k4s576": "device:main:event:",
+	"1pw4umsgxzld8kwvsm28ndz2f0dktty8": "device:main:event:",
+	"1pw4ums6ty0d5pyua33f65h100yf45z2": "device:ffx:event:",
+	"1pw4umsgxzlda7g4p1zcd6v2m0ukxftv": "device:newfan:event:",
+	"1pw4umsimu4dcvsvph9iepl300r7d7v9": "device:wifi:event:",
+	"1pw4umsgxzldbd8wyvknk623f0e2obdb": "device:fp:event:",
+	"1pw4umsgxzld9eikeie2o822g0hy0yyn": "device:th4G:event:",
+	"1pw4umsiq2hd4hplv2mhp9mba0l1bv87": "device:twb:event:",
+	"1pw4umsgxzldbd6c46r0qpn3e0jekjgj": "device:wj:event:",
+}
+
+const (
+	dataExpires = 7200
 )
 
 type SplitStatus struct {
@@ -47,17 +80,7 @@ func NewDevStatusManager(host string, port int) *DevStatusManager {
 }
 
 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
-	}
+	key := StatusKey[productId] + id
 	// get status from redis
 	result, err := mgr.redisClient.DoVar("GET", key)
 	if err != nil {
@@ -67,11 +90,7 @@ func (mgr *DevStatusManager) GetDeviceStatus(id, productId string) (string, erro
 }
 
 func (mgr *DevStatusManager) GetDeviceInfo(id, productId string) (string, error) {
-	var key string
-	switch productId {
-	case "1pw4ums6ty0d5pyua33f65h100yf45z2":
-		key = ffxInfoKeyPrefix + id
-	}
+	key := InfoKey[productId] + id
 	// get status from redis
 	result, err := mgr.redisClient.DoVar("GET", key)
 	if err != nil {
@@ -111,3 +130,12 @@ func (mgr *DevStatusManager) SetWeatherInfo(location string, info string) error
 	}
 	return nil
 }
+
+func (mgr *DevStatusManager) GetDeviceEvent(id, productId string) (string, error) {
+	key := EventKey[productId] + id
+	result, err := mgr.redisClient.DoVar("GET", key)
+	if err != nil {
+		return "", err
+	}
+	return result.String(), nil
+}

+ 4 - 0
pkg/rpcs/access.go

@@ -75,6 +75,10 @@ type ReplayInfo struct {
 	Info string
 }
 
+type ReplayEvent struct {
+	Event string
+}
+
 type ArgsGetWeather struct {
 	Location string
 }

+ 11 - 0
services/devicemanager/manager.go

@@ -149,6 +149,17 @@ func (dm *DeviceManager) GetDeviceInfo(args rpcs.ArgsGetStatus, reply *rpcs.Repl
 	return nil
 }
 
+func (dm *DeviceManager) GetDeviceEvent(args rpcs.ArgsGetStatus, reply *rpcs.ReplayEvent) error {
+	event, err := dm.statusManager.GetDeviceEvent(args.Id, args.ProductId)
+	if err != nil {
+		return err
+	}
+	if event != "" {
+		reply.Event = event
+	}
+	return nil
+}
+
 func (dm *DeviceManager) GetDeviceStatusByKey(args rpcs.ArgsGetStatus, reply *rpcs.ReplyStatus) error {
 	status, err := dm.statusManager.GetDeviceStatusByKey(args.Key)
 	if err != nil {

+ 15 - 0
services/knowoapi/controllers/device.go

@@ -276,6 +276,21 @@ func (a *DeviceController) GetStatus() {
 	})
 }
 
+// GetEvent 获取设备事件信息
+// GET /device/event?device_id=&product_id=
+func (a *DeviceController) GetEvent() {
+	deviceId := a.Ctx.URLParam("device_id")
+	productId := a.Ctx.URLParam("product_id")
+	data, err := a.Service.GetDeviceEvent(deviceId, productId)
+	if err != nil {
+		responseError(a.Ctx, ErrNormal, err.Error())
+		return
+	}
+	done(a.Ctx, map[string]interface{}{
+		"data": data.MustToJsonString(),
+	})
+}
+
 // PostReport  获取设备状态
 // POST /device/report
 func (a *DeviceController) PostReport() {

+ 16 - 0
services/knowoapi/services/device.go

@@ -34,6 +34,8 @@ type DeviceService interface {
 	GetUpgradeProgress(deviceId string) (rpcs.ReplyOtaProgress, error)
 	// GetDeviceStatus 获取设备状态数据
 	GetDeviceStatus(deviceId, productId string) (*gjson.Json, error)
+	// GetDeviceEvent 获取设备事件信息
+	GetDeviceEvent(deviceId, productId string) (*gjson.Json, error)
 	// SetReport 获取设备状态
 	SetReport(params models.SendSplitCommandParams) (*gjson.Json, error)
 	// Restart 重启设备
@@ -225,6 +227,20 @@ func (a deviceservice) GetDeviceStatus(deviceId, productId string) (*gjson.Json,
 	return gjson.New(reply.Status), nil
 }
 
+func (a deviceservice) GetDeviceEvent(deviceId, productId string) (*gjson.Json, error) {
+	var args rpcs.ArgsGetStatus
+	args.Id = deviceId
+	args.ProductId = productId
+	var reply rpcs.ReplayEvent
+
+	err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceEvent", args, &reply)
+	if err != nil {
+		server.Log.Errorf("设备事件数据获取失败:%v", err)
+		return nil, err
+	}
+	return gjson.New(reply.Event), nil
+}
+
 // SetReport 获取设备状态
 func (a deviceservice) SetReport(params models.SendSplitCommandParams) (*gjson.Json, error) {
 	var args rpcs.ArgsSendCommand