device.go 11 KB

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