device.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package services
  2. import (
  3. "github.com/gogf/gf/encoding/gjson"
  4. "sparrow/pkg/models"
  5. "sparrow/pkg/rpcs"
  6. "sparrow/pkg/server"
  7. "sparrow/services/knowoapi/model"
  8. )
  9. // DeviceService device service接口
  10. type DeviceService interface {
  11. // 获取厂商已经激活的设备总数
  12. GetDeviceCount(vendorid string) (int, error)
  13. //返回厂商某天激活的设备数
  14. GetActiveNumberOfDate(vendor string, datetime string) (int, error)
  15. //获取厂商某天活跃的设备数
  16. GetLivelyCountOfDate(string, string) (int, error)
  17. //获取近N日激活设备数据
  18. GetActiveOfNumDays(string, int) ([]map[string]interface{}, error)
  19. //获取近N日活跃设备数据
  20. GetLivelyOfNumDays(string, int) ([]map[string]interface{}, error)
  21. //获取已经激活的设备列表
  22. GetDevices(vendorid, proid string, pi, ps int, deviceid string) ([]*models.Devices, int, error)
  23. //获取用户下所有设备的数量,在线设备的数量,离线设备的数量
  24. GetDevicesCountByVenderId(vendorid string) (map[string]interface{}, error)
  25. // 发起设备OTA升级
  26. Upgrade(params *models.UpgradeParams) error
  27. // GetUpgradeProgress 获取ota升级进度
  28. GetUpgradeProgress(deviceId string) (rpcs.ReplyOtaProgress, error)
  29. // GetDeviceStatus 获取设备状态数据
  30. GetDeviceStatus(deviceId string) (*gjson.Json, error)
  31. // SetReport 获取设备状态
  32. SetReport(params models.SendCommandParams) error
  33. // Restart 重启设备
  34. Restart(params models.SendCommandParams) error
  35. // ClearData 清除设备配置数据
  36. ClearData(params models.SendCommandParams) error
  37. // SetDataTrans 设备端自动上报配置
  38. SetDataTrans(params models.SendCommandParams) error
  39. // GetInfo 获取网关信息
  40. GetInfo(params models.SendCommandParams) error
  41. // ForceRun 远程控制某个模块强制运行
  42. ForceRun(params models.SendCommandParams) error
  43. // SetDeviceId 写入设备id
  44. SetDeviceId(params models.SendCommandParams) error
  45. // SetFjsqStatus 智能分集水器控制
  46. SetFjsqStatus(params models.SendCommandParams) error
  47. // SetOutdoorPower 设置水系统外机电源状态
  48. SetOutdoorPower(params models.SendCommandParams) error
  49. // SetOutdoorTemp 设置水系统外机出水温度
  50. SetOutdoorTemp(params models.SendCommandParams) error
  51. }
  52. type deviceservice struct {
  53. models *model.All
  54. }
  55. // NewDeviceService create device service
  56. func NewDeviceService(models *model.All) DeviceService {
  57. return deviceservice{
  58. models: models,
  59. }
  60. }
  61. func (a deviceservice) GetDevices(vendorid, proid string, pi, ps int, deviceid string) ([]*models.Devices, int, error) {
  62. data, total, err := a.models.Device.GetDevices(vendorid, proid, pi, ps, deviceid)
  63. if err != nil {
  64. return nil, 0, err
  65. }
  66. dataMap := make([]*models.Devices, 0)
  67. for _, device := range data {
  68. onlineargs := rpcs.ArgsGetDeviceOnlineStatus{
  69. Id: device.DeviceIdentifier,
  70. }
  71. onlinereply := rpcs.ReplyGetDeviceOnlineStatus{}
  72. err = server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceOnlineStatus", onlineargs, &onlinereply)
  73. if err != nil && err.Error() != "redigo: nil returned" {
  74. server.Log.Errorf("get devie online status error: %v", err)
  75. return nil, 0, err
  76. }
  77. devices := new(models.Devices)
  78. devices.Device = device
  79. if int(onlinereply.HeartbeatInterval) > 0 && len(onlinereply.ClientIP) > 0 {
  80. devices.Status = 1
  81. }
  82. dataMap = append(dataMap, devices)
  83. }
  84. return dataMap, total, nil
  85. }
  86. func (a deviceservice) GetDeviceCount(vendorid string) (int, error) {
  87. return a.models.Device.GetDeviceCount(vendorid)
  88. }
  89. func (a deviceservice) GetActiveNumberOfDate(vendorid string, datetime string) (int, error) {
  90. return a.models.Device.GetActiveNumberOfDate(vendorid, datetime)
  91. }
  92. func (a deviceservice) GetLivelyCountOfDate(vendorid string, datetime string) (int, error) {
  93. return a.models.Device.GetLivelyCountOfDate(vendorid, datetime)
  94. }
  95. func (a deviceservice) GetActiveOfNumDays(vendorid string, days int) ([]map[string]interface{}, error) {
  96. return a.models.Device.GetActiveOfNumDays(vendorid, days)
  97. }
  98. func (a deviceservice) GetLivelyOfNumDays(vendorid string, days int) ([]map[string]interface{}, error) {
  99. return a.models.Device.GetLivelyOfNumDays(vendorid, days)
  100. }
  101. func (a deviceservice) GetDevicesCountByVenderId(vendorid string) (map[string]interface{}, error) {
  102. data, err := a.models.Device.GetDevicesByVenderId(vendorid)
  103. if err != nil {
  104. return nil, err
  105. }
  106. var onlineCount int
  107. var offlineCount int
  108. for _, device := range data {
  109. onlineargs := rpcs.ArgsGetDeviceOnlineStatus{
  110. Id: device.DeviceIdentifier,
  111. }
  112. onlinereply := rpcs.ReplyGetDeviceOnlineStatus{}
  113. err = server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceOnlineStatus", onlineargs, &onlinereply)
  114. if err != nil && err.Error() != "redigo: nil returned" {
  115. server.Log.Errorf("get devie online status error: %v", err)
  116. return nil, err
  117. }
  118. if int(onlinereply.HeartbeatInterval) > 0 && len(onlinereply.ClientIP) > 0 {
  119. onlineCount += 1
  120. } else {
  121. offlineCount += 1
  122. }
  123. }
  124. deviceCount := map[string]interface{}{
  125. "TotalData": len(data),
  126. "OnlineCount": onlineCount,
  127. "OfflineCount": offlineCount,
  128. }
  129. return deviceCount, nil
  130. }
  131. func (a deviceservice) Upgrade(param *models.UpgradeParams) error {
  132. //var fileArgs rpcs.ArgsOtaFile
  133. //fileArgs.FileData = param.File
  134. //fileArgs.FileId = guid.S()
  135. //var reply rpcs.ReplyEmptyResult
  136. //
  137. //err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.SavaFile", fileArgs, &reply)
  138. //if err != nil {
  139. // server.Log.Errorf("OTA升级文件保存失败:%v", err)
  140. // return err
  141. //}
  142. //
  143. //var args rpcs.ArgsUpgrade4G
  144. //args.DeviceId = param.DeviceID
  145. //args.FileId = fileArgs.FileId
  146. //args.FileSize = param.FileSize
  147. //
  148. //err = server.RPCCallByName(nil, rpcs.MQTTAccessName, "Access.UpgradeFor4G", args, &reply)
  149. //if err != nil {
  150. // server.Log.Errorf("4G模组OTA升级失败:%v", err)
  151. // return err
  152. //}
  153. server.Log.Debugf("ota升级请求成功")
  154. return nil
  155. }
  156. func (a deviceservice) GetUpgradeProgress(deviceId string) (rpcs.ReplyOtaProgress, error) {
  157. //var args rpcs.ArgsOtaProgress
  158. //args.DeviceId = deviceId
  159. //
  160. var reply rpcs.ReplyOtaProgress
  161. //
  162. //err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetProgress", args, &reply)
  163. //if err != nil {
  164. // server.Log.Errorf("OTA升级进度获取失败:%v", err)
  165. // return reply, err
  166. //}
  167. server.Log.Debugf("获取升级进度请求成功")
  168. return reply, nil
  169. }
  170. func (a deviceservice) GetDeviceStatus(deviceId string) (*gjson.Json, error) {
  171. var args rpcs.ArgsGetStatus
  172. args.Id = deviceId
  173. var reply rpcs.ReplyStatus
  174. err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceStatus", args, &reply)
  175. if err != nil {
  176. server.Log.Errorf("设备状态数据获取失败:%v", err)
  177. return nil, err
  178. }
  179. return gjson.New(reply.Status), nil
  180. }
  181. // SetReport 获取设备状态
  182. func (a deviceservice) SetReport(params models.SendCommandParams) error {
  183. var args rpcs.ArgsSendCommand
  184. args.DeviceId = params.DeviceId
  185. args.Cmd = string(models.Report)
  186. return a.sendCommand(args)
  187. }
  188. // Restart 重启设备
  189. func (a deviceservice) Restart(params models.SendCommandParams) error {
  190. var args rpcs.ArgsSendCommand
  191. args.DeviceId = params.DeviceId
  192. args.Cmd = string(models.Restart)
  193. return a.sendCommand(args)
  194. }
  195. // ClearData 清除设备配置数据
  196. func (a deviceservice) ClearData(params models.SendCommandParams) error {
  197. var args rpcs.ArgsSendCommand
  198. args.DeviceId = params.DeviceId
  199. args.Cmd = string(models.ClearData)
  200. return a.sendCommand(args)
  201. }
  202. // SetDataTrans 设备端自动上报配置
  203. func (a deviceservice) SetDataTrans(params models.SendCommandParams) error {
  204. var args rpcs.ArgsSendCommand
  205. args.DeviceId = params.DeviceId
  206. args.Params = map[string]interface{}{
  207. "enable": params.Enable,
  208. "internal": params.Internal,
  209. }
  210. args.Cmd = string(models.SetDataTrans)
  211. return a.sendCommand(args)
  212. }
  213. // GetInfo 获取网关信息
  214. func (a deviceservice) GetInfo(params models.SendCommandParams) error {
  215. var args rpcs.ArgsSendCommand
  216. args.DeviceId = params.DeviceId
  217. args.Cmd = string(models.GetInfo)
  218. return a.sendCommand(args)
  219. }
  220. // ForceRun 远程控制某个模块强制运行
  221. func (a deviceservice) ForceRun(params models.SendCommandParams) error {
  222. var args rpcs.ArgsSendCommand
  223. args.DeviceId = params.DeviceId
  224. args.Cmd = string(models.ForceRun)
  225. args.Params = map[string]interface{}{
  226. "module": params.Module,
  227. "power": params.ModulePower,
  228. }
  229. return a.sendCommand(args)
  230. }
  231. // SetDeviceId 写入设备id
  232. func (a deviceservice) SetDeviceId(params models.SendCommandParams) error {
  233. var args rpcs.ArgsSendCommand
  234. args.DeviceId = params.DeviceId
  235. args.Cmd = string(models.SetDeviceId)
  236. args.Params = map[string]interface{}{
  237. "id": params.Id,
  238. }
  239. return a.sendCommand(args)
  240. }
  241. // SetFjsqStatus 智能分集水器控制
  242. func (a deviceservice) SetFjsqStatus(params models.SendCommandParams) error {
  243. var args rpcs.ArgsSendCommand
  244. args.DeviceId = params.DeviceId
  245. args.Cmd = string(models.SetFjsqStatus)
  246. return a.sendCommand(args)
  247. }
  248. // SetOutdoorPower 设置水系统外机电源状态
  249. func (a deviceservice) SetOutdoorPower(params models.SendCommandParams) error {
  250. var args rpcs.ArgsSendCommand
  251. args.DeviceId = params.DeviceId
  252. args.Cmd = string(models.SetOutdoorPower)
  253. args.Params = map[string]interface{}{
  254. "power": params.OutdoorPower,
  255. }
  256. return a.sendCommand(args)
  257. }
  258. // SetOutdoorTemp 设置水系统外机出水温度
  259. func (a deviceservice) SetOutdoorTemp(params models.SendCommandParams) error {
  260. var args rpcs.ArgsSendCommand
  261. args.DeviceId = params.DeviceId
  262. args.Cmd = string(models.SetOutdoorTemp)
  263. args.Params = map[string]interface{}{
  264. "cool_mode_temp": params.CoolModeTemp,
  265. "heat_mode_temp": params.HeatModeTemp,
  266. }
  267. return a.sendCommand(args)
  268. }
  269. // SendCommand 下发指令
  270. func (a deviceservice) sendCommand(args rpcs.ArgsSendCommand) error {
  271. var reply rpcs.ReplySendCommand
  272. err := server.RPCCallByName(nil, rpcs.ControllerName, "Controller.SendCommand", args, &reply)
  273. if err != nil {
  274. server.Log.Errorf("设备状态数据获取失败:%v", err)
  275. return err
  276. }
  277. return nil
  278. }