1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188 |
- package controllers
- import (
- "Cold_Api/Nats/NatsServer"
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "Cold_Api/models/Account"
- "Cold_Api/models/Company"
- "Cold_Api/models/Device"
- "Cold_Api/models/System"
- "Cold_Api/models/Warning"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/shopspring/decimal"
- "math"
- "strconv"
- "strings"
- "time"
- )
- // Handle
- type DeviceController struct {
- beego.Controller
- Admin_r *Account.Admin // 登陆的用户
- T_pid int // 公司id
- }
- func (c *DeviceController) Prepare() {
- GetCookie := c.Ctx.GetCookie("User_tokey")
- GetString := c.GetString("User_tokey")
- User_tokey := GetCookie
- if len(User_tokey) == 0 {
- User_tokey = GetString
- }
- if Account.Admin_r == nil {
- return
- }
- c.Admin_r = Account.Admin_r
- T_pid := c.Admin_r.T_pid
- EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
- if EntryPid > 0 {
- T_pid = EntryPid
- }
- c.T_pid = T_pid
- }
- // 设备 ------------------------------------------
- func (c *DeviceController) Device_List() {
- type R_JSONS struct {
- //必须的大写开头
- Device_lite []Device.Device_R
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- Name := c.GetString("T_name")
- T_abandon, _ := c.GetInt("T_abandon")
- T_online := c.GetString("T_online")
- T_monitor := c.GetString("T_monitor")
- bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- r_jsons.Device_lite, r_jsons.Num = Device.Read_Device_List(c.Admin_r, bindSN, c.T_pid, Name, T_monitor, T_online, T_abandon, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) Device_Get() {
- var r_jsons lib.R_JSONS
- T_sn := c.GetString("T_sn")
- Device_r, err := Device.Read_Device_ByT_sn(T_sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- if Device_r.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"}
- c.ServeJSON()
- return
- }
- r_jsons.Data = Device.DeviceToDevice_R(Device_r)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) Device_Add() {
- var err error
- T_sn := c.GetString("T_sn")
- var_ := Device.Device{
- T_pid: c.T_pid,
- T_sn: T_sn,
- T_protocol: 3,
- T_monitor: 0, // 监控状态 已监控
- T_State: 1, // 状态 正常
- T_ist: 1, // 温度 1开启
- T_ish: 1, // 湿度 1开启
- }
- if len(T_sn) < 8 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN 太短 必须8个字符以上"}
- c.ServeJSON()
- return
- }
- _, err = Device.Read_Device_ByT_sn(T_sn)
- if err == nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复SN!"}
- c.ServeJSON()
- return
- }
- var_.T_devName = T_sn
- // 创建数据库
- if !Device.CREATE_DeviceData(T_sn) {
- c.Data["json"] = lib.JSONS{Code: 302, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- err = Device.Add_Device(var_)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- // 添加设备后自动重启
- MqttIds := strings.Split(conf.MqttIds, ",")
- for _, id := range MqttIds {
- NatsServer.Set_RestartShutdown(Device.Device_task{
- T_sn: T_sn,
- T_task: "1",
- }, id)
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "设备添加", var_)
- NatsServer.Read_DeviceParameter(T_sn, "") // 获取主机信息
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) Device_Edit() {
- T_SN := c.GetString("T_sn")
- Device_r, err := Device.Read_Device_ByT_sn(T_SN)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- clos := make([]string, 0)
- // 监控状态 0 未监控 1 监控
- T_monitor, T_monitor_err := c.GetInt("T_monitor")
- if T_monitor_err == nil {
- Device_r.T_monitor = T_monitor
- clos = append(clos, "T_monitor")
- }
- // 温度显示
- T_ist, T_ist_err := c.GetInt("T_ist")
- if T_ist_err == nil {
- Device_r.T_ist = T_ist
- clos = append(clos, "T_ist")
- }
- // 适度显示
- T_ish, T_ish_err := c.GetInt("T_ish")
- if T_ish_err == nil {
- Device_r.T_ish = T_ish
- clos = append(clos, "T_ish")
- }
- // 是否弃用
- T_State, T_State_err := c.GetInt("T_State")
- if T_State_err == nil {
- Device_r.T_State = T_State
- if T_State == 0 {
- Device_r.T_abandonTime = time.Now()
- clos = append(clos, "T_abandonTime")
- }
- clos = append(clos, "T_State")
- }
- // 验证时间
- T_VerifyTime := c.GetString("T_VerifyTime")
- VerifyTime, VerifyTime_is := lib.TimeStrToTime(T_VerifyTime)
- if VerifyTime_is {
- Device_r.T_VerifyTime = VerifyTime
- clos = append(clos, "T_VerifyTime")
- }
- // 校准时间
- T_CalibrationTime := c.GetString("T_CalibrationTime")
- CalibrationTime, CalibrationTime_is := lib.TimeStrToTime(T_CalibrationTime)
- if CalibrationTime_is {
- Device_r.T_CalibrationTime = CalibrationTime
- clos = append(clos, "T_CalibrationTime")
- // 同步冷链验证校准到期时间
- NatsServer.Update_Task_BySN(T_SN, CalibrationTime)
- }
- // 巡检时间
- T_PatrolTime := c.GetString("T_PatrolTime")
- PatrolTime, PatrolTime_is := lib.TimeStrToTime(T_PatrolTime)
- if PatrolTime_is {
- Device_r.T_PatrolTime = PatrolTime
- clos = append(clos, "T_PatrolTime")
- }
- if is := Device.Update_Device(Device_r, clos...); !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "修改", Device_r)
- if is := Device.Update_Device_To_DeviceSensor(Device_r); is {
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器修改", Device_r)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) Device_Applet_Get() {
- T_snid := c.GetString("T_snid")
- sn_id := strings.Split(strings.Trim(T_snid, "|"), ",")
- if len(sn_id) != 2 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid err!"}
- c.ServeJSON()
- return
- }
- T_sn, T_id := sn_id[0], lib.To_int(sn_id[1])
- Device_r, err := Device.Read_Device_ByT_sn(T_sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- if Device_r.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"}
- c.ServeJSON()
- return
- }
- Dsp, _ := Device.Read_DeviceSensorParameter(T_sn, T_id)
- DeviceData := Device.Read_DeviceData(T_sn, T_id)
- type JSONS struct {
- T_name string // 设备名称
- T_monitor int // 监控状态 0 未监控 1 监控 停止记录
- T_ist int // 温度 1开启 2关闭
- T_ish int // 湿度 1开启 2关闭
- T_t float32 // 温度
- T_rh float32 // 湿度
- T_Tlower float32 // 温度下限
- T_Tupper float32 // 温度上限
- T_RHlower float32 // 湿度下限
- T_RHupper float32 // 湿度上限
- }
- r_jsons := JSONS{
- T_name: Device_r.T_devName,
- T_monitor: Device_r.T_monitor,
- T_ist: Device_r.T_ist,
- T_ish: Device_r.T_ish,
- T_t: DeviceData.T_t,
- T_rh: DeviceData.T_rh,
- T_Tlower: Dsp.T_Tlower,
- T_Tupper: Dsp.T_Tupper,
- T_RHlower: Dsp.T_RHlower,
- T_RHupper: Dsp.T_RHupper,
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 重启/关机
- func (c *DeviceController) Device_RestartShutdown() {
- T_sn := c.GetString("T_sn")
- T_task := c.GetString("T_task")
- device, err := Device.Read_Device_ByT_sn(T_sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- NatsServer.Set_RestartShutdown(Device.Device_task{
- T_sn: T_sn,
- T_task: T_task,
- }, device.T_mqttid)
- System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "重启/关机", T_sn+"-"+T_task)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 版本升级
- func (c *DeviceController) Device_ProductUpgrade() {
- T_sn := c.GetString("T_sn")
- T_version := c.GetString("T_version")
- T_file := c.GetString("T_file")
- device, err := Device.Read_Device_ByT_sn(T_sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- NatsServer.Up_ProductUpgrade(T_sn, T_version, T_file, device.T_mqttid)
- System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "版本升级", T_sn+"-"+T_version+"-"+T_file)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 2023-02-02 弃用
- //func (c *DeviceController) Device_Del() {
- // if admin_r.Id != 1 {
- // c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"}
- // c.ServeJSON()
- // return
- // }
- //
- // T_sn := c.GetString("T_sn")
- //
- // Device_r, err := Device.Read_Device_ByT_sn(T_sn)
- // if err != nil {
- // c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- // c.ServeJSON()
- // return
- // }
- //
- // Device.Delete_Device(Device_r)
- // DeviceSensor_list, _ := Device.Read_DeviceSensor_ByTsn(Device_r.T_sn)
- // for _, v := range DeviceSensor_list {
- // Device.Delete_DeviceSensor_ById(Device_r.T_sn, v.T_id)
- // }
- // Device.DELETE_DeviceSensor(Device_r.T_sn)
- // Device.DELETE_DeviceDatar(Device_r.T_sn)
- // Device.DELETE_DeviceParameter(Device_r.T_sn)
- // Device.DELETE_DeviceSensorParameter(Device_r.T_sn)
- //
- // System.Add_UserLogs(admin_r.T_uuid, "设备管理", "设备删除", T_sn)
- // c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- // c.ServeJSON()
- // return
- //
- //}
- // 设备日志
- func (c *DeviceController) DeviceLogs() {
- type R_JSONS struct {
- //必须的大写开头
- DeviceLogs []Warning.DeviceLogs
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_sn := c.GetString("T_sn")
- c.Data["T_sn"] = T_sn
- r_jsons.DeviceLogs, r_jsons.Num = Warning.Read_DeviceLogs_List(T_sn, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- }
- // 设备参数 ------------------------------------------
- // 设备参数列表
- func (c *DeviceController) Device_Parameter_List() {
- Sn := c.GetString("T_sn")
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- DeviceParameter_lite := Device.Read_DeviceParameter_SN_List(Sn, page, page_z)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceParameter_lite}
- c.ServeJSON()
- return
- }
- // 获取设备参数
- func (c *DeviceController) Device_Parameter_Get() {
- Sn := c.GetString("T_sn")
- device, err := Device.Read_Device_ByT_sn(Sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- DeviceParameter_lite := Device.Read_DeviceParameter_SN(Sn)
- if len(DeviceParameter_lite) == 0 {
- NatsServer.Read_DeviceParameter(Sn, device.T_mqttid)
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "未同步参数,请检查设备是否正常!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceParameter_lite}
- c.ServeJSON()
- return
- }
- // 修改设备参数
- func (c *DeviceController) Device_Parameter_Pu() {
- //T_SN := c.GetString("T_sn")
- //T_name := c.GetString("T_name")
- //
- //T_saveT, _ := c.GetInt("T_saveT")
- //T_overA, _ := c.GetInt("T_overA")
- //T_outA, _ := c.GetInt("T_outA")
- //T_lostA, _ := c.GetInt("T_lostA")
- //T_bat, _ := c.GetInt("T_bat")
- //
- //T_warn, _ := c.GetInt("T_warn")
- //T_warnD, _ := c.GetInt("T_warnD")
- //
- //T_btname := c.GetString("T_btname")
- //T_btsid, _ := c.GetInt("T_btsid")
- //T_btchar, _ := c.GetInt("T_btchar")
- //
- //T_Msid, _ := c.GetInt("T_Msid")
- //T_State, _ := c.GetInt("T_State")
- //
- //Device_r, err := Device.Read_Device_ByT_sn(T_SN)
- //if err != nil {
- // c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- // c.ServeJSON()
- // return
- //}
- //
- //Deviceparameter := Device.DeviceParameter{
- // T_sn: T_SN,
- // T_name: T_name,
- // T_saveT: T_saveT,
- // T_overA: T_overA,
- // T_outA: T_outA,
- // T_lostA: T_lostA,
- // T_bat: T_bat,
- // T_warn: T_warn,
- // T_warnD: T_warnD,
- // T_btname: T_btname,
- // T_btsid: T_btsid,
- // T_btchar: T_btchar,
- // T_Msid: int64(T_Msid),
- // T_uuid: c.Admin_r.T_uuid,
- // T_SendState: 0,
- // T_State: T_State,
- //}
- //Deviceparameter, is := Device.Add_DeviceParameter(Deviceparameter)
- //if !is {
- // c.Data["json"] = lib.JSONS{Code: 202, Msg: "Add_DeviceParameter ERR"}
- // c.ServeJSON()
- // return
- //}
- //System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "设备参数操作", Deviceparameter)
- //NatsServer.Pu_DeviceParameter(Deviceparameter)
- //
- //// 更新名称
- //Device_r.T_devName = Deviceparameter.T_name
- //Device.Update_Device(Device_r, "T_devName")
- //System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "修改", Device_r)
- T_SN := c.GetString("T_sn")
- Device_r, err := Device.Read_Device_ByT_sn(T_SN)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- DeviceParameter_lite := Device.Read_DeviceParameter_SN(T_SN)
- if len(DeviceParameter_lite) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "未同步参数,请检查设备是否正常!"}
- c.ServeJSON()
- return
- }
- DeviceParameter := DeviceParameter_lite[0]
- if v, err := c.GetInt("T_saveT"); err == nil {
- DeviceParameter.T_saveT = v
- }
- if v, err := c.GetInt("T_overA"); err == nil {
- DeviceParameter.T_overA = v
- }
- if v, err := c.GetInt("T_outA"); err == nil {
- DeviceParameter.T_outA = v
- }
- if v, err := c.GetInt("T_lostA"); err == nil {
- DeviceParameter.T_lostA = v
- }
- if v, err := c.GetInt("T_bat"); err == nil {
- DeviceParameter.T_bat = v
- }
- if v, err := c.GetInt("T_warn"); err == nil {
- DeviceParameter.T_warn = v
- }
- if v, err := c.GetInt("T_warnD"); err == nil {
- DeviceParameter.T_warnD = v
- }
- if v, err := c.GetInt("T_scan"); err == nil {
- DeviceParameter.T_scan = v
- }
- if v, err := c.GetInt("T_dormancy"); err == nil {
- DeviceParameter.T_dormancy = v
- }
- if v, err := c.GetInt("T_snum"); err == nil {
- DeviceParameter.T_snum = v
- }
- if v := c.GetString("T_btname"); len(v) > 0 {
- DeviceParameter.T_btname = v
- }
- if v, err := c.GetInt("T_btsid"); err == nil {
- DeviceParameter.T_btsid = v
- }
- if v, err := c.GetInt("T_btchar"); err == nil {
- DeviceParameter.T_btchar = v
- }
- if v, err := c.GetInt("T_Msid"); err == nil {
- DeviceParameter.T_Msid = int64(v)
- }
- if v := c.GetString("T_name"); len(v) > 0 {
- DeviceParameter.T_name = v
- }
- DeviceParameter.Id = 0
- DeviceParameter.T_State = 2
- DeviceParameter.T_uuid = c.Admin_r.T_uuid
- DeviceParameter.T_SendState = 0
- DeviceParameter.CreateTime = time.Now()
- DeviceParameter.UpdateTime = time.Now()
- Deviceparameter, is := Device.Add_DeviceParameter(DeviceParameter)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Add_DeviceParameter ERR"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "设备参数操作", Deviceparameter)
- NatsServer.Pu_DeviceParameter(Deviceparameter, Device_r.T_mqttid)
- if v := c.GetString("T_name"); len(v) > 0 {
- // 更新名称
- Device_r.T_devName = Deviceparameter.T_name
- Device.Update_Device(Device_r, "T_devName")
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "修改设备名称", Device_r)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 设备参数同步
- func (c *DeviceController) Device_Parameter_Sync() {
- T_SN := c.GetString("T_sn")
- Device_r, err := Device.Read_Device_ByT_sn(T_SN)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- go func() {
- NatsServer.Read_DeviceParameter(T_SN, Device_r.T_mqttid)
- time.Sleep(5 * time.Second)
- NatsServer.Read_DeviceSensorParameter(T_SN, Device_r.T_mqttid)
- }()
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 删除设备 传感器
- // 2023-02-02 弃用
- func (c *DeviceController) Device_Parameter_Del_Device() {
- Sn := c.GetString("Sn")
- Id, _ := c.GetInt("Id")
- device, err := Device.Read_Device_ByT_sn(Sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
- //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
- Device.Delete_DeviceSensor_ById(Sn, Id)
- NatsServer.Del_DeviceSensor(Device.DeviceSensor_Del{
- T_sn: Sn, T_id: Id,
- }, device.T_mqttid)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
- c.ServeJSON()
- return
- }
- // 设备-传感器 ------------------------------------------
- // 传感器列表
- func (c *DeviceController) DeviceSensor_List() {
- type R_JSONS struct {
- //必须的大写开头
- Data []Device.DeviceSensor_R
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_sn := c.GetString("T_sn")
- T_name := c.GetString("T_name") // 包含 T_name、T_sn
- T_class_id, _ := c.GetInt("T_calssid")
- T_type := c.GetString("T_type")
- T_State, T_State_err := c.GetInt("T_state")
- if T_State_err != nil {
- T_State = -1
- }
- T_datashow, T_datashow_err := c.GetInt("T_datashow") //0 屏蔽数据展示 1 正常数据展示
- if T_datashow_err != nil {
- T_datashow = -1
- }
- if T_class_id > 0 {
- R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceClass.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "R_DeviceClass.T_pid != T_pid Err!"}
- c.ServeJSON()
- return
- }
- }
- bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- r_jsons.Data, r_jsons.Num = Device.Read_DeviceSensorList(c.Admin_r, bindSN, c.T_pid, T_sn, T_name, T_class_id, T_datashow, T_type, T_State, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 修改传感器
- func (c *DeviceController) DeviceSensor_Edit() {
- Sn := c.GetString("T_sn")
- Id, _ := c.GetInt("T_id")
- DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "信息错误!"}
- c.ServeJSON()
- return
- }
- // 数据视图3D视图订阅
- if v, err := c.GetInt("T_type"); err == nil {
- if v == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "类型不能为默认!"}
- c.ServeJSON()
- return
- }
- DeviceSensor.T_type = v
- Device.Update_DeviceSensor(DeviceSensor, "T_type")
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 类型", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(v))
- }
- // 实时数据显示排序
- if v, err := c.GetInt("T_sort"); err == nil {
- DeviceSensor.T_sort = v
- Device.Update_DeviceSensor(DeviceSensor, "T_sort")
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 排序操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(v))
- }
- // 屏蔽数据展示
- if v, err := c.GetInt("T_datashow"); err == nil {
- DeviceSensor.T_datashow = v
- Device.Update_DeviceSensor(DeviceSensor, "T_datashow")
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 数据展示", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(v))
- }
- // 数据视图3D视图订阅
- if v := c.GetString("T_3dview"); len(v) > 0 {
- DeviceSensor.T_3dview = v
- Device.Update_DeviceSensor(DeviceSensor, "T_3dview")
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 3D视图订阅", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+v)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
- c.ServeJSON()
- return
- }
- // 删除传感器
- func (c *DeviceController) DeviceSensor_Del() {
- Sn := c.GetString("T_sn")
- Id, _ := c.GetInt("T_id")
- device, err := Device.Read_Device_ByT_sn(Sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- _, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "信息错误!"}
- c.ServeJSON()
- return
- }
- // 0默认:从数据库删除 1:从设备删除
- T_mode, _ := c.GetInt("T_mode")
- if T_mode == 1 {
- Device.Delete_DeviceSensor_ById(Sn, Id)
- NatsServer.Del_DeviceSensor(Device.DeviceSensor_Del{
- T_sn: Sn, T_id: Id,
- }, device.T_mqttid)
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
- } else {
- Device.Delete_DeviceSensor_ById(Sn, Id)
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
- c.ServeJSON()
- return
- }
- // 传感器参数
- func (c *DeviceController) DeviceSensor_Parameter_Get() {
- Sn := c.GetString("T_sn")
- Id, _ := c.GetInt("T_id")
- device, err := Device.Read_Device_ByT_sn(Sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- //c.Data["DeviceSensor"], _ = Device.Read_DeviceSensor_ByTsn_Tid(Sn, Id)
- DeviceSensorParameter_lite := Device.Read_DeviceSensorParameter_T_sn_T_id(Sn, Id)
- if len(DeviceSensorParameter_lite) == 0 {
- NatsServer.Read_DeviceSensorParameter(Sn, device.T_mqttid)
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "未同步参数,请检查设备是否正常!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceSensorParameter_lite}
- c.ServeJSON()
- return
- }
- // 传感器参数列表
- func (c *DeviceController) DeviceSensor_Parameter_List() {
- Sn := c.GetString("T_sn")
- Id, _ := c.GetInt("T_id")
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- DeviceSensorParameter_lite := Device.Read_DeviceSensorParameter_List(Sn, Id, 2, page, page_z)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceSensorParameter_lite}
- c.ServeJSON()
- return
- }
- // 修改传感器参数
- func (c *DeviceController) DeviceSensor_Parameter_Pu() {
- T_SN := c.GetString("T_sn")
- T_id, _ := c.GetInt("T_id")
- device, err := Device.Read_Device_ByT_sn(T_SN)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(T_SN, T_id)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id Err!"}
- c.ServeJSON()
- return
- }
- DeviceSensorParameter, is := Device.Read_DeviceSensorParameter(T_SN, T_id)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id Err!"}
- c.ServeJSON()
- return
- }
- T_name := DeviceSensor.T_name
- if v := c.GetString("T_name"); len(v) > 0 {
- T_name = v
- }
- T_Tlower := DeviceSensorParameter.T_Tlower
- if v, T_Tlower_err := c.GetFloat("T_Tlower"); T_Tlower_err == nil {
- temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
- T_Tlower = float32(temp)
- }
- T_Tupper := DeviceSensorParameter.T_Tupper
- if v, T_Tupper_err := c.GetFloat("T_Tupper"); T_Tupper_err == nil {
- temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
- T_Tupper = float32(temp)
- }
- T_RHlower := DeviceSensorParameter.T_RHlower
- if v, T_RHlower_err := c.GetFloat("T_RHlower"); T_RHlower_err == nil {
- temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
- T_RHlower = float32(temp)
- }
- T_RHupper := DeviceSensorParameter.T_RHupper
- if v, T_RHupper_err := c.GetFloat("T_RHupper"); T_RHupper_err == nil {
- temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
- T_RHupper = float32(temp)
- }
- T_enprel := DeviceSensorParameter.T_enprel
- if v, T_enprel_err := c.GetInt("T_enprel"); T_enprel_err == nil {
- T_enprel = v
- }
- T_tprel := DeviceSensorParameter.T_tprel
- if v, T_tprel_err := c.GetFloat("T_tprel"); T_tprel_err == nil {
- temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
- T_tprel = float32(temp)
- }
- T_tpreu := DeviceSensorParameter.T_tpreu
- if v, T_tpreu_err := c.GetFloat("T_tpreu"); T_tpreu_err == nil {
- temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
- T_tpreu = float32(temp)
- }
- T_hprel := DeviceSensorParameter.T_hprel
- if v, T_hprel_err := c.GetFloat("T_hprel"); T_hprel_err == nil {
- temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
- T_hprel = float32(temp)
- }
- T_hpreu := DeviceSensorParameter.T_hpreu
- if v, T_hpreu_err := c.GetFloat("T_hpreu"); T_hpreu_err == nil {
- temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
- T_hpreu = float32(temp)
- }
- T_enprelnote := DeviceSensorParameter.T_enprelnote
- if v, T_enprel_err := c.GetInt("T_enprelnote"); T_enprel_err == nil {
- T_enprelnote = v
- }
- //T_speed := DeviceSensorParameter.T_speed
- //if v, T_speed_err := c.GetInt("T_speed"); T_speed_err == nil {
- // T_speed = v
- //}
- //T_sense := DeviceSensorParameter.T_sense
- //if v, T_sense_err := c.GetInt("T_sense"); T_sense_err == nil {
- // T_sense = v
- //}
- T_en := DeviceSensorParameter.T_en
- if v, T_en_err := c.GetInt("T_en"); T_en_err == nil {
- T_en = v
- }
- T_free := DeviceSensorParameter.T_free
- if v, T_free_err := c.GetInt("T_free"); T_free_err == nil {
- T_free = v
- }
- dsp := Device.DeviceSensorParameter{
- T_sn: T_SN,
- T_id: T_id,
- T_name: T_name,
- T_Tlower: T_Tlower,
- T_Tupper: T_Tupper,
- T_RHlower: T_RHlower,
- T_RHupper: T_RHupper,
- //T_speed: T_speed,
- //T_sense: T_sense,
- T_en: T_en,
- T_free: T_free,
- T_enprel: T_enprel,
- T_tprel: T_tprel,
- T_tpreu: T_tpreu,
- T_hprel: T_hprel,
- T_hpreu: T_hpreu,
- T_enprelnote: T_enprelnote,
- T_uuid: c.Admin_r.T_uuid,
- T_SendState: 0,
- T_State: 2, // 用户提交
- }
- dsp, is = Device.Add_DeviceSensorParameter(dsp)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Add_DeviceSensorParameter Err!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器参数操作", dsp)
- // 更新名称
- if len(T_name) > 0 {
- DeviceSensor.T_name = T_name
- if is = Device.Update_DeviceSensor(DeviceSensor, "T_name"); !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Update_DeviceSensor Err!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "修改传感器名称", DeviceSensor)
- }
- NatsServer.Pu_DeviceParameter_Sensor(dsp, device.T_mqttid)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 传感器管理列表
- func (c *DeviceController) DeviceSensor_Manage_List() {
- type R_JSONS struct {
- //必须的大写开头
- Data []Device.DeviceSensor_P
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_name := c.GetString("T_name") // 包含 T_name、T_sn
- T_calss_id, _ := c.GetInt("T_calssid")
- if T_calss_id > 0 {
- R_DeviceClass, err := Company.Read_CompanyClass_ById(T_calss_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceClass.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "CompanyClass.T_pid != T_pid Err!"}
- c.ServeJSON()
- return
- }
- }
- T_en, T_en_err := c.GetInt("T_en") //启用停用 0 停用 1 启用 空 所有
- if T_en_err != nil {
- T_en = -1
- }
- T_free, T_free_err := c.GetInt("T_free") // 空库 0 正常 1 空库 空 所有
- if T_free_err != nil {
- T_free = -1
- }
- T_datashow, T_datashow_err := c.GetInt("T_datashow") //数据展示 空 全部 0 屏蔽数据展示 1 正常数据展示
- if T_datashow_err != nil {
- T_datashow = -1
- }
- T_sort, T_sort_err := c.GetInt("T_sort") //排序 空 升序 1 降序
- if T_sort_err != nil {
- T_sort = -1
- }
- T_abandon, _ := c.GetInt("T_abandon")
- bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- r_jsons.Data, r_jsons.Num = Device.Read_DeviceSensorManageList(c.Admin_r, bindSN, c.T_pid, T_name, T_calss_id, T_en, T_free, T_datashow, T_sort, T_abandon, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 设备任务 ------------------------------------------
- // 任务列表
- func (c *DeviceController) DeviceTask_List() {
- type R_JSONS struct {
- //必须的大写开头
- Data []Device.DeviceTask
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_sn := c.GetString("T_sn")
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- if len(T_sn) < 6 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn!!!"}
- c.ServeJSON()
- return
- }
- r_jsons.Data, r_jsons.Num = Device.Read_DeviceTask_List(T_sn, Time_start, Time_end, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) DeviceTask_All() {
- var r_jsons lib.R_JSONS
- T_sn := c.GetString("T_sn")
- if len(T_sn) < 6 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn!!!"}
- c.ServeJSON()
- return
- }
- r_jsons.Data, r_jsons.Num = Device.Read_DeviceTask_All(T_sn)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 任务启停
- func (c *DeviceController) DeviceTask_Post() {
- T_sn := c.GetString("T_sn")
- T_task := c.GetString("T_task")
- device, err := Device.Read_Device_ByT_sn(T_sn)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
- c.ServeJSON()
- return
- }
- NatsServer.Set_DeviceTask(Device.Device_task{
- T_sn: T_sn,
- T_task: T_task,
- }, device.T_mqttid)
- System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "远程启停", T_sn+"-"+T_task)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 设备分类 ------------------------------------------
- func (c *DeviceController) CompanyClass_List() {
- type R_JSONS struct {
- //必须的大写开头
- Data []Company.CompanyClass_R
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_name := c.GetString("T_name")
- r_jsons.Data, r_jsons.Num = Company.Read_CompanyClass_List(c.T_pid, T_name, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyClass_All() {
- type R_JSONS struct {
- //必须的大写开头
- Data []Company.CompanyClass_R
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- T_name := c.GetString("T_name")
- r_jsons.Data = Company.Read_CompanyClass_All(c.T_pid, T_name)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 查询公司及公司下所有子公司的公司分类
- func (c *DeviceController) CompanyClass_All_For_Company() {
- var r_jsons lib.R_JSONS
- T_name := c.GetString("T_name")
- // 查询公司
- Company_r, err := Account.Read_Company_ById(c.T_pid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
- c.ServeJSON()
- return
- }
- // 查询公司下面所有子公司id
- T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
- Account.Read_Company_All_Maps()
- r_jsons.Data = Company.Read_CompanyClass_All_For_Company(T_pids, T_name)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyClass_Get() {
- id, _ := c.GetInt("T_id")
- R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceClass.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Company.CompanyClassToCompanyClass_R(R_DeviceClass)}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyClass_Add() {
- T_name := c.GetString("T_name")
- t_c := Company.CompanyClass{
- T_pid: c.T_pid,
- T_name: T_name,
- T_State: 1,
- }
- Id, err := Company.Add_CompanyClass(t_c)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "分类添加", t_c)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyClass_Edit() {
- T_name := c.GetString("T_name")
- id, _ := c.GetInt("T_id")
- R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceClass.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- if len(T_name) > 0 {
- R_DeviceClass.T_name = T_name
- }
- if is := Company.Update_CompanyClass(R_DeviceClass, "T_name"); !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "分类修改", R_DeviceClass)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyClass_Del() {
- id, _ := c.GetInt("T_id")
- R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceClass.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- if is := Company.Delete_CompanyClass_ById(id); !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败"}
- c.ServeJSON()
- return
- }
- // 将传感器T_Class绑定的分类id替换为空字符串
- DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_Class_Id(id)
- for _, v := range DeviceSensor_class_list {
- Device.DeviceSensor_Bind_T_Class_Del(v.T_sn, v.T_id, id)
- }
- System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "分类删除", strconv.Itoa(id))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 设备分类绑定
- func (c *DeviceController) DeviceClassBind_List() {
- type R_JSONS struct {
- //必须的大写开头
- DeviceSensor_lite []Device.DeviceSensor_
- Num int
- Page int
- Page_size int
- Pages []lib.Page_T
- }
- var r_jsons R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_name := c.GetString("T_name")
- T_sn := c.GetString("T_sn")
- T_class_id, _ := c.GetInt("T_class_id")
- T_type, _ := c.GetInt("T_type")
- R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceClass.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- var cnt int64
- Device_lite, cnt := Device.Read_DeviceSensor_List_T_Class(c.T_pid, T_class_id, T_sn, T_name, T_type, page, page_z)
- r_jsons.DeviceSensor_lite = Device_lite
- page_size := math.Ceil(float64(cnt) / float64(page_z))
- r_jsons.Page = page
- r_jsons.Page_size = int(page_size)
- r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
- r_jsons.Num = int(cnt)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) ClassBind_Add() {
- T_class_id, _ := c.GetInt("T_class_id")
- T_sn := c.GetString("T_sn")
- T_id, _ := c.GetInt("T_id")
- if T_class_id == 0 || len(T_sn) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceClass.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Class(T_sn, T_id, T_class_id)
- if len(DeviceSensor_class_list) > 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复绑定!"}
- c.ServeJSON()
- return
- }
- if err = Device.DeviceSensor_Bind_T_Class_Add(T_sn, T_id, T_class_id); err != nil {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "绑定失败"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "分类绑定操作", strconv.Itoa(T_class_id)+"+>"+T_sn+"-"+strconv.Itoa(T_id))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) ClassBind_Del() {
- T_class_id, _ := c.GetInt("T_class_id")
- T_sn := c.GetString("T_sn")
- T_id, _ := c.GetInt("T_id")
- if T_class_id == 0 || len(T_sn) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceClass.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Class(T_sn, T_id, T_class_id)
- if len(DeviceSensor_class_list) == 0 {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- if err = Device.DeviceSensor_Bind_T_Class_Del(T_sn, T_id, T_class_id); err != nil {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除失败"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "分类删除绑定操作", strconv.Itoa(T_class_id)+"->"+T_sn+"-"+strconv.Itoa(T_id))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 报警策略 ------------------------------------------
- func (c *DeviceController) CompanyNotice_List() {
- type R_JSONS struct {
- //必须的大写开头
- Data []Company.CompanyNotice_R
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_name := c.GetString("T_name")
- r_jsons.Data, r_jsons.Num = Company.Read_CompanyNotice_List(c.T_pid, T_name, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyNotice_All() {
- type R_JSONS struct {
- //必须的大写开头
- Data []Company.CompanyNotice_R
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- T_name := c.GetString("T_name")
- r_jsons.Data = Company.Read_CompanyNotice_All(c.T_pid, T_name)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyNotice_Get() {
- id, _ := c.GetInt("T_id")
- R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceNotice.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Company.CompanyNoticeToCompanyNotice_R(R_DeviceNotice)}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyNotice_Add() {
- T_name := c.GetString("T_name")
- t_c := Company.CompanyNotice{
- T_pid: c.T_pid,
- T_name: T_name,
- T_Notice_mechanism: Warning.Read_WarningType_All_T_Notice_mechanism(),
- T_State: 1,
- }
- Id, err := Company.Add_CompanyNotice(t_c)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "报警策略添加", t_c)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyNotice_Edit() {
- id, _ := c.GetInt("T_id")
- T_name := c.GetString("T_name")
- T_Notice_wx := c.GetString("T_Notice_wx")
- T_Notice_wx2 := c.GetString("T_Notice_wx2")
- T_Notice_app := c.GetString("T_Notice_app")
- T_Notice_phone := c.GetString("T_Notice_phone")
- T_Notice_message := c.GetString("T_Notice_message")
- T_Notice_mailbox := c.GetString("T_Notice_mailbox")
- T_Notice_mechanism := c.GetString("T_Notice_mechanism")
- R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceNotice.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- R_DeviceNotice.Id = id
- if len(T_name) > 0 {
- R_DeviceNotice.T_name = T_name
- }
- if len(T_Notice_wx) > 0 {
- if T_Notice_wx == "-" {
- T_Notice_wx = ""
- }
- R_DeviceNotice.T_Notice_wx = T_Notice_wx
- }
- if len(T_Notice_wx2) > 0 {
- if T_Notice_wx2 == "-" {
- T_Notice_wx2 = ""
- }
- R_DeviceNotice.T_Notice_wx2 = T_Notice_wx2
- }
- if len(T_Notice_app) > 0 {
- if T_Notice_app == "-" {
- T_Notice_app = ""
- }
- R_DeviceNotice.T_Notice_app = T_Notice_app
- }
- if len(T_Notice_phone) > 0 {
- if T_Notice_phone == "-" {
- T_Notice_phone = ""
- }
- R_DeviceNotice.T_Notice_phone = T_Notice_phone
- }
- if len(T_Notice_message) > 0 {
- R_DeviceNotice.T_Notice_message = T_Notice_message
- }
- if len(T_Notice_mailbox) > 0 {
- if T_Notice_mailbox == "-" {
- T_Notice_mailbox = ""
- }
- R_DeviceNotice.T_Notice_mailbox = T_Notice_mailbox
- }
- if len(T_Notice_mechanism) > 0 {
- if T_Notice_mechanism == "-" {
- T_Notice_mechanism = ""
- }
- R_DeviceNotice.T_Notice_mechanism = T_Notice_mechanism
- }
- if err = Company.Update_CompanyNotice(R_DeviceNotice, "T_name", "T_Notice_wx", "T_Notice_wx2", "T_Notice_app", "T_Notice_phone", "T_Notice_message", "T_Notice_mailbox", "T_Notice_mechanism"); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "报警策略修改", R_DeviceNotice)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyNotice_Reset_Mechanism() {
- id, _ := c.GetInt("T_id")
- R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceNotice.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- R_DeviceNotice.T_Notice_mechanism = Warning.Read_WarningType_All_T_Notice_mechanism()
- if err = Company.Update_CompanyNotice(R_DeviceNotice, "T_Notice_mechanism"); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "报警策略", R_DeviceNotice)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) CompanyNotice_Del() {
- id, _ := c.GetInt("T_id")
- R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceNotice.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- if is := Company.Delete_CompanyNotice_ById(id); !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败"}
- c.ServeJSON()
- return
- }
- DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_Notice_Id(id)
- for _, v := range DeviceSensor_class_list {
- Device.DeviceSensor_Bind_T_Notice_Del(v.T_sn, v.T_id, id)
- }
- System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警策略删除", strconv.Itoa(id))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 设备通知绑定
- func (c *DeviceController) DeviceNoticeBind_List() {
- type R_JSONS struct {
- //必须的大写开头
- DeviceSensor_lite []Device.DeviceSensor_
- Num int
- Page int
- Page_size int
- Pages []lib.Page_T
- }
- var r_jsons R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_name := c.GetString("T_name")
- T_sn := c.GetString("T_sn")
- T_notice_id, _ := c.GetInt("T_notice_id")
- T_type, _ := c.GetInt("T_type")
- R_DeviceNotice, err := Company.Read_CompanyNotice_ById(T_notice_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceNotice.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- var cnt int64
- Device_lite, cnt := Device.Read_DeviceSensor_List_T_Notice(c.T_pid, T_notice_id, page, page_z, T_sn, T_name, T_type)
- r_jsons.DeviceSensor_lite = Device_lite
- page_size := math.Ceil(float64(cnt) / float64(page_z))
- r_jsons.Page = page
- r_jsons.Page_size = int(page_size)
- r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
- r_jsons.Num = int(cnt)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) NoticeBind_Add() {
- T_notice_id, _ := c.GetInt("T_notice_id")
- T_sn := c.GetString("T_sn")
- T_id, _ := c.GetInt("T_id")
- if T_notice_id == 0 || len(T_sn) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- R_CompanyNotice, err := Company.Read_CompanyNotice_ById(T_notice_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if R_CompanyNotice.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- DeviceSensor_Notice_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn, T_id, T_notice_id)
- if len(DeviceSensor_Notice_list) > 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复绑定!"}
- c.ServeJSON()
- return
- }
- if err = Company.Add_T_Notice_bind(T_sn, T_id, T_notice_id); err != nil {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知绑定失败"}
- c.ServeJSON()
- return
- }
- if err = Device.DeviceSensor_Bind_T_Notice_Add(T_sn, T_id, T_notice_id); err != nil {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知绑定失败"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警策略绑定添加", strconv.Itoa(T_notice_id)+"+>"+T_sn+"-"+strconv.Itoa(T_id))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *DeviceController) NoticeBind_Del() {
- T_notice_id, _ := c.GetInt("T_notice_id")
- T_sn := c.GetString("T_sn")
- T_id, _ := c.GetInt("T_id")
- if T_notice_id == 0 || len(T_sn) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- R_DeviceClass, err := Company.Read_CompanyNotice_ById(T_notice_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
- c.ServeJSON()
- return
- }
- if R_DeviceClass.T_pid != c.T_pid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
- c.ServeJSON()
- return
- }
- DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn, T_id, T_notice_id)
- if len(DeviceSensor_class_list) == 0 {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- if err = Company.Delete_T_Notice_bind(T_sn, T_id, T_notice_id); err != nil {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知删除绑定失败"}
- c.ServeJSON()
- return
- }
- if err = Device.DeviceSensor_Bind_T_Notice_Del(T_sn, T_id, T_notice_id); err != nil {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知删除绑定失败"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "通知删除绑定删除", strconv.Itoa(T_notice_id)+"->"+T_sn+"-"+strconv.Itoa(T_id))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 传感器类型
- func (c *DeviceController) DeviceSensorType_List_All() {
- type R_JSONS struct {
- //必须的大写开头
- Data []Device.DeviceSensorType
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- r_jsons.Data = Device.Read_DeviceSensorType_List_All()
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 传感器类型 - 小程序
- func (c *DeviceController) DeviceSensorType_List() {
- var r_jsons lib.R_JSONS
- bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- var T_pids []int
- if c.Admin_r.T_pid > 0 {
- // 查询公司
- Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
- c.ServeJSON()
- return
- }
- // 查询公司下面所有子公司id
- T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
- }
- list, err := Device.Read_DeviceSensor_T_type(c.Admin_r, c.T_pid, bindSN, T_pids)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- if len(list) == 0 {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- var ids []int
- for _, v := range list {
- id, _ := strconv.Atoi(v.(string))
- ids = append(ids, id)
- }
- r_jsons.Data = Device.Read_DeviceSensorType_List_By_Ids(ids)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 设备统计 - 小程序
- func (c *DeviceController) Device_Applet_Stat() {
- type Online struct {
- T_type int
- Count int64
- Online int64 // 监控
- NoNetwork int64 // 无网络
- NotEnabled int64 // 未启用
- }
- type R_JSONS struct {
- //必须的大写开头
- Data Online
- }
- T_type, T_type_err := c.GetInt("T_type")
- if T_type_err != nil || T_type == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_type Err!"}
- c.ServeJSON()
- return
- }
- bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- var T_pids []int
- if c.Admin_r.T_pid > 0 {
- // 查询公司
- Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
- c.ServeJSON()
- return
- }
- // 查询公司下面所有子公司id
- T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
- }
- snList, err := Device.Read_DeviceSensor_T_sn_ByT_type(c.Admin_r, c.T_pid, bindSN, T_pids, T_type)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- var r_jsons R_JSONS
- r_jsons.Data.T_type = T_type
- if len(snList) == 0 {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 0 未启用 1 在线 2 离线
- r_jsons.Data.Count = Device.Read_Device_Count_ByT_online(snList, "")
- r_jsons.Data.Online = Device.Read_Device_Count_ByT_online(snList, "1")
- r_jsons.Data.NoNetwork = Device.Read_Device_Count_ByT_online(snList, "2")
- r_jsons.Data.NotEnabled = Device.Read_Device_Count_ByT_online(snList, "0")
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 传感器列表 - 小程序 View==1
- func (c *DeviceController) DeviceSensor_Applet_List_View1() {
- var r_jsons lib.R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_name := c.GetString("T_name")
- //T_online := c.GetString("T_online")
- T_type, T_type_err := c.GetInt("T_type")
- if T_type_err != nil {
- T_type = -1
- }
- bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- var T_pids []int
- if c.Admin_r.T_pid > 0 {
- // 查询公司
- Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
- c.ServeJSON()
- return
- }
- // 查询公司下面所有子公司id
- T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
- }
- Account.Read_Company_All_Maps()
- r_jsons.Data, r_jsons.Num = Device.Read_DeviceSensor_List_For_Applet(c.Admin_r, bindSN, c.T_pid, T_pids, T_name, "3", 1, T_type, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 传感器列表 - 小程序 View==2
- func (c *DeviceController) DeviceSensor_Applet_List_View2() {
- var r_jsons lib.R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_name := c.GetString("T_name")
- T_online := c.GetString("T_online")
- T_type, T_type_err := c.GetInt("T_type")
- if T_type_err != nil {
- T_type = -1
- }
- bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- var T_pids []int
- if c.Admin_r.T_pid > 0 {
- // 查询公司
- Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
- c.ServeJSON()
- return
- }
- // 查询公司下面所有子公司id
- T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
- }
- Account.Read_Company_All_Maps()
- dsList, _ := Device.Read_DeviceSensor_List_For_Applet(c.Admin_r, bindSN, c.T_pid, T_pids, T_name, T_online, 1, T_type, page, 9999)
- var deviceMap = make(map[string][]Device.DeviceSensor_Applet)
- var num int64
- type DeviceList struct {
- T_sn string
- DeviceSensorList []Device.DeviceSensor_Applet
- }
- var deviceList []DeviceList
- for _, ds := range dsList {
- _, ok := deviceMap[ds.T_sn]
- if !ok {
- num += 1
- var list []Device.DeviceSensor_Applet
- list = append(list, ds)
- deviceMap[ds.T_sn] = list
- device := DeviceList{
- T_sn: ds.T_sn,
- }
- deviceList = append(deviceList, device)
- } else {
- deviceMap[ds.T_sn] = append(deviceMap[ds.T_sn], ds)
- }
- }
- for i := 0; i < len(deviceList); i++ {
- deviceList[i].DeviceSensorList = deviceMap[deviceList[i].T_sn]
- }
- var offset int
- if page <= 1 {
- offset = 0
- } else {
- offset = (page - 1) * page_z
- }
- offset_z := offset + page_z
- if num < int64(offset_z) {
- offset_z = int(num)
- }
- if offset > offset_z {
- offset = offset_z
- }
- r_jsons.Data = deviceList[offset:offset_z]
- r_jsons.Num = num
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func Update_DeviceSensor_Parameter() {
- list := Device.Read_DeviceSensor_All()
- for _, sensor := range list {
- dsp, is := Device.Read_DeviceSensorParameter(sensor.T_sn, sensor.T_id)
- if !is {
- continue
- }
- if sensor.T_sp == dsp.Id {
- continue
- }
- sensor.T_sp = dsp.Id
- Device.Update_DeviceSensor(sensor, "T_sp")
- }
- }
- // 传感器列表 - 冷链物流运输客户端
- func (c *DeviceController) DeviceSensor_List_BySN() {
- type R_JSONS struct {
- //必须的大写开头
- Data []Device.DeviceSensor_R
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- T_sn := c.GetString("T_sn")
- if len(T_sn) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn err!"}
- c.ServeJSON()
- return
- }
- r_jsons.Data, r_jsons.Num = Device.Read_DeviceSensorList_BySN(T_sn)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
|