修改操作

This commit is contained in:
Jason
2025-11-25 17:01:32 +08:00
parent b1540e657e
commit adbc4dc7ca
6 changed files with 83 additions and 69 deletions

View File

@@ -13,7 +13,6 @@ func init() {
api.Register("GET", "iot/device/:id/sync", deviceSync)
api.Register("GET", "iot/device/:id/read", deviceRead)
api.Register("POST", "iot/device/:id/write", deviceWrite)
api.Register("POST", "iot/device/:id/action/:action", deviceAction)
api.Register("GET", "iot/device/extend/fields", deviceExtendFields)
}
@@ -100,27 +99,3 @@ func deviceWrite(ctx *gin.Context) {
api.OK(ctx, result)
}
func deviceAction(ctx *gin.Context) {
d := devices.Load(ctx.Param("id"))
if d == nil {
api.Fail(ctx, "设备未上线")
return
}
action := ctx.Param("action")
var values map[string]any
err := ctx.ShouldBind(&values)
if err != nil {
api.Error(ctx, err)
return
}
result, err := d.Action(action, values, 30)
if err != nil {
api.Error(ctx, err)
return
}
api.OK(ctx, result)
}

View File

@@ -17,6 +17,9 @@ func init() {
api.Register("GET", "iot/device/:id/model", curd.ApiGet[DeviceModel]())
api.Register("POST", "iot/device/:id/model", deviceModelUpdate)
//执行操作
api.Register("POST", "iot/device/:id/action/:action", deviceAction)
//参数
api.Register("GET", "iot/device/:id/setting/:name", deviceSetting)
api.Register("POST", "iot/device/:id/setting/:name", deviceSettingUpdate)
@@ -113,3 +116,27 @@ func deviceSettingUpdate(ctx *gin.Context) {
api.OK(ctx, content)
}
func deviceAction(ctx *gin.Context) {
d := devices.Load(ctx.Param("id"))
if d == nil {
api.Fail(ctx, "设备未上线")
return
}
action := ctx.Param("action")
var values map[string]any
err := ctx.ShouldBind(&values)
if err != nil {
api.Error(ctx, err)
return
}
result, err := d.Action(action, values, 30)
if err != nil {
api.Error(ctx, err)
return
}
api.OK(ctx, result)
}

View File

@@ -201,6 +201,13 @@ func (d *Device) waitResponse(msg_id string, timeout int) (any, error) {
}
func (d *Device) Sync(timeout int) (map[string]any, error) {
if d.protocol == "" || d.linker == "" || d.LinkId == "" {
mqtt.Publish("device/"+d.Id+"/sync", nil)
//TODO 等待 /device/{id}/sync/response
return nil, nil
//return nil, errors.New("无协议和连接")
}
req := protocol.SyncRequest{
MsgId: strconv.FormatInt(rand.Int63(), 10),
DeviceId: d.Id,
@@ -235,6 +242,12 @@ func (d *Device) onSyncResponse(resp *protocol.SyncResponse) {
}
func (d *Device) Read(points []string, timeout int) (map[string]any, error) {
if d.protocol == "" || d.linker == "" || d.LinkId == "" {
mqtt.Publish("device/"+d.Id+"/read", points)
//TODO 等待 /device/{id}/read/response
return nil, nil
}
req := protocol.ReadRequest{
MsgId: strconv.FormatInt(rand.Int63(), 10),
DeviceId: d.Id,
@@ -270,6 +283,12 @@ func (d *Device) onReadResponse(resp *protocol.ReadResponse) {
}
func (d *Device) Write(values map[string]any, timeout int) (map[string]bool, error) {
if d.protocol == "" || d.linker == "" || d.LinkId == "" {
mqtt.Publish("device/"+d.Id+"/write", values)
//TODO 等待 /device/{id}/write/response
return nil, nil
}
req := protocol.WriteRequest{
MsgId: strconv.FormatInt(rand.Int63(), 10),
DeviceId: d.Id,
@@ -305,6 +324,12 @@ func (d *Device) onWriteResponse(resp *protocol.WriteResponse) {
}
func (d *Device) Action(action string, parameters map[string]any, timeout int) (map[string]any, error) {
if d.protocol == "" || d.linker == "" || d.LinkId == "" {
mqtt.Publish("device/"+d.Id+"/action/"+action, parameters)
//TODO 等待 /device/{id}/action/{name}/response
return nil, nil
}
req := protocol.ActionRequest{
MsgId: strconv.FormatInt(rand.Int63(), 10),
DeviceId: d.Id,

View File

@@ -153,6 +153,35 @@
"label": "显示",
"type": "text"
},
{
"key": "type",
"label": "类型",
"type": "select",
"default": "number",
"options": [
{
"label": "按钮",
"value": "button"
},
{
"label": "开关",
"value": "switch"
},
{
"label": "滑块",
"value": "slider"
},
{
"label": "表单",
"value": "form"
}
]
},
{
"key": "bind",
"label": "数据绑定",
"type": "text"
},
{
"key": "parameters",
"type": "table",
@@ -359,35 +388,6 @@
"label": "显示",
"type": "text"
},
{
"key": "type",
"label": "类型",
"type": "select",
"default": "number",
"options": [
{
"label": "按钮",
"value": "button"
},
{
"label": "开关",
"value": "switch"
},
{
"label": "滑块",
"value": "slider"
},
{
"label": "表单",
"value": "form"
}
]
},
{
"key": "bind",
"label": "数据绑定",
"type": "text"
},
{
"key": "parameters",
"type": "table",

View File

@@ -37,19 +37,12 @@ type Event struct {
}
type Action struct {
Name string `json:"name,omitempty"`
Label string `json:"label,omitempty"`
Description string `json:"description,omitempty"`
Parameters []Parameter `json:"parameters,omitempty"`
Returns []Parameter `json:"returns,omitempty"`
}
type Operator struct {
Name string `json:"name,omitempty"`
Label string `json:"label,omitempty"`
Type string `json:"type,omitempty"` //类型 button switch/toggle slider
Bind string `json:"bind,omitempty"` //绑定值(状态)
Parameters []Parameter `json:"parameters,omitempty"`
Returns []Parameter `json:"returns,omitempty"`
}
type Setting struct {
@@ -57,13 +50,13 @@ type Setting struct {
Label string `json:"label,omitempty"`
Fields []smart.Field `json:"fields,omitempty"`
}
type ProductModel struct {
Id string `json:"id,omitempty" xorm:"pk"`
Properties []*Property `json:"properties,omitempty" xorm:"json"` //属性表,分组的形式
Events []*Event `json:"events,omitempty" xorm:"json"` //事件表
Actions []*Action `json:"actions,omitempty" xorm:"json"` //响应表
Validators []*Validator `json:"validators,omitempty" xorm:"json"` //检查
Operators []*Operator `json:"operators,omitempty" xorm:"json"` //按钮操作
Settings []*Setting `json:"settings,omitempty" xorm:"json"` //参数配置
Updated time.Time `json:"updated,omitempty" xorm:"updated"`
Created time.Time `json:"created,omitempty" xorm:"created"`

View File

@@ -33,12 +33,6 @@
"type": "txt",
"json": true
},
{
"name": "operators",
"comment": "操作项",
"type": "txt",
"json": true
},
{
"name": "settings",
"comment": "配置项",