Device.go 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Account"
  7. "Cold_Api/models/Company"
  8. "Cold_Api/models/Device"
  9. "Cold_Api/models/System"
  10. "Cold_Api/models/Warning"
  11. beego "github.com/beego/beego/v2/server/web"
  12. "github.com/shopspring/decimal"
  13. "math"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // Handle
  19. type DeviceController struct {
  20. beego.Controller
  21. Admin_r *Account.Admin // 登陆的用户
  22. T_pid int // 公司id
  23. }
  24. func (c *DeviceController) Prepare() {
  25. GetCookie := c.Ctx.GetCookie("User_tokey")
  26. GetString := c.GetString("User_tokey")
  27. User_tokey := GetCookie
  28. if len(User_tokey) == 0 {
  29. User_tokey = GetString
  30. }
  31. if Account.Admin_r == nil {
  32. return
  33. }
  34. c.Admin_r = Account.Admin_r
  35. T_pid := c.Admin_r.T_pid
  36. EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
  37. if EntryPid > 0 {
  38. T_pid = EntryPid
  39. }
  40. c.T_pid = T_pid
  41. }
  42. // 设备 ------------------------------------------
  43. func (c *DeviceController) Device_List() {
  44. type R_JSONS struct {
  45. //必须的大写开头
  46. Device_lite []Device.Device_R
  47. Num int64
  48. Page int
  49. Page_size int
  50. }
  51. var r_jsons R_JSONS
  52. page, _ := c.GetInt("page")
  53. if page < 1 {
  54. page = 1
  55. }
  56. page_z, _ := c.GetInt("page_z")
  57. if page_z < 1 {
  58. page_z = conf.Page_size
  59. }
  60. Name := c.GetString("T_name")
  61. T_abandon, _ := c.GetInt("T_abandon")
  62. T_online := c.GetString("T_online")
  63. T_monitor := c.GetString("T_monitor")
  64. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  65. if err != nil {
  66. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  67. c.ServeJSON()
  68. return
  69. }
  70. 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)
  71. r_jsons.Page = page
  72. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  73. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  74. c.ServeJSON()
  75. return
  76. }
  77. func (c *DeviceController) Device_Get() {
  78. var r_jsons lib.R_JSONS
  79. T_sn := c.GetString("T_sn")
  80. Device_r, err := Device.Read_Device_ByT_sn(T_sn)
  81. if err != nil {
  82. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  83. c.ServeJSON()
  84. return
  85. }
  86. if Device_r.T_pid != c.T_pid {
  87. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"}
  88. c.ServeJSON()
  89. return
  90. }
  91. r_jsons.Data = Device.DeviceToDevice_R(Device_r)
  92. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  93. c.ServeJSON()
  94. return
  95. }
  96. func (c *DeviceController) Device_Add() {
  97. var err error
  98. T_sn := c.GetString("T_sn")
  99. var_ := Device.Device{
  100. T_pid: c.T_pid,
  101. T_sn: T_sn,
  102. T_protocol: 3,
  103. T_monitor: 0, // 监控状态 已监控
  104. T_State: 1, // 状态 正常
  105. T_ist: 1, // 温度 1开启
  106. T_ish: 1, // 湿度 1开启
  107. }
  108. if len(T_sn) < 8 {
  109. c.Data["json"] = lib.JSONS{Code: 202, Msg: "SN 太短 必须8个字符以上"}
  110. c.ServeJSON()
  111. return
  112. }
  113. _, err = Device.Read_Device_ByT_sn(T_sn)
  114. if err == nil {
  115. c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复SN!"}
  116. c.ServeJSON()
  117. return
  118. }
  119. var_.T_devName = T_sn
  120. // 创建数据库
  121. if !Device.CREATE_DeviceData(T_sn) {
  122. c.Data["json"] = lib.JSONS{Code: 302, Msg: "添加失败!"}
  123. c.ServeJSON()
  124. return
  125. }
  126. err = Device.Add_Device(var_)
  127. if err != nil {
  128. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  129. c.ServeJSON()
  130. return
  131. }
  132. // 添加设备后自动重启
  133. MqttIds := strings.Split(conf.MqttIds, ",")
  134. for _, id := range MqttIds {
  135. NatsServer.Set_RestartShutdown(Device.Device_task{
  136. T_sn: T_sn,
  137. T_task: "1",
  138. }, id)
  139. }
  140. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "设备添加", var_)
  141. NatsServer.Read_DeviceParameter(T_sn, "") // 获取主机信息
  142. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  143. c.ServeJSON()
  144. return
  145. }
  146. func (c *DeviceController) Device_Edit() {
  147. T_SN := c.GetString("T_sn")
  148. Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  149. if err != nil {
  150. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  151. c.ServeJSON()
  152. return
  153. }
  154. clos := make([]string, 0)
  155. // 监控状态 0 未监控 1 监控
  156. T_monitor, T_monitor_err := c.GetInt("T_monitor")
  157. if T_monitor_err == nil {
  158. Device_r.T_monitor = T_monitor
  159. clos = append(clos, "T_monitor")
  160. }
  161. // 温度显示
  162. T_ist, T_ist_err := c.GetInt("T_ist")
  163. if T_ist_err == nil {
  164. Device_r.T_ist = T_ist
  165. clos = append(clos, "T_ist")
  166. }
  167. // 适度显示
  168. T_ish, T_ish_err := c.GetInt("T_ish")
  169. if T_ish_err == nil {
  170. Device_r.T_ish = T_ish
  171. clos = append(clos, "T_ish")
  172. }
  173. // 是否弃用
  174. T_State, T_State_err := c.GetInt("T_State")
  175. if T_State_err == nil {
  176. Device_r.T_State = T_State
  177. if T_State == 0 {
  178. Device_r.T_abandonTime = time.Now()
  179. clos = append(clos, "T_abandonTime")
  180. }
  181. clos = append(clos, "T_State")
  182. }
  183. // 验证时间
  184. T_VerifyTime := c.GetString("T_VerifyTime")
  185. VerifyTime, VerifyTime_is := lib.TimeStrToTime(T_VerifyTime)
  186. if VerifyTime_is {
  187. Device_r.T_VerifyTime = VerifyTime
  188. clos = append(clos, "T_VerifyTime")
  189. }
  190. // 校准时间
  191. T_CalibrationTime := c.GetString("T_CalibrationTime")
  192. CalibrationTime, CalibrationTime_is := lib.TimeStrToTime(T_CalibrationTime)
  193. if CalibrationTime_is {
  194. Device_r.T_CalibrationTime = CalibrationTime
  195. clos = append(clos, "T_CalibrationTime")
  196. // 同步冷链验证校准到期时间
  197. NatsServer.Update_Task_BySN(T_SN, CalibrationTime)
  198. }
  199. // 巡检时间
  200. T_PatrolTime := c.GetString("T_PatrolTime")
  201. PatrolTime, PatrolTime_is := lib.TimeStrToTime(T_PatrolTime)
  202. if PatrolTime_is {
  203. Device_r.T_PatrolTime = PatrolTime
  204. clos = append(clos, "T_PatrolTime")
  205. }
  206. if is := Device.Update_Device(Device_r, clos...); !is {
  207. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  208. c.ServeJSON()
  209. return
  210. }
  211. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "修改", Device_r)
  212. if is := Device.Update_Device_To_DeviceSensor(Device_r); is {
  213. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器修改", Device_r)
  214. }
  215. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  216. c.ServeJSON()
  217. return
  218. }
  219. func (c *DeviceController) Device_Applet_Get() {
  220. T_snid := c.GetString("T_snid")
  221. sn_id := strings.Split(strings.Trim(T_snid, "|"), ",")
  222. if len(sn_id) != 2 {
  223. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid err!"}
  224. c.ServeJSON()
  225. return
  226. }
  227. T_sn, T_id := sn_id[0], lib.To_int(sn_id[1])
  228. Device_r, err := Device.Read_Device_ByT_sn(T_sn)
  229. if err != nil {
  230. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  231. c.ServeJSON()
  232. return
  233. }
  234. if Device_r.T_pid != c.T_pid {
  235. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"}
  236. c.ServeJSON()
  237. return
  238. }
  239. Dsp, _ := Device.Read_DeviceSensorParameter(T_sn, T_id)
  240. DeviceData := Device.Read_DeviceData(T_sn, T_id)
  241. type JSONS struct {
  242. T_name string // 设备名称
  243. T_monitor int // 监控状态 0 未监控 1 监控 停止记录
  244. T_ist int // 温度 1开启 2关闭
  245. T_ish int // 湿度 1开启 2关闭
  246. T_t float32 // 温度
  247. T_rh float32 // 湿度
  248. T_Tlower float32 // 温度下限
  249. T_Tupper float32 // 温度上限
  250. T_RHlower float32 // 湿度下限
  251. T_RHupper float32 // 湿度上限
  252. }
  253. r_jsons := JSONS{
  254. T_name: Device_r.T_devName,
  255. T_monitor: Device_r.T_monitor,
  256. T_ist: Device_r.T_ist,
  257. T_ish: Device_r.T_ish,
  258. T_t: DeviceData.T_t,
  259. T_rh: DeviceData.T_rh,
  260. T_Tlower: Dsp.T_Tlower,
  261. T_Tupper: Dsp.T_Tupper,
  262. T_RHlower: Dsp.T_RHlower,
  263. T_RHupper: Dsp.T_RHupper,
  264. }
  265. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  266. c.ServeJSON()
  267. return
  268. }
  269. // 重启/关机
  270. func (c *DeviceController) Device_RestartShutdown() {
  271. T_sn := c.GetString("T_sn")
  272. T_task := c.GetString("T_task")
  273. device, err := Device.Read_Device_ByT_sn(T_sn)
  274. if err != nil {
  275. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  276. c.ServeJSON()
  277. return
  278. }
  279. NatsServer.Set_RestartShutdown(Device.Device_task{
  280. T_sn: T_sn,
  281. T_task: T_task,
  282. }, device.T_mqttid)
  283. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "重启/关机", T_sn+"-"+T_task)
  284. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  285. c.ServeJSON()
  286. return
  287. }
  288. // 版本升级
  289. func (c *DeviceController) Device_ProductUpgrade() {
  290. T_sn := c.GetString("T_sn")
  291. T_version := c.GetString("T_version")
  292. T_file := c.GetString("T_file")
  293. device, err := Device.Read_Device_ByT_sn(T_sn)
  294. if err != nil {
  295. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  296. c.ServeJSON()
  297. return
  298. }
  299. NatsServer.Up_ProductUpgrade(T_sn, T_version, T_file, device.T_mqttid)
  300. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "版本升级", T_sn+"-"+T_version+"-"+T_file)
  301. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  302. c.ServeJSON()
  303. return
  304. }
  305. // 2023-02-02 弃用
  306. //func (c *DeviceController) Device_Del() {
  307. // if admin_r.Id != 1 {
  308. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"}
  309. // c.ServeJSON()
  310. // return
  311. // }
  312. //
  313. // T_sn := c.GetString("T_sn")
  314. //
  315. // Device_r, err := Device.Read_Device_ByT_sn(T_sn)
  316. // if err != nil {
  317. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  318. // c.ServeJSON()
  319. // return
  320. // }
  321. //
  322. // Device.Delete_Device(Device_r)
  323. // DeviceSensor_list, _ := Device.Read_DeviceSensor_ByTsn(Device_r.T_sn)
  324. // for _, v := range DeviceSensor_list {
  325. // Device.Delete_DeviceSensor_ById(Device_r.T_sn, v.T_id)
  326. // }
  327. // Device.DELETE_DeviceSensor(Device_r.T_sn)
  328. // Device.DELETE_DeviceDatar(Device_r.T_sn)
  329. // Device.DELETE_DeviceParameter(Device_r.T_sn)
  330. // Device.DELETE_DeviceSensorParameter(Device_r.T_sn)
  331. //
  332. // System.Add_UserLogs(admin_r.T_uuid, "设备管理", "设备删除", T_sn)
  333. // c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  334. // c.ServeJSON()
  335. // return
  336. //
  337. //}
  338. // 设备日志
  339. func (c *DeviceController) DeviceLogs() {
  340. type R_JSONS struct {
  341. //必须的大写开头
  342. DeviceLogs []Warning.DeviceLogs
  343. Num int64
  344. Page int
  345. Page_size int
  346. }
  347. var r_jsons R_JSONS
  348. page, _ := c.GetInt("page")
  349. if page < 1 {
  350. page = 1
  351. }
  352. page_z, _ := c.GetInt("page_z")
  353. if page_z < 1 {
  354. page_z = conf.Page_size
  355. }
  356. T_sn := c.GetString("T_sn")
  357. c.Data["T_sn"] = T_sn
  358. r_jsons.DeviceLogs, r_jsons.Num = Warning.Read_DeviceLogs_List(T_sn, page, page_z)
  359. r_jsons.Page = page
  360. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  361. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  362. c.ServeJSON()
  363. }
  364. // 设备参数 ------------------------------------------
  365. // 设备参数列表
  366. func (c *DeviceController) Device_Parameter_List() {
  367. Sn := c.GetString("T_sn")
  368. page, _ := c.GetInt("page")
  369. if page < 1 {
  370. page = 1
  371. }
  372. page_z, _ := c.GetInt("page_z")
  373. if page_z < 1 {
  374. page_z = conf.Page_size
  375. }
  376. DeviceParameter_lite := Device.Read_DeviceParameter_SN_List(Sn, page, page_z)
  377. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceParameter_lite}
  378. c.ServeJSON()
  379. return
  380. }
  381. // 获取设备参数
  382. func (c *DeviceController) Device_Parameter_Get() {
  383. Sn := c.GetString("T_sn")
  384. device, err := Device.Read_Device_ByT_sn(Sn)
  385. if err != nil {
  386. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  387. c.ServeJSON()
  388. return
  389. }
  390. DeviceParameter_lite := Device.Read_DeviceParameter_SN(Sn)
  391. if len(DeviceParameter_lite) == 0 {
  392. NatsServer.Read_DeviceParameter(Sn, device.T_mqttid)
  393. c.Data["json"] = lib.JSONS{Code: 202, Msg: "未同步参数,请检查设备是否正常!"}
  394. c.ServeJSON()
  395. return
  396. }
  397. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceParameter_lite}
  398. c.ServeJSON()
  399. return
  400. }
  401. // 修改设备参数
  402. func (c *DeviceController) Device_Parameter_Pu() {
  403. //T_SN := c.GetString("T_sn")
  404. //T_name := c.GetString("T_name")
  405. //
  406. //T_saveT, _ := c.GetInt("T_saveT")
  407. //T_overA, _ := c.GetInt("T_overA")
  408. //T_outA, _ := c.GetInt("T_outA")
  409. //T_lostA, _ := c.GetInt("T_lostA")
  410. //T_bat, _ := c.GetInt("T_bat")
  411. //
  412. //T_warn, _ := c.GetInt("T_warn")
  413. //T_warnD, _ := c.GetInt("T_warnD")
  414. //
  415. //T_btname := c.GetString("T_btname")
  416. //T_btsid, _ := c.GetInt("T_btsid")
  417. //T_btchar, _ := c.GetInt("T_btchar")
  418. //
  419. //T_Msid, _ := c.GetInt("T_Msid")
  420. //T_State, _ := c.GetInt("T_State")
  421. //
  422. //Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  423. //if err != nil {
  424. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  425. // c.ServeJSON()
  426. // return
  427. //}
  428. //
  429. //Deviceparameter := Device.DeviceParameter{
  430. // T_sn: T_SN,
  431. // T_name: T_name,
  432. // T_saveT: T_saveT,
  433. // T_overA: T_overA,
  434. // T_outA: T_outA,
  435. // T_lostA: T_lostA,
  436. // T_bat: T_bat,
  437. // T_warn: T_warn,
  438. // T_warnD: T_warnD,
  439. // T_btname: T_btname,
  440. // T_btsid: T_btsid,
  441. // T_btchar: T_btchar,
  442. // T_Msid: int64(T_Msid),
  443. // T_uuid: c.Admin_r.T_uuid,
  444. // T_SendState: 0,
  445. // T_State: T_State,
  446. //}
  447. //Deviceparameter, is := Device.Add_DeviceParameter(Deviceparameter)
  448. //if !is {
  449. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "Add_DeviceParameter ERR"}
  450. // c.ServeJSON()
  451. // return
  452. //}
  453. //System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "设备参数操作", Deviceparameter)
  454. //NatsServer.Pu_DeviceParameter(Deviceparameter)
  455. //
  456. //// 更新名称
  457. //Device_r.T_devName = Deviceparameter.T_name
  458. //Device.Update_Device(Device_r, "T_devName")
  459. //System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "修改", Device_r)
  460. T_SN := c.GetString("T_sn")
  461. Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  462. if err != nil {
  463. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  464. c.ServeJSON()
  465. return
  466. }
  467. DeviceParameter_lite := Device.Read_DeviceParameter_SN(T_SN)
  468. if len(DeviceParameter_lite) == 0 {
  469. c.Data["json"] = lib.JSONS{Code: 202, Msg: "未同步参数,请检查设备是否正常!"}
  470. c.ServeJSON()
  471. return
  472. }
  473. DeviceParameter := DeviceParameter_lite[0]
  474. if v, err := c.GetInt("T_saveT"); err == nil {
  475. DeviceParameter.T_saveT = v
  476. }
  477. if v, err := c.GetInt("T_overA"); err == nil {
  478. DeviceParameter.T_overA = v
  479. }
  480. if v, err := c.GetInt("T_outA"); err == nil {
  481. DeviceParameter.T_outA = v
  482. }
  483. if v, err := c.GetInt("T_lostA"); err == nil {
  484. DeviceParameter.T_lostA = v
  485. }
  486. if v, err := c.GetInt("T_bat"); err == nil {
  487. DeviceParameter.T_bat = v
  488. }
  489. if v, err := c.GetInt("T_warn"); err == nil {
  490. DeviceParameter.T_warn = v
  491. }
  492. if v, err := c.GetInt("T_warnD"); err == nil {
  493. DeviceParameter.T_warnD = v
  494. }
  495. if v, err := c.GetInt("T_scan"); err == nil {
  496. DeviceParameter.T_scan = v
  497. }
  498. if v, err := c.GetInt("T_dormancy"); err == nil {
  499. DeviceParameter.T_dormancy = v
  500. }
  501. if v, err := c.GetInt("T_snum"); err == nil {
  502. DeviceParameter.T_snum = v
  503. }
  504. if v := c.GetString("T_btname"); len(v) > 0 {
  505. DeviceParameter.T_btname = v
  506. }
  507. if v, err := c.GetInt("T_btsid"); err == nil {
  508. DeviceParameter.T_btsid = v
  509. }
  510. if v, err := c.GetInt("T_btchar"); err == nil {
  511. DeviceParameter.T_btchar = v
  512. }
  513. if v, err := c.GetInt("T_Msid"); err == nil {
  514. DeviceParameter.T_Msid = int64(v)
  515. }
  516. if v := c.GetString("T_name"); len(v) > 0 {
  517. DeviceParameter.T_name = v
  518. }
  519. DeviceParameter.Id = 0
  520. DeviceParameter.T_State = 2
  521. DeviceParameter.T_uuid = c.Admin_r.T_uuid
  522. DeviceParameter.T_SendState = 0
  523. DeviceParameter.CreateTime = time.Now()
  524. DeviceParameter.UpdateTime = time.Now()
  525. Deviceparameter, is := Device.Add_DeviceParameter(DeviceParameter)
  526. if !is {
  527. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Add_DeviceParameter ERR"}
  528. c.ServeJSON()
  529. return
  530. }
  531. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "设备参数操作", Deviceparameter)
  532. NatsServer.Pu_DeviceParameter(Deviceparameter, Device_r.T_mqttid)
  533. if v := c.GetString("T_name"); len(v) > 0 {
  534. // 更新名称
  535. Device_r.T_devName = Deviceparameter.T_name
  536. Device.Update_Device(Device_r, "T_devName")
  537. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "修改设备名称", Device_r)
  538. }
  539. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  540. c.ServeJSON()
  541. return
  542. }
  543. // 设备参数同步
  544. func (c *DeviceController) Device_Parameter_Sync() {
  545. T_SN := c.GetString("T_sn")
  546. Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  547. if err != nil {
  548. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  549. c.ServeJSON()
  550. return
  551. }
  552. go func() {
  553. NatsServer.Read_DeviceParameter(T_SN, Device_r.T_mqttid)
  554. time.Sleep(5 * time.Second)
  555. NatsServer.Read_DeviceSensorParameter(T_SN, Device_r.T_mqttid)
  556. }()
  557. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  558. c.ServeJSON()
  559. return
  560. }
  561. // 删除设备 传感器
  562. // 2023-02-02 弃用
  563. func (c *DeviceController) Device_Parameter_Del_Device() {
  564. Sn := c.GetString("Sn")
  565. Id, _ := c.GetInt("Id")
  566. device, err := Device.Read_Device_ByT_sn(Sn)
  567. if err != nil {
  568. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  569. c.ServeJSON()
  570. return
  571. }
  572. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
  573. //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
  574. Device.Delete_DeviceSensor_ById(Sn, Id)
  575. NatsServer.Del_DeviceSensor(Device.DeviceSensor_Del{
  576. T_sn: Sn, T_id: Id,
  577. }, device.T_mqttid)
  578. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  579. c.ServeJSON()
  580. return
  581. }
  582. // 设备-传感器 ------------------------------------------
  583. // 传感器列表
  584. func (c *DeviceController) DeviceSensor_List() {
  585. type R_JSONS struct {
  586. //必须的大写开头
  587. Data []Device.DeviceSensor_R
  588. Num int64
  589. Page int
  590. Page_size int
  591. }
  592. var r_jsons R_JSONS
  593. page, _ := c.GetInt("page")
  594. if page < 1 {
  595. page = 1
  596. }
  597. page_z, _ := c.GetInt("page_z")
  598. if page_z < 1 {
  599. page_z = conf.Page_size
  600. }
  601. T_sn := c.GetString("T_sn")
  602. T_name := c.GetString("T_name") // 包含 T_name、T_sn
  603. T_class_id, _ := c.GetInt("T_calssid")
  604. T_type := c.GetString("T_type")
  605. T_State, T_State_err := c.GetInt("T_state")
  606. if T_State_err != nil {
  607. T_State = -1
  608. }
  609. T_datashow, T_datashow_err := c.GetInt("T_datashow") //0 屏蔽数据展示 1 正常数据展示
  610. if T_datashow_err != nil {
  611. T_datashow = -1
  612. }
  613. if T_class_id > 0 {
  614. R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
  615. if err != nil {
  616. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
  617. c.ServeJSON()
  618. return
  619. }
  620. if R_DeviceClass.T_pid != c.T_pid {
  621. c.Data["json"] = lib.JSONS{Code: 202, Msg: "R_DeviceClass.T_pid != T_pid Err!"}
  622. c.ServeJSON()
  623. return
  624. }
  625. }
  626. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  627. if err != nil {
  628. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  629. c.ServeJSON()
  630. return
  631. }
  632. 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)
  633. r_jsons.Page = page
  634. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  635. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  636. c.ServeJSON()
  637. return
  638. }
  639. // 修改传感器
  640. func (c *DeviceController) DeviceSensor_Edit() {
  641. Sn := c.GetString("T_sn")
  642. Id, _ := c.GetInt("T_id")
  643. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  644. if !is {
  645. c.Data["json"] = lib.JSONS{Code: 202, Msg: "信息错误!"}
  646. c.ServeJSON()
  647. return
  648. }
  649. // 数据视图3D视图订阅
  650. if v, err := c.GetInt("T_type"); err == nil {
  651. if v == 0 {
  652. c.Data["json"] = lib.JSONS{Code: 202, Msg: "类型不能为默认!"}
  653. c.ServeJSON()
  654. return
  655. }
  656. DeviceSensor.T_type = v
  657. Device.Update_DeviceSensor(DeviceSensor, "T_type")
  658. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 类型", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(v))
  659. }
  660. // 实时数据显示排序
  661. if v, err := c.GetInt("T_sort"); err == nil {
  662. DeviceSensor.T_sort = v
  663. Device.Update_DeviceSensor(DeviceSensor, "T_sort")
  664. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 排序操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(v))
  665. }
  666. // 屏蔽数据展示
  667. if v, err := c.GetInt("T_datashow"); err == nil {
  668. DeviceSensor.T_datashow = v
  669. Device.Update_DeviceSensor(DeviceSensor, "T_datashow")
  670. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 数据展示", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(v))
  671. }
  672. // 数据视图3D视图订阅
  673. if v := c.GetString("T_3dview"); len(v) > 0 {
  674. DeviceSensor.T_3dview = v
  675. Device.Update_DeviceSensor(DeviceSensor, "T_3dview")
  676. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 3D视图订阅", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+v)
  677. }
  678. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  679. c.ServeJSON()
  680. return
  681. }
  682. // 删除传感器
  683. func (c *DeviceController) DeviceSensor_Del() {
  684. Sn := c.GetString("T_sn")
  685. Id, _ := c.GetInt("T_id")
  686. device, err := Device.Read_Device_ByT_sn(Sn)
  687. if err != nil {
  688. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  689. c.ServeJSON()
  690. return
  691. }
  692. _, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  693. if !is {
  694. c.Data["json"] = lib.JSONS{Code: 202, Msg: "信息错误!"}
  695. c.ServeJSON()
  696. return
  697. }
  698. // 0默认:从数据库删除 1:从设备删除
  699. T_mode, _ := c.GetInt("T_mode")
  700. if T_mode == 1 {
  701. Device.Delete_DeviceSensor_ById(Sn, Id)
  702. NatsServer.Del_DeviceSensor(Device.DeviceSensor_Del{
  703. T_sn: Sn, T_id: Id,
  704. }, device.T_mqttid)
  705. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
  706. } else {
  707. Device.Delete_DeviceSensor_ById(Sn, Id)
  708. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
  709. }
  710. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  711. c.ServeJSON()
  712. return
  713. }
  714. // 传感器参数
  715. func (c *DeviceController) DeviceSensor_Parameter_Get() {
  716. Sn := c.GetString("T_sn")
  717. Id, _ := c.GetInt("T_id")
  718. device, err := Device.Read_Device_ByT_sn(Sn)
  719. if err != nil {
  720. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  721. c.ServeJSON()
  722. return
  723. }
  724. //c.Data["DeviceSensor"], _ = Device.Read_DeviceSensor_ByTsn_Tid(Sn, Id)
  725. DeviceSensorParameter_lite := Device.Read_DeviceSensorParameter_T_sn_T_id(Sn, Id)
  726. if len(DeviceSensorParameter_lite) == 0 {
  727. NatsServer.Read_DeviceSensorParameter(Sn, device.T_mqttid)
  728. c.Data["json"] = lib.JSONS{Code: 202, Msg: "未同步参数,请检查设备是否正常!"}
  729. c.ServeJSON()
  730. return
  731. }
  732. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceSensorParameter_lite}
  733. c.ServeJSON()
  734. return
  735. }
  736. // 传感器参数列表
  737. func (c *DeviceController) DeviceSensor_Parameter_List() {
  738. Sn := c.GetString("T_sn")
  739. Id, _ := c.GetInt("T_id")
  740. page, _ := c.GetInt("page")
  741. if page < 1 {
  742. page = 1
  743. }
  744. page_z, _ := c.GetInt("page_z")
  745. if page_z < 1 {
  746. page_z = conf.Page_size
  747. }
  748. DeviceSensorParameter_lite := Device.Read_DeviceSensorParameter_List(Sn, Id, 2, page, page_z)
  749. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceSensorParameter_lite}
  750. c.ServeJSON()
  751. return
  752. }
  753. // 修改传感器参数
  754. func (c *DeviceController) DeviceSensor_Parameter_Pu() {
  755. T_SN := c.GetString("T_sn")
  756. T_id, _ := c.GetInt("T_id")
  757. device, err := Device.Read_Device_ByT_sn(T_SN)
  758. if err != nil {
  759. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  760. c.ServeJSON()
  761. return
  762. }
  763. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(T_SN, T_id)
  764. if !is {
  765. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id Err!"}
  766. c.ServeJSON()
  767. return
  768. }
  769. DeviceSensorParameter, is := Device.Read_DeviceSensorParameter(T_SN, T_id)
  770. if !is {
  771. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id Err!"}
  772. c.ServeJSON()
  773. return
  774. }
  775. T_name := DeviceSensor.T_name
  776. if v := c.GetString("T_name"); len(v) > 0 {
  777. T_name = v
  778. }
  779. T_Tlower := DeviceSensorParameter.T_Tlower
  780. if v, T_Tlower_err := c.GetFloat("T_Tlower"); T_Tlower_err == nil {
  781. temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
  782. T_Tlower = float32(temp)
  783. }
  784. T_Tupper := DeviceSensorParameter.T_Tupper
  785. if v, T_Tupper_err := c.GetFloat("T_Tupper"); T_Tupper_err == nil {
  786. temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
  787. T_Tupper = float32(temp)
  788. }
  789. T_RHlower := DeviceSensorParameter.T_RHlower
  790. if v, T_RHlower_err := c.GetFloat("T_RHlower"); T_RHlower_err == nil {
  791. temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
  792. T_RHlower = float32(temp)
  793. }
  794. T_RHupper := DeviceSensorParameter.T_RHupper
  795. if v, T_RHupper_err := c.GetFloat("T_RHupper"); T_RHupper_err == nil {
  796. temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
  797. T_RHupper = float32(temp)
  798. }
  799. T_enprel := DeviceSensorParameter.T_enprel
  800. if v, T_enprel_err := c.GetInt("T_enprel"); T_enprel_err == nil {
  801. T_enprel = v
  802. }
  803. T_tprel := DeviceSensorParameter.T_tprel
  804. if v, T_tprel_err := c.GetFloat("T_tprel"); T_tprel_err == nil {
  805. temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
  806. T_tprel = float32(temp)
  807. }
  808. T_tpreu := DeviceSensorParameter.T_tpreu
  809. if v, T_tpreu_err := c.GetFloat("T_tpreu"); T_tpreu_err == nil {
  810. temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
  811. T_tpreu = float32(temp)
  812. }
  813. T_hprel := DeviceSensorParameter.T_hprel
  814. if v, T_hprel_err := c.GetFloat("T_hprel"); T_hprel_err == nil {
  815. temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
  816. T_hprel = float32(temp)
  817. }
  818. T_hpreu := DeviceSensorParameter.T_hpreu
  819. if v, T_hpreu_err := c.GetFloat("T_hpreu"); T_hpreu_err == nil {
  820. temp, _ := decimal.NewFromFloat(v).Round(2).Float64()
  821. T_hpreu = float32(temp)
  822. }
  823. T_enprelnote := DeviceSensorParameter.T_enprelnote
  824. if v, T_enprel_err := c.GetInt("T_enprelnote"); T_enprel_err == nil {
  825. T_enprelnote = v
  826. }
  827. //T_speed := DeviceSensorParameter.T_speed
  828. //if v, T_speed_err := c.GetInt("T_speed"); T_speed_err == nil {
  829. // T_speed = v
  830. //}
  831. //T_sense := DeviceSensorParameter.T_sense
  832. //if v, T_sense_err := c.GetInt("T_sense"); T_sense_err == nil {
  833. // T_sense = v
  834. //}
  835. T_en := DeviceSensorParameter.T_en
  836. if v, T_en_err := c.GetInt("T_en"); T_en_err == nil {
  837. T_en = v
  838. }
  839. T_free := DeviceSensorParameter.T_free
  840. if v, T_free_err := c.GetInt("T_free"); T_free_err == nil {
  841. T_free = v
  842. }
  843. dsp := Device.DeviceSensorParameter{
  844. T_sn: T_SN,
  845. T_id: T_id,
  846. T_name: T_name,
  847. T_Tlower: T_Tlower,
  848. T_Tupper: T_Tupper,
  849. T_RHlower: T_RHlower,
  850. T_RHupper: T_RHupper,
  851. //T_speed: T_speed,
  852. //T_sense: T_sense,
  853. T_en: T_en,
  854. T_free: T_free,
  855. T_enprel: T_enprel,
  856. T_tprel: T_tprel,
  857. T_tpreu: T_tpreu,
  858. T_hprel: T_hprel,
  859. T_hpreu: T_hpreu,
  860. T_enprelnote: T_enprelnote,
  861. T_uuid: c.Admin_r.T_uuid,
  862. T_SendState: 0,
  863. T_State: 2, // 用户提交
  864. }
  865. dsp, is = Device.Add_DeviceSensorParameter(dsp)
  866. if !is {
  867. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Add_DeviceSensorParameter Err!"}
  868. c.ServeJSON()
  869. return
  870. }
  871. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "传感器参数操作", dsp)
  872. // 更新名称
  873. if len(T_name) > 0 {
  874. DeviceSensor.T_name = T_name
  875. if is = Device.Update_DeviceSensor(DeviceSensor, "T_name"); !is {
  876. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Update_DeviceSensor Err!"}
  877. c.ServeJSON()
  878. return
  879. }
  880. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "修改传感器名称", DeviceSensor)
  881. }
  882. NatsServer.Pu_DeviceParameter_Sensor(dsp, device.T_mqttid)
  883. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  884. c.ServeJSON()
  885. return
  886. }
  887. // 传感器管理列表
  888. func (c *DeviceController) DeviceSensor_Manage_List() {
  889. type R_JSONS struct {
  890. //必须的大写开头
  891. Data []Device.DeviceSensor_P
  892. Num int64
  893. Page int
  894. Page_size int
  895. }
  896. var r_jsons R_JSONS
  897. page, _ := c.GetInt("page")
  898. if page < 1 {
  899. page = 1
  900. }
  901. page_z, _ := c.GetInt("page_z")
  902. if page_z < 1 {
  903. page_z = conf.Page_size
  904. }
  905. T_name := c.GetString("T_name") // 包含 T_name、T_sn
  906. T_calss_id, _ := c.GetInt("T_calssid")
  907. if T_calss_id > 0 {
  908. R_DeviceClass, err := Company.Read_CompanyClass_ById(T_calss_id)
  909. if err != nil {
  910. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
  911. c.ServeJSON()
  912. return
  913. }
  914. if R_DeviceClass.T_pid != c.T_pid {
  915. c.Data["json"] = lib.JSONS{Code: 202, Msg: "CompanyClass.T_pid != T_pid Err!"}
  916. c.ServeJSON()
  917. return
  918. }
  919. }
  920. T_en, T_en_err := c.GetInt("T_en") //启用停用 0 停用 1 启用 空 所有
  921. if T_en_err != nil {
  922. T_en = -1
  923. }
  924. T_free, T_free_err := c.GetInt("T_free") // 空库 0 正常 1 空库 空 所有
  925. if T_free_err != nil {
  926. T_free = -1
  927. }
  928. T_datashow, T_datashow_err := c.GetInt("T_datashow") //数据展示 空 全部 0 屏蔽数据展示 1 正常数据展示
  929. if T_datashow_err != nil {
  930. T_datashow = -1
  931. }
  932. T_sort, T_sort_err := c.GetInt("T_sort") //排序 空 升序 1 降序
  933. if T_sort_err != nil {
  934. T_sort = -1
  935. }
  936. T_abandon, _ := c.GetInt("T_abandon")
  937. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  938. if err != nil {
  939. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  940. c.ServeJSON()
  941. return
  942. }
  943. 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)
  944. r_jsons.Page = page
  945. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  946. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  947. c.ServeJSON()
  948. return
  949. }
  950. // 设备任务 ------------------------------------------
  951. // 任务列表
  952. func (c *DeviceController) DeviceTask_List() {
  953. type R_JSONS struct {
  954. //必须的大写开头
  955. Data []Device.DeviceTask
  956. Num int64
  957. Page int
  958. Page_size int
  959. }
  960. var r_jsons R_JSONS
  961. page, _ := c.GetInt("page")
  962. if page < 1 {
  963. page = 1
  964. }
  965. page_z, _ := c.GetInt("page_z")
  966. if page_z < 1 {
  967. page_z = conf.Page_size
  968. }
  969. T_sn := c.GetString("T_sn")
  970. Time_start := c.GetString("Time_start")
  971. Time_end := c.GetString("Time_end")
  972. if len(T_sn) < 6 {
  973. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn!!!"}
  974. c.ServeJSON()
  975. return
  976. }
  977. r_jsons.Data, r_jsons.Num = Device.Read_DeviceTask_List(T_sn, Time_start, Time_end, page, page_z)
  978. r_jsons.Page = page
  979. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  980. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  981. c.ServeJSON()
  982. return
  983. }
  984. func (c *DeviceController) DeviceTask_All() {
  985. var r_jsons lib.R_JSONS
  986. T_sn := c.GetString("T_sn")
  987. if len(T_sn) < 6 {
  988. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn!!!"}
  989. c.ServeJSON()
  990. return
  991. }
  992. r_jsons.Data, r_jsons.Num = Device.Read_DeviceTask_All(T_sn)
  993. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  994. c.ServeJSON()
  995. return
  996. }
  997. // 任务启停
  998. func (c *DeviceController) DeviceTask_Post() {
  999. T_sn := c.GetString("T_sn")
  1000. T_task := c.GetString("T_task")
  1001. device, err := Device.Read_Device_ByT_sn(T_sn)
  1002. if err != nil {
  1003. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  1004. c.ServeJSON()
  1005. return
  1006. }
  1007. NatsServer.Set_DeviceTask(Device.Device_task{
  1008. T_sn: T_sn,
  1009. T_task: T_task,
  1010. }, device.T_mqttid)
  1011. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "远程启停", T_sn+"-"+T_task)
  1012. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1013. c.ServeJSON()
  1014. return
  1015. }
  1016. // 设备分类 ------------------------------------------
  1017. func (c *DeviceController) CompanyClass_List() {
  1018. type R_JSONS struct {
  1019. //必须的大写开头
  1020. Data []Company.CompanyClass_R
  1021. Num int64
  1022. Page int
  1023. Page_size int
  1024. }
  1025. var r_jsons R_JSONS
  1026. page, _ := c.GetInt("page")
  1027. if page < 1 {
  1028. page = 1
  1029. }
  1030. page_z, _ := c.GetInt("page_z")
  1031. if page_z < 1 {
  1032. page_z = conf.Page_size
  1033. }
  1034. T_name := c.GetString("T_name")
  1035. r_jsons.Data, r_jsons.Num = Company.Read_CompanyClass_List(c.T_pid, T_name, page, page_z)
  1036. r_jsons.Page = page
  1037. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1038. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1039. c.ServeJSON()
  1040. return
  1041. }
  1042. func (c *DeviceController) CompanyClass_All() {
  1043. type R_JSONS struct {
  1044. //必须的大写开头
  1045. Data []Company.CompanyClass_R
  1046. Num int64
  1047. Page int
  1048. Page_size int
  1049. }
  1050. var r_jsons R_JSONS
  1051. T_name := c.GetString("T_name")
  1052. r_jsons.Data = Company.Read_CompanyClass_All(c.T_pid, T_name)
  1053. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1054. c.ServeJSON()
  1055. return
  1056. }
  1057. // 查询公司及公司下所有子公司的公司分类
  1058. func (c *DeviceController) CompanyClass_All_For_Company() {
  1059. var r_jsons lib.R_JSONS
  1060. T_name := c.GetString("T_name")
  1061. // 查询公司
  1062. Company_r, err := Account.Read_Company_ById(c.T_pid)
  1063. if err != nil {
  1064. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1065. c.ServeJSON()
  1066. return
  1067. }
  1068. // 查询公司下面所有子公司id
  1069. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1070. Account.Read_Company_All_Maps()
  1071. r_jsons.Data = Company.Read_CompanyClass_All_For_Company(T_pids, T_name)
  1072. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1073. c.ServeJSON()
  1074. return
  1075. }
  1076. func (c *DeviceController) CompanyClass_Get() {
  1077. id, _ := c.GetInt("T_id")
  1078. R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
  1079. if err != nil {
  1080. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1081. c.ServeJSON()
  1082. return
  1083. }
  1084. if R_DeviceClass.T_pid != c.T_pid {
  1085. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1086. c.ServeJSON()
  1087. return
  1088. }
  1089. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Company.CompanyClassToCompanyClass_R(R_DeviceClass)}
  1090. c.ServeJSON()
  1091. return
  1092. }
  1093. func (c *DeviceController) CompanyClass_Add() {
  1094. T_name := c.GetString("T_name")
  1095. t_c := Company.CompanyClass{
  1096. T_pid: c.T_pid,
  1097. T_name: T_name,
  1098. T_State: 1,
  1099. }
  1100. Id, err := Company.Add_CompanyClass(t_c)
  1101. if err != nil {
  1102. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  1103. c.ServeJSON()
  1104. return
  1105. }
  1106. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "分类添加", t_c)
  1107. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  1108. c.ServeJSON()
  1109. return
  1110. }
  1111. func (c *DeviceController) CompanyClass_Edit() {
  1112. T_name := c.GetString("T_name")
  1113. id, _ := c.GetInt("T_id")
  1114. R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
  1115. if err != nil {
  1116. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1117. c.ServeJSON()
  1118. return
  1119. }
  1120. if R_DeviceClass.T_pid != c.T_pid {
  1121. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1122. c.ServeJSON()
  1123. return
  1124. }
  1125. if len(T_name) > 0 {
  1126. R_DeviceClass.T_name = T_name
  1127. }
  1128. if is := Company.Update_CompanyClass(R_DeviceClass, "T_name"); !is {
  1129. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"}
  1130. c.ServeJSON()
  1131. return
  1132. }
  1133. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "分类修改", R_DeviceClass)
  1134. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1135. c.ServeJSON()
  1136. return
  1137. }
  1138. func (c *DeviceController) CompanyClass_Del() {
  1139. id, _ := c.GetInt("T_id")
  1140. R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
  1141. if err != nil {
  1142. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1143. c.ServeJSON()
  1144. return
  1145. }
  1146. if R_DeviceClass.T_pid != c.T_pid {
  1147. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1148. c.ServeJSON()
  1149. return
  1150. }
  1151. if is := Company.Delete_CompanyClass_ById(id); !is {
  1152. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败"}
  1153. c.ServeJSON()
  1154. return
  1155. }
  1156. // 将传感器T_Class绑定的分类id替换为空字符串
  1157. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_Class_Id(id)
  1158. for _, v := range DeviceSensor_class_list {
  1159. Device.DeviceSensor_Bind_T_Class_Del(v.T_sn, v.T_id, id)
  1160. }
  1161. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "分类删除", strconv.Itoa(id))
  1162. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1163. c.ServeJSON()
  1164. return
  1165. }
  1166. // 设备分类绑定
  1167. func (c *DeviceController) DeviceClassBind_List() {
  1168. type R_JSONS struct {
  1169. //必须的大写开头
  1170. DeviceSensor_lite []Device.DeviceSensor_
  1171. Num int
  1172. Page int
  1173. Page_size int
  1174. Pages []lib.Page_T
  1175. }
  1176. var r_jsons R_JSONS
  1177. page, _ := c.GetInt("page")
  1178. if page < 1 {
  1179. page = 1
  1180. }
  1181. page_z, _ := c.GetInt("page_z")
  1182. if page_z < 1 {
  1183. page_z = conf.Page_size
  1184. }
  1185. T_name := c.GetString("T_name")
  1186. T_sn := c.GetString("T_sn")
  1187. T_class_id, _ := c.GetInt("T_class_id")
  1188. T_type, _ := c.GetInt("T_type")
  1189. R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
  1190. if err != nil {
  1191. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
  1192. c.ServeJSON()
  1193. return
  1194. }
  1195. if R_DeviceClass.T_pid != c.T_pid {
  1196. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1197. c.ServeJSON()
  1198. return
  1199. }
  1200. var cnt int64
  1201. Device_lite, cnt := Device.Read_DeviceSensor_List_T_Class(c.T_pid, T_class_id, T_sn, T_name, T_type, page, page_z)
  1202. r_jsons.DeviceSensor_lite = Device_lite
  1203. page_size := math.Ceil(float64(cnt) / float64(page_z))
  1204. r_jsons.Page = page
  1205. r_jsons.Page_size = int(page_size)
  1206. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  1207. r_jsons.Num = int(cnt)
  1208. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1209. c.ServeJSON()
  1210. return
  1211. }
  1212. func (c *DeviceController) ClassBind_Add() {
  1213. T_class_id, _ := c.GetInt("T_class_id")
  1214. T_sn := c.GetString("T_sn")
  1215. T_id, _ := c.GetInt("T_id")
  1216. if T_class_id == 0 || len(T_sn) == 0 {
  1217. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1218. c.ServeJSON()
  1219. return
  1220. }
  1221. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1222. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1223. c.ServeJSON()
  1224. return
  1225. }
  1226. R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
  1227. if err != nil {
  1228. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
  1229. c.ServeJSON()
  1230. return
  1231. }
  1232. if R_DeviceClass.T_pid != c.T_pid {
  1233. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1234. c.ServeJSON()
  1235. return
  1236. }
  1237. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Class(T_sn, T_id, T_class_id)
  1238. if len(DeviceSensor_class_list) > 0 {
  1239. c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复绑定!"}
  1240. c.ServeJSON()
  1241. return
  1242. }
  1243. if err = Device.DeviceSensor_Bind_T_Class_Add(T_sn, T_id, T_class_id); err != nil {
  1244. c.Data["json"] = lib.JSONS{Code: 200, Msg: "绑定失败"}
  1245. c.ServeJSON()
  1246. return
  1247. }
  1248. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "分类绑定操作", strconv.Itoa(T_class_id)+"+>"+T_sn+"-"+strconv.Itoa(T_id))
  1249. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1250. c.ServeJSON()
  1251. return
  1252. }
  1253. func (c *DeviceController) ClassBind_Del() {
  1254. T_class_id, _ := c.GetInt("T_class_id")
  1255. T_sn := c.GetString("T_sn")
  1256. T_id, _ := c.GetInt("T_id")
  1257. if T_class_id == 0 || len(T_sn) == 0 {
  1258. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1259. c.ServeJSON()
  1260. return
  1261. }
  1262. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1263. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1264. c.ServeJSON()
  1265. return
  1266. }
  1267. R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
  1268. if err != nil {
  1269. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
  1270. c.ServeJSON()
  1271. return
  1272. }
  1273. if R_DeviceClass.T_pid != c.T_pid {
  1274. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1275. c.ServeJSON()
  1276. return
  1277. }
  1278. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Class(T_sn, T_id, T_class_id)
  1279. if len(DeviceSensor_class_list) == 0 {
  1280. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1281. c.ServeJSON()
  1282. return
  1283. }
  1284. if err = Device.DeviceSensor_Bind_T_Class_Del(T_sn, T_id, T_class_id); err != nil {
  1285. c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除失败"}
  1286. c.ServeJSON()
  1287. return
  1288. }
  1289. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "分类删除绑定操作", strconv.Itoa(T_class_id)+"->"+T_sn+"-"+strconv.Itoa(T_id))
  1290. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1291. c.ServeJSON()
  1292. return
  1293. }
  1294. // 报警策略 ------------------------------------------
  1295. func (c *DeviceController) CompanyNotice_List() {
  1296. type R_JSONS struct {
  1297. //必须的大写开头
  1298. Data []Company.CompanyNotice_R
  1299. Num int64
  1300. Page int
  1301. Page_size int
  1302. }
  1303. var r_jsons R_JSONS
  1304. page, _ := c.GetInt("page")
  1305. if page < 1 {
  1306. page = 1
  1307. }
  1308. page_z, _ := c.GetInt("page_z")
  1309. if page_z < 1 {
  1310. page_z = conf.Page_size
  1311. }
  1312. T_name := c.GetString("T_name")
  1313. r_jsons.Data, r_jsons.Num = Company.Read_CompanyNotice_List(c.T_pid, T_name, page, page_z)
  1314. r_jsons.Page = page
  1315. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1316. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1317. c.ServeJSON()
  1318. return
  1319. }
  1320. func (c *DeviceController) CompanyNotice_All() {
  1321. type R_JSONS struct {
  1322. //必须的大写开头
  1323. Data []Company.CompanyNotice_R
  1324. Num int64
  1325. Page int
  1326. Page_size int
  1327. }
  1328. var r_jsons R_JSONS
  1329. T_name := c.GetString("T_name")
  1330. r_jsons.Data = Company.Read_CompanyNotice_All(c.T_pid, T_name)
  1331. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1332. c.ServeJSON()
  1333. return
  1334. }
  1335. func (c *DeviceController) CompanyNotice_Get() {
  1336. id, _ := c.GetInt("T_id")
  1337. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
  1338. if err != nil {
  1339. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1340. c.ServeJSON()
  1341. return
  1342. }
  1343. if R_DeviceNotice.T_pid != c.T_pid {
  1344. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1345. c.ServeJSON()
  1346. return
  1347. }
  1348. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Company.CompanyNoticeToCompanyNotice_R(R_DeviceNotice)}
  1349. c.ServeJSON()
  1350. return
  1351. }
  1352. func (c *DeviceController) CompanyNotice_Add() {
  1353. T_name := c.GetString("T_name")
  1354. t_c := Company.CompanyNotice{
  1355. T_pid: c.T_pid,
  1356. T_name: T_name,
  1357. T_Notice_mechanism: Warning.Read_WarningType_All_T_Notice_mechanism(),
  1358. T_State: 1,
  1359. }
  1360. Id, err := Company.Add_CompanyNotice(t_c)
  1361. if err != nil {
  1362. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  1363. c.ServeJSON()
  1364. return
  1365. }
  1366. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "报警策略添加", t_c)
  1367. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  1368. c.ServeJSON()
  1369. return
  1370. }
  1371. func (c *DeviceController) CompanyNotice_Edit() {
  1372. id, _ := c.GetInt("T_id")
  1373. T_name := c.GetString("T_name")
  1374. T_Notice_wx := c.GetString("T_Notice_wx")
  1375. T_Notice_wx2 := c.GetString("T_Notice_wx2")
  1376. T_Notice_app := c.GetString("T_Notice_app")
  1377. T_Notice_phone := c.GetString("T_Notice_phone")
  1378. T_Notice_message := c.GetString("T_Notice_message")
  1379. T_Notice_mailbox := c.GetString("T_Notice_mailbox")
  1380. T_Notice_mechanism := c.GetString("T_Notice_mechanism")
  1381. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
  1382. if err != nil {
  1383. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1384. c.ServeJSON()
  1385. return
  1386. }
  1387. if R_DeviceNotice.T_pid != c.T_pid {
  1388. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1389. c.ServeJSON()
  1390. return
  1391. }
  1392. R_DeviceNotice.Id = id
  1393. if len(T_name) > 0 {
  1394. R_DeviceNotice.T_name = T_name
  1395. }
  1396. if len(T_Notice_wx) > 0 {
  1397. if T_Notice_wx == "-" {
  1398. T_Notice_wx = ""
  1399. }
  1400. R_DeviceNotice.T_Notice_wx = T_Notice_wx
  1401. }
  1402. if len(T_Notice_wx2) > 0 {
  1403. if T_Notice_wx2 == "-" {
  1404. T_Notice_wx2 = ""
  1405. }
  1406. R_DeviceNotice.T_Notice_wx2 = T_Notice_wx2
  1407. }
  1408. if len(T_Notice_app) > 0 {
  1409. if T_Notice_app == "-" {
  1410. T_Notice_app = ""
  1411. }
  1412. R_DeviceNotice.T_Notice_app = T_Notice_app
  1413. }
  1414. if len(T_Notice_phone) > 0 {
  1415. if T_Notice_phone == "-" {
  1416. T_Notice_phone = ""
  1417. }
  1418. R_DeviceNotice.T_Notice_phone = T_Notice_phone
  1419. }
  1420. if len(T_Notice_message) > 0 {
  1421. R_DeviceNotice.T_Notice_message = T_Notice_message
  1422. }
  1423. if len(T_Notice_mailbox) > 0 {
  1424. if T_Notice_mailbox == "-" {
  1425. T_Notice_mailbox = ""
  1426. }
  1427. R_DeviceNotice.T_Notice_mailbox = T_Notice_mailbox
  1428. }
  1429. if len(T_Notice_mechanism) > 0 {
  1430. if T_Notice_mechanism == "-" {
  1431. T_Notice_mechanism = ""
  1432. }
  1433. R_DeviceNotice.T_Notice_mechanism = T_Notice_mechanism
  1434. }
  1435. 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 {
  1436. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"}
  1437. c.ServeJSON()
  1438. return
  1439. }
  1440. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "报警策略修改", R_DeviceNotice)
  1441. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1442. c.ServeJSON()
  1443. return
  1444. }
  1445. func (c *DeviceController) CompanyNotice_Reset_Mechanism() {
  1446. id, _ := c.GetInt("T_id")
  1447. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
  1448. if err != nil {
  1449. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1450. c.ServeJSON()
  1451. return
  1452. }
  1453. if R_DeviceNotice.T_pid != c.T_pid {
  1454. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1455. c.ServeJSON()
  1456. return
  1457. }
  1458. R_DeviceNotice.T_Notice_mechanism = Warning.Read_WarningType_All_T_Notice_mechanism()
  1459. if err = Company.Update_CompanyNotice(R_DeviceNotice, "T_Notice_mechanism"); err != nil {
  1460. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败"}
  1461. c.ServeJSON()
  1462. return
  1463. }
  1464. System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备管理", "报警策略", R_DeviceNotice)
  1465. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1466. c.ServeJSON()
  1467. return
  1468. }
  1469. func (c *DeviceController) CompanyNotice_Del() {
  1470. id, _ := c.GetInt("T_id")
  1471. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
  1472. if err != nil {
  1473. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1474. c.ServeJSON()
  1475. return
  1476. }
  1477. if R_DeviceNotice.T_pid != c.T_pid {
  1478. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1479. c.ServeJSON()
  1480. return
  1481. }
  1482. if is := Company.Delete_CompanyNotice_ById(id); !is {
  1483. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败"}
  1484. c.ServeJSON()
  1485. return
  1486. }
  1487. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_Notice_Id(id)
  1488. for _, v := range DeviceSensor_class_list {
  1489. Device.DeviceSensor_Bind_T_Notice_Del(v.T_sn, v.T_id, id)
  1490. }
  1491. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警策略删除", strconv.Itoa(id))
  1492. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1493. c.ServeJSON()
  1494. return
  1495. }
  1496. // 设备通知绑定
  1497. func (c *DeviceController) DeviceNoticeBind_List() {
  1498. type R_JSONS struct {
  1499. //必须的大写开头
  1500. DeviceSensor_lite []Device.DeviceSensor_
  1501. Num int
  1502. Page int
  1503. Page_size int
  1504. Pages []lib.Page_T
  1505. }
  1506. var r_jsons R_JSONS
  1507. page, _ := c.GetInt("page")
  1508. if page < 1 {
  1509. page = 1
  1510. }
  1511. page_z, _ := c.GetInt("page_z")
  1512. if page_z < 1 {
  1513. page_z = conf.Page_size
  1514. }
  1515. T_name := c.GetString("T_name")
  1516. T_sn := c.GetString("T_sn")
  1517. T_notice_id, _ := c.GetInt("T_notice_id")
  1518. T_type, _ := c.GetInt("T_type")
  1519. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(T_notice_id)
  1520. if err != nil {
  1521. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
  1522. c.ServeJSON()
  1523. return
  1524. }
  1525. if R_DeviceNotice.T_pid != c.T_pid {
  1526. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1527. c.ServeJSON()
  1528. return
  1529. }
  1530. var cnt int64
  1531. Device_lite, cnt := Device.Read_DeviceSensor_List_T_Notice(c.T_pid, T_notice_id, page, page_z, T_sn, T_name, T_type)
  1532. r_jsons.DeviceSensor_lite = Device_lite
  1533. page_size := math.Ceil(float64(cnt) / float64(page_z))
  1534. r_jsons.Page = page
  1535. r_jsons.Page_size = int(page_size)
  1536. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  1537. r_jsons.Num = int(cnt)
  1538. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1539. c.ServeJSON()
  1540. return
  1541. }
  1542. func (c *DeviceController) NoticeBind_Add() {
  1543. T_notice_id, _ := c.GetInt("T_notice_id")
  1544. T_sn := c.GetString("T_sn")
  1545. T_id, _ := c.GetInt("T_id")
  1546. if T_notice_id == 0 || len(T_sn) == 0 {
  1547. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1548. c.ServeJSON()
  1549. return
  1550. }
  1551. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1552. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1553. c.ServeJSON()
  1554. return
  1555. }
  1556. R_CompanyNotice, err := Company.Read_CompanyNotice_ById(T_notice_id)
  1557. if err != nil {
  1558. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
  1559. c.ServeJSON()
  1560. return
  1561. }
  1562. if R_CompanyNotice.T_pid != c.T_pid {
  1563. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1564. c.ServeJSON()
  1565. return
  1566. }
  1567. DeviceSensor_Notice_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn, T_id, T_notice_id)
  1568. if len(DeviceSensor_Notice_list) > 0 {
  1569. c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复绑定!"}
  1570. c.ServeJSON()
  1571. return
  1572. }
  1573. if err = Company.Add_T_Notice_bind(T_sn, T_id, T_notice_id); err != nil {
  1574. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知绑定失败"}
  1575. c.ServeJSON()
  1576. return
  1577. }
  1578. if err = Device.DeviceSensor_Bind_T_Notice_Add(T_sn, T_id, T_notice_id); err != nil {
  1579. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知绑定失败"}
  1580. c.ServeJSON()
  1581. return
  1582. }
  1583. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "报警策略绑定添加", strconv.Itoa(T_notice_id)+"+>"+T_sn+"-"+strconv.Itoa(T_id))
  1584. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1585. c.ServeJSON()
  1586. return
  1587. }
  1588. func (c *DeviceController) NoticeBind_Del() {
  1589. T_notice_id, _ := c.GetInt("T_notice_id")
  1590. T_sn := c.GetString("T_sn")
  1591. T_id, _ := c.GetInt("T_id")
  1592. if T_notice_id == 0 || len(T_sn) == 0 {
  1593. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1594. c.ServeJSON()
  1595. return
  1596. }
  1597. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1598. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1599. c.ServeJSON()
  1600. return
  1601. }
  1602. R_DeviceClass, err := Company.Read_CompanyNotice_ById(T_notice_id)
  1603. if err != nil {
  1604. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id Err!"}
  1605. c.ServeJSON()
  1606. return
  1607. }
  1608. if R_DeviceClass.T_pid != c.T_pid {
  1609. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1610. c.ServeJSON()
  1611. return
  1612. }
  1613. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn, T_id, T_notice_id)
  1614. if len(DeviceSensor_class_list) == 0 {
  1615. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1616. c.ServeJSON()
  1617. return
  1618. }
  1619. if err = Company.Delete_T_Notice_bind(T_sn, T_id, T_notice_id); err != nil {
  1620. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知删除绑定失败"}
  1621. c.ServeJSON()
  1622. return
  1623. }
  1624. if err = Device.DeviceSensor_Bind_T_Notice_Del(T_sn, T_id, T_notice_id); err != nil {
  1625. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知删除绑定失败"}
  1626. c.ServeJSON()
  1627. return
  1628. }
  1629. System.Add_UserLogs(c.Admin_r.T_uuid, "设备管理", "通知删除绑定删除", strconv.Itoa(T_notice_id)+"->"+T_sn+"-"+strconv.Itoa(T_id))
  1630. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1631. c.ServeJSON()
  1632. return
  1633. }
  1634. // 传感器类型
  1635. func (c *DeviceController) DeviceSensorType_List_All() {
  1636. type R_JSONS struct {
  1637. //必须的大写开头
  1638. Data []Device.DeviceSensorType
  1639. Num int64
  1640. Page int
  1641. Page_size int
  1642. }
  1643. var r_jsons R_JSONS
  1644. r_jsons.Data = Device.Read_DeviceSensorType_List_All()
  1645. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1646. c.ServeJSON()
  1647. return
  1648. }
  1649. // 传感器类型 - 小程序
  1650. func (c *DeviceController) DeviceSensorType_List() {
  1651. var r_jsons lib.R_JSONS
  1652. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  1653. if err != nil {
  1654. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1655. c.ServeJSON()
  1656. return
  1657. }
  1658. var T_pids []int
  1659. if c.Admin_r.T_pid > 0 {
  1660. // 查询公司
  1661. Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
  1662. if err != nil {
  1663. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1664. c.ServeJSON()
  1665. return
  1666. }
  1667. // 查询公司下面所有子公司id
  1668. T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
  1669. }
  1670. list, err := Device.Read_DeviceSensor_T_type(c.Admin_r, c.T_pid, bindSN, T_pids)
  1671. if err != nil {
  1672. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1673. c.ServeJSON()
  1674. return
  1675. }
  1676. if len(list) == 0 {
  1677. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1678. c.ServeJSON()
  1679. return
  1680. }
  1681. var ids []int
  1682. for _, v := range list {
  1683. id, _ := strconv.Atoi(v.(string))
  1684. ids = append(ids, id)
  1685. }
  1686. r_jsons.Data = Device.Read_DeviceSensorType_List_By_Ids(ids)
  1687. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1688. c.ServeJSON()
  1689. return
  1690. }
  1691. // 设备统计 - 小程序
  1692. func (c *DeviceController) Device_Applet_Stat() {
  1693. type Online struct {
  1694. T_type int
  1695. Count int64
  1696. Online int64 // 监控
  1697. NoNetwork int64 // 无网络
  1698. NotEnabled int64 // 未启用
  1699. }
  1700. type R_JSONS struct {
  1701. //必须的大写开头
  1702. Data Online
  1703. }
  1704. T_type, T_type_err := c.GetInt("T_type")
  1705. if T_type_err != nil || T_type == 0 {
  1706. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_type Err!"}
  1707. c.ServeJSON()
  1708. return
  1709. }
  1710. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  1711. if err != nil {
  1712. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1713. c.ServeJSON()
  1714. return
  1715. }
  1716. var T_pids []int
  1717. if c.Admin_r.T_pid > 0 {
  1718. // 查询公司
  1719. Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
  1720. if err != nil {
  1721. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1722. c.ServeJSON()
  1723. return
  1724. }
  1725. // 查询公司下面所有子公司id
  1726. T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
  1727. }
  1728. snList, err := Device.Read_DeviceSensor_T_sn_ByT_type(c.Admin_r, c.T_pid, bindSN, T_pids, T_type)
  1729. if err != nil {
  1730. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1731. c.ServeJSON()
  1732. return
  1733. }
  1734. var r_jsons R_JSONS
  1735. r_jsons.Data.T_type = T_type
  1736. if len(snList) == 0 {
  1737. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1738. c.ServeJSON()
  1739. return
  1740. }
  1741. // 0 未启用 1 在线 2 离线
  1742. r_jsons.Data.Count = Device.Read_Device_Count_ByT_online(snList, "")
  1743. r_jsons.Data.Online = Device.Read_Device_Count_ByT_online(snList, "1")
  1744. r_jsons.Data.NoNetwork = Device.Read_Device_Count_ByT_online(snList, "2")
  1745. r_jsons.Data.NotEnabled = Device.Read_Device_Count_ByT_online(snList, "0")
  1746. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1747. c.ServeJSON()
  1748. return
  1749. }
  1750. // 传感器列表 - 小程序 View==1
  1751. func (c *DeviceController) DeviceSensor_Applet_List_View1() {
  1752. var r_jsons lib.R_JSONS
  1753. page, _ := c.GetInt("page")
  1754. if page < 1 {
  1755. page = 1
  1756. }
  1757. page_z, _ := c.GetInt("page_z")
  1758. if page_z < 1 {
  1759. page_z = conf.Page_size
  1760. }
  1761. T_name := c.GetString("T_name")
  1762. //T_online := c.GetString("T_online")
  1763. T_type, T_type_err := c.GetInt("T_type")
  1764. if T_type_err != nil {
  1765. T_type = -1
  1766. }
  1767. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  1768. if err != nil {
  1769. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1770. c.ServeJSON()
  1771. return
  1772. }
  1773. var T_pids []int
  1774. if c.Admin_r.T_pid > 0 {
  1775. // 查询公司
  1776. Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
  1777. if err != nil {
  1778. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1779. c.ServeJSON()
  1780. return
  1781. }
  1782. // 查询公司下面所有子公司id
  1783. T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
  1784. }
  1785. Account.Read_Company_All_Maps()
  1786. 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)
  1787. r_jsons.Page = page
  1788. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1789. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1790. c.ServeJSON()
  1791. return
  1792. }
  1793. // 传感器列表 - 小程序 View==2
  1794. func (c *DeviceController) DeviceSensor_Applet_List_View2() {
  1795. var r_jsons lib.R_JSONS
  1796. page, _ := c.GetInt("page")
  1797. if page < 1 {
  1798. page = 1
  1799. }
  1800. page_z, _ := c.GetInt("page_z")
  1801. if page_z < 1 {
  1802. page_z = conf.Page_size
  1803. }
  1804. T_name := c.GetString("T_name")
  1805. T_online := c.GetString("T_online")
  1806. T_type, T_type_err := c.GetInt("T_type")
  1807. if T_type_err != nil {
  1808. T_type = -1
  1809. }
  1810. bindSN, err := Account.Read_UserDevice_List(*c.Admin_r)
  1811. if err != nil {
  1812. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1813. c.ServeJSON()
  1814. return
  1815. }
  1816. var T_pids []int
  1817. if c.Admin_r.T_pid > 0 {
  1818. // 查询公司
  1819. Company_r, err := Account.Read_Company_ById(c.Admin_r.T_pid)
  1820. if err != nil {
  1821. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1822. c.ServeJSON()
  1823. return
  1824. }
  1825. // 查询公司下面所有子公司id
  1826. T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
  1827. }
  1828. Account.Read_Company_All_Maps()
  1829. 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)
  1830. var deviceMap = make(map[string][]Device.DeviceSensor_Applet)
  1831. var num int64
  1832. type DeviceList struct {
  1833. T_sn string
  1834. DeviceSensorList []Device.DeviceSensor_Applet
  1835. }
  1836. var deviceList []DeviceList
  1837. for _, ds := range dsList {
  1838. _, ok := deviceMap[ds.T_sn]
  1839. if !ok {
  1840. num += 1
  1841. var list []Device.DeviceSensor_Applet
  1842. list = append(list, ds)
  1843. deviceMap[ds.T_sn] = list
  1844. device := DeviceList{
  1845. T_sn: ds.T_sn,
  1846. }
  1847. deviceList = append(deviceList, device)
  1848. } else {
  1849. deviceMap[ds.T_sn] = append(deviceMap[ds.T_sn], ds)
  1850. }
  1851. }
  1852. for i := 0; i < len(deviceList); i++ {
  1853. deviceList[i].DeviceSensorList = deviceMap[deviceList[i].T_sn]
  1854. }
  1855. var offset int
  1856. if page <= 1 {
  1857. offset = 0
  1858. } else {
  1859. offset = (page - 1) * page_z
  1860. }
  1861. offset_z := offset + page_z
  1862. if num < int64(offset_z) {
  1863. offset_z = int(num)
  1864. }
  1865. if offset > offset_z {
  1866. offset = offset_z
  1867. }
  1868. r_jsons.Data = deviceList[offset:offset_z]
  1869. r_jsons.Num = num
  1870. r_jsons.Page = page
  1871. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1872. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1873. c.ServeJSON()
  1874. return
  1875. }
  1876. func Update_DeviceSensor_Parameter() {
  1877. list := Device.Read_DeviceSensor_All()
  1878. for _, sensor := range list {
  1879. dsp, is := Device.Read_DeviceSensorParameter(sensor.T_sn, sensor.T_id)
  1880. if !is {
  1881. continue
  1882. }
  1883. if sensor.T_sp == dsp.Id {
  1884. continue
  1885. }
  1886. sensor.T_sp = dsp.Id
  1887. Device.Update_DeviceSensor(sensor, "T_sp")
  1888. }
  1889. }
  1890. // 传感器列表 - 冷链物流运输客户端
  1891. func (c *DeviceController) DeviceSensor_List_BySN() {
  1892. type R_JSONS struct {
  1893. //必须的大写开头
  1894. Data []Device.DeviceSensor_R
  1895. Num int64
  1896. Page int
  1897. Page_size int
  1898. }
  1899. var r_jsons R_JSONS
  1900. T_sn := c.GetString("T_sn")
  1901. if len(T_sn) == 0 {
  1902. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn err!"}
  1903. c.ServeJSON()
  1904. return
  1905. }
  1906. r_jsons.Data, r_jsons.Num = Device.Read_DeviceSensorList_BySN(T_sn)
  1907. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1908. c.ServeJSON()
  1909. return
  1910. }