alerts.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package trigger
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "time"
  6. "dashoo.cn/backend/api/business/actions"
  7. "dashoo.cn/backend/api/business/device"
  8. "dashoo.cn/backend/api/business/userchannels"
  9. . "dashoo.cn/backend/api/controllers"
  10. "dashoo.cn/utils"
  11. )
  12. // 报警器接口说明
  13. type AlertsController struct {
  14. BaseController
  15. }
  16. type AlertModel struct {
  17. Title string `json:"title"`
  18. Serial string `json:"serial"`
  19. TagCode string `json:"tagCode"`
  20. BindDevices []string `json:"binddevices"`
  21. }
  22. type LedInfoModel struct {
  23. TimeRange []time.Time `json:"timerange"`
  24. Message string `json:"message"`
  25. Serial string `json:"serial"`
  26. Id int `json:"id"`
  27. }
  28. type LedMessageList struct {
  29. CurrentItemCount int64 `json:"currentItemCount,omitempty"` //结果集中的条目数目
  30. ItemsPerPage int64 `json:"itemsPerPage,omitempty"` //每页记录数目
  31. PageIndex int64 `json:"pageIndex,omitempty"` //条目的当前页索引
  32. // Items []ledscreenmessage.LED_ScreenMessage `json:"items"` //数据列表
  33. }
  34. // @Title 报警器列表
  35. // @Description 设备列表
  36. // @Success 200 {object} business.device.DeviceChannels
  37. // @router /list [get]
  38. func (this *AlertsController) List() {
  39. page := this.GetPageInfoForm()
  40. svc := device.GetDeviceService(utils.DBE)
  41. svcuc := userchannels.GetUserChannelService(utils.DBE)
  42. channelid := svcuc.GetChannelids(utils.ToStr(this.User.Id))
  43. Uid := utils.ToStr(this.User.Id)
  44. where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.Id in (" + strings.Join(channelid, ",") + ")) and a.DataItem in (" + Alertor_Alarm + ") "
  45. keyword := this.GetString("keyword")
  46. if keyword != "" {
  47. where = where + " and (a.Title like '%" + keyword + "%' or a.TagCode like '%" + keyword + "%' or a.Serial like '%" + keyword + "%')"
  48. }
  49. total, devices := svc.GetPagingEntitiesWithOrderSearch(page.CurrentPage, page.Size, "c.sortcode, a.CreateOn desc", where, Uid)
  50. var datainfo DataInfo
  51. datainfo.Items = devices
  52. datainfo.CurrentItemCount = total
  53. this.Data["json"] = &datainfo
  54. this.ServeJSON()
  55. }
  56. // @Title 验证序列号
  57. // @Description 验证序列号
  58. // @Param code path string true "设备SN"
  59. // @Success 200 {object} ErrorInfo
  60. // @Failure 403 :code 为空
  61. // @router /validcode/:code [get]
  62. func (this *AlertsController) ValidCode() {
  63. code := this.Ctx.Input.Param(":code")
  64. var errinfo ErrorInfo
  65. var entity device.Device
  66. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/devices/serial?serial=" + code
  67. json.Unmarshal(Apiget(strUrl), &entity)
  68. if entity.Id != 0 && DeviceItemContainint(Device_Alertor, entity.DataItem) {
  69. errinfo.Message = "验证通过!"
  70. errinfo.Code = 0
  71. } else {
  72. errinfo.Message = "报警器序列号不存在!"
  73. errinfo.Code = -1
  74. }
  75. this.Data["json"] = &errinfo
  76. this.ServeJSON()
  77. }
  78. // @Title 创建报警器
  79. // @Description 创建报警器
  80. // @Param body body business.device.DeviceChannels "报警器信息"
  81. // @Success 200 {object} controllers.Request
  82. // @router / [post]
  83. func (this *AlertsController) AddPost() {
  84. var model AlertModel
  85. var jsonblob = this.Ctx.Input.RequestBody
  86. json.Unmarshal(jsonblob, &model)
  87. var errinfo ErrorInfo
  88. u, p := this.GetuAndp()
  89. var devices device.Device
  90. devices.Serial = model.Serial
  91. devices.Code = model.TagCode
  92. devices.Title = model.Title
  93. svc := device.GetDeviceService(utils.DBE)
  94. var entity device.Device
  95. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/devices/serial?serial=" + devices.Serial
  96. json.Unmarshal(Apiget(strUrl), &entity)
  97. if svc.VerifyDevice(entity.Id) {
  98. errinfo.Message = "报警器已存在!"
  99. errinfo.Code = -1
  100. this.Data["json"] = &errinfo
  101. this.ServeJSON()
  102. return
  103. } else {
  104. if devices.Title == "" {
  105. devices.Title = entity.Title
  106. }
  107. if devices.About == "" {
  108. devices.About = entity.About
  109. }
  110. if devices.Tags == "" {
  111. devices.Tags = entity.Tags
  112. }
  113. if devices.Local == "" {
  114. devices.Local = entity.Local
  115. }
  116. if devices.Latitude == 0 {
  117. devices.Latitude = entity.Latitude
  118. devices.Longitude = entity.Longitude
  119. }
  120. devices.Wdid = entity.Id
  121. devices.Dtype = 1
  122. devices.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
  123. devices.CreateBy = this.User.Realname
  124. devices.DataItem = entity.DataItem
  125. //添加device表
  126. _, err := svc.InsertEntity(&devices)
  127. //添加device成功则添加channel表
  128. if err == nil {
  129. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/devices/" + utils.ToStr(entity.Id) + "?u=" + u + "&p=" + p
  130. Apipost(strUrl, "PUT", devices)
  131. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/devices/sourse/" + utils.ToStr(entity.Id) + "?u=" + u + "&p=" + p + "&sourse=coldchain&account=" + this.GetAccode() + "&accountname=" + this.User.Realname
  132. Apipost(strUrl, "PUT", devices)
  133. if entity.Id != 0 {
  134. var channels []device.Channels
  135. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/devices/" + utils.ToStr(entity.Id) + "/channelsallclos?u=" + u + "&p=" + p
  136. json.Unmarshal(Apiget(strUrl), &channels)
  137. //默认添加动作
  138. isaddaction := true
  139. for _, v := range channels {
  140. if DeviceItemContainint(Alertor_AlarmBindData, v.DataItem) || DeviceItemContainint(Alertor_Alarm, v.DataItem) {
  141. v.Title = devices.Title
  142. v.TagCode = devices.Code
  143. v.DId = devices.Id
  144. v.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
  145. v.CreateBy = this.User.Realname
  146. svc.InsertEntity(&v)
  147. }
  148. //绑定设备
  149. if DeviceItemContainint(Alertor_AlarmBindData, v.DataItem) {
  150. if len(model.BindDevices) > 0 {
  151. bindchannels := "c" + strings.Join(model.BindDevices, ",c")
  152. WriteAlertBindValue("alert"+model.Serial, bindchannels)
  153. }
  154. }
  155. //不需要添加动作
  156. if DeviceItemContainint(Alertor_NotNeedAction, v.DataItem) {
  157. isaddaction = false
  158. }
  159. }
  160. //添加一条动作
  161. if isaddaction {
  162. var action actions.Actions
  163. action.AItem = 4
  164. action.AName = devices.Title
  165. action.SPara4 = devices.Serial
  166. action.Enabled = 1
  167. var status Status
  168. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/actions/?u=" + u + "&p=" + p
  169. json.Unmarshal(Apipost(strUrl, "POST", action), &status)
  170. if status.Status == 0 {
  171. action.AccCode = this.GetAccode()
  172. action.Wdid = status.Id
  173. action.Id, _ = utils.StrTo(status.Id).Int()
  174. action.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
  175. action.CreateBy = this.User.Realname
  176. svc.InsertEntity(&action) //插入数据库
  177. }
  178. }
  179. }
  180. errinfo.Message = "保存成功!"
  181. errinfo.Code = 0
  182. this.Data["json"] = &errinfo
  183. this.ServeJSON()
  184. return
  185. } else {
  186. errinfo.Message = "保存失败!" + utils.AlertProcess(err.Error())
  187. errinfo.Code = -2
  188. this.Data["json"] = &errinfo
  189. this.ServeJSON()
  190. return
  191. }
  192. }
  193. }
  194. // @Title 获取绑定设备
  195. // @Description 获取绑定设备
  196. // @Param code path string true "设备SN"
  197. // @Success 200 {object} []string
  198. // @Failure 403 :code 为空
  199. // @router /binddevices/:code [get]
  200. func (this *AlertsController) GetBindDevices() {
  201. code := this.Ctx.Input.Param(":code")
  202. var bddevices []string = []string{}
  203. selectstr := ""
  204. lastdata, err := GetChannelLast("alert" + code)
  205. if err == nil && lastdata.Time.Unix() > 0 {
  206. selectstr = strings.Replace(lastdata.RequestData, "c", "", -1)
  207. }
  208. if selectstr != "" {
  209. bddevices = strings.Split(selectstr, ",")
  210. }
  211. this.Data["json"] = bddevices
  212. this.ServeJSON()
  213. }
  214. // @Title 编辑报警器
  215. // @Description 编辑报警器
  216. // @Param code path string true "需要修改的报警器编号"
  217. // @Param body body business.device.DeviceChannels "报警器信息"
  218. // @Success 200 {object} ErrorInfo
  219. // @router /:code [put]
  220. //func (this *AlertsController) EditPost() {
  221. // code := this.Ctx.Input.Param(":code")
  222. // var errinfo ErrorInfo
  223. // if code == "" {
  224. // errinfo.Message = "操作失败!请求信息不完整"
  225. // errinfo.Code = -2
  226. // this.Data["json"] = &errinfo
  227. // this.ServeJSON()
  228. // return
  229. // }
  230. // code = "c" + code
  231. // var model AlertModel
  232. // var jsonblob = this.Ctx.Input.RequestBody
  233. // json.Unmarshal(jsonblob, &model)
  234. // u, p := this.GetuAndp()
  235. // var devices logsinfo.AlertorLog
  236. // svc := device.GetDeviceService(utils.DBE)
  237. // has := svc.GetEntity(&devices, "Serial='"+model.Serial+"' and DataItem=4")
  238. // if !has {
  239. // errinfo.Message = "操作失败!请求数据有误"
  240. // errinfo.Code = -3
  241. // this.Data["json"] = &errinfo
  242. // this.ServeJSON()
  243. // return
  244. // }
  245. // deviceLog_Log := Logcompare{Value1: DeviceLog_Log{devices.Id, devices.Serial, devices.Code, devices.Title, devices.Local, devices.About}, Value2: DeviceLog_Log{devices.Id, devices.Serial, model.TagCode, model.Title, devices.Local, devices.About}}
  246. // svc.InsertUpdateLog(0, &deviceLog_Log, utils.ToStr(this.User.Id), this.User.Realname, AlertorLogTName)
  247. // devices.Code = model.TagCode
  248. // devices.Title = model.Title
  249. // devices.ModifiedUserId, _ = utils.StrTo(this.User.Id).Int()
  250. // devices.ModifiedBy = this.User.Realname
  251. // var cols []string = []string{"Title", "Code", "ModifiedUserId", "ModifiedBy"}
  252. // _, err := svc.UpdateEntityByIdCols(devices.Id, &devices, cols)
  253. // if err == nil {
  254. // strUrl := utils.Cfg.MustValue("server", "apiurl") + "/devices/" + utils.ToStr(devices.Wdid) + "?u=" + u + "&p=" + p
  255. // Apipost(strUrl, "PUT", devices)
  256. // //绑定设备
  257. // if len(model.BindDevices) > 0 {
  258. // bindchannels := "c" + strings.Join(model.BindDevices, ",c")
  259. // WriteAlertBindValue("alert"+model.Serial, bindchannels)
  260. // }
  261. // //修改动作
  262. // svcaction := actions.GetActionsService(utils.DBE)
  263. // action := svcaction.GetAlertor(devices.Serial)
  264. // if action.Id > 0 {
  265. // action.AName = devices.Title
  266. // var status Status
  267. // strUrl = utils.Cfg.MustValue("server", "apiurl") + "/actions/" + utils.ToStr(action.Id) + "?u=" + u + "&p=" + p
  268. // json.Unmarshal(Apipost(strUrl, "PUT", action), &status)
  269. // if status.Status == 0 {
  270. // var cols []string = []string{"AName"}
  271. // svcaction.UpdateEntityByIdCols(action.Id, &action, cols)
  272. // }
  273. // }
  274. // errinfo.Message = "保存成功!"
  275. // errinfo.Code = 0
  276. // this.Data["json"] = &errinfo
  277. // this.ServeJSON()
  278. // } else {
  279. // errinfo.Message = "保存失败!" + utils.AlertProcess(err.Error())
  280. // errinfo.Code = -1
  281. // this.Data["json"] = &errinfo
  282. // this.ServeJSON()
  283. // }
  284. //}
  285. // @Title 删除报警器
  286. // @Description 删除报警器
  287. // @Param code path string true "需要删除的报警器编号"
  288. // @Success 200 {object} ErrorInfo
  289. // @Failure 403 :code 为空
  290. // @router /:code [delete]
  291. func (this *AlertsController) Delete() {
  292. code := this.Ctx.Input.Param(":code")
  293. var errinfo ErrorInfo
  294. if code == "" {
  295. errinfo.Message = "操作失败!请求信息不完整"
  296. errinfo.Code = -3
  297. this.Data["json"] = &errinfo
  298. this.ServeJSON()
  299. return
  300. }
  301. code = "c" + code
  302. var entity device.Channels
  303. var devices device.Device
  304. var devicesempty device.Device
  305. var entityempty device.Channels
  306. svc := device.GetDeviceService(utils.DBE)
  307. has := svc.GetEntity(&entity, "Code='"+code+"'")
  308. if has {
  309. dataitem := entity.DataItem
  310. err := svc.DeleteEntityAndBackup(entity.Id, &entity, &entityempty, utils.ToStr(this.User.Id), this.User.Username)
  311. if err == nil {
  312. //取消waterdrop绑定状态
  313. u, p := this.GetuAndp()
  314. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/unbingding/" + code + "?u=" + u + "&p=" + p
  315. Apipost(strUrl, "PUT", nil)
  316. //删除设备
  317. //取消waterdrop绑定状态
  318. wdid := svc.GetWDidByDid(entity.DId)
  319. strUrldevice := utils.Cfg.MustValue("server", "apiurl") + "/devices/unbingding/" + utils.ToStr(wdid) + "?u=" + u + "&p=" + p
  320. Apipost(strUrldevice, "PUT", nil)
  321. svc.DeleteEntityAndBackup(entity.DId, &devices, &devicesempty, utils.ToStr(this.User.Id), this.User.Username)
  322. var channeldevice device.Channels
  323. haschannel := svc.GetEntity(&channeldevice, " DataItem=3 and DId="+utils.ToStr(entity.DId))
  324. if haschannel {
  325. svc.DeleteEntityAndBackup(channeldevice.Id, &channeldevice, &entityempty, utils.ToStr(this.User.Id), this.User.Username)
  326. cstrUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/unbingding/" + channeldevice.Code + "?u=" + u + "&p=" + p
  327. Apipost(cstrUrl, "PUT", nil)
  328. }
  329. //无动作不需要删除
  330. if DeviceItemContain(Alertor_NotNeedAction, utils.ToStr(dataitem)) {
  331. //删除屏显内容
  332. strUrldeletemsg := utils.Cfg.MustValue("server", "apiurl") + "/ledscreenmessage/clear?u=" + u + "&p=" + p + "&code=" + entity.Serial + "&sourse=" + ProjectSourse + "&account=" + this.GetAccode() + "&dataitem=1"
  333. Apipost(strUrldeletemsg, "DELETE", nil)
  334. } else {
  335. //删除动作
  336. svcaction := actions.GetActionsService(utils.DBE)
  337. action := svcaction.GetAlertor(entity.Serial)
  338. if action.Id > 0 {
  339. var status Status
  340. //删除报警动作
  341. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/triggers/deletebyaid/" + utils.ToStr(action.Id) + "?u=" + u + "&p=" + p
  342. json.Unmarshal(Apipost(strUrl, "DELETE", nil), &status)
  343. //删除动作
  344. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/actions/" + utils.ToStr(action.Id) + "?u=" + u + "&p=" + p
  345. json.Unmarshal(Apipost(strUrl, "DELETE", nil), &status)
  346. if status.Status == 0 {
  347. var actionempty actions.Actions
  348. svc.DeleteEntityById(action.Id, actionempty)
  349. }
  350. }
  351. }
  352. //删除绑定设备
  353. WriteAlertBindValue("alert"+strings.Replace(code, "c", "", 1), "")
  354. errinfo.Message = "删除成功!"
  355. errinfo.Code = 0
  356. this.Data["json"] = &errinfo
  357. this.ServeJSON()
  358. } else {
  359. errinfo.Message = "删除失败!" + utils.AlertProcess(err.Error())
  360. errinfo.Code = -1
  361. this.Data["json"] = &errinfo
  362. this.ServeJSON()
  363. }
  364. } else {
  365. errinfo.Message = "删除失败!"
  366. errinfo.Code = -2
  367. this.Data["json"] = &errinfo
  368. this.ServeJSON()
  369. }
  370. }