Device.go 56 KB

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