yufan_httpv1_commander.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package gate_command
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.getensh.com/common/gopkgs/database"
  6. "github.com/tidwall/gjson"
  7. "property-device/errors"
  8. dbmodel "property-device/model"
  9. "property-device/utils/gate_utils"
  10. "time"
  11. )
  12. type YufanHttpv1Commander struct {
  13. ByIp bool
  14. command bool
  15. GateInfo *dbmodel.TGate
  16. }
  17. func (p *YufanHttpv1Commander) Open() error {
  18. if p.ByIp {
  19. return p.OpenByIp()
  20. }
  21. task := gate_utils.DeviceOpenTask{
  22. TaskNo: "",
  23. InterfaceName: gate_utils.DeviceOpenTaskName,
  24. Result: true,
  25. OType: 1,
  26. }
  27. bytes, _ := json.Marshal(task)
  28. now := time.Now()
  29. gcmd := &dbmodel.TGateCommand{
  30. CreatedAt: now,
  31. UpdatedAt: now,
  32. DeviceId: p.GateInfo.ID,
  33. Param: string(bytes),
  34. Desc: gate_utils.CommandCodeMap[gate_utils.OpenCommand],
  35. Status: gate_utils.CommandStatusWait,
  36. ResultStatus: 0,
  37. ResultStatusDesc: "",
  38. Code: gate_utils.OpenCommand,
  39. }
  40. if err := gcmd.Insert(database.DB()); err != nil {
  41. return errors.DataBaseError
  42. }
  43. p.command = true
  44. return nil
  45. }
  46. func (p *YufanHttpv1Commander) Reboot() error {
  47. if p.ByIp {
  48. return p.RebootByIp()
  49. }
  50. task := gate_utils.DeviceRebootTask{
  51. TaskNo: "",
  52. InterfaceName: gate_utils.DeviceRebootTaskName,
  53. Result: true,
  54. }
  55. bytes, _ := json.Marshal(task)
  56. now := time.Now()
  57. gcmd := &dbmodel.TGateCommand{
  58. CreatedAt: now,
  59. UpdatedAt: now,
  60. DeviceId: p.GateInfo.ID,
  61. Param: string(bytes),
  62. Desc: gate_utils.CommandCodeMap[gate_utils.RebootCommand],
  63. Status: gate_utils.CommandStatusWait,
  64. ResultStatus: 0,
  65. ResultStatusDesc: "",
  66. Code: gate_utils.RebootCommand,
  67. }
  68. if err := gcmd.Insert(database.DB()); err != nil {
  69. return errors.DataBaseError
  70. }
  71. p.command = true
  72. return nil
  73. }
  74. func (p *YufanHttpv1Commander) OpenByIp() error {
  75. now := time.Now()
  76. body := map[string]interface{}{
  77. "pass": p.GateInfo.Password,
  78. "type": 1,
  79. }
  80. bytes, _ := json.Marshal(body)
  81. h := gate_utils.HttpRequestWithHead{
  82. Method: "POST",
  83. Body: bytes,
  84. TimeOut: 20 * time.Second,
  85. Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
  86. Url: fmt.Sprintf("http://%s:%d/device/openDoorControl", p.GateInfo.Ip, p.GateInfo.Port),
  87. }
  88. resultStatus := 1
  89. msg := ""
  90. bytes, err := h.Request()
  91. if err != nil {
  92. resultStatus = 2
  93. msg = err.Error()
  94. } else {
  95. code := gjson.GetBytes(bytes, "code").String()
  96. if code != "LAN_SUS-0" {
  97. msg = fmt.Sprintf("%s-%s", code, gjson.GetBytes(bytes, "msg").String())
  98. }
  99. }
  100. tgc := &dbmodel.TGateCommand{
  101. CreatedAt: now,
  102. UpdatedAt: now,
  103. DeviceId: p.GateInfo.ID,
  104. //Param: req.CmdParams,
  105. Desc: gate_utils.CommandCodeMap[gate_utils.OpenCommand],
  106. Status: gate_utils.CommandStatusOver,
  107. ResultStatus: int32(resultStatus),
  108. ResultStatusDesc: msg,
  109. Code: gate_utils.OpenCommand,
  110. }
  111. tgc.Insert(database.DB())
  112. return nil
  113. }
  114. func (p *YufanHttpv1Commander) RebootByIp() error {
  115. now := time.Now()
  116. body := map[string]interface{}{
  117. "pass": p.GateInfo.Password,
  118. }
  119. bytes, _ := json.Marshal(body)
  120. h := gate_utils.HttpRequestWithHead{
  121. Method: "POST",
  122. Body: bytes,
  123. TimeOut: 20 * time.Second,
  124. Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
  125. Url: fmt.Sprintf("http://%s:%d/restartDevice", p.GateInfo.Ip, p.GateInfo.Port),
  126. }
  127. resultStatus := 1
  128. msg := ""
  129. bytes, err := h.Request()
  130. if err != nil {
  131. resultStatus = 2
  132. msg = err.Error()
  133. } else {
  134. code := gjson.GetBytes(bytes, "code").String()
  135. if code != "LAN_SUS-0" {
  136. msg = fmt.Sprintf("%s-%s", code, gjson.GetBytes(bytes, "msg").String())
  137. }
  138. }
  139. tgc := &dbmodel.TGateCommand{
  140. CreatedAt: now,
  141. UpdatedAt: now,
  142. DeviceId: p.GateInfo.ID,
  143. //Param: req.CmdParams,
  144. Desc: gate_utils.CommandCodeMap[gate_utils.RebootCommand],
  145. Status: gate_utils.CommandStatusOver,
  146. ResultStatus: int32(resultStatus),
  147. ResultStatusDesc: msg,
  148. Code: gate_utils.RebootCommand,
  149. }
  150. tgc.Insert(database.DB())
  151. return nil
  152. }
  153. func (p *YufanHttpv1Commander) Command() bool {
  154. return p.command
  155. }