alerts.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. package controllers
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "dashoo.cn/base_common/labsop"
  10. "dashoo.cn/base_common/utils"
  11. "dashoo.cn/mcs_api/business/device"
  12. "dashoo.cn/mcs_api/business/ledscreenmessage"
  13. "dashoo.cn/mcs_api/business/logsinfo"
  14. "dashoo.cn/mcs_common/business/actions"
  15. "dashoo.cn/mcs_common/business/equipment"
  16. "dashoo.cn/mcs_common/business/userchannels"
  17. )
  18. // 报警器接口说明
  19. type AlertsController struct {
  20. BaseController
  21. }
  22. type AlertModel struct {
  23. Title string `json:"title"`
  24. Serial string `json:"serial"`
  25. TagCode string `json:"tagCode"`
  26. BindDevices []string `json:"binddevices"`
  27. }
  28. type LedInfoModel struct {
  29. TimeRange []time.Time `json:"timerange"`
  30. Message string `json:"message"`
  31. Serial string `json:"serial"`
  32. Id int `json:"id"`
  33. }
  34. type LedMessageList struct {
  35. CurrentItemCount int64 `json:"currentItemCount,omitempty"` //结果集中的条目数目
  36. ItemsPerPage int64 `json:"itemsPerPage,omitempty"` //每页记录数目
  37. PageIndex int64 `json:"pageIndex,omitempty"` //条目的当前页索引
  38. Items []ledscreenmessage.LED_ScreenMessage `json:"items"` //数据列表
  39. }
  40. // @Title 报警器列表
  41. // @Description 设备列表
  42. // @Success 200 {object} business.device.DeviceChannels
  43. // @router /list [get]
  44. func (this *AlertsController) List() {
  45. page := this.GetPageInfoForm()
  46. svc := device.GetDeviceService(utils.DBE)
  47. svcuc := userchannels.GetUserChannelService(utils.DBE)
  48. channelid := svcuc.GetChannelids(utils.ToStr(this.User.Id))
  49. Uid := utils.ToStr(this.User.Id)
  50. where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.Id in (" + strings.Join(channelid, ",") + ")) and a.DataItem in (" + Alertor_Alarm + ") "
  51. keyword := this.GetString("keyword")
  52. if keyword != "" {
  53. where = where + " and (a.Title like '%" + keyword + "%' or a.TagCode like '%" + keyword + "%' or a.Serial like '%" + keyword + "%')"
  54. }
  55. total, devices := svc.GetChannelsListOrder(page.CurrentPage, page.Size, "c.sortcode, a.CreateOn desc", where, Uid)
  56. var datainfo DataInfo
  57. datainfo.Items = devices
  58. datainfo.CurrentItemCount = total
  59. this.Data["json"] = &datainfo
  60. this.ServeJSON()
  61. }
  62. // @Title 验证序列号
  63. // @Description 验证序列号
  64. // @Param code path string true "设备SN"
  65. // @Success 200 {object} ErrorInfo
  66. // @Failure 403 :code 为空
  67. // @router /validcode/:code [get]
  68. func (this *AlertsController) ValidCode() {
  69. code := this.Ctx.Input.Param(":code")
  70. var errinfo ErrorInfo
  71. var entity device.Device
  72. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/devices/serial?serial=" + code
  73. fmt.Println("--------strUrlstrUrl------------", strUrl)
  74. json.Unmarshal(Apiget(strUrl), &entity)
  75. if entity.Id != 0 && DeviceItemContainint(Device_Alertor, entity.DataItem) {
  76. errinfo.Message = "验证通过!"
  77. errinfo.Code = 0
  78. } else {
  79. errinfo.Message = "报警器序列号不存在!"
  80. errinfo.Code = -1
  81. }
  82. this.Data["json"] = &errinfo
  83. this.ServeJSON()
  84. }
  85. // @Title 创建报警器
  86. // @Description 创建报警器
  87. // @Param body body business.device.DeviceChannels "报警器信息"
  88. // @Success 200 {object} controllers.Request
  89. // @router / [post]
  90. func (this *AlertsController) AddPost() {
  91. var model AlertModel
  92. var jsonblob = this.Ctx.Input.RequestBody
  93. json.Unmarshal(jsonblob, &model)
  94. var errinfo ErrorInfo
  95. u, p := this.GetuAndp()
  96. var devices device.Device
  97. devices.Serial = model.Serial
  98. devices.Code = model.TagCode
  99. devices.Title = model.Title
  100. svc := device.GetDeviceService(utils.DBE)
  101. var entity device.Device
  102. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/devices/serial?serial=" + devices.Serial
  103. json.Unmarshal(Apiget(strUrl), &entity)
  104. if svc.VerifyDevice(entity.Id) {
  105. errinfo.Message = "报警器已存在!"
  106. errinfo.Code = -1
  107. this.Data["json"] = &errinfo
  108. this.ServeJSON()
  109. return
  110. } else {
  111. if devices.Title == "" {
  112. devices.Title = entity.Title
  113. }
  114. if devices.About == "" {
  115. devices.About = entity.About
  116. }
  117. if devices.Tags == "" {
  118. devices.Tags = entity.Tags
  119. }
  120. if devices.Local == "" {
  121. devices.Local = entity.Local
  122. }
  123. if devices.Latitude == 0 {
  124. devices.Latitude = entity.Latitude
  125. devices.Longitude = entity.Longitude
  126. }
  127. devices.Wdid = entity.Id
  128. devices.Dtype = 1
  129. devices.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
  130. devices.CreateBy = this.User.Realname
  131. devices.DataItem = entity.DataItem
  132. //添加device表
  133. _, err := svc.InsertEntity(&devices)
  134. fmt.Println("----------------------")
  135. //添加device成功则添加channel表
  136. if err == nil {
  137. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/devices/" + utils.ToStr(entity.Id) + "?u=" + u + "&p=" + p
  138. Apipost(strUrl, "PUT", devices)
  139. 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
  140. Apipost(strUrl, "PUT", devices)
  141. if entity.Id != 0 {
  142. var channels []device.Channels
  143. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/devices/" + utils.ToStr(entity.Id) + "/channelsallclos?u=" + u + "&p=" + p
  144. json.Unmarshal(Apiget(strUrl), &channels)
  145. //默认添加动作
  146. isaddaction := true
  147. for _, v := range channels {
  148. if DeviceItemContainint(Alertor_AlarmBindData, v.DataItem) || DeviceItemContainint(Alertor_Alarm, v.DataItem) {
  149. v.Title = devices.Title
  150. v.TagCode = devices.Code
  151. v.DId = devices.Id
  152. v.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
  153. v.CreateBy = this.User.Realname
  154. svc.InsertEntity(&v)
  155. }
  156. //绑定设备
  157. if DeviceItemContainint(Alertor_AlarmBindData, v.DataItem) {
  158. if len(model.BindDevices) > 0 {
  159. // bindchannels := "c" + strings.Join(model.BindDevices, ",c")
  160. // WriteAlertBindValue("alert"+model.Serial, bindchannels)
  161. //查询绑定设备
  162. bindchannels := "'" + strings.Join(model.BindDevices, "','") + "'"
  163. svdevc := device.GetDeviceService(utils.DBE)
  164. svceq := equipment.GetEquipmentService(utils.DBE)
  165. eids := svceq.GetEquipmentidsByUid(this.User.Id)
  166. where := " (a.CreateUserId=" + this.User.Id + " or a.EquipMentId in (" + eids + ")) and a.DataItem in (" + ChannelItem_HomeList + ") "
  167. where += " and a.Serial in (" + bindchannels + ")"
  168. _, devices := svdevc.GetChannelsList(-1, -1, where, this.User.Id)
  169. //报警器网关一体机下发数据
  170. var downdatas labsop.AlarmGatewayBindings
  171. downdatadetail := make([]labsop.AlarmGatewayBinding, 0)
  172. //报警器绑定数据
  173. bindchannelcodes := make([]string, 0)
  174. for _, v := range devices {
  175. if v.Code != "" {
  176. //报警器绑定数据
  177. bindchannelcodes = append(bindchannelcodes, v.Code)
  178. //网关一体机下发数据
  179. if len(v.Serial) == 8 {
  180. mac1, _ := strconv.ParseUint(v.Serial[0:2], 16, 0)
  181. mac2, _ := strconv.ParseUint(v.Serial[2:4], 16, 0)
  182. mac3, _ := strconv.ParseUint(v.Serial[4:6], 16, 0)
  183. mac4, _ := strconv.ParseUint(v.Serial[6:], 16, 0)
  184. max := strconv.FormatFloat(v.MaxValue, 'E', -1, 64)
  185. //Cmd和需绑定的无线记录仪编号
  186. sendbyte := []byte{0x00, byte(mac1), byte(mac2), byte(mac3), byte(mac4)}
  187. name := utils.Substr(v.Title, 0, 10)
  188. namebyte := []byte(CodertoGBK(name))
  189. fuzai := []byte{0x73, byte(len(namebyte))}
  190. fuzai = append(fuzai, namebyte...)
  191. //yuzhi := []byte{0x65, 0x01, 0x4E, 0x20, 0x65, 0x00, 0x4E, 0x20, 0x66, 0x01, 0x4E, 0x20, 0x66, 0x00, 0xB1, 0xE0}
  192. //fuzai = append(yuzhi, fuzai...)
  193. fuzailen := byte(len(fuzai))
  194. //拼接负载长度
  195. if v.MaxValue != 0 && max != "" {
  196. v.MaxValue = v.MaxValue * 100
  197. data := v.MaxValue
  198. i := uint16(data)
  199. c := make([]byte, 2)
  200. binary.BigEndian.PutUint16(c, i)
  201. v.MinValue = v.MinValue * 100
  202. data1 := v.MinValue
  203. i1 := uint16(data1)
  204. c1 := make([]byte, 2)
  205. binary.BigEndian.PutUint16(c1, i1)
  206. sendms := []byte{0x65, 0x00, c1[0], c1[1], 0x65, 0x01, c[0], c[1], 0x66, 0x00, 0x00, 0x00, 0x66, 0x01, 0x26, 0xAC}
  207. changdu1 := append(sendms, fuzai...)
  208. changdu := byte(len(changdu1))
  209. sendms = []byte{changdu, 0x65, 0x00, c1[0], c1[1], 0x65, 0x01, c[0], c[1], 0x66, 0x00, 0x00, 0x00, 0x66, 0x01, 0x26, 0xAC}
  210. sendbyte = append(sendbyte, sendms...)
  211. } else {
  212. sendbyte = append(sendbyte, fuzailen)
  213. }
  214. //拼接负载数据
  215. sendbyte = append(sendbyte, fuzai...)
  216. downdatadetail = append(downdatadetail, labsop.AlarmGatewayBinding{v.Serial, sendbyte})
  217. }
  218. }
  219. }
  220. // 写入绑定缓存
  221. for i, _ := range model.BindDevices {
  222. model.BindDevices[i] = "c" + model.BindDevices[i]
  223. }
  224. WriteAlertBindValue("alert"+model.Serial, strings.Join(model.BindDevices, ","))
  225. // 写入网关报警一体机下发数据
  226. downdatas.AlarmBinds = downdatadetail
  227. labsop.UpdateAlarmbinddata(model.Serial, downdatas)
  228. }
  229. }
  230. //不需要添加动作
  231. if DeviceItemContainint(Alertor_NotNeedAction, v.DataItem) {
  232. isaddaction = false
  233. }
  234. }
  235. //添加一条动作
  236. if isaddaction {
  237. var action actions.Actions
  238. action.AItem = 4
  239. action.AName = devices.Title
  240. action.SPara4 = devices.Serial
  241. action.Enabled = 1
  242. var status Status
  243. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/actions/?u=" + u + "&p=" + p
  244. json.Unmarshal(Apipost(strUrl, "POST", action), &status)
  245. if status.Status == 0 {
  246. action.AccCode = this.GetAccode()
  247. action.Wdid = status.Id
  248. action.Id, _ = utils.StrTo(status.Id).Int()
  249. action.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
  250. action.CreateBy = this.User.Realname
  251. svc.InsertEntity(&action) //插入数据库
  252. }
  253. }
  254. }
  255. errinfo.Message = "保存成功!"
  256. errinfo.Code = 0
  257. this.Data["json"] = &errinfo
  258. this.ServeJSON()
  259. return
  260. } else {
  261. errinfo.Message = "保存失败!" + utils.AlertProcess(err.Error())
  262. errinfo.Code = -2
  263. this.Data["json"] = &errinfo
  264. this.ServeJSON()
  265. return
  266. }
  267. }
  268. }
  269. // @Title 获取绑定设备
  270. // @Description 获取绑定设备
  271. // @Param code path string true "设备SN"
  272. // @Success 200 {object} []string
  273. // @Failure 403 :code 为空
  274. // @router /binddevices/:code [get]
  275. func (this *AlertsController) GetBindDevices() {
  276. code := this.Ctx.Input.Param(":code")
  277. var bddevices []string = []string{}
  278. selectstr := ""
  279. lastdata, err := GetChannelLast("alert" + code)
  280. if err == nil && lastdata.Time.Unix() > 0 {
  281. selectstr = strings.Replace(lastdata.RequestData, "c", "", -1)
  282. }
  283. if selectstr != "" {
  284. bddevices = strings.Split(selectstr, ",")
  285. }
  286. this.Data["json"] = bddevices
  287. this.ServeJSON()
  288. }
  289. // @Title 编辑报警器
  290. // @Description 编辑报警器
  291. // @Param code path string true "需要修改的报警器编号"
  292. // @Param body body business.device.DeviceChannels "报警器信息"
  293. // @Success 200 {object} ErrorInfo
  294. // @router /:code [put]
  295. func (this *AlertsController) EditPost() {
  296. code := this.Ctx.Input.Param(":code")
  297. var errinfo ErrorInfo
  298. if code == "" {
  299. errinfo.Message = "操作失败!请求信息不完整"
  300. errinfo.Code = -2
  301. this.Data["json"] = &errinfo
  302. this.ServeJSON()
  303. return
  304. }
  305. code = "c" + code
  306. var model AlertModel
  307. var jsonblob = this.Ctx.Input.RequestBody
  308. json.Unmarshal(jsonblob, &model)
  309. u, p := this.GetuAndp()
  310. var devices logsinfo.AlertorLog
  311. svc := device.GetDeviceService(utils.DBE)
  312. has := svc.GetEntity(&devices, "Serial='"+model.Serial+"' and DataItem=4")
  313. if !has {
  314. errinfo.Message = "操作失败!请求数据有误"
  315. errinfo.Code = -3
  316. this.Data["json"] = &errinfo
  317. this.ServeJSON()
  318. return
  319. }
  320. 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}}
  321. svc.InsertUpdateLog(0, &deviceLog_Log, utils.ToStr(this.User.Id), this.User.Realname, AlertorLogTName)
  322. devices.Code = model.TagCode
  323. devices.Title = model.Title
  324. devices.ModifiedUserId, _ = utils.StrTo(this.User.Id).Int()
  325. devices.ModifiedBy = this.User.Realname
  326. var cols []string = []string{"Title", "Code", "ModifiedUserId", "ModifiedBy"}
  327. _, err := svc.UpdateEntityByIdCols(devices.Id, &devices, cols)
  328. if err == nil {
  329. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/devices/" + utils.ToStr(devices.Wdid) + "?u=" + u + "&p=" + p
  330. Apipost(strUrl, "PUT", devices)
  331. //绑定设备
  332. if len(model.BindDevices) > 0 {
  333. //查询绑定设备
  334. bindchannels := "'" + strings.Join(model.BindDevices, "','") + "'"
  335. svdevc := device.GetDeviceService(utils.DBE)
  336. svceq := equipment.GetEquipmentService(utils.DBE)
  337. eids := svceq.GetEquipmentidsByUid(this.User.Id)
  338. where := " (a.CreateUserId=" + this.User.Id + " or a.EquipMentId in (" + eids + ")) and a.DataItem in (" + ChannelItem_HomeList + ") "
  339. where += " and a.Serial in (" + bindchannels + ")"
  340. _, devices := svdevc.GetChannelsList(-1, -1, where, this.User.Id)
  341. //报警器网关一体机下发数据
  342. var downdatas labsop.AlarmGatewayBindings
  343. downdatadetail := make([]labsop.AlarmGatewayBinding, 0)
  344. //报警器绑定数据
  345. bindchannelcodes := make([]string, 0)
  346. for _, v := range devices {
  347. if v.Code != "" {
  348. //报警器绑定数据
  349. bindchannelcodes = append(bindchannelcodes, v.Code)
  350. //网关一体机下发数据
  351. if len(v.Serial) == 8 {
  352. mac1, _ := strconv.ParseUint(v.Serial[0:2], 16, 0)
  353. mac2, _ := strconv.ParseUint(v.Serial[2:4], 16, 0)
  354. mac3, _ := strconv.ParseUint(v.Serial[4:6], 16, 0)
  355. mac4, _ := strconv.ParseUint(v.Serial[6:], 16, 0)
  356. //Cmd和需绑定的无线记录仪编号
  357. sendbyte := []byte{0x00, byte(mac1), byte(mac2), byte(mac3), byte(mac4)}
  358. name := utils.Substr(v.Title, 0, 10)
  359. namebyte := []byte(CodertoGBK(name))
  360. fuzai := []byte{0x73, byte(len(namebyte))}
  361. fuzai = append(fuzai, namebyte...)
  362. //yuzhi := []byte{0x65, 0x01, 0x4E, 0x20, 0x65, 0x00, 0x4E, 0x20, 0x66, 0x01, 0x4E, 0x20, 0x66, 0x00, 0xB1, 0xE0}
  363. //fuzai = append(yuzhi, fuzai...)
  364. fuzailen := byte(len(fuzai))
  365. //如果有温度阀值则 给服务器下发增加温度阀值
  366. max := strconv.FormatFloat(v.MaxValue, 'E', -1, 64)
  367. //拼接负载长度
  368. if v.MaxValue != 0 && max != "" {
  369. v.MaxValue = v.MaxValue * 100
  370. data := v.MaxValue
  371. i := uint16(data)
  372. c := make([]byte, 2)
  373. binary.BigEndian.PutUint16(c, i)
  374. v.MinValue = v.MinValue * 100
  375. data1 := v.MinValue
  376. i1 := uint16(data1)
  377. c1 := make([]byte, 2)
  378. binary.BigEndian.PutUint16(c1, i1)
  379. sendms := []byte{0x65, 0x00, c1[0], c1[1], 0x65, 0x01, c[0], c[1], 0x66, 0x00, 0x00, 0x00, 0x66, 0x01, 0x26, 0xAC}
  380. changdu1 := append(sendms, fuzai...)
  381. changdu := byte(len(changdu1))
  382. sendms = []byte{changdu, 0x65, 0x00, c1[0], c1[1], 0x65, 0x01, c[0], c[1], 0x66, 0x00, 0x00, 0x00, 0x66, 0x01, 0x26, 0xAC}
  383. sendbyte = append(sendbyte, sendms...)
  384. } else {
  385. sendbyte = append(sendbyte, fuzailen)
  386. }
  387. //拼接负载数据
  388. sendbyte = append(sendbyte, fuzai...)
  389. downdatadetail = append(downdatadetail, labsop.AlarmGatewayBinding{v.Serial, sendbyte})
  390. }
  391. }
  392. }
  393. fmt.Println("-------downdatadetail-------", model.Serial, downdatadetail)
  394. // 写入绑定缓存
  395. for i, _ := range model.BindDevices {
  396. model.BindDevices[i] = "c" + model.BindDevices[i]
  397. }
  398. WriteAlertBindValue("alert"+model.Serial, strings.Join(model.BindDevices, ","))
  399. // 写入网关报警一体机下发数据
  400. downdatas.AlarmBinds = downdatadetail
  401. labsop.UpdateAlarmbinddata(model.Serial, downdatas)
  402. }
  403. //修改动作
  404. svcaction := actions.GetActionsService(utils.DBE)
  405. action := svcaction.GetAlertor(devices.Serial)
  406. if action.Id > 0 {
  407. action.AName = devices.Title
  408. var status Status
  409. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/actions/" + utils.ToStr(action.Id) + "?u=" + u + "&p=" + p
  410. json.Unmarshal(Apipost(strUrl, "PUT", action), &status)
  411. if status.Status == 0 {
  412. var cols []string = []string{"AName"}
  413. svcaction.UpdateEntityByIdCols(action.Id, &action, cols)
  414. }
  415. }
  416. errinfo.Message = "保存成功!"
  417. errinfo.Code = 0
  418. this.Data["json"] = &errinfo
  419. this.ServeJSON()
  420. } else {
  421. errinfo.Message = "保存失败!" + utils.AlertProcess(err.Error())
  422. errinfo.Code = -1
  423. this.Data["json"] = &errinfo
  424. this.ServeJSON()
  425. }
  426. }
  427. // @Title 删除报警器
  428. // @Description 删除报警器
  429. // @Param code path string true "需要删除的报警器编号"
  430. // @Success 200 {object} ErrorInfo
  431. // @Failure 403 :code 为空
  432. // @router /:code [delete]
  433. func (this *AlertsController) Delete() {
  434. code := this.Ctx.Input.Param(":code")
  435. var errinfo ErrorInfo
  436. if code == "" {
  437. errinfo.Message = "操作失败!请求信息不完整"
  438. errinfo.Code = -3
  439. this.Data["json"] = &errinfo
  440. this.ServeJSON()
  441. return
  442. }
  443. code = "c" + code
  444. var entity device.Channels
  445. var devices device.Device
  446. var devicesempty device.Device
  447. var entityempty device.Channels
  448. svc := device.GetDeviceService(utils.DBE)
  449. has := svc.GetEntity(&entity, "Code='"+code+"'")
  450. if has {
  451. dataitem := entity.DataItem
  452. err := svc.DeleteEntityAndBackup(entity.Id, &entity, &entityempty, utils.ToStr(this.User.Id), this.User.Username)
  453. if err == nil {
  454. //取消waterdrop绑定状态
  455. u, p := this.GetuAndp()
  456. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/unbingding/" + code + "?u=" + u + "&p=" + p
  457. Apipost(strUrl, "PUT", nil)
  458. //删除设备
  459. //取消waterdrop绑定状态
  460. wdid := svc.GetWDidByDid(entity.DId)
  461. strUrldevice := utils.Cfg.MustValue("server", "apiurl") + "/devices/unbingding/" + utils.ToStr(wdid) + "?u=" + u + "&p=" + p
  462. Apipost(strUrldevice, "PUT", nil)
  463. svc.DeleteEntityAndBackup(entity.DId, &devices, &devicesempty, utils.ToStr(this.User.Id), this.User.Username)
  464. var channeldevice device.Channels
  465. haschannel := svc.GetEntity(&channeldevice, " DataItem=3 and DId="+utils.ToStr(entity.DId))
  466. if haschannel {
  467. svc.DeleteEntityAndBackup(channeldevice.Id, &channeldevice, &entityempty, utils.ToStr(this.User.Id), this.User.Username)
  468. cstrUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/unbingding/" + channeldevice.Code + "?u=" + u + "&p=" + p
  469. Apipost(cstrUrl, "PUT", nil)
  470. }
  471. //无动作不需要删除
  472. if DeviceItemContain(Alertor_NotNeedAction, utils.ToStr(dataitem)) {
  473. //删除屏显内容
  474. strUrldeletemsg := utils.Cfg.MustValue("server", "apiurl") + "/ledscreenmessage/clear?u=" + u + "&p=" + p + "&code=" + entity.Serial + "&sourse=" + ProjectSourse + "&account=" + this.GetAccode() + "&dataitem=1"
  475. Apipost(strUrldeletemsg, "DELETE", nil)
  476. } else {
  477. //删除动作
  478. svcaction := actions.GetActionsService(utils.DBE)
  479. action := svcaction.GetAlertor(entity.Serial)
  480. if action.Id > 0 {
  481. var status Status
  482. //删除报警动作
  483. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/triggers/deletebyaid/" + utils.ToStr(action.Id) + "?u=" + u + "&p=" + p
  484. json.Unmarshal(Apipost(strUrl, "DELETE", nil), &status)
  485. //删除动作
  486. strUrl = utils.Cfg.MustValue("server", "apiurl") + "/actions/" + utils.ToStr(action.Id) + "?u=" + u + "&p=" + p
  487. json.Unmarshal(Apipost(strUrl, "DELETE", nil), &status)
  488. if status.Status == 0 {
  489. var actionempty actions.Actions
  490. svc.DeleteEntityById(action.Id, actionempty)
  491. }
  492. }
  493. }
  494. //删除绑定设备
  495. WriteAlertBindValue("alert"+strings.Replace(code, "c", "", 1), "")
  496. errinfo.Message = "删除成功!"
  497. errinfo.Code = 0
  498. this.Data["json"] = &errinfo
  499. this.ServeJSON()
  500. } else {
  501. errinfo.Message = "删除失败!" + utils.AlertProcess(err.Error())
  502. errinfo.Code = -1
  503. this.Data["json"] = &errinfo
  504. this.ServeJSON()
  505. }
  506. } else {
  507. errinfo.Message = "删除失败!"
  508. errinfo.Code = -2
  509. this.Data["json"] = &errinfo
  510. this.ServeJSON()
  511. }
  512. }
  513. // @Title 创建屏显信息
  514. // @Description 创建屏显信息
  515. // @Param body body business.ledscreenmessage.LED_ScreenMessage "屏显信息"
  516. // @Success 200 {object} controllers.Request
  517. // @router /ledinfo/add [put]
  518. func (this *AlertsController) LedInfoAdd() {
  519. var model LedInfoModel
  520. var jsonblob = this.Ctx.Input.RequestBody
  521. json.Unmarshal(jsonblob, &model)
  522. var entity ledscreenmessage.LED_ScreenMessage
  523. var status Status
  524. var errinfo ErrorInfo
  525. entity.StartTime = model.TimeRange[0]
  526. entity.EndTime = model.TimeRange[1]
  527. entity.Message = model.Message
  528. entity.Code = model.Serial
  529. entity.DataItem = 1
  530. entity.ProjectSourse = ProjectSourse
  531. entity.ProjectAccount = this.GetAccode()
  532. entity.ProjectAccountName = this.User.Realname
  533. u, p := this.GetuAndp()
  534. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/ledscreenmessage?u=" + u + "&p=" + p
  535. json.Unmarshal(Apipost(strUrl, "POST", entity), &status)
  536. if status.Status == 0 {
  537. errinfo.Message = "屏显信息保存成功!"
  538. errinfo.Code = 0
  539. this.Data["json"] = &errinfo
  540. this.ServeJSON()
  541. } else {
  542. errinfo.Message = "屏显信息保存失败!"
  543. errinfo.Code = -1
  544. this.Data["json"] = &errinfo
  545. this.ServeJSON()
  546. }
  547. }
  548. // @Title 屏显信息列表
  549. // @Description 屏显信息列表
  550. // @Success 200 {object} business.ledscreenmessage.LED_ScreenMessage
  551. // @router /ledinfo/list [get]
  552. func (this *AlertsController) LedInfoList() {
  553. var entity LedMessageList
  554. u, p := this.GetuAndp()
  555. code := this.GetString("serial")
  556. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/ledscreenmessage?u=" + u + "&p=" + p + "&page=1&itemsPerPage=-1&code=" + code + "&sourse=" + ProjectSourse + "&account=" + this.GetAccode() + "&dataitem=1"
  557. json.Unmarshal(Apiget(strUrl), &entity)
  558. fmt.Println(entity, strUrl)
  559. this.Data["json"] = entity
  560. this.ServeJSON()
  561. }
  562. // @Title 创建屏显信息
  563. // @Description 创建屏显信息
  564. // @Param body body business.ledscreenmessage.LED_ScreenMessage "屏显信息"
  565. // @Success 200 {object} controllers.Request
  566. // @router /ledinfo/update [put]
  567. func (this *AlertsController) LedInfoUpdate() {
  568. var model LedInfoModel
  569. var jsonblob = this.Ctx.Input.RequestBody
  570. json.Unmarshal(jsonblob, &model)
  571. var entity ledscreenmessage.LED_ScreenMessage
  572. var status Status
  573. var errinfo ErrorInfo
  574. entity.StartTime = model.TimeRange[0]
  575. entity.EndTime = model.TimeRange[1]
  576. entity.Message = model.Message
  577. entity.DataState = 0
  578. u, p := this.GetuAndp()
  579. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/ledscreenmessage/" + utils.ToStr(model.Id) + "?u=" + u + "&p=" + p
  580. json.Unmarshal(Apipost(strUrl, "PUT", entity), &status)
  581. if status.Status == 0 {
  582. errinfo.Message = "屏显信息保存成功!"
  583. errinfo.Code = 0
  584. this.Data["json"] = &errinfo
  585. this.ServeJSON()
  586. } else {
  587. errinfo.Message = "屏显信息保存失败!"
  588. errinfo.Code = -1
  589. this.Data["json"] = &errinfo
  590. this.ServeJSON()
  591. }
  592. }
  593. // @Title 删除屏显信息
  594. // @Description 删除屏显信息
  595. // @Param body body business.ledscreenmessage.LED_ScreenMessage "屏显信息"
  596. // @Success 200 {object} controllers.Request
  597. // @router /ledinfo/delete [put]
  598. func (this *AlertsController) LedInfoDelete() {
  599. var model LedInfoModel
  600. var jsonblob = this.Ctx.Input.RequestBody
  601. json.Unmarshal(jsonblob, &model)
  602. var status Status
  603. var errinfo ErrorInfo
  604. u, p := this.GetuAndp()
  605. strUrl := utils.Cfg.MustValue("server", "apiurl") + "/ledscreenmessage/" + utils.ToStr(model.Id) + "?u=" + u + "&p=" + p
  606. json.Unmarshal(Apipost(strUrl, "DELETE", nil), &status)
  607. if status.Status == 0 {
  608. errinfo.Message = "屏显信息已删除!"
  609. errinfo.Code = 0
  610. this.Data["json"] = &errinfo
  611. this.ServeJSON()
  612. } else {
  613. errinfo.Message = "屏显信息删除失败!"
  614. errinfo.Code = -1
  615. this.Data["json"] = &errinfo
  616. this.ServeJSON()
  617. }
  618. }