Device.go 57 KB

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