新增获取设备属性参数&修改返回数据结构
This commit is contained in:
28
README.md
Normal file
28
README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# 奇点物联-RPC客户端
|
||||
|
||||
奇点物理Go的RPC客户端
|
||||
|
||||
## 使用示例
|
||||
~~~go
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"gitee.com/LifetimeNine/singularity-rpc-client/pkg/rpc"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 初始化
|
||||
rpc.InitiateRequest("127.0.0.1", 9631, "/root/cert.pem")
|
||||
// 获取设备详情
|
||||
result, err := rpc.GetDevice().GetDetail(10001)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
log.Println(result)
|
||||
}
|
||||
|
||||
~~~
|
||||
@@ -1,18 +1,10 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 设备详情
|
||||
type DeviceDetail struct {
|
||||
Type uint8 `json:"type"`
|
||||
ConnectType uint8 `json:"connect_type"`
|
||||
Secret string `json:"secret"`
|
||||
PId uint32 `json:"p_id"`
|
||||
DataId uint32 `json:"data_id"`
|
||||
}
|
||||
|
||||
var device *Device
|
||||
|
||||
// 获取设备相关RPC类
|
||||
@@ -27,7 +19,7 @@ func GetDevice() *Device {
|
||||
type Device struct{}
|
||||
|
||||
// 获取设备详情
|
||||
func (d *Device) GetDetail(deviceId uint32) (*Result[DeviceDetail], error) {
|
||||
func (d *Device) GetDetail(deviceId uint32) (*DeviceDetail, error) {
|
||||
body := map[string]any{
|
||||
"device_id": deviceId,
|
||||
"field_list": []string{"type", "secret", "connect_type", "p_id", "data_id"},
|
||||
@@ -36,20 +28,38 @@ func (d *Device) GetDetail(deviceId uint32) (*Result[DeviceDetail], error) {
|
||||
if err := GetRequest().Send("device.get_detail", body, result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
if result.Code != 0 {
|
||||
return nil, fmt.Errorf("%s", result.Message)
|
||||
}
|
||||
return &result.Data, nil
|
||||
}
|
||||
|
||||
// 获取设备属性列表
|
||||
func (d *Device) GetAttributeOptionList() (*[]DeviceAttributeOption, error) {
|
||||
result := &Result[[]DeviceAttributeOption]{}
|
||||
if err := GetRequest().Send("device.get_attribute_option_list", map[string]any{}, result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result.Code != 0 {
|
||||
return nil, fmt.Errorf("%s", result.Message)
|
||||
}
|
||||
return &result.Data, nil
|
||||
}
|
||||
|
||||
// 重置设备在线状态
|
||||
func (d *Device) ResetOnlineStatus() (*Result[any], error) {
|
||||
func (d *Device) ResetOnlineStatus() error {
|
||||
result := &Result[any]{}
|
||||
if err := GetRequest().Send("device.reset_online_status", map[string]any{}, result); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return result, nil
|
||||
if result.Code != 0 {
|
||||
return fmt.Errorf("%s", result.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 更新设备在线状态
|
||||
func (d *Device) UpdateOnlineStatus(deviceId uint32, online bool, updateTime time.Time) (*Result[any], error) {
|
||||
func (d *Device) UpdateOnlineStatus(deviceId uint32, online bool, updateTime time.Time) error {
|
||||
body := map[string]any{
|
||||
"device_id": deviceId,
|
||||
"online": online,
|
||||
@@ -57,13 +67,16 @@ func (d *Device) UpdateOnlineStatus(deviceId uint32, online bool, updateTime tim
|
||||
}
|
||||
result := &Result[any]{}
|
||||
if err := GetRequest().Send("device.update_online_status", body, result); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return result, nil
|
||||
if result.Code != 0 {
|
||||
return fmt.Errorf("%s", result.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 更新从设备在线状态
|
||||
func (d *Device) UpdateSlaveOnlineStatus(deviceId uint32, online bool, updateTime time.Time, parentId *uint32) (*Result[any], error) {
|
||||
func (d *Device) UpdateSlaveOnlineStatus(deviceId uint32, online bool, updateTime time.Time, parentId *uint32) error {
|
||||
body := map[string]any{
|
||||
"device_id": deviceId,
|
||||
"online": online,
|
||||
@@ -74,13 +87,16 @@ func (d *Device) UpdateSlaveOnlineStatus(deviceId uint32, online bool, updateTim
|
||||
}
|
||||
result := &Result[any]{}
|
||||
if err := GetRequest().Send("device.update_salve_online_status", body, result); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return result, nil
|
||||
if result.Code != 0 {
|
||||
return fmt.Errorf("%s", result.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 更新设备属性上报时间
|
||||
func (d *Device) UpdateReportTime(deviceId uint32, reportTime *time.Time) (*Result[any], error) {
|
||||
func (d *Device) UpdateReportTime(deviceId uint32, reportTime *time.Time) error {
|
||||
body := map[string]any{
|
||||
"device_id": deviceId,
|
||||
}
|
||||
@@ -89,26 +105,32 @@ func (d *Device) UpdateReportTime(deviceId uint32, reportTime *time.Time) (*Resu
|
||||
}
|
||||
result := &Result[any]{}
|
||||
if err := GetRequest().Send("device.update_report_time", body, result); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return result, nil
|
||||
if result.Code != 0 {
|
||||
return fmt.Errorf("%s", result.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 设置设备属性
|
||||
func (d *Device) SetAttribute(deviceId uint32, attributeSet map[string]any) (*Result[any], error) {
|
||||
func (d *Device) SetAttribute(deviceId uint32, attributeSet map[string]any) error {
|
||||
body := map[string]any{
|
||||
"device_id": deviceId,
|
||||
"attribute": attributeSet,
|
||||
}
|
||||
result := &Result[any]{}
|
||||
if err := GetRequest().Send("device.set_attribute", body, result); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return result, nil
|
||||
if result.Code != 0 {
|
||||
return fmt.Errorf("%s", result.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取设备属性
|
||||
func (d *Device) GetAttribute(deviceId uint32) (*Result[map[string]any], error) {
|
||||
func (d *Device) GetAttribute(deviceId uint32) (*map[string]any, error) {
|
||||
body := map[string]any{
|
||||
"device_id": deviceId,
|
||||
}
|
||||
@@ -116,24 +138,30 @@ func (d *Device) GetAttribute(deviceId uint32) (*Result[map[string]any], error)
|
||||
if err := GetRequest().Send("device.get_attribute", body, result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
if result.Code != 0 {
|
||||
return nil, fmt.Errorf("%s", result.Message)
|
||||
}
|
||||
return &result.Data, nil
|
||||
}
|
||||
|
||||
// 更新设备版本信息
|
||||
func (d *Device) UpdateVersion(deviceId uint32, versionNumber uint) (*Result[any], error) {
|
||||
func (d *Device) UpdateVersion(deviceId uint32, versionNumber uint) error {
|
||||
body := map[string]any{
|
||||
"device_id": deviceId,
|
||||
"version_number": versionNumber,
|
||||
}
|
||||
result := &Result[any]{}
|
||||
if err := GetRequest().Send("device.update_version", body, result); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return result, nil
|
||||
if result.Code != 0 {
|
||||
return fmt.Errorf("%s", result.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 保存升级结果
|
||||
func (d *Device) SaveUpgradeResult(upgradeTaskId uint32, status uint8, finishTime time.Time) (*Result[any], error) {
|
||||
func (d *Device) SaveUpgradeResult(upgradeTaskId uint32, status uint8, finishTime time.Time) error {
|
||||
body := map[string]any{
|
||||
"upgrade_task_id": upgradeTaskId,
|
||||
"status": status,
|
||||
@@ -141,7 +169,10 @@ func (d *Device) SaveUpgradeResult(upgradeTaskId uint32, status uint8, finishTim
|
||||
}
|
||||
result := &Result[any]{}
|
||||
if err := GetRequest().Send("device.save_upgrade_result", body, result); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return result, nil
|
||||
if result.Code != 0 {
|
||||
return fmt.Errorf("%s", result.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -41,3 +41,28 @@ type Result[D any] struct {
|
||||
Message string `json:"message"`
|
||||
Data D `json:"data"`
|
||||
}
|
||||
|
||||
// 设备详情
|
||||
type DeviceDetail struct {
|
||||
Type uint8 `json:"type"`
|
||||
ConnectType uint8 `json:"connect_type"`
|
||||
Secret string `json:"secret"`
|
||||
PId uint32 `json:"p_id"`
|
||||
DataId uint32 `json:"data_id"`
|
||||
}
|
||||
|
||||
// 设备属性参数
|
||||
type DeviceAttributeOption struct {
|
||||
Type uint8 `json:"type"`
|
||||
ContentType uint8 `json:"content_type"`
|
||||
Attribute []struct {
|
||||
Type uint8 `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Flag uint8 `json:"flag"`
|
||||
Field string `json:"field"`
|
||||
Readonly bool `json:"readonly"`
|
||||
AllowItem *[]int `json:"allow_item,omitempty"`
|
||||
Min *uint32 `json:"min,omitempty"`
|
||||
Max *uint32 `json:"max,omitempty"`
|
||||
} `json:"attribute"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user