|
@@ -8,9 +8,11 @@ import (
|
|
|
"sparrow/pkg/protocol"
|
|
|
"sparrow/pkg/rpcs"
|
|
|
"sparrow/pkg/server"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type Agent struct {
|
|
|
+ client SubDev
|
|
|
}
|
|
|
|
|
|
// Message 收到设备上报消息处理
|
|
@@ -204,6 +206,55 @@ func (a *Agent) Disconnected(status *protocol.DevConnectStatus) error {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
-func NewAgent() *Agent {
|
|
|
- return &Agent{}
|
|
|
+// SendCommand rpc 发送设备命令
|
|
|
+func (a *Agent) SendCommand(args rpcs.ArgsSendCommand, reply *rpcs.ReplySendCommand) error {
|
|
|
+ // 查询设备信息
|
|
|
+ device := &models.Device{}
|
|
|
+ err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.FindDeviceByIdentifier", args.DeviceId, device)
|
|
|
+ if err != nil {
|
|
|
+ server.Log.Errorf("device not found %s", args.DeviceId)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ product := &models.Product{}
|
|
|
+ err = server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.FindProduct", device.ProductID, device)
|
|
|
+ if err != nil {
|
|
|
+ server.Log.Errorf("device not found %s", args.DeviceId)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ cmd := &klink.CloudSend{
|
|
|
+ Action: "cloudSend",
|
|
|
+ MsgId: 0,
|
|
|
+ DeviceCode: args.DeviceId,
|
|
|
+ SubDeviceId: args.SubDevice,
|
|
|
+ Timestamp: time.Now().Unix(),
|
|
|
+ Data: &klink.CloudSendData{
|
|
|
+ Cmd: args.Cmd,
|
|
|
+ Params: args.Params,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ msg, err := cmd.Marshal()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return a.client.PublishToMsgToDev(protocol.GetCommandTopic(args.DeviceId, product.RecordId), msg)
|
|
|
+}
|
|
|
+
|
|
|
+// GetStatus rpc 获取设备状态
|
|
|
+func (a *Agent) GetStatus(args rpcs.ArgsGetStatus, reply *rpcs.ReplyGetStatus) error {
|
|
|
+ server.Log.Infof("Access Get Status: %v", args)
|
|
|
+ // first send a get status command
|
|
|
+ cmdArgs := rpcs.ArgsSendCommand{
|
|
|
+ DeviceId: args.Id,
|
|
|
+ WaitTime: 0,
|
|
|
+ SubDevice: args.SubDeviceId,
|
|
|
+ Cmd: "report",
|
|
|
+ }
|
|
|
+ cmdReply := rpcs.ReplySendCommand{}
|
|
|
+ return a.SendCommand(cmdArgs, &cmdReply)
|
|
|
+}
|
|
|
+
|
|
|
+func NewAgent(client SubDev) *Agent {
|
|
|
+ return &Agent{
|
|
|
+ client: client,
|
|
|
+ }
|
|
|
}
|