alerts.go 24 KB

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