Device.go 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Admin"
  7. "Cold_Api/models/Device"
  8. "Cold_Api/models/System"
  9. "fmt"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "github.com/xuri/excelize/v2"
  12. "math"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. //Handle
  18. type DeviceController struct {
  19. beego.Controller
  20. }
  21. // 列表 -
  22. func (c *DeviceController) Device_html() {
  23. // 验证登录
  24. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  25. if !b_ {
  26. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  27. c.ServeJSON()
  28. return
  29. }
  30. c.Data["Admin_r"] = admin_r
  31. c.Data["Admin_Power"], _ = Admin.Read_AdminPower_ById(admin_r.Admin_power)
  32. var Device_lite_r []Device.R_Device
  33. page, _ := c.GetInt("page")
  34. println(page)
  35. if page < 1 {
  36. page = 1
  37. }
  38. page_z := 4
  39. Class_1 := c.GetString("Class_1")
  40. Name := c.GetString("Name")
  41. T_sn := c.GetString("T_sn")
  42. c.Data["Class_1"] = Class_1
  43. c.Data["Name"] = Name
  44. c.Data["T_sn"] = T_sn
  45. var cnt int64
  46. Device_lite, cnt := Device.Read_Device_ALL_bind_1(admin_r, page, page_z, T_sn, Name, Class_1, "", "")
  47. for _, v := range Device_lite {
  48. r_Device := Device.R_Device{}
  49. r_Device.T_sn = v.T_sn
  50. r_Device.T_MSISDN = v.T_MSISDN
  51. r_Device.T_devName = v.T_devName
  52. r_Device.T_give = v.T_give
  53. r_Device.T_online = v.T_online
  54. r_Device.T_monitor = v.T_monitor
  55. r_Device.T_Dattery = v.T_Dattery
  56. r_Device.T_sensor_list, r_Device.T_sensor = Device.Read_DeviceSensor_ByTsn(v.T_sn)
  57. Device_lite_r = append(Device_lite_r, r_Device)
  58. }
  59. c.Data["Device_lite"] = Device_lite_r
  60. page_size := math.Ceil(float64(cnt) / float64(page_z))
  61. c.Data["Page"] = page
  62. c.Data["Page_size"] = page_size
  63. c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
  64. c.Data["cnt"] = cnt
  65. c.TplName = "Device/Device.html"
  66. }
  67. func (c *DeviceController) Device_() {
  68. id, _ := c.GetInt("id")
  69. c.Data["id"] = id
  70. if id > 0 {
  71. c.Data["Date"] = Device.Read_Device_ById(id)
  72. //c.Data["FormulaList"] = Formula.ReadFormulaListByid_List(id)
  73. }
  74. c.Data["DeviceSnOld_List"] = Device.Read_DeviceSnOld_1()
  75. c.TplName = "Device/Device-.html"
  76. }
  77. func (c *DeviceController) Device_Parameter_html() {
  78. // 验证登录
  79. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  80. if !b_ {
  81. c.Ctx.Redirect(302, "Login")
  82. return
  83. }
  84. c.Data["Admin_Power"], _ = Admin.Read_AdminPower_ById(admin_r.Admin_power)
  85. c.Data["Admin"] = admin_r
  86. Sn := c.GetString("Sn")
  87. DeviceParameter_lite := Device.Read_DeviceParameter_SN(Sn)
  88. if len(DeviceParameter_lite) > 0 {
  89. c.Data["DeviceParameter"] = DeviceParameter_lite[0]
  90. } else {
  91. NatsServer.Read_DeviceParameter(Sn)
  92. }
  93. c.Data["DeviceParameter_lite_z"] = len(DeviceParameter_lite)
  94. c.Data["DeviceParameter_lite_SendState_z"] = len(Device.Read_DeviceParameter_SN_T_SendState_0(Sn))
  95. DeviceParameter_lite_s_1 := Device.Read_DeviceParameter_SN_T_State_1(Sn)
  96. c.Data["DeviceParameter_lite"] = DeviceParameter_lite_s_1
  97. c.Data["Device"], _ = Device.Read_Device_ByT_sn(Sn)
  98. c.TplName = "Device/Device_Parameter-.html"
  99. }
  100. func (c *DeviceController) DeviceSensor_List_html() {
  101. // 验证登录
  102. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  103. if !b_ {
  104. c.Ctx.Redirect(302, "Login")
  105. return
  106. }
  107. c.Data["Admin_Power"], _ = Admin.Read_AdminPower_ById(admin_r.Admin_power)
  108. Sn := c.GetString("Sn")
  109. Id, _ := c.GetInt("Id")
  110. //
  111. //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
  112. c.Data["DeviceSensor"], _ = Device.Read_DeviceSensor_ByTsn_Tid(Sn, Id)
  113. DeviceSensorParameter_list := Device.Read_DeviceSensorParameter_SN_T_id(Sn, Id)
  114. c.Data["DeviceSensorParameter_lite"] = DeviceSensorParameter_list
  115. c.Data["DeviceSensorParameter_T_SendState_0"] = 0
  116. if len(DeviceSensorParameter_list) > 0 {
  117. if DeviceSensorParameter_list[0].T_SendState == 0 {
  118. c.Data["DeviceSensorParameter_T_SendState_0"] = 1
  119. }
  120. }
  121. c.Data["Device"], _ = Device.Read_Device_ByT_sn(Sn)
  122. c.TplName = "Device/Device_Sensor-.html"
  123. }
  124. func (c *DeviceController) Device_Parameter_Del_Device() {
  125. // 验证登录
  126. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  127. if !b_ {
  128. c.Ctx.Redirect(302, "Login")
  129. return
  130. }
  131. Sn := c.GetString("Sn")
  132. Id, _ := c.GetInt("Id")
  133. System.Add_UserLogs_T(admin_r.Admin_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
  134. //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
  135. Device.Delete_DeviceSensor_ById(Sn, Id)
  136. NatsServer.Del_DeviceSensor(Device.DeviceSensor_Del{
  137. T_sn: Sn, T_id: Id,
  138. })
  139. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  140. c.ServeJSON()
  141. return
  142. }
  143. func (c *DeviceController) Device_Parameter_Del() {
  144. // 验证登录
  145. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  146. if !b_ {
  147. c.Ctx.Redirect(302, "Login")
  148. return
  149. }
  150. Sn := c.GetString("Sn")
  151. Id, _ := c.GetInt("Id")
  152. System.Add_UserLogs_T(admin_r.Admin_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
  153. //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
  154. Device.Delete_DeviceSensor_ById(Sn, Id)
  155. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  156. c.ServeJSON()
  157. return
  158. }
  159. func (c *DeviceController) UpDeviceSensor_Tsort() {
  160. // 验证登录
  161. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  162. if !b_ {
  163. c.Ctx.Redirect(302, "Login")
  164. return
  165. }
  166. Sn := c.GetString("Sn")
  167. Id, _ := c.GetInt("Id")
  168. T_sort, _ := c.GetInt("T_sort")
  169. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  170. if !is {
  171. c.Data["json"] = lib.JSONS{Code: 201, Msg: "信息错误!"}
  172. c.ServeJSON()
  173. return
  174. }
  175. DeviceSensor.T_sort = T_sort
  176. //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
  177. Device.Update_DeviceSensor(DeviceSensor, "T_sort")
  178. System.Add_UserLogs_T(admin_r.Admin_uuid, "设备管理", "传感器 排序操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(T_sort))
  179. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  180. c.ServeJSON()
  181. return
  182. }
  183. func (c *DeviceController) UpDeviceSensor_T_datashow() {
  184. // 验证登录
  185. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  186. if !b_ {
  187. c.Ctx.Redirect(302, "Login")
  188. return
  189. }
  190. Sn := c.GetString("Sn")
  191. Id, _ := c.GetInt("Id")
  192. T_datashow, _ := c.GetInt("T_datashow")
  193. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  194. if !is {
  195. c.Data["json"] = lib.JSONS{Code: 201, Msg: "信息错误!"}
  196. c.ServeJSON()
  197. return
  198. }
  199. DeviceSensor.T_datashow = T_datashow
  200. Device.Update_DeviceSensor(DeviceSensor, "T_datashow")
  201. System.Add_UserLogs_T(admin_r.Admin_uuid, "设备管理", "传感器 数据展示(0 屏蔽数据展示 1 正常数据展示)", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(T_datashow))
  202. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  203. c.ServeJSON()
  204. return
  205. }
  206. func (c *DeviceController) Device_Post() {
  207. /// 验证登录
  208. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  209. if !b_ {
  210. c.Ctx.Redirect(302, "Login")
  211. return
  212. }
  213. var err error
  214. T_sn := c.GetString("T_sn")
  215. T_MSISDN := c.GetString("T_MSISDN")
  216. var_ := Device.Device{
  217. T_sn: T_sn,
  218. T_MSISDN: T_MSISDN,
  219. T_Bind: "U" + strconv.Itoa(admin_r.Id) + "|",
  220. T_l_p: 0,
  221. T_give: 1,
  222. T_monitor: 1,
  223. T_State: 1,
  224. }
  225. if len(T_sn) < 8 {
  226. c.Data["json"] = lib.JSONS{Code: 303, Msg: "SN 太短 必须8个字符以上"}
  227. c.ServeJSON()
  228. return
  229. }
  230. if !(strings.Contains(T_sn, "KF") || strings.Contains(T_sn, "YD")) {
  231. c.Data["json"] = lib.JSONS{Code: 303, Msg: "必须包含 KF或YD!"}
  232. c.ServeJSON()
  233. return
  234. }
  235. _, err = Device.Read_Device_ByT_sn(T_sn)
  236. if err == nil {
  237. c.Data["json"] = lib.JSONS{Code: 303, Msg: "重复SN!"}
  238. c.ServeJSON()
  239. return
  240. }
  241. var_.T_devName = "等待设备上线.."
  242. // 创建数据库
  243. if !Device.CREATE_DeviceData(T_sn) {
  244. c.Data["json"] = lib.JSONS{Code: 302, Msg: "添加失败!"}
  245. c.ServeJSON()
  246. return
  247. }
  248. _, err = Device.Add_Device(var_)
  249. if err != nil {
  250. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  251. c.ServeJSON()
  252. return
  253. }
  254. System.Add_UserLogs_T(admin_r.Admin_uuid, "设备管理", "设备添加", var_)
  255. //MqttServer.Get_Device_Realtime(T_sn) // 更新传感器 !!!
  256. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  257. c.ServeJSON()
  258. return
  259. }
  260. func (c *DeviceController) Device_Del() {
  261. // 验证登录
  262. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  263. if !b_ {
  264. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  265. c.ServeJSON()
  266. return
  267. }
  268. if admin_r.Id != 1 {
  269. c.Data["json"] = lib.JSONS{Code: 201, Msg: "没有权限!"}
  270. c.ServeJSON()
  271. return
  272. }
  273. T_sn := c.GetString("T_sn")
  274. Device_r, err := Device.Read_Device_ByT_sn(T_sn)
  275. if err != nil {
  276. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_sn Err!"}
  277. c.ServeJSON()
  278. return
  279. }
  280. Device.Delete_Device(Device_r)
  281. DeviceSensor_list, _ := Device.Read_DeviceSensor_ByTsn(Device_r.T_sn)
  282. for _, v := range DeviceSensor_list {
  283. Device.Delete_DeviceSensor_ById(Device_r.T_sn, v.T_id)
  284. }
  285. Device.DELETE_DeviceSensor(Device_r.T_sn)
  286. Device.DELETE_DeviceDatar(Device_r.T_sn)
  287. Device.DELETE_DeviceParameter(Device_r.T_sn)
  288. Device.DELETE_DeviceSensorParameter(Device_r.T_sn)
  289. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "设备删除", T_sn)
  290. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  291. c.ServeJSON()
  292. return
  293. }
  294. // 设备弃用
  295. func (c *DeviceController) Device_Give() {
  296. // 验证登录
  297. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  298. if !b_ {
  299. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  300. c.ServeJSON()
  301. return
  302. }
  303. if admin_r.Admin_master != 1 {
  304. c.Data["json"] = lib.JSONS{Code: 201, Msg: "没有权限!"}
  305. c.ServeJSON()
  306. return
  307. }
  308. T_sn := c.GetString("T_sn")
  309. Device_r, err := Device.Read_Device_ByT_sn(T_sn)
  310. if err != nil {
  311. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_sn Err!"}
  312. c.ServeJSON()
  313. return
  314. }
  315. if Device_r.T_give == 0 {
  316. Device_r.T_give = 1
  317. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "设备开启", T_sn)
  318. } else {
  319. Device_r.T_give = 0
  320. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "设备弃用", T_sn)
  321. }
  322. Device.Update_Device(Device_r, "T_give")
  323. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  324. c.ServeJSON()
  325. return
  326. }
  327. //-------------------------------
  328. func (c *DeviceController) Device_List() {
  329. // 验证登录
  330. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  331. if !b_ {
  332. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  333. c.ServeJSON()
  334. return
  335. }
  336. type R_JSONS struct {
  337. //必须的大写开头
  338. Device_lite []Device.R_Device
  339. Num int
  340. Page int
  341. Page_size int
  342. Pages []lib.Page_T
  343. }
  344. var r_jsons R_JSONS
  345. page, _ := c.GetInt("page")
  346. println(page)
  347. if page < 1 {
  348. page = 1
  349. }
  350. Class_1 := c.GetString("Class_1")
  351. Name := c.GetString("Name")
  352. T_sn := c.GetString("T_sn")
  353. T_online := c.GetString("T_online")
  354. //
  355. //c.Data["Class_1"] = Class_1
  356. //c.Data["T_sn"] = T_sn
  357. //c.Data["Class_List"] = Device.Read_Class_All_1()
  358. Device_r, err := Device.Read_Device_ByT_sn(T_sn)
  359. if err != nil {
  360. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  361. c.ServeJSON()
  362. return
  363. }
  364. if !strings.Contains(Device_r.T_Bind, "U"+strconv.Itoa(admin_r.Id)+"|") && admin_r.Admin_master != 0 {
  365. c.Data["json"] = lib.JSONS{Code: 202, Msg: "! U" + strconv.Itoa(admin_r.Id)}
  366. c.ServeJSON()
  367. return
  368. }
  369. var cnt int64
  370. Device_lite, cnt := Device.Read_Device_ALL_bind_1(admin_r, page, 0, T_sn, Name, Class_1, "", T_online)
  371. for _, v := range Device_lite {
  372. r_Device := Device.R_Device{}
  373. r_Device.T_sn = v.T_sn
  374. r_Device.T_MSISDN = v.T_MSISDN
  375. r_Device.T_devName = v.T_devName
  376. r_Device.T_give = v.T_give
  377. r_Device.T_online = v.T_online
  378. r_Device.T_monitor = v.T_monitor
  379. r_Device.T_Dattery = v.T_Dattery
  380. r_Device.T_sensor_list, r_Device.T_sensor = Device.Read_DeviceSensor_ByTsn(v.T_sn)
  381. r_jsons.Device_lite = append(r_jsons.Device_lite, r_Device)
  382. }
  383. page_size := math.Ceil(float64(cnt) / float64(conf.Page_size))
  384. r_jsons.Page = int(page)
  385. r_jsons.Page_size = int(page_size)
  386. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  387. r_jsons.Num = int(cnt)
  388. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  389. c.ServeJSON()
  390. return
  391. }
  392. func (c *DeviceController) DeviceSensor_List() {
  393. // 验证登录
  394. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  395. if !b_ {
  396. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  397. c.ServeJSON()
  398. return
  399. }
  400. type R_JSONS struct {
  401. //必须的大写开头
  402. DeviceSensor_lite []Device.DeviceSensor_R
  403. Num int
  404. Page int
  405. Page_size int
  406. Pages []lib.Page_T
  407. }
  408. var r_jsons R_JSONS
  409. page, _ := c.GetInt("page")
  410. println(page)
  411. if page < 1 {
  412. page = 1
  413. }
  414. page_z, _ := c.GetInt("page_z")
  415. if page_z == 0 {
  416. page_z = conf.Page_size
  417. }
  418. T_sn := c.GetString("Sn")
  419. Device_r, err := Device.Read_Device_ByT_sn(T_sn)
  420. if err != nil {
  421. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  422. c.ServeJSON()
  423. return
  424. }
  425. if admin_r.Admin_master > 1 {
  426. if !strings.Contains(Device_r.T_Bind, "U"+strconv.Itoa(admin_r.Id)+"|") && admin_r.Admin_master != 0 {
  427. c.Data["json"] = lib.JSONS{Code: 202, Msg: "! U" + strconv.Itoa(admin_r.Id)}
  428. c.ServeJSON()
  429. return
  430. }
  431. }
  432. //c.Data["Class_List"] = Device.Read_Class_All_1()
  433. var cnt int64
  434. r_jsons.DeviceSensor_lite, cnt = Device.Read_DeviceSensor_ALL_T_sn(T_sn, page, page_z)
  435. page_size := math.Ceil(float64(cnt) / float64(page_z))
  436. r_jsons.Page = int(page)
  437. r_jsons.Page_size = int(page_size)
  438. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  439. r_jsons.Num = int(cnt)
  440. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  441. c.ServeJSON()
  442. return
  443. }
  444. // ------------------------------------------
  445. func (c *DeviceController) Read_DeviceParameter() {
  446. // 验证登录
  447. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  448. if !b_ {
  449. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  450. c.ServeJSON()
  451. return
  452. }
  453. T_SN := c.GetString("SN")
  454. //Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  455. //if err != nil {
  456. // c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  457. // c.ServeJSON()
  458. // return
  459. //}
  460. ////if !strings.Contains(Device_r.T_Bind, "U"+strconv.Itoa(r_admin.Id)) {
  461. //// c.Data["json"] = lib.JSONS{Code: 202, Msg: "! U" + strconv.Itoa(r_admin.Id)}
  462. //// c.ServeJSON()
  463. //// return
  464. ////}
  465. NatsServer.Read_DeviceParameter(T_SN)
  466. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  467. c.ServeJSON()
  468. return
  469. }
  470. func (c *DeviceController) Read_DeviceSensorParameter() {
  471. // 验证登录
  472. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  473. if !b_ {
  474. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  475. c.ServeJSON()
  476. return
  477. }
  478. T_SN := c.GetString("SN")
  479. //Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  480. //if err != nil {
  481. // c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  482. // c.ServeJSON()
  483. // return
  484. //}
  485. ////if !strings.Contains(Device_r.T_Bind, "U"+strconv.Itoa(r_admin.Id)) {
  486. //// c.Data["json"] = lib.JSONS{Code: 202, Msg: "! U" + strconv.Itoa(r_admin.Id)}
  487. //// c.ServeJSON()
  488. //// return
  489. ////}
  490. NatsServer.Read_DeviceSensorParameter(T_SN)
  491. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  492. c.ServeJSON()
  493. return
  494. }
  495. //
  496. func (c *DeviceController) Pu_DeviceParameter() {
  497. // 验证登录
  498. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  499. if !b_ {
  500. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  501. c.ServeJSON()
  502. return
  503. }
  504. T_SN := c.GetString("SN")
  505. T_devName := c.GetString("T_devName")
  506. T_saveTime, _ := c.GetInt("T_saveTime")
  507. T_overrunSave, _ := c.GetInt("T_overrunSave")
  508. T_overrunAlarm, _ := c.GetInt("T_overrunAlarm")
  509. T_outageAlarm, _ := c.GetInt("T_outageAlarm")
  510. T_warningDelay, _ := c.GetInt("T_warningDelay")
  511. T_lostAlarm, _ := c.GetInt("T_lostAlarm")
  512. T_batteryLimit, _ := c.GetInt("T_batteryLimit")
  513. T_warningTime, _ := c.GetInt("T_warningTime")
  514. T_tempPre, _ := c.GetFloat("T_tempPre")
  515. T_humPre, _ := c.GetFloat("T_humPre")
  516. T_enwarning, _ := c.GetInt("T_enwarning")
  517. T_decTotal, _ := c.GetInt("T_decTotal")
  518. T_chDecTotal := c.GetString("T_chDecTotal")
  519. T_company := c.GetString("T_company")
  520. T_btname := c.GetString("T_btname")
  521. T_btserverID, _ := c.GetInt("T_btserverID")
  522. T_btchar, _ := c.GetInt("T_btchar")
  523. Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  524. if err != nil {
  525. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  526. c.ServeJSON()
  527. return
  528. }
  529. Admin_Power_, _ := Admin.Read_AdminPower_ById(admin_r.Admin_power)
  530. if Admin_Power_.Power_DeviceSensor_Compensate_e == 0 {
  531. c.Data["json"] = lib.JSONS{Code: 202, Msg: "! U" + strconv.Itoa(admin_r.Id)}
  532. c.ServeJSON()
  533. return
  534. }
  535. Deviceparameter := Device.DeviceParameter{
  536. T_uuid: admin_r.Admin_uuid,
  537. T_SendState: 0,
  538. T_State: 2,
  539. T_sn: T_SN,
  540. T_devName: T_devName,
  541. T_saveTime: T_saveTime,
  542. T_overrunSave: T_overrunSave,
  543. T_overrunAlarm: T_overrunAlarm,
  544. T_outageAlarm: T_outageAlarm,
  545. T_lostAlarm: T_lostAlarm,
  546. T_warningTime: T_warningTime,
  547. T_warningDelay: T_warningDelay,
  548. T_batteryLimit: T_batteryLimit,
  549. T_tempPre: float32(T_tempPre),
  550. T_humPre: float32(T_humPre),
  551. T_chDecTotal: T_chDecTotal,
  552. T_decTotal: strconv.Itoa(T_decTotal),
  553. T_enwarning: T_enwarning,
  554. T_company: T_company,
  555. T_btname: T_btname,
  556. T_btserverID: strconv.Itoa(T_btserverID),
  557. T_btchar: strconv.Itoa(T_btchar),
  558. }
  559. Deviceparameter, is := Device.Add_DeviceParameter(Deviceparameter)
  560. if !is {
  561. c.Data["json"] = lib.JSONS{Code: 203, Msg: "Add_DeviceParameter ERR"}
  562. c.ServeJSON()
  563. return
  564. }
  565. System.Add_UserLogs_T(admin_r.Admin_uuid, "设备管理", "设备参数操作", Deviceparameter)
  566. NatsServer.Pu_DeviceParameter(Deviceparameter)
  567. Device_r.T_devName = Deviceparameter.T_devName
  568. Device.Update_Device(Device_r, "T_devName")
  569. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  570. c.ServeJSON()
  571. return
  572. }
  573. //
  574. func (c *DeviceController) Pu_DeviceParameter_T_l_p() {
  575. // 验证登录
  576. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  577. if !b_ {
  578. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  579. c.ServeJSON()
  580. return
  581. }
  582. T_SN := c.GetString("SN")
  583. T_l_p, _ := c.GetInt("T_l_p")
  584. Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  585. if err != nil {
  586. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  587. c.ServeJSON()
  588. return
  589. }
  590. // // 1物流端 2药店端
  591. Device_r.T_l_p = T_l_p
  592. Device.Update_Device(Device_r, "T_l_p")
  593. Device.DeviceSensor_T_l_p_ALL(T_SN, T_l_p)
  594. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "设备参数操作", "类型选择(1物流端 2药店端):"+string(T_l_p))
  595. c.Data["json"] = lib.JSONS{Code: 222, Msg: "ok!"}
  596. c.ServeJSON()
  597. return
  598. }
  599. //
  600. func (c *DeviceController) Pu_DeviceParameter_T_give() {
  601. // 验证登录
  602. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  603. if !b_ {
  604. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  605. c.ServeJSON()
  606. return
  607. }
  608. T_SN := c.GetString("SN")
  609. T_give, _ := c.GetInt("T_give")
  610. Device_r, err := Device.Read_Device_ByT_sn(T_SN)
  611. if err != nil {
  612. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  613. c.ServeJSON()
  614. return
  615. }
  616. // // 1物流端 2药店端
  617. Device_r.T_give = T_give
  618. Device.Update_Device(Device_r, "T_give")
  619. Device.DeviceSensor_t_give_ALL(T_SN, T_give)
  620. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "设备丢弃参数操作", "设备丢弃( 0 丢弃 1 正常):"+string(T_give))
  621. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  622. c.ServeJSON()
  623. return
  624. }
  625. //
  626. //func (c *DeviceController) Pu_DeviceParameter_Sensor_() {
  627. // // 验证登录
  628. // b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  629. // if !b_ {
  630. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  631. // c.ServeJSON()
  632. // return
  633. // }
  634. //
  635. // T_SN := c.GetString("SN")
  636. // _, err := Device.Read_Device_ByT_sn(T_SN)
  637. // if err != nil {
  638. // c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  639. // c.ServeJSON()
  640. // return
  641. // }
  642. // Admin_Power_, _ := Admin.Read_AdminPower_ById(admin_r.Admin_power)
  643. // if Admin_Power_.Power_DeviceSensor_Compensate_e == 0 {
  644. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "! U" + strconv.Itoa(admin_r.Id)}
  645. // c.ServeJSON()
  646. // return
  647. // }
  648. //
  649. // T_sensor_list := c.GetString("T_sensor_list")
  650. // jsonStu_map_ := strings.Replace(string(T_sensor_list), "\"{", "{", -1)
  651. // jsonStu_map_ = strings.Replace(jsonStu_map_, "}\"", "}", -1)
  652. // jsonStu_map_ = strings.Replace(jsonStu_map_, "\\\"", "\"", -1)
  653. // println("jsonStu_map_:", jsonStu_map_)
  654. // var Rt_parameter_sensor_ []MqttServer.Rt_Parameter_Sensor_
  655. // err = json.Unmarshal([]byte(jsonStu_map_), &Rt_parameter_sensor_)
  656. // if err != nil {
  657. // c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_sensor_list Err!"}
  658. // c.ServeJSON()
  659. // return
  660. // }
  661. //
  662. // Handle.Pu_DeviceParameter_Sensor(admin_r.Admin_uuid, T_SN, Rt_parameter_sensor_)
  663. // System.Add_UserLogs(admin_r.Admin_uuid,"设备管理", "传感器参数操作", jsonStu_map_)
  664. //
  665. // DeviceSensor,is := Device.Read_DeviceSensor_ByT_sn(T_SN, Rt_parameter_sensor_[0].T_id)
  666. // if is {
  667. // DeviceSensor.T_name = Rt_parameter_sensor_[0].T_name
  668. // Device.Update_TDeviceSensor(DeviceSensor,"T_name")
  669. // }
  670. //
  671. //
  672. // c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  673. // c.ServeJSON()
  674. // return
  675. //
  676. //}
  677. //
  678. func (c *DeviceController) Pu_DeviceParameter_Sensor() {
  679. // 验证登录
  680. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  681. if !b_ {
  682. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  683. c.ServeJSON()
  684. return
  685. }
  686. T_SN := c.GetString("T_SN")
  687. T_name := c.GetString("T_name")
  688. T_id, _ := c.GetInt("T_id")
  689. T_Tlower, _ := c.GetFloat("T_Tlower")
  690. T_Tupper, _ := c.GetFloat("T_Tupper")
  691. T_RHlower, _ := c.GetFloat("T_RHlower")
  692. T_RHupper, _ := c.GetFloat("T_RHupper")
  693. T_en, _ := c.GetInt("T_en")
  694. T_free, _ := c.GetInt("T_free")
  695. // 权限 过滤
  696. Admin_Power_, _ := Admin.Read_AdminPower_ById(admin_r.Admin_power)
  697. if Admin_Power_.Power_DeviceSensor_Compensate_e == 0 {
  698. c.Data["json"] = lib.JSONS{Code: 202, Msg: "! U" + strconv.Itoa(admin_r.Id)}
  699. c.ServeJSON()
  700. return
  701. }
  702. _, err := Device.Read_Device_ByT_sn(T_SN)
  703. if err != nil {
  704. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  705. c.ServeJSON()
  706. return
  707. }
  708. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(T_SN, T_id)
  709. if !is {
  710. c.Data["json"] = lib.JSONS{Code: 208, Msg: "T_sn T_id Err!"}
  711. c.ServeJSON()
  712. return
  713. }
  714. Devicesensorparameter := Device.DeviceSensorParameter{
  715. T_sn: T_SN,
  716. T_id: T_id,
  717. T_name: T_name,
  718. T_Tlower: float32(T_Tlower),
  719. T_Tupper: float32(T_Tupper),
  720. T_RHlower: float32(T_RHlower),
  721. T_RHupper: float32(T_RHupper),
  722. T_en: T_en,
  723. T_free: T_free,
  724. T_uuid: admin_r.Admin_uuid,
  725. T_SendState: 0,
  726. T_State: 2,
  727. }
  728. dsp_id, is := Device.Add_DeviceSensorParameter(Devicesensorparameter)
  729. if !is {
  730. c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
  731. c.ServeJSON()
  732. return
  733. }
  734. Devicesensorparameter.Id = int(dsp_id)
  735. // 更新名称
  736. DeviceSensor.T_name = T_name
  737. Device.Update_DeviceSensor(DeviceSensor, "T_name")
  738. System.Add_UserLogs_T(admin_r.Admin_uuid, "设备管理", "传感器参数操作", Devicesensorparameter)
  739. NatsServer.Pu_DeviceParameter_Sensor(Devicesensorparameter)
  740. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  741. c.ServeJSON()
  742. return
  743. }
  744. ////
  745. //func (c *DeviceController) Pu_DeviceParameter_Sensor_Cache() {
  746. // // 验证登录
  747. // b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  748. // if !b_ {
  749. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  750. // c.ServeJSON()
  751. // return
  752. // }
  753. //
  754. // T_SN := c.GetString("SN")
  755. // T_id := c.GetString("T_id")
  756. // _, err := Device.Read_Device_ByT_sn(T_SN)
  757. // if err != nil {
  758. // c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  759. // c.ServeJSON()
  760. // return
  761. // }
  762. // Admin_Power_, _ := Admin.Read_AdminPower_ById(admin_r.Admin_power)
  763. // if Admin_Power_.Power_DeviceSensor_Compensate_e == 0 {
  764. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "! U" + strconv.Itoa(admin_r.Id)}
  765. // c.ServeJSON()
  766. // return
  767. // }
  768. //
  769. // T_sensor_list := c.GetString("T_sensor_list")
  770. // jsonStu_map_ := strings.Replace(string(T_sensor_list), "\"{", "{", -1)
  771. // jsonStu_map_ = strings.Replace(jsonStu_map_, "}\"", "}", -1)
  772. // jsonStu_map_ = strings.Replace(jsonStu_map_, "\\\"", "\"", -1)
  773. // println("jsonStu_map_:", jsonStu_map_)
  774. // var Rt_parameter_sensor_ []MqttServer.Rt_Parameter_Sensor_
  775. // err = json.Unmarshal([]byte(jsonStu_map_), &Rt_parameter_sensor_)
  776. // if err != nil {
  777. // c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_sensor_list Err!"}
  778. // c.ServeJSON()
  779. // return
  780. // }
  781. // DeviceSensor,is := Device.Read_DeviceSensor_ByT_sn(T_SN, Rt_parameter_sensor_[0].T_id)
  782. // if is {
  783. // DeviceSensor.T_name = Rt_parameter_sensor_[0].T_name
  784. // Device.Update_TDeviceSensor(DeviceSensor,"T_name")
  785. // }
  786. //
  787. // Device.Redis_DeviceSensorParameterCache_Set(T_SN+"_"+T_id,jsonStu_map_)
  788. // //Handle.Pu_DeviceParameter_Sensor(admin_r.Admin_uuid, T_SN, Rt_parameter_sensor_)
  789. // System.Add_UserLogs(admin_r.Admin_uuid,"设备管理", "传感器参数操作-离线操作", jsonStu_map_)
  790. // c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  791. // c.ServeJSON()
  792. // return
  793. //
  794. //}
  795. // 列表 -
  796. func (c *DeviceController) DeviceWarning_List_html() {
  797. // 验证登录
  798. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  799. if !b_ {
  800. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  801. c.ServeJSON()
  802. return
  803. }
  804. c.Data["Admin_r"] = admin_r
  805. page, _ := c.GetInt("page")
  806. println(page)
  807. if page < 1 {
  808. page = 1
  809. }
  810. c.Data["Admin_r"] = admin_r
  811. T_Name := c.GetString("T_Name")
  812. //T_sn := c.GetString("T_sn")
  813. Time_start := c.GetString("Time_start")
  814. Time_end := c.GetString("Time_end")
  815. if len(Time_start) == 0 && len(Time_end) == 0 {
  816. Time_start = time.Now().Format("2006-01-02") + " 00:00:00"
  817. Time_end = time.Now().Format("2006-01-02") + " 23:59:59"
  818. }
  819. c.Data["Time_start"] = Time_start
  820. c.Data["Time_end"] = Time_end
  821. c.Data["T_Name"] = T_Name
  822. //c.Data["Class_List"] = Device.Read_DeviceWarningList_All_1()
  823. //T_Title := ""
  824. //if Class_1 > 0 {
  825. // T_Title = Device.Read_DeviceWarningList_ById(Class_1).T_name
  826. //}
  827. var cnt int64
  828. DeviceWarning_List, cnt := Device.Read_DeviceWarning_1(admin_r, page, "", T_Name, Time_start, Time_end)
  829. //for i, v := range DeviceWarning_List {
  830. // DeviceWarning_List[i].T_State = 0
  831. // if strings.Contains(v.T_sn, "YD") {
  832. // DeviceWarning_List[i].T_State = 1
  833. // }
  834. //}
  835. c.Data["List"] = DeviceWarning_List
  836. page_size := math.Ceil(float64(cnt) / float64(conf.Page_size))
  837. c.Data["Page"] = page
  838. c.Data["Page_size"] = page_size
  839. c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
  840. c.Data["cnt"] = cnt
  841. c.TplName = "Device/DeviceWarning.html"
  842. }
  843. // 列表 -
  844. func (c *DeviceController) DeviceWarning_List() {
  845. // 验证登录
  846. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  847. if !b_ {
  848. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  849. c.ServeJSON()
  850. return
  851. }
  852. type R_JSONS struct {
  853. //必须的大写开头
  854. DeviceWarning []Device.DeviceWarning
  855. //DeviceWarningclass []Device.DeviceWarningList
  856. Num int
  857. Page int
  858. Page_size int
  859. Pages []lib.Page_T
  860. }
  861. var r_jsons R_JSONS
  862. page, _ := c.GetInt("page")
  863. println(page)
  864. if page < 1 {
  865. page = 1
  866. }
  867. T_sn := c.GetString("T_sn")
  868. //T_sn := c.GetString("Sn")
  869. //Class_1, _ := c.GetInt("Class_1")
  870. T_Name := c.GetString("T_Name")
  871. Time_start := c.GetString("Time_start")
  872. Time_end := c.GetString("Time_end")
  873. //r_jsons.DeviceWarningclass = Device.Read_DeviceWarningList_All_1()
  874. //T_Title := ""
  875. //if Class_1 > 0 {
  876. // T_Title = Device.Read_DeviceWarningList_ById(Class_1).T_name
  877. //}
  878. var cnt int64
  879. r_jsons.DeviceWarning, cnt = Device.Read_DeviceWarning_1(admin_r, page, T_sn, T_Name, Time_start, Time_end)
  880. page_size := math.Ceil(float64(cnt) / float64(conf.Page_size))
  881. r_jsons.Page = int(page)
  882. r_jsons.Page_size = int(page_size)
  883. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  884. r_jsons.Num = int(cnt)
  885. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  886. c.ServeJSON()
  887. return
  888. }
  889. // 列表 -
  890. func (c *DeviceController) DeviceWarning_Data_Excel() {
  891. // 验证登录
  892. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  893. if !b_ {
  894. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  895. c.ServeJSON()
  896. return
  897. }
  898. //T_sn := c.GetString("T_sn")
  899. Class_1, _ := c.GetInt("Class_1")
  900. T_Name := c.GetString("T_Name")
  901. Time_start := c.GetString("Time_start")
  902. Time_end := c.GetString("Time_end")
  903. c.Data["Class_1"] = Class_1
  904. if len(Time_start) == 0 && len(Time_end) == 0 {
  905. Time_start = time.Now().Format("2006-01-02") + " 00:00:00"
  906. Time_end = time.Now().Format("2006-01-02") + " 23:59:59"
  907. }
  908. //
  909. //T_Title := ""
  910. //if Class_1 > 0 {
  911. // T_Title = Device.Read_DeviceWarningList_ById(Class_1).T_name
  912. //}
  913. Device_data, _ := Device.Read_DeviceWarning_1(admin_r, 9999, "", T_Name, Time_start, Time_end)
  914. f := excelize.NewFile() // 设置单元格的值
  915. // 这里设置表头
  916. f.SetCellValue("Sheet1", "A1", "报警类型")
  917. f.SetCellValue("Sheet1", "B1", "Sn")
  918. f.SetCellValue("Sheet1", "C1", "设备名称")
  919. f.SetCellValue("Sheet1", "D1", "传感器")
  920. f.SetCellValue("Sheet1", "E1", "温度℃")
  921. f.SetCellValue("Sheet1", "F1", "湿度%")
  922. f.SetCellValue("Sheet1", "G1", "报警内容")
  923. f.SetCellValue("Sheet1", "H1", "记录时间")
  924. f.SetCellValue("Sheet1", "I1", "处理")
  925. f.SetCellValue("Sheet1", "J1", "处理时间")
  926. // 设置列宽
  927. f.SetColWidth("Sheet1", "A", "A", 20)
  928. f.SetColWidth("Sheet1", "B", "B", 25)
  929. f.SetColWidth("Sheet1", "C", "C", 30)
  930. f.SetColWidth("Sheet1", "D", "D", 30)
  931. f.SetColWidth("Sheet1", "E", "E", 10)
  932. f.SetColWidth("Sheet1", "F", "F", 10)
  933. f.SetColWidth("Sheet1", "G", "G", 30)
  934. f.SetColWidth("Sheet1", "H", "H", 15)
  935. f.SetColWidth("Sheet1", "I", "I", 30)
  936. f.SetColWidth("Sheet1", "J", "J", 15)
  937. line := 1
  938. // 循环写入数据
  939. for _, v := range Device_data {
  940. line++
  941. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_Title)
  942. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_sn+"["+strconv.Itoa(v.T_Id)+"]")
  943. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_Addr)
  944. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_Name)
  945. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_T)
  946. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_RH)
  947. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Remark)
  948. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_Ut)
  949. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), v.T_Text)
  950. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), v.UpdateTime)
  951. }
  952. timeStr := time.Now().Format("20060102150405")
  953. // 保存文件
  954. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  955. fmt.Println(err)
  956. }
  957. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  958. if !is {
  959. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  960. c.ServeJSON()
  961. return
  962. }
  963. //删除目录
  964. //err := os.Remove("ofile/" + timeStr + ".xlsx")
  965. //if err != nil {
  966. // fmt.Println(err)
  967. //}
  968. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  969. c.ServeJSON()
  970. return
  971. }
  972. func (c *DeviceController) DeviceWarning_() {
  973. id, _ := c.GetInt("id")
  974. c.Data["T"] = Device.Read_DeviceWarning_ById(id)
  975. c.Data["id"] = id
  976. c.TplName = "Device/DeviceWarning-.html"
  977. }
  978. func (c *DeviceController) DeviceWarning_log() {
  979. id, _ := c.GetInt("id")
  980. T_Log := Device.Read_DeviceWarning_ById(id).T_Log
  981. T_Log = strings.Replace(T_Log, "\n", "<hr>", -1)
  982. c.Data["T_Log"] = T_Log
  983. c.TplName = "Device/DeviceWarning-log.html"
  984. }
  985. func (c *DeviceController) DeviceWarning_M() {
  986. id := c.GetString("id")
  987. c.Data["id"] = id
  988. c.TplName = "Device/DeviceWarning-M.html"
  989. }
  990. func (c *DeviceController) DeviceWarning_Post() {
  991. // 验证登录
  992. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  993. if !b_ {
  994. c.Ctx.Redirect(302, "Login")
  995. return
  996. }
  997. Id, _ := c.GetInt("Id")
  998. T_Text := c.GetString("T_Text")
  999. t_c := Device.DeviceWarning{
  1000. Id: Id,
  1001. T_Text: T_Text,
  1002. }
  1003. Device.Update_DeviceWarning_ById(t_c)
  1004. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "报警处理操作", strconv.Itoa(Id)+"->"+T_Text)
  1005. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1006. c.ServeJSON()
  1007. return
  1008. }
  1009. func (c *DeviceController) DeviceWarning_Del() {
  1010. // 验证登录
  1011. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1012. if !b_ {
  1013. c.Ctx.Redirect(302, "Login")
  1014. return
  1015. }
  1016. Id, _ := c.GetInt("Id")
  1017. t_c := Device.DeviceWarning{
  1018. Id: Id,
  1019. }
  1020. Device.Update_DeviceWarning_Delete(t_c)
  1021. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "报警删除操作", strconv.Itoa(Id))
  1022. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1023. c.ServeJSON()
  1024. return
  1025. }
  1026. // 用户管理
  1027. func (c *DeviceController) DeviceBind_html() {
  1028. // 验证登录
  1029. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1030. if !b_ {
  1031. c.Ctx.Redirect(302, "Login")
  1032. return
  1033. }
  1034. c.Data["Admin_Power"], _ = Admin.Read_AdminPower_ById(admin_r.Admin_power)
  1035. page, _ := c.GetInt("page")
  1036. println(page)
  1037. if page < 1 {
  1038. page = 1
  1039. }
  1040. Name := c.GetString("Name")
  1041. c.Data["Name"] = Name
  1042. c.Data["Power_List"] = Admin.Read_Power_List_ALL_Power_Administration(0)
  1043. Adminpower, _ := Admin.Read_AdminPower_ById(admin_r.Admin_power)
  1044. var cnt int64
  1045. if Adminpower.Power_User_r != 1 {
  1046. c.Ctx.Redirect(302, "Login")
  1047. return
  1048. }
  1049. c.Data["List"], cnt = Admin.Read_Admin_List_ALL_User(page, Name, admin_r.Id)
  1050. fmt.Println("cnt:", cnt)
  1051. page_size := math.Ceil(float64(cnt) / float64(conf.Page_size))
  1052. c.Data["Page"] = page
  1053. c.Data["Page_size"] = page_size
  1054. c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
  1055. c.Data["cnt"] = cnt
  1056. c.TplName = "Device/DeviceBind.html"
  1057. }
  1058. func (c *DeviceController) DeviceBind_List() {
  1059. // 验证登录
  1060. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1061. if !b_ {
  1062. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1063. c.ServeJSON()
  1064. return
  1065. }
  1066. if len(c.GetString("Admin_uuid")) > 1 {
  1067. s_, user_r := Admin.Read_Admin_ByAdmin_uuid(c.GetString("Admin_uuid"))
  1068. if s_ != nil {
  1069. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Admin_uuid Err!"}
  1070. c.ServeJSON()
  1071. return
  1072. }
  1073. admin_r = user_r
  1074. }
  1075. type R_JSONS struct {
  1076. //必须的大写开头
  1077. Device_lite []Device.Device
  1078. Num int
  1079. Page int
  1080. Page_size int
  1081. Pages []lib.Page_T
  1082. }
  1083. var r_jsons R_JSONS
  1084. page, _ := c.GetInt("page")
  1085. println(page)
  1086. if page < 1 {
  1087. page = 1
  1088. }
  1089. page_z, _ := c.GetInt("page_z")
  1090. println(page_z)
  1091. if page_z == 0 {
  1092. page_z = conf.Page_size
  1093. }
  1094. Class_1 := c.GetString("Class_1")
  1095. Name := c.GetString("Name")
  1096. T_sn := c.GetString("T_sn")
  1097. T_monitor := c.GetString("T_monitor")
  1098. T_online := c.GetString("T_online")
  1099. //Class_1, _ := c.GetInt("Class_1")
  1100. //Name := c.GetString("Name")
  1101. //T_sn := c.GetString("T_sn")
  1102. //c.Data["Class_1"] = Class_1
  1103. ////c.Data["Name"] = Name
  1104. //c.Data["T_sn"] = T_sn
  1105. var cnt int64
  1106. Device_lite, cnt := Device.Read_Device_ALL_bind_1(admin_r, page, page_z, T_sn, Name, Class_1, T_monitor, T_online)
  1107. r_jsons.Device_lite = Device_lite
  1108. page_size := math.Ceil(float64(cnt) / float64(page_z))
  1109. r_jsons.Page = page
  1110. r_jsons.Page_size = int(page_size)
  1111. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  1112. r_jsons.Num = int(cnt)
  1113. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1114. c.ServeJSON()
  1115. return
  1116. }
  1117. func (c *DeviceController) DeviceBind_Sensor_List() {
  1118. // 验证登录
  1119. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1120. if !b_ {
  1121. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1122. c.ServeJSON()
  1123. return
  1124. }
  1125. if len(c.GetString("Admin_uuid")) > 1 {
  1126. s_, user_r := Admin.Read_Admin_ByAdmin_uuid(c.GetString("Admin_uuid"))
  1127. if s_ != nil {
  1128. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Admin_uuid Err!"}
  1129. c.ServeJSON()
  1130. return
  1131. }
  1132. admin_r = user_r
  1133. }
  1134. type R_JSONS struct {
  1135. //必须的大写开头
  1136. Device_lite []Device.R_Device
  1137. Num int
  1138. Page int
  1139. Page_size int
  1140. Pages []lib.Page_T
  1141. }
  1142. var r_jsons R_JSONS
  1143. page, _ := c.GetInt("page")
  1144. println(page)
  1145. if page < 1 {
  1146. page = 1
  1147. }
  1148. page_z, _ := c.GetInt("page_z")
  1149. println(page_z)
  1150. if page_z == 0 {
  1151. page_z = conf.Page_size
  1152. }
  1153. Class_1 := c.GetString("Class_1")
  1154. Name := c.GetString("Name")
  1155. T_sn := c.GetString("T_sn")
  1156. T_monitor := c.GetString("T_monitor")
  1157. T_online := c.GetString("T_online")
  1158. //Class_1, _ := c.GetInt("Class_1")
  1159. //Name := c.GetString("Name")
  1160. //T_sn := c.GetString("T_sn")
  1161. //c.Data["Class_1"] = Class_1
  1162. ////c.Data["Name"] = Name
  1163. //c.Data["T_sn"] = T_sn
  1164. var cnt int64
  1165. Device_lite, cnt := Device.Read_Device_ALL_bind_1(admin_r, page, page_z, T_sn, Name, Class_1, T_monitor, T_online)
  1166. for _, v := range Device_lite {
  1167. r_Device := Device.R_Device{}
  1168. r_Device.T_sn = v.T_sn
  1169. r_Device.T_MSISDN = v.T_MSISDN
  1170. r_Device.T_devName = v.T_devName
  1171. r_Device.T_give = v.T_give
  1172. r_Device.T_online = v.T_online
  1173. r_Device.T_monitor = v.T_monitor
  1174. r_Device.T_Dattery = v.T_Dattery
  1175. r_Device.T_sensor_list, r_Device.T_sensor = Device.Read_DeviceSensor_ByTsn(v.T_sn)
  1176. r_jsons.Device_lite = append(r_jsons.Device_lite, r_Device)
  1177. }
  1178. page_size := math.Ceil(float64(cnt) / float64(page_z))
  1179. r_jsons.Page = page
  1180. r_jsons.Page_size = int(page_size)
  1181. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  1182. r_jsons.Num = int(cnt)
  1183. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1184. c.ServeJSON()
  1185. return
  1186. }
  1187. func (c *DeviceController) DeviceBind_html_() {
  1188. // 验证登录
  1189. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1190. if !b_ {
  1191. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1192. c.ServeJSON()
  1193. return
  1194. }
  1195. c.TplName = "Device/DeviceBind-.html"
  1196. }
  1197. func (c *DeviceController) DeviceBind_Add_All() {
  1198. // 验证登录
  1199. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1200. if !b_ {
  1201. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1202. c.ServeJSON()
  1203. return
  1204. }
  1205. s_, user_r := Admin.Read_Admin_ByAdmin_uuid(c.GetString("Admin_uuid"))
  1206. if s_ != nil {
  1207. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Admin_uuid Err!"}
  1208. c.ServeJSON()
  1209. return
  1210. }
  1211. Name := c.GetString("Name")
  1212. T_sn := c.GetString("T_sn")
  1213. num_x := 0
  1214. Device_l := Device.Read_Device_ALL_T_sn_T_devName_bind_1(T_sn, Name, admin_r)
  1215. fmt.Println("len(Device_l):", len(Device_l))
  1216. for _, v := range Device_l {
  1217. Device_bind_list := Device.Read_Device_ALL_T_sn_bind_1(v.T_sn, user_r)
  1218. if len(Device_bind_list) > 0 {
  1219. continue
  1220. }
  1221. T_Bind := Device.Device_Bind_Add(v.T_sn, user_r)
  1222. Device.DeviceSensor_T_Bind_ALL(v.T_sn, T_Bind)
  1223. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "设备绑定", v.T_sn+"+>"+c.GetString("Admin_uuid"))
  1224. num_x = num_x + 1
  1225. }
  1226. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: num_x}
  1227. c.ServeJSON()
  1228. return
  1229. }
  1230. func (c *DeviceController) DeviceBind_Add() {
  1231. // 验证登录
  1232. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1233. if !b_ {
  1234. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1235. c.ServeJSON()
  1236. return
  1237. }
  1238. s_, user_r := Admin.Read_Admin_ByAdmin_uuid(c.GetString("Admin_uuid"))
  1239. if s_ != nil {
  1240. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Admin_uuid Err!"}
  1241. c.ServeJSON()
  1242. return
  1243. }
  1244. T_sn := c.GetString("T_sn")
  1245. if len(T_sn) < 5 {
  1246. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  1247. c.ServeJSON()
  1248. return
  1249. }
  1250. _, err := Device.Read_Device_ByT_sn(T_sn)
  1251. if err != nil {
  1252. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  1253. c.ServeJSON()
  1254. return
  1255. }
  1256. Device_bind_list := Device.Read_Device_ALL_T_sn_bind_1(T_sn, user_r)
  1257. if len(Device_bind_list) > 0 {
  1258. c.Data["json"] = lib.JSONS{Code: 203, Msg: "ok!"}
  1259. c.ServeJSON()
  1260. return
  1261. }
  1262. // 同步权限
  1263. T_Bind := Device.Device_Bind_Add(T_sn, user_r)
  1264. Device.DeviceSensor_T_Bind_ALL(T_sn, T_Bind)
  1265. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "设备绑定", T_sn+"+>"+c.GetString("Admin_uuid"))
  1266. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1267. c.ServeJSON()
  1268. return
  1269. }
  1270. func (c *DeviceController) DeviceBind_Del() {
  1271. // 验证登录
  1272. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1273. if !b_ {
  1274. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1275. c.ServeJSON()
  1276. return
  1277. }
  1278. s_, user_r := Admin.Read_Admin_ByAdmin_uuid(c.GetString("Admin_uuid"))
  1279. if s_ != nil {
  1280. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Admin_uuid Err!"}
  1281. c.ServeJSON()
  1282. return
  1283. }
  1284. T_sn := c.GetString("T_sn")
  1285. if len(T_sn) < 5 {
  1286. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  1287. c.ServeJSON()
  1288. return
  1289. }
  1290. _, err := Device.Read_Device_ByT_sn(T_sn)
  1291. if err != nil {
  1292. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  1293. c.ServeJSON()
  1294. return
  1295. }
  1296. Device_bind_list := Device.Read_Device_ALL_T_sn_bind_1(T_sn, user_r)
  1297. if len(Device_bind_list) == 0 {
  1298. c.Data["json"] = lib.JSONS{Code: 204, Msg: "ok!"}
  1299. c.ServeJSON()
  1300. return
  1301. }
  1302. // 同步权限
  1303. T_Bind := Device.Device_Bind_Del(T_sn, user_r)
  1304. Device.DeviceSensor_T_Bind_ALL(T_sn, T_Bind)
  1305. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "设备解除绑定", T_sn+"->"+c.GetString("Admin_uuid"))
  1306. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1307. c.ServeJSON()
  1308. return
  1309. }
  1310. // 设备分类
  1311. func (c *DeviceController) DeviceClass_html() {
  1312. // 验证登录
  1313. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1314. if !b_ {
  1315. c.Ctx.Redirect(302, "Login")
  1316. return
  1317. }
  1318. page, _ := c.GetInt("page")
  1319. println(page)
  1320. if page < 1 {
  1321. page = 1
  1322. }
  1323. Name := c.GetString("Name")
  1324. c.Data["Name"] = Name
  1325. var cnt int64
  1326. c.Data["List"], cnt = Device.Read_DeviceClass_ALL_1(admin_r.Admin_uuid, page, Name)
  1327. fmt.Println("cnt:", cnt)
  1328. page_size := math.Ceil(float64(cnt) / float64(conf.Page_size))
  1329. c.Data["Page"] = page
  1330. c.Data["Page_size"] = page_size
  1331. c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
  1332. c.Data["cnt"] = cnt
  1333. c.TplName = "Device/DeviceClass.html"
  1334. }
  1335. func (c *DeviceController) DeviceClass_() {
  1336. id, _ := c.GetInt("id")
  1337. c.Data["T"], _ = Device.Read_Class_ById(id)
  1338. c.Data["id"] = id
  1339. c.TplName = "Device/DeviceClass-.html"
  1340. }
  1341. func (c *DeviceController) DeviceClass_Post() {
  1342. // 验证登录
  1343. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1344. if !b_ {
  1345. c.Ctx.Redirect(302, "Login")
  1346. return
  1347. }
  1348. Id, _ := c.GetInt("Id")
  1349. T_name := c.GetString("T_name")
  1350. T_Notice_wx := c.GetString("T_Notice_wx")
  1351. T_Notice_wx2 := c.GetString("T_Notice_wx2")
  1352. T_Notice_phone := c.GetString("T_Notice_phone")
  1353. T_Notice_message := c.GetString("T_Notice_message")
  1354. T_Notice_mailbox := c.GetString("T_Notice_mailbox")
  1355. T_Notice_mechanism := c.GetString("T_Notice_mechanism")
  1356. t_c := Device.DeviceClass{
  1357. Id: Id,
  1358. T_name: T_name,
  1359. T_Notice_wx: T_Notice_wx,
  1360. T_Notice_wx2: T_Notice_wx2,
  1361. T_Notice_phone: T_Notice_phone,
  1362. T_Notice_message: T_Notice_message,
  1363. T_Notice_mailbox: T_Notice_mailbox,
  1364. T_Notice_mechanism: T_Notice_mechanism,
  1365. }
  1366. if Id == 0 {
  1367. t_c.T_State = 1
  1368. t_c.T_uuid = admin_r.Admin_uuid
  1369. t_c.T_Notice_wx = ""
  1370. t_c.T_Notice_wx2 = ""
  1371. t_c.T_Notice_phone = ""
  1372. t_c.T_Notice_message = ""
  1373. t_c.T_Notice_mailbox = ""
  1374. t_c.T_Notice_mechanism = Device.Read_DeviceWarningList_All_T_Notice_mechanism()
  1375. Device.Add_Class(t_c)
  1376. } else {
  1377. t_c.T_uuid = admin_r.Admin_uuid
  1378. Device.Update_Class_ById(t_c)
  1379. }
  1380. System.Add_UserLogs_T(admin_r.Admin_uuid, "设备管理", "分类操作", t_c)
  1381. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1382. c.ServeJSON()
  1383. return
  1384. }
  1385. func (c *DeviceController) DeviceWarningList_T_Tips() {
  1386. // 验证登录
  1387. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1388. if !b_ {
  1389. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1390. c.ServeJSON()
  1391. return
  1392. }
  1393. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Device.Read_DeviceWarningList_All_T_Notice_mechanism()}
  1394. c.ServeJSON()
  1395. return
  1396. }
  1397. func (c *DeviceController) DeviceClass_Del() {
  1398. // 验证登录
  1399. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1400. if !b_ {
  1401. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1402. c.ServeJSON()
  1403. return
  1404. }
  1405. id, _ := c.GetInt("Id")
  1406. class_r, err := Device.Read_Class_ById(id)
  1407. if err != nil {
  1408. c.Data["json"] = lib.JSONS{Code: 202, Msg: "id Err!"}
  1409. c.ServeJSON()
  1410. return
  1411. }
  1412. if class_r.T_uuid != admin_r.Admin_uuid {
  1413. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_uuid Err!"}
  1414. c.ServeJSON()
  1415. return
  1416. }
  1417. Device.Delete_Class_ById(id)
  1418. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_class_1(id)
  1419. for _, v := range DeviceSensor_class_list {
  1420. Device.DeviceSensor_Bind_Del(v.T_sn, v.T_id, id)
  1421. }
  1422. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "删除分类操作", strconv.Itoa(id))
  1423. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1424. c.ServeJSON()
  1425. return
  1426. }
  1427. func (c *DeviceController) DeviceClass_Syn() {
  1428. // 验证登录
  1429. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1430. if !b_ {
  1431. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1432. c.ServeJSON()
  1433. return
  1434. }
  1435. s_, user_r := Admin.Read_Admin_ByAdmin_uuid(c.GetString("Admin_uuid"))
  1436. if s_ != nil {
  1437. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Admin_uuid Err!"}
  1438. c.ServeJSON()
  1439. return
  1440. }
  1441. // 先全部 清掉
  1442. Device.Device_Bind_ALL_Del(user_r)
  1443. class_r := Device.Read_DeviceClass_ALL_T_uuid_1(user_r.Admin_uuid)
  1444. for _, v := range class_r {
  1445. fmt.Println("清掉:", v.Id)
  1446. // 删除分类
  1447. Device.Delete_Class_ById(v.Id)
  1448. // 删除 传感器分类
  1449. Device.DeviceSensor_T_Calss_ALL_Del(v.Id)
  1450. //Device.DeviceSensor_T_Bind_ALL_Del(user_r)
  1451. }
  1452. // 重新插入 分类
  1453. num_x := 0
  1454. class_r = Device.Read_DeviceClass_ALL_T_uuid_1(admin_r.Admin_uuid)
  1455. for _, v := range class_r {
  1456. fmt.Println("插入:", v.Id)
  1457. t_c := Device.DeviceClass{
  1458. T_uuid: user_r.Admin_uuid,
  1459. T_name: v.T_name,
  1460. T_Notice_wx: "",
  1461. T_Notice_phone: "",
  1462. T_Notice_message: "",
  1463. T_Notice_mailbox: "",
  1464. T_State: 1,
  1465. }
  1466. num_x = num_x + 1
  1467. // 添加分类
  1468. cid, err := Device.Add_Class(t_c)
  1469. if err != nil {
  1470. c.Data["json"] = lib.JSONS{Code: 200, Msg: "Add_Class E!"}
  1471. c.ServeJSON()
  1472. return
  1473. }
  1474. fmt.Println("cid:", cid)
  1475. // 添加 传感器分类
  1476. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_class_1(int(v.Id))
  1477. for _, v_c := range DeviceSensor_class_list {
  1478. // 绑定所有分类
  1479. Device.DeviceSensor_T_Calss_Add(v_c.T_sn, v_c.T_id, int(cid))
  1480. // 绑定所有权限
  1481. //Device_r, _ := Device.Read_Device_ByT_sn(v_c.T_sn)
  1482. //Device.DeviceSensor_T_Bind_Add(v_c.T_sn, v_c.T_id, user_r)
  1483. T_Bind := Device.Device_Bind_Add(v_c.T_sn, user_r)
  1484. Device.DeviceSensor_T_Bind_ALL(v_c.T_sn, T_Bind)
  1485. }
  1486. //
  1487. }
  1488. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "同步分类操作", "->"+user_r.Admin_uuid)
  1489. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: num_x}
  1490. c.ServeJSON()
  1491. return
  1492. }
  1493. func (c *DeviceController) DeviceClassBind_html_() {
  1494. // 验证登录
  1495. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1496. if !b_ {
  1497. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1498. c.ServeJSON()
  1499. return
  1500. }
  1501. c.TplName = "Device/DeviceClassBind-.html"
  1502. }
  1503. func (c *DeviceController) DeviceClassBind_List() {
  1504. // 验证登录
  1505. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1506. if !b_ {
  1507. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1508. c.ServeJSON()
  1509. return
  1510. }
  1511. type R_JSONS struct {
  1512. //必须的大写开头
  1513. DeviceSensor_lite []Device.DeviceSensor_R
  1514. Num int
  1515. Page int
  1516. Page_size int
  1517. Pages []lib.Page_T
  1518. }
  1519. var r_jsons R_JSONS
  1520. page, _ := c.GetInt("page")
  1521. println(page)
  1522. if page < 1 {
  1523. page = 1
  1524. }
  1525. Name := c.GetString("Name")
  1526. T_sn := c.GetString("T_sn")
  1527. T_calss_id, _ := c.GetInt("T_calss_id")
  1528. page_z, _ := c.GetInt("page_z")
  1529. if page_z == 0 {
  1530. page_z = conf.Page_size
  1531. }
  1532. //Name := c.GetString("Name")
  1533. //T_sn := c.GetString("T_sn")
  1534. //c.Data["Class_1"] = Class_1
  1535. ////c.Data["Name"] = Name
  1536. //c.Data["T_sn"] = T_sn
  1537. R_DeviceClass, err := Device.Read_Class_ById(T_calss_id)
  1538. if err != nil {
  1539. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1540. c.ServeJSON()
  1541. return
  1542. }
  1543. if R_DeviceClass.T_uuid != admin_r.Admin_uuid {
  1544. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id != Admin_uuid Err!"}
  1545. c.ServeJSON()
  1546. return
  1547. }
  1548. var cnt int64
  1549. Device_lite, cnt := Device.Read_DeviceSensor_class_ALL_1(admin_r, T_calss_id, page, page_z, T_sn, Name, "")
  1550. r_jsons.DeviceSensor_lite = Device_lite
  1551. page_size := math.Ceil(float64(cnt) / float64(page_z))
  1552. r_jsons.Page = page
  1553. r_jsons.Page_size = int(page_size)
  1554. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  1555. r_jsons.Num = int(cnt)
  1556. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1557. c.ServeJSON()
  1558. return
  1559. }
  1560. func (c *DeviceController) DeviceClassBind_Add() {
  1561. // 验证登录
  1562. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1563. if !b_ {
  1564. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1565. c.ServeJSON()
  1566. return
  1567. }
  1568. T_class_id, _ := c.GetInt("T_class_id")
  1569. T_sn := c.GetString("T_sn")
  1570. T_id, _ := c.GetInt("T_id")
  1571. if T_class_id == 0 || T_id == 0 || len(T_sn) == 0 {
  1572. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1573. c.ServeJSON()
  1574. return
  1575. }
  1576. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1577. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  1578. c.ServeJSON()
  1579. return
  1580. }
  1581. R_DeviceClass, err := Device.Read_Class_ById(T_class_id)
  1582. if err != nil {
  1583. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1584. c.ServeJSON()
  1585. return
  1586. }
  1587. if R_DeviceClass.T_uuid != admin_r.Admin_uuid {
  1588. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id != Admin_uuid Err!"}
  1589. c.ServeJSON()
  1590. return
  1591. }
  1592. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_class_1(T_sn, T_id, T_class_id)
  1593. if len(DeviceSensor_class_list) > 0 {
  1594. c.Data["json"] = lib.JSONS{Code: 203, Msg: "ok!"}
  1595. c.ServeJSON()
  1596. return
  1597. }
  1598. Device.DeviceSensor_T_Calss_Add(T_sn, T_id, T_class_id)
  1599. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "分类绑定操作", strconv.Itoa(T_class_id)+"+>"+T_sn+"-"+strconv.Itoa(T_id))
  1600. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1601. c.ServeJSON()
  1602. return
  1603. }
  1604. func (c *DeviceController) DeviceClassBind_Del() {
  1605. // 验证登录
  1606. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1607. if !b_ {
  1608. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1609. c.ServeJSON()
  1610. return
  1611. }
  1612. T_class_id, _ := c.GetInt("T_class_id")
  1613. T_sn := c.GetString("T_sn")
  1614. T_id, _ := c.GetInt("T_id")
  1615. if T_class_id == 0 || T_id == 0 || len(T_sn) == 0 {
  1616. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1617. c.ServeJSON()
  1618. return
  1619. }
  1620. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1621. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  1622. c.ServeJSON()
  1623. return
  1624. }
  1625. R_DeviceClass, err := Device.Read_Class_ById(T_class_id)
  1626. if err != nil {
  1627. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1628. c.ServeJSON()
  1629. return
  1630. }
  1631. if R_DeviceClass.T_uuid != admin_r.Admin_uuid {
  1632. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id != Admin_uuid Err!"}
  1633. c.ServeJSON()
  1634. return
  1635. }
  1636. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_class_1(T_sn, T_id, T_class_id)
  1637. if len(DeviceSensor_class_list) == 0 {
  1638. c.Data["json"] = lib.JSONS{Code: 203, Msg: "ok!"}
  1639. c.ServeJSON()
  1640. return
  1641. }
  1642. Device.DeviceSensor_Bind_Del(T_sn, T_id, T_class_id)
  1643. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "分类删除绑定操作", strconv.Itoa(T_class_id)+"->"+T_sn+"-"+strconv.Itoa(T_id))
  1644. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1645. c.ServeJSON()
  1646. return
  1647. }
  1648. func (c *DeviceController) Get_DeviceClassId() {
  1649. // 验证登录
  1650. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1651. if !b_ {
  1652. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1653. c.ServeJSON()
  1654. return
  1655. }
  1656. T_class_id, _ := c.GetInt("T_class_id")
  1657. R_DeviceClass, err := Device.Read_Class_ById(T_class_id)
  1658. if err != nil {
  1659. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_class_id Err!"}
  1660. c.ServeJSON()
  1661. return
  1662. }
  1663. if R_DeviceClass.T_uuid != admin_r.Admin_uuid {
  1664. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class_id != T_uuid Err!"}
  1665. c.ServeJSON()
  1666. return
  1667. }
  1668. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: R_DeviceClass}
  1669. c.ServeJSON()
  1670. return
  1671. }
  1672. // 任务列表
  1673. func (c *DeviceController) DeviceTask_List() {
  1674. // 验证登录
  1675. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1676. if !b_ {
  1677. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1678. c.ServeJSON()
  1679. return
  1680. }
  1681. T_sn := c.GetString("T_sn")
  1682. Time_end := c.GetString("Time_end")
  1683. Time_start := c.GetString("Time_start")
  1684. DeviceTask_lite, _ := Device.Read_DeviceTask_All_50(T_sn, Time_end, Time_start)
  1685. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceTask_lite}
  1686. c.ServeJSON()
  1687. return
  1688. }
  1689. // 任务列表
  1690. func (c *DeviceController) DeviceTask_Post() {
  1691. // 验证登录
  1692. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1693. if !b_ {
  1694. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1695. c.ServeJSON()
  1696. return
  1697. }
  1698. T_sn := c.GetString("T_sn")
  1699. T_task := c.GetString("T_task")
  1700. NatsServer.Set_DeviceTask(Device.Device_task{
  1701. T_sn: T_sn,
  1702. T_task: T_task,
  1703. })
  1704. System.Add_UserLogs(admin_r.Admin_uuid, "设备管理", "远程启停", T_sn+"-"+T_task)
  1705. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1706. c.ServeJSON()
  1707. return
  1708. }
  1709. func (c *DeviceController) Get_DeviceClassId_QRCode() {
  1710. // 验证登录
  1711. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1712. if !b_ {
  1713. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1714. c.ServeJSON()
  1715. return
  1716. }
  1717. T_calss_id := c.GetString("T_class_id")
  1718. if len(T_calss_id) == 0 {
  1719. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_calss_id Err!"}
  1720. c.ServeJSON()
  1721. return
  1722. }
  1723. T_calss_id_str, _ := NatsServer.Wx_GenerateQRCode("-|" + lib.AesEncryptCBC(T_calss_id, "0123456789012345") + "|- @宝智达 微信公众号通知")
  1724. if len(T_calss_id_str) == 0 {
  1725. c.Data["json"] = lib.JSONS{Code: 201, Msg: "Err!"}
  1726. c.ServeJSON()
  1727. return
  1728. }
  1729. type Text struct {
  1730. QR string
  1731. Code string
  1732. }
  1733. T_calss_id_str = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + T_calss_id_str
  1734. Text_r := Text{
  1735. QR: T_calss_id_str,
  1736. Code: "请将本内容发送到 深圳市宝智达科技有限公司 微信公众号-|" + lib.AesEncryptCBC(T_calss_id, "0123456789012345") + "|- @宝智达 微信公众号通知 ",
  1737. }
  1738. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Text_r}
  1739. c.ServeJSON()
  1740. return
  1741. }
  1742. // 列表 - 设备日志
  1743. func (c *DeviceController) DeviceLogs() {
  1744. page, _ := c.GetInt("page")
  1745. println(page)
  1746. if page < 1 {
  1747. page = 1
  1748. }
  1749. T_sn := c.GetString("T_sn")
  1750. c.Data["T_sn"] = T_sn
  1751. var cnt int64
  1752. c.Data["List"], cnt = System.Read_DeviceLogs_ALL(page, T_sn)
  1753. page_size := math.Ceil(float64(cnt) / float64(conf.Page_size))
  1754. c.Data["Page"] = page
  1755. c.Data["Page_size"] = page_size
  1756. c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
  1757. c.Data["cnt"] = cnt
  1758. c.TplName = "Device/DeviceLogs.html"
  1759. }