access.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "sparrow/pkg/klink"
  4. "sparrow/pkg/mqtt"
  5. "sparrow/pkg/protocol"
  6. "sparrow/pkg/rpcs"
  7. "sparrow/pkg/server"
  8. "time"
  9. )
  10. const (
  11. defaultTimeoutSecond = 5
  12. commandGetCurrentStatus = uint16(65528)
  13. )
  14. type Access struct {
  15. MqttBroker *mqtt.Broker
  16. }
  17. func NewAccess() (*Access, error) {
  18. p := NewMQTTProvider()
  19. return &Access{
  20. mqtt.NewBroker(p),
  21. }, nil
  22. }
  23. func (a *Access) SetStatus(args rpcs.ArgsSetStatus, reply *rpcs.ReplySetStatus) error {
  24. server.Log.Infof("Access Set Status: %v", args)
  25. data := &protocol.Data{}
  26. data.Head.Timestamp = uint64(time.Now().Unix())
  27. token, err := a.MqttBroker.GetToken(args.DeviceId)
  28. if err != nil {
  29. return err
  30. }
  31. copy(data.Head.Token[:], token[:16])
  32. data.SubData = args.Status
  33. msg, err := data.Marshal()
  34. if err != nil {
  35. return err
  36. }
  37. return a.MqttBroker.SendMessageToDevice(args.DeviceId, "s", msg, defaultTimeoutSecond*time.Second)
  38. }
  39. func (a *Access) GetStatus(args rpcs.ArgsGetStatus, reply *rpcs.ReplyGetStatus) error {
  40. server.Log.Infof("Access Get Status: %v", args)
  41. // first send a get status command
  42. cmdArgs := rpcs.ArgsSendCommand{
  43. DeviceId: args.Id,
  44. WaitTime: 0,
  45. SubDevice: args.SubDeviceId,
  46. Cmd: "report",
  47. }
  48. cmdReply := rpcs.ReplySendCommand{}
  49. return a.SendCommand(cmdArgs, &cmdReply)
  50. }
  51. func (a *Access) SendCommand(args rpcs.ArgsSendCommand, reply *rpcs.ReplySendCommand) error {
  52. cmd := &klink.CloudSend{
  53. Action: "cloudSend",
  54. MsgId: 0,
  55. DeviceCode: args.DeviceId,
  56. SubDeviceId: args.SubDevice,
  57. Timestamp: time.Now().Unix(),
  58. Data: &klink.CloudSendData{
  59. Cmd: args.Cmd,
  60. Params: args.Params,
  61. },
  62. }
  63. msg, err := cmd.Marshal()
  64. if err != nil {
  65. return err
  66. }
  67. print("Access Send Command: %v, %v,%s\r\n", args.DeviceId, args.Cmd, string(msg))
  68. return a.MqttBroker.SendMessageToDevice(args.DeviceId, "c", msg, time.Duration(args.WaitTime)*time.Second)
  69. }
  70. // Upgrade 设备OTA升级指令
  71. func (a *Access) Upgrade(args rpcs.ArgsDeviceUpgrade, reply *rpcs.ReplyEmptyResult) error {
  72. server.Log.Infof("设备OTA升级:%s, %s", args.DeviceId, args.Version)
  73. cmd := &klink.CloudSend{
  74. Action: "cloudSend",
  75. MsgId: 0,
  76. DeviceCode: args.DeviceId,
  77. Timestamp: time.Now().Unix(),
  78. Data: &klink.CloudSendData{
  79. Cmd: "devUpgrade",
  80. Params: map[string]interface{}{
  81. "md5": args.Md5,
  82. "url": args.Url,
  83. "version": args.Version,
  84. "file_size": args.FileSize,
  85. },
  86. },
  87. SubDeviceId: args.SudDeviceId,
  88. }
  89. msg, err := cmd.Marshal()
  90. if err != nil {
  91. return err
  92. }
  93. return a.MqttBroker.SendMessageToDevice(args.DeviceId, "c", msg, 5*time.Second)
  94. }