package trigger import ( "encoding/json" "strings" "time" "dashoo.cn/backend/api/business/actions" "dashoo.cn/backend/api/business/device" "dashoo.cn/backend/api/business/userchannels" . "dashoo.cn/backend/api/controllers" "dashoo.cn/utils" ) // 报警器接口说明 type AlertsController struct { BaseController } type AlertModel struct { Title string `json:"title"` Serial string `json:"serial"` TagCode string `json:"tagCode"` BindDevices []string `json:"binddevices"` } type LedInfoModel struct { TimeRange []time.Time `json:"timerange"` Message string `json:"message"` Serial string `json:"serial"` Id int `json:"id"` } type LedMessageList struct { CurrentItemCount int64 `json:"currentItemCount,omitempty"` //结果集中的条目数目 ItemsPerPage int64 `json:"itemsPerPage,omitempty"` //每页记录数目 PageIndex int64 `json:"pageIndex,omitempty"` //条目的当前页索引 // Items []ledscreenmessage.LED_ScreenMessage `json:"items"` //数据列表 } // @Title 报警器列表 // @Description 设备列表 // @Success 200 {object} business.device.DeviceChannels // @router /list [get] func (this *AlertsController) List() { page := this.GetPageInfoForm() svc := device.GetDeviceService(utils.DBE) svcuc := userchannels.GetUserChannelService(utils.DBE) channelid := svcuc.GetChannelids(utils.ToStr(this.User.Id)) Uid := utils.ToStr(this.User.Id) where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.Id in (" + strings.Join(channelid, ",") + ")) and a.DataItem in (" + Alertor_Alarm + ") " keyword := this.GetString("keyword") if keyword != "" { where = where + " and (a.Title like '%" + keyword + "%' or a.TagCode like '%" + keyword + "%' or a.Serial like '%" + keyword + "%')" } total, devices := svc.GetPagingEntitiesWithOrderSearch(page.CurrentPage, page.Size, "c.sortcode, a.CreateOn desc", where, Uid) var datainfo DataInfo datainfo.Items = devices datainfo.CurrentItemCount = total this.Data["json"] = &datainfo this.ServeJSON() } // @Title 验证序列号 // @Description 验证序列号 // @Param code path string true "设备SN" // @Success 200 {object} ErrorInfo // @Failure 403 :code 为空 // @router /validcode/:code [get] func (this *AlertsController) ValidCode() { code := this.Ctx.Input.Param(":code") var errinfo ErrorInfo var entity device.Device strUrl := utils.Cfg.MustValue("server", "apiurl") + "/devices/serial?serial=" + code json.Unmarshal(Apiget(strUrl), &entity) if entity.Id != 0 && DeviceItemContainint(Device_Alertor, entity.DataItem) { errinfo.Message = "验证通过!" errinfo.Code = 0 } else { errinfo.Message = "报警器序列号不存在!" errinfo.Code = -1 } this.Data["json"] = &errinfo this.ServeJSON() } // @Title 创建报警器 // @Description 创建报警器 // @Param body body business.device.DeviceChannels "报警器信息" // @Success 200 {object} controllers.Request // @router / [post] func (this *AlertsController) AddPost() { var model AlertModel var jsonblob = this.Ctx.Input.RequestBody json.Unmarshal(jsonblob, &model) var errinfo ErrorInfo u, p := this.GetuAndp() var devices device.Device devices.Serial = model.Serial devices.Code = model.TagCode devices.Title = model.Title svc := device.GetDeviceService(utils.DBE) var entity device.Device strUrl := utils.Cfg.MustValue("server", "apiurl") + "/devices/serial?serial=" + devices.Serial json.Unmarshal(Apiget(strUrl), &entity) if svc.VerifyDevice(entity.Id) { errinfo.Message = "报警器已存在!" errinfo.Code = -1 this.Data["json"] = &errinfo this.ServeJSON() return } else { if devices.Title == "" { devices.Title = entity.Title } if devices.About == "" { devices.About = entity.About } if devices.Tags == "" { devices.Tags = entity.Tags } if devices.Local == "" { devices.Local = entity.Local } if devices.Latitude == 0 { devices.Latitude = entity.Latitude devices.Longitude = entity.Longitude } devices.Wdid = entity.Id devices.Dtype = 1 devices.CreateUserId, _ = utils.StrTo(this.User.Id).Int() devices.CreateBy = this.User.Realname devices.DataItem = entity.DataItem //添加device表 _, err := svc.InsertEntity(&devices) //添加device成功则添加channel表 if err == nil { strUrl = utils.Cfg.MustValue("server", "apiurl") + "/devices/" + utils.ToStr(entity.Id) + "?u=" + u + "&p=" + p Apipost(strUrl, "PUT", devices) 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 Apipost(strUrl, "PUT", devices) if entity.Id != 0 { var channels []device.Channels strUrl = utils.Cfg.MustValue("server", "apiurl") + "/devices/" + utils.ToStr(entity.Id) + "/channelsallclos?u=" + u + "&p=" + p json.Unmarshal(Apiget(strUrl), &channels) //默认添加动作 isaddaction := true for _, v := range channels { if DeviceItemContainint(Alertor_AlarmBindData, v.DataItem) || DeviceItemContainint(Alertor_Alarm, v.DataItem) { v.Title = devices.Title v.TagCode = devices.Code v.DId = devices.Id v.CreateUserId, _ = utils.StrTo(this.User.Id).Int() v.CreateBy = this.User.Realname svc.InsertEntity(&v) } //绑定设备 if DeviceItemContainint(Alertor_AlarmBindData, v.DataItem) { if len(model.BindDevices) > 0 { bindchannels := "c" + strings.Join(model.BindDevices, ",c") WriteAlertBindValue("alert"+model.Serial, bindchannels) } } //不需要添加动作 if DeviceItemContainint(Alertor_NotNeedAction, v.DataItem) { isaddaction = false } } //添加一条动作 if isaddaction { var action actions.Actions action.AItem = 4 action.AName = devices.Title action.SPara4 = devices.Serial action.Enabled = 1 var status Status strUrl = utils.Cfg.MustValue("server", "apiurl") + "/actions/?u=" + u + "&p=" + p json.Unmarshal(Apipost(strUrl, "POST", action), &status) if status.Status == 0 { action.AccCode = this.GetAccode() action.Wdid = status.Id action.Id, _ = utils.StrTo(status.Id).Int() action.CreateUserId, _ = utils.StrTo(this.User.Id).Int() action.CreateBy = this.User.Realname svc.InsertEntity(&action) //插入数据库 } } } errinfo.Message = "保存成功!" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() return } else { errinfo.Message = "保存失败!" + utils.AlertProcess(err.Error()) errinfo.Code = -2 this.Data["json"] = &errinfo this.ServeJSON() return } } } // @Title 获取绑定设备 // @Description 获取绑定设备 // @Param code path string true "设备SN" // @Success 200 {object} []string // @Failure 403 :code 为空 // @router /binddevices/:code [get] func (this *AlertsController) GetBindDevices() { code := this.Ctx.Input.Param(":code") var bddevices []string = []string{} selectstr := "" lastdata, err := GetChannelLast("alert" + code) if err == nil && lastdata.Time.Unix() > 0 { selectstr = strings.Replace(lastdata.RequestData, "c", "", -1) } if selectstr != "" { bddevices = strings.Split(selectstr, ",") } this.Data["json"] = bddevices this.ServeJSON() } // @Title 编辑报警器 // @Description 编辑报警器 // @Param code path string true "需要修改的报警器编号" // @Param body body business.device.DeviceChannels "报警器信息" // @Success 200 {object} ErrorInfo // @router /:code [put] //func (this *AlertsController) EditPost() { // code := this.Ctx.Input.Param(":code") // var errinfo ErrorInfo // if code == "" { // errinfo.Message = "操作失败!请求信息不完整" // errinfo.Code = -2 // this.Data["json"] = &errinfo // this.ServeJSON() // return // } // code = "c" + code // var model AlertModel // var jsonblob = this.Ctx.Input.RequestBody // json.Unmarshal(jsonblob, &model) // u, p := this.GetuAndp() // var devices logsinfo.AlertorLog // svc := device.GetDeviceService(utils.DBE) // has := svc.GetEntity(&devices, "Serial='"+model.Serial+"' and DataItem=4") // if !has { // errinfo.Message = "操作失败!请求数据有误" // errinfo.Code = -3 // this.Data["json"] = &errinfo // this.ServeJSON() // return // } // 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}} // svc.InsertUpdateLog(0, &deviceLog_Log, utils.ToStr(this.User.Id), this.User.Realname, AlertorLogTName) // devices.Code = model.TagCode // devices.Title = model.Title // devices.ModifiedUserId, _ = utils.StrTo(this.User.Id).Int() // devices.ModifiedBy = this.User.Realname // var cols []string = []string{"Title", "Code", "ModifiedUserId", "ModifiedBy"} // _, err := svc.UpdateEntityByIdCols(devices.Id, &devices, cols) // if err == nil { // strUrl := utils.Cfg.MustValue("server", "apiurl") + "/devices/" + utils.ToStr(devices.Wdid) + "?u=" + u + "&p=" + p // Apipost(strUrl, "PUT", devices) // //绑定设备 // if len(model.BindDevices) > 0 { // bindchannels := "c" + strings.Join(model.BindDevices, ",c") // WriteAlertBindValue("alert"+model.Serial, bindchannels) // } // //修改动作 // svcaction := actions.GetActionsService(utils.DBE) // action := svcaction.GetAlertor(devices.Serial) // if action.Id > 0 { // action.AName = devices.Title // var status Status // strUrl = utils.Cfg.MustValue("server", "apiurl") + "/actions/" + utils.ToStr(action.Id) + "?u=" + u + "&p=" + p // json.Unmarshal(Apipost(strUrl, "PUT", action), &status) // if status.Status == 0 { // var cols []string = []string{"AName"} // svcaction.UpdateEntityByIdCols(action.Id, &action, cols) // } // } // errinfo.Message = "保存成功!" // errinfo.Code = 0 // this.Data["json"] = &errinfo // this.ServeJSON() // } else { // errinfo.Message = "保存失败!" + utils.AlertProcess(err.Error()) // errinfo.Code = -1 // this.Data["json"] = &errinfo // this.ServeJSON() // } //} // @Title 删除报警器 // @Description 删除报警器 // @Param code path string true "需要删除的报警器编号" // @Success 200 {object} ErrorInfo // @Failure 403 :code 为空 // @router /:code [delete] func (this *AlertsController) Delete() { code := this.Ctx.Input.Param(":code") var errinfo ErrorInfo if code == "" { errinfo.Message = "操作失败!请求信息不完整" errinfo.Code = -3 this.Data["json"] = &errinfo this.ServeJSON() return } code = "c" + code var entity device.Channels var devices device.Device var devicesempty device.Device var entityempty device.Channels svc := device.GetDeviceService(utils.DBE) has := svc.GetEntity(&entity, "Code='"+code+"'") if has { dataitem := entity.DataItem err := svc.DeleteEntityAndBackup(entity.Id, &entity, &entityempty, utils.ToStr(this.User.Id), this.User.Username) if err == nil { //取消waterdrop绑定状态 u, p := this.GetuAndp() strUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/unbingding/" + code + "?u=" + u + "&p=" + p Apipost(strUrl, "PUT", nil) //删除设备 //取消waterdrop绑定状态 wdid := svc.GetWDidByDid(entity.DId) strUrldevice := utils.Cfg.MustValue("server", "apiurl") + "/devices/unbingding/" + utils.ToStr(wdid) + "?u=" + u + "&p=" + p Apipost(strUrldevice, "PUT", nil) svc.DeleteEntityAndBackup(entity.DId, &devices, &devicesempty, utils.ToStr(this.User.Id), this.User.Username) var channeldevice device.Channels haschannel := svc.GetEntity(&channeldevice, " DataItem=3 and DId="+utils.ToStr(entity.DId)) if haschannel { svc.DeleteEntityAndBackup(channeldevice.Id, &channeldevice, &entityempty, utils.ToStr(this.User.Id), this.User.Username) cstrUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/unbingding/" + channeldevice.Code + "?u=" + u + "&p=" + p Apipost(cstrUrl, "PUT", nil) } //无动作不需要删除 if DeviceItemContain(Alertor_NotNeedAction, utils.ToStr(dataitem)) { //删除屏显内容 strUrldeletemsg := utils.Cfg.MustValue("server", "apiurl") + "/ledscreenmessage/clear?u=" + u + "&p=" + p + "&code=" + entity.Serial + "&sourse=" + ProjectSourse + "&account=" + this.GetAccode() + "&dataitem=1" Apipost(strUrldeletemsg, "DELETE", nil) } else { //删除动作 svcaction := actions.GetActionsService(utils.DBE) action := svcaction.GetAlertor(entity.Serial) if action.Id > 0 { var status Status //删除报警动作 strUrl = utils.Cfg.MustValue("server", "apiurl") + "/triggers/deletebyaid/" + utils.ToStr(action.Id) + "?u=" + u + "&p=" + p json.Unmarshal(Apipost(strUrl, "DELETE", nil), &status) //删除动作 strUrl = utils.Cfg.MustValue("server", "apiurl") + "/actions/" + utils.ToStr(action.Id) + "?u=" + u + "&p=" + p json.Unmarshal(Apipost(strUrl, "DELETE", nil), &status) if status.Status == 0 { var actionempty actions.Actions svc.DeleteEntityById(action.Id, actionempty) } } } //删除绑定设备 WriteAlertBindValue("alert"+strings.Replace(code, "c", "", 1), "") errinfo.Message = "删除成功!" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() } else { errinfo.Message = "删除失败!" + utils.AlertProcess(err.Error()) errinfo.Code = -1 this.Data["json"] = &errinfo this.ServeJSON() } } else { errinfo.Message = "删除失败!" errinfo.Code = -2 this.Data["json"] = &errinfo this.ServeJSON() } }