Device.go 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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_CompanyNotice_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. 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)
  493. r_jsons.Page = page
  494. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  495. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  496. c.ServeJSON()
  497. return
  498. }
  499. // 修改传感器
  500. func (c *DeviceController) DeviceSensor_Edit() {
  501. // 验证登录
  502. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  503. if !b_ {
  504. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey!"}
  505. c.ServeJSON()
  506. return
  507. }
  508. Sn := c.GetString("T_sn")
  509. Id, _ := c.GetInt("T_id")
  510. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  511. if !is {
  512. c.Data["json"] = lib.JSONS{Code: 203, Msg: "信息错误!"}
  513. c.ServeJSON()
  514. return
  515. }
  516. // 实时数据显示排序
  517. if v, err := c.GetInt("T_sort"); err == nil {
  518. DeviceSensor.T_sort = v
  519. Device.Update_DeviceSensor(DeviceSensor, "T_sort")
  520. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 排序操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(v))
  521. }
  522. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  523. c.ServeJSON()
  524. return
  525. }
  526. // 删除传感器
  527. func (c *DeviceController) DeviceSensor_Del() {
  528. // 验证登录
  529. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  530. if !b_ {
  531. c.Ctx.Redirect(302, "Login")
  532. return
  533. }
  534. Sn := c.GetString("T_sn")
  535. Id, _ := c.GetInt("T_id")
  536. _, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  537. if !is {
  538. c.Data["json"] = lib.JSONS{Code: 205, Msg: "信息错误!"}
  539. c.ServeJSON()
  540. return
  541. }
  542. // 0默认:从数据库删除 1:从设备删除
  543. T_mode, _ := c.GetInt("T_mode")
  544. if T_mode == 1 {
  545. Device.Delete_DeviceSensor_ById(Sn, Id)
  546. NatsServer.Del_DeviceSensor(Device.DeviceSensor_Del{
  547. T_sn: Sn, T_id: Id,
  548. })
  549. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
  550. } else {
  551. Device.Delete_DeviceSensor_ById(Sn, Id)
  552. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 删除操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]")
  553. }
  554. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  555. c.ServeJSON()
  556. return
  557. }
  558. // 传感器参数列表
  559. func (c *DeviceController) DeviceSensor_Parameter_List() {
  560. // 验证登录
  561. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  562. if !b_ {
  563. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  564. c.ServeJSON()
  565. return
  566. }
  567. Sn := c.GetString("T_sn")
  568. Id, _ := c.GetInt("T_id")
  569. //c.Data["DeviceSensor"], _ = Device.Read_DeviceSensor_ByTsn_Tid(Sn, Id)
  570. DeviceSensorParameter_lite := Device.Read_DeviceSensorParameter_List(Sn, Id, 2, 10)
  571. if len(DeviceSensorParameter_lite) == 0 {
  572. NatsServer.Read_DeviceSensorParameter(Sn)
  573. c.Data["json"] = lib.JSONS{Code: 203, Msg: "未同步参数,请检查设备是否正常!"}
  574. c.ServeJSON()
  575. return
  576. }
  577. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceSensorParameter_lite}
  578. c.ServeJSON()
  579. return
  580. }
  581. // 修改传感器参数
  582. func (c *DeviceController) DeviceSensor_Parameter_Pu() {
  583. // 验证登录
  584. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  585. if !b_ {
  586. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  587. c.ServeJSON()
  588. return
  589. }
  590. T_SN := c.GetString("T_sn")
  591. T_name := c.GetString("T_name")
  592. T_id, _ := c.GetInt("T_id")
  593. T_Tlower, _ := c.GetFloat("T_Tlower")
  594. T_Tupper, _ := c.GetFloat("T_Tupper")
  595. T_RHlower, _ := c.GetFloat("T_RHlower")
  596. T_RHupper, _ := c.GetFloat("T_RHupper")
  597. T_en, _ := c.GetInt("T_en")
  598. T_free, _ := c.GetInt("T_free")
  599. T_enprel, _ := c.GetInt("T_enprel")
  600. T_tprel, _ := c.GetFloat("T_tprel")
  601. T_tpreu, _ := c.GetFloat("T_tpreu")
  602. T_hprel, _ := c.GetFloat("T_hprel")
  603. T_hpreu, _ := c.GetFloat("T_hpreu")
  604. _, err := Device.Read_Device_ByT_sn(T_SN)
  605. if err != nil {
  606. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_sn Err!"}
  607. c.ServeJSON()
  608. return
  609. }
  610. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(T_SN, T_id)
  611. if !is {
  612. c.Data["json"] = lib.JSONS{Code: 208, Msg: "T_sn T_id Err!"}
  613. c.ServeJSON()
  614. return
  615. }
  616. Devicesensorparameter := Device.DeviceSensorParameter{
  617. T_sn: T_SN,
  618. T_id: T_id,
  619. T_Tlower: float32(T_Tlower),
  620. T_Tupper: float32(T_Tupper),
  621. T_RHlower: float32(T_RHlower),
  622. T_RHupper: float32(T_RHupper),
  623. T_en: T_en,
  624. T_free: T_free,
  625. T_enprel: T_enprel,
  626. T_tprel: float32(T_tprel),
  627. T_tpreu: float32(T_tpreu),
  628. T_hprel: float32(T_hprel),
  629. T_hpreu: float32(T_hpreu),
  630. T_uuid: admin_r.T_uuid,
  631. T_SendState: 0,
  632. T_State: 2,
  633. }
  634. dsp_id, is := Device.Add_DeviceSensorParameter(Devicesensorparameter)
  635. if !is {
  636. c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
  637. c.ServeJSON()
  638. return
  639. }
  640. Devicesensorparameter.Id = int(dsp_id)
  641. // 更新名称
  642. DeviceSensor.T_name = T_name
  643. Device.Update_DeviceSensor(DeviceSensor, "T_name")
  644. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器参数操作", Devicesensorparameter)
  645. NatsServer.Pu_DeviceParameter_Sensor(Devicesensorparameter)
  646. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  647. c.ServeJSON()
  648. return
  649. }
  650. // 传感器 实时数据显示排序
  651. // fixme 没有检查
  652. func (c *DeviceController) UpDeviceSensor_Tsort() {
  653. // 验证登录
  654. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  655. if !b_ {
  656. c.Ctx.Redirect(302, "Login")
  657. return
  658. }
  659. Sn := c.GetString("Sn")
  660. Id, _ := c.GetInt("Id")
  661. T_sort, _ := c.GetInt("T_sort")
  662. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  663. if !is {
  664. c.Data["json"] = lib.JSONS{Code: 201, Msg: "信息错误!"}
  665. c.ServeJSON()
  666. return
  667. }
  668. DeviceSensor.T_sort = T_sort
  669. //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
  670. Device.Update_DeviceSensor(DeviceSensor, "T_sort")
  671. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 排序操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(T_sort))
  672. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  673. c.ServeJSON()
  674. return
  675. }
  676. // 传感器 3D视图
  677. // fixme 没有检查
  678. func (c *DeviceController) UpDeviceSensor_T3dview() {
  679. // 验证登录
  680. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  681. if !b_ {
  682. c.Ctx.Redirect(302, "Login")
  683. return
  684. }
  685. Sn := c.GetString("Sn")
  686. Id, _ := c.GetInt("Id")
  687. T_3dview := c.GetString("T_3dview")
  688. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  689. if !is {
  690. c.Data["json"] = lib.JSONS{Code: 201, Msg: "信息错误!"}
  691. c.ServeJSON()
  692. return
  693. }
  694. DeviceSensor.T_3dview = T_3dview
  695. //c.Data["Device_lite"] = Device.Read_DeviceParameter_SN(Sn)
  696. Device.Update_DeviceSensor(DeviceSensor, "T_3dview")
  697. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "3D 视图ID操作", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+T_3dview)
  698. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  699. c.ServeJSON()
  700. return
  701. }
  702. // 传感器 屏蔽数据展示 0 屏蔽数据展示 1 正常数据展示
  703. // fixme 没有检查
  704. func (c *DeviceController) UpDeviceSensor_T_datashow() {
  705. // 验证登录
  706. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  707. if !b_ {
  708. c.Ctx.Redirect(302, "Login")
  709. return
  710. }
  711. Sn := c.GetString("Sn")
  712. Id, _ := c.GetInt("Id")
  713. T_datashow, _ := c.GetInt("T_datashow")
  714. DeviceSensor, is := Device.Read_DeviceSensor_ByT_sn(Sn, Id)
  715. if !is {
  716. c.Data["json"] = lib.JSONS{Code: 201, Msg: "信息错误!"}
  717. c.ServeJSON()
  718. return
  719. }
  720. DeviceSensor.T_datashow = T_datashow
  721. Device.Update_DeviceSensor(DeviceSensor, "T_datashow")
  722. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "传感器 数据展示(0 屏蔽数据展示 1 正常数据展示)", "SN:"+Sn+" ["+strconv.Itoa(Id)+"]"+strconv.Itoa(T_datashow))
  723. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK"}
  724. c.ServeJSON()
  725. return
  726. }
  727. // 设备任务 ------------------------------------------
  728. // 任务列表
  729. func (c *DeviceController) DeviceTask_List() {
  730. // 验证登录
  731. b_, _ := 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.DeviceTask
  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_sn := c.GetString("T_sn")
  755. Time_start := c.GetString("Time_start")
  756. Time_end := c.GetString("Time_end")
  757. if len(T_sn) < 6 {
  758. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_sn!!!"}
  759. c.ServeJSON()
  760. return
  761. }
  762. r_jsons.Data, r_jsons.Num = Device.Read_DeviceTask_All(T_sn, Time_start, Time_end, page, page_z)
  763. r_jsons.Page = page
  764. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  765. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  766. c.ServeJSON()
  767. return
  768. }
  769. // 任务启停
  770. func (c *DeviceController) DeviceTask_Post() {
  771. // 验证登录
  772. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  773. if !b_ {
  774. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  775. c.ServeJSON()
  776. return
  777. }
  778. T_sn := c.GetString("T_sn")
  779. T_task := c.GetString("T_task")
  780. NatsServer.Set_DeviceTask(Device.Device_task{
  781. T_sn: T_sn,
  782. T_task: T_task,
  783. })
  784. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "远程启停", T_sn+"-"+T_task)
  785. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  786. c.ServeJSON()
  787. return
  788. }
  789. // 设备告警 ------------------------------------------
  790. // 告警列表
  791. func (c *DeviceController) DeviceWarning_List() {
  792. // 验证登录
  793. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  794. if !b_ {
  795. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  796. c.ServeJSON()
  797. return
  798. }
  799. type R_JSONS struct {
  800. //必须的大写开头
  801. Data []Warning.Warning_R
  802. Num int64
  803. Page int
  804. Page_size int
  805. }
  806. var r_jsons R_JSONS
  807. page, _ := c.GetInt("page")
  808. println(page)
  809. if page < 1 {
  810. page = 1
  811. }
  812. page_z, _ := c.GetInt("page_z")
  813. if page_z < 1 {
  814. page_z = conf.Page_size
  815. }
  816. T_name := c.GetString("T_name")
  817. T_tp := c.GetString("T_tp")
  818. Time_start := c.GetString("Time_start")
  819. Time_end := c.GetString("Time_end")
  820. T_handle, _ := c.GetInt("T_handle")
  821. T_admin, _ := c.GetInt("T_admin")
  822. T_history, _ := c.GetInt("T_history")
  823. T_pid := admin_r.T_pid
  824. if T_pid == 0 {
  825. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  826. }
  827. var T_year, T_month string
  828. if T_history == 1 {
  829. date, err := time.Parse("2006-01-02 15:04:05", Time_start)
  830. if err != nil {
  831. c.Data["json"] = lib.JSONS{Code: 200, Msg: "时间格式错误"}
  832. c.ServeJSON()
  833. return
  834. }
  835. T_year, T_month = date.Format("2006"), date.Format("01")
  836. }
  837. Warning.Read_WarningType_All_Maps()
  838. if T_admin == 1 {
  839. if T_history == 1 {
  840. // 获取备份
  841. 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)
  842. } else {
  843. // 获取最新
  844. 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)
  845. }
  846. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  847. c.ServeJSON()
  848. return
  849. }
  850. if T_history == 1 {
  851. // 获取备份
  852. 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)
  853. } else {
  854. // 获取最新
  855. r_jsons.Data, r_jsons.Num = Warning.Read_Warning(T_pid, T_tp, T_name, T_handle, Time_start, Time_end, page, page_z)
  856. }
  857. r_jsons.Page = page
  858. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  859. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  860. c.ServeJSON()
  861. return
  862. }
  863. // 查询告警
  864. func (c *DeviceController) DeviceWarning_Get() {
  865. // 验证登录
  866. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  867. if !b_ {
  868. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  869. c.ServeJSON()
  870. return
  871. }
  872. id, _ := c.GetInt("T_id")
  873. T := Warning.Read_Warning_ById(id)
  874. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.WarningToWarning_R(T)}
  875. c.ServeJSON()
  876. return
  877. }
  878. // 编辑告警
  879. func (c *DeviceController) DeviceWarning_Post() {
  880. // 验证登录
  881. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  882. if !b_ {
  883. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  884. c.ServeJSON()
  885. return
  886. }
  887. Id, _ := c.GetInt("T_id")
  888. T_Text := c.GetString("T_Text")
  889. T := Warning.Read_Warning_ById(Id)
  890. if T.Id == 0 {
  891. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_id Err!"}
  892. c.ServeJSON()
  893. return
  894. }
  895. T.T_Text = T_Text
  896. T.T_State = 2
  897. Warning.Update_Warning(T, "T_Text", "T_State")
  898. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "报警处理操作", strconv.Itoa(Id)+"->"+T_Text)
  899. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  900. c.ServeJSON()
  901. return
  902. }
  903. // 删除告警
  904. func (c *DeviceController) DeviceWarning_Del() {
  905. // 验证登录
  906. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  907. if !b_ {
  908. c.Ctx.Redirect(302, "Login")
  909. return
  910. }
  911. Id, _ := c.GetInt("T_id")
  912. T := Warning.Read_Warning_ById(Id)
  913. if T.Id == 0 {
  914. c.Data["json"] = lib.JSONS{Code: 206, Msg: "T_id Err!"}
  915. c.ServeJSON()
  916. return
  917. }
  918. T.T_State = 0
  919. Warning.Update_Warning(T, "T_State")
  920. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "报警删除操作", strconv.Itoa(Id))
  921. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  922. c.ServeJSON()
  923. return
  924. }
  925. // 导出告警
  926. func (c *DeviceController) DeviceWarning_Data_Excel() {
  927. // 验证登录
  928. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  929. if !b_ {
  930. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  931. c.ServeJSON()
  932. return
  933. }
  934. T_name := c.GetString("T_name")
  935. T_tp := c.GetString("T_tp")
  936. Time_start := c.GetString("Time_start")
  937. Time_end := c.GetString("Time_end")
  938. T_handle, _ := c.GetInt("T_handle")
  939. T_pid := admin_r.T_pid
  940. if T_pid == 0 {
  941. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  942. }
  943. Device_data, _ := Warning.Read_Warning(T_pid, T_tp, T_name, T_handle, Time_start, Time_end, 0, 9999)
  944. f := excelize.NewFile() // 设置单元格的值
  945. // 这里设置表头
  946. f.SetCellValue("Sheet1", "A1", "报警类型")
  947. f.SetCellValue("Sheet1", "B1", "Sn")
  948. f.SetCellValue("Sheet1", "C1", "设备名称")
  949. f.SetCellValue("Sheet1", "D1", "传感器")
  950. f.SetCellValue("Sheet1", "E1", "报警内容")
  951. f.SetCellValue("Sheet1", "F1", "记录时间")
  952. f.SetCellValue("Sheet1", "G1", "处理")
  953. f.SetCellValue("Sheet1", "H1", "处理时间")
  954. // 设置列宽
  955. f.SetColWidth("Sheet1", "A", "A", 20)
  956. f.SetColWidth("Sheet1", "B", "B", 25)
  957. f.SetColWidth("Sheet1", "C", "C", 30)
  958. f.SetColWidth("Sheet1", "D", "D", 30)
  959. f.SetColWidth("Sheet1", "G", "E", 30)
  960. f.SetColWidth("Sheet1", "H", "F", 15)
  961. f.SetColWidth("Sheet1", "I", "G", 30)
  962. f.SetColWidth("Sheet1", "J", "H", 15)
  963. line := 1
  964. // 循环写入数据
  965. for _, v := range Device_data {
  966. line++
  967. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_tp)
  968. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_sn+"["+strconv.Itoa(v.Id)+"]")
  969. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_D_name)
  970. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_DS_name)
  971. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_Remark)
  972. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_Ut)
  973. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_Text)
  974. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.CreateTime)
  975. }
  976. timeStr := time.Now().Format("20060102150405")
  977. // 保存文件
  978. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  979. fmt.Println(err)
  980. }
  981. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  982. if !is {
  983. c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  984. c.ServeJSON()
  985. return
  986. }
  987. //删除文件
  988. //err := os.Remove("ofile/" + timeStr + ".xlsx")
  989. //if err != nil {
  990. // fmt.Println(err)
  991. //}
  992. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  993. c.ServeJSON()
  994. return
  995. }
  996. // 告警提示列表
  997. func (c *DeviceController) DeviceWarningList_T_Tips() {
  998. // 验证登录
  999. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1000. if !b_ {
  1001. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1002. c.ServeJSON()
  1003. return
  1004. }
  1005. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All_T_Notice_mechanism()}
  1006. c.ServeJSON()
  1007. return
  1008. }
  1009. // 告警类型列表
  1010. func (c *DeviceController) WarningType_List_All() {
  1011. // 验证登录
  1012. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1013. if !b_ {
  1014. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1015. c.ServeJSON()
  1016. return
  1017. }
  1018. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_All()}
  1019. c.ServeJSON()
  1020. return
  1021. }
  1022. // 告警类型列表
  1023. func (c *DeviceController) WarningType_Power_List_All() {
  1024. // 验证登录
  1025. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1026. if !b_ {
  1027. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1028. c.ServeJSON()
  1029. return
  1030. }
  1031. power, err := Account.Read_Power_ById(admin_r.T_power)
  1032. if err != nil {
  1033. c.Data["json"] = lib.JSONS{Code: 200, Msg: "获取菜单失败"}
  1034. c.ServeJSON()
  1035. return
  1036. }
  1037. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Warning.Read_WarningType_Power_All(power.T_warning)}
  1038. c.ServeJSON()
  1039. return
  1040. }
  1041. // 设备分类 ------------------------------------------
  1042. func (c *DeviceController) CompanyClass_List() {
  1043. // 验证登录
  1044. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1045. if !b_ {
  1046. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1047. c.ServeJSON()
  1048. return
  1049. }
  1050. type R_JSONS struct {
  1051. //必须的大写开头
  1052. Data []Company.CompanyClass_R
  1053. Num int64
  1054. Page int
  1055. Page_size int
  1056. }
  1057. var r_jsons R_JSONS
  1058. page, _ := c.GetInt("page")
  1059. println(page)
  1060. if page < 1 {
  1061. page = 1
  1062. }
  1063. page_z, _ := c.GetInt("page_z")
  1064. if page_z < 1 {
  1065. page_z = conf.Page_size
  1066. }
  1067. T_name := c.GetString("T_name")
  1068. T_pid := admin_r.T_pid
  1069. if T_pid == 0 {
  1070. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1071. }
  1072. r_jsons.Data, r_jsons.Num = Company.Read_CompanyClass_List(T_pid, T_name, page, page_z)
  1073. r_jsons.Page = page
  1074. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1075. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1076. c.ServeJSON()
  1077. return
  1078. }
  1079. func (c *DeviceController) CompanyClass_All() {
  1080. // 验证登录
  1081. b_, admin_r := 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. type R_JSONS struct {
  1088. //必须的大写开头
  1089. Data []Company.CompanyClass_R
  1090. Num int64
  1091. Page int
  1092. Page_size int
  1093. }
  1094. var r_jsons R_JSONS
  1095. T_name := c.GetString("T_name")
  1096. T_pid := admin_r.T_pid
  1097. if T_pid == 0 {
  1098. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1099. }
  1100. r_jsons.Data = Company.Read_CompanyClass_All(T_pid, T_name)
  1101. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1102. c.ServeJSON()
  1103. return
  1104. }
  1105. func (c *DeviceController) CompanyClass_Get() {
  1106. // 验证登录
  1107. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1108. if !b_ {
  1109. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1110. c.ServeJSON()
  1111. return
  1112. }
  1113. id, _ := c.GetInt("T_id")
  1114. T_pid := admin_r.T_pid
  1115. if T_pid == 0 {
  1116. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1117. }
  1118. R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
  1119. if err != nil {
  1120. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1121. c.ServeJSON()
  1122. return
  1123. }
  1124. if R_DeviceClass.T_pid != T_pid {
  1125. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1126. c.ServeJSON()
  1127. return
  1128. }
  1129. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Company.CompanyClassToCompanyClass_R(R_DeviceClass)}
  1130. c.ServeJSON()
  1131. return
  1132. }
  1133. func (c *DeviceController) CompanyClass_Add() {
  1134. // 验证登录
  1135. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1136. if !b_ {
  1137. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1138. c.ServeJSON()
  1139. return
  1140. }
  1141. T_name := c.GetString("T_name")
  1142. T_pid := admin_r.T_pid
  1143. if T_pid == 0 {
  1144. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1145. }
  1146. t_c := Company.CompanyClass{
  1147. T_pid: T_pid,
  1148. T_name: T_name,
  1149. T_State: 1,
  1150. }
  1151. Id, err := Company.Add_CompanyClass(t_c)
  1152. if err != nil {
  1153. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  1154. c.ServeJSON()
  1155. return
  1156. }
  1157. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "分类添加", t_c)
  1158. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  1159. c.ServeJSON()
  1160. return
  1161. }
  1162. func (c *DeviceController) CompanyClass_Edit() {
  1163. // 验证登录
  1164. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1165. if !b_ {
  1166. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1167. c.ServeJSON()
  1168. return
  1169. }
  1170. T_name := c.GetString("T_name")
  1171. id, _ := c.GetInt("T_id")
  1172. T_pid := admin_r.T_pid
  1173. if T_pid == 0 {
  1174. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1175. }
  1176. R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
  1177. if err != nil {
  1178. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1179. c.ServeJSON()
  1180. return
  1181. }
  1182. if R_DeviceClass.T_pid != T_pid {
  1183. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1184. c.ServeJSON()
  1185. return
  1186. }
  1187. if len(T_name) > 0 {
  1188. R_DeviceClass.T_name = T_name
  1189. }
  1190. if is := Company.Update_CompanyClass(R_DeviceClass, "T_name"); !is {
  1191. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  1192. c.ServeJSON()
  1193. return
  1194. }
  1195. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "分类修改", R_DeviceClass)
  1196. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1197. c.ServeJSON()
  1198. return
  1199. }
  1200. func (c *DeviceController) CompanyClass_Del() {
  1201. // 验证登录
  1202. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1203. if !b_ {
  1204. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1205. c.ServeJSON()
  1206. return
  1207. }
  1208. id, _ := c.GetInt("T_id")
  1209. T_pid := admin_r.T_pid
  1210. if T_pid == 0 {
  1211. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1212. }
  1213. R_DeviceClass, err := Company.Read_CompanyClass_ById(id)
  1214. if err != nil {
  1215. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1216. c.ServeJSON()
  1217. return
  1218. }
  1219. if R_DeviceClass.T_pid != T_pid {
  1220. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1221. c.ServeJSON()
  1222. return
  1223. }
  1224. if is := Company.Delete_CompanyClass_ById(id); !is {
  1225. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除失败"}
  1226. c.ServeJSON()
  1227. return
  1228. }
  1229. // 将传感器T_Class绑定的分类id替换为空字符串
  1230. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_Class_Id(id)
  1231. for _, v := range DeviceSensor_class_list {
  1232. Device.DeviceSensor_Bind_T_Class_Del(v.T_sn, v.T_id, id)
  1233. }
  1234. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "分类删除", strconv.Itoa(id))
  1235. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1236. c.ServeJSON()
  1237. return
  1238. }
  1239. // 设备分类绑定
  1240. func (c *DeviceController) DeviceClassBind_List() {
  1241. // 验证登录
  1242. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1243. if !b_ {
  1244. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1245. c.ServeJSON()
  1246. return
  1247. }
  1248. type R_JSONS struct {
  1249. //必须的大写开头
  1250. DeviceSensor_lite []Device.DeviceSensor_R
  1251. Num int
  1252. Page int
  1253. Page_size int
  1254. Pages []lib.Page_T
  1255. }
  1256. var r_jsons R_JSONS
  1257. page, _ := c.GetInt("page")
  1258. Name := c.GetString("Name")
  1259. T_sn := c.GetString("T_sn")
  1260. T_class_id, _ := c.GetInt("T_class_id")
  1261. page_z, _ := c.GetInt("page_z")
  1262. T_pid := admin_r.T_pid
  1263. if T_pid == 0 {
  1264. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1265. }
  1266. R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
  1267. if err != nil {
  1268. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1269. c.ServeJSON()
  1270. return
  1271. }
  1272. if R_DeviceClass.T_pid != T_pid {
  1273. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1274. c.ServeJSON()
  1275. return
  1276. }
  1277. var cnt int64
  1278. Device_lite, cnt := Device.Read_DeviceSensor_class_ALL_1(admin_r, T_pid, T_class_id, page, page_z, T_sn, Name, "")
  1279. r_jsons.DeviceSensor_lite = Device_lite
  1280. page_size := math.Ceil(float64(cnt) / float64(page_z))
  1281. r_jsons.Page = page
  1282. r_jsons.Page_size = int(page_size)
  1283. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  1284. r_jsons.Num = int(cnt)
  1285. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1286. c.ServeJSON()
  1287. return
  1288. }
  1289. func (c *DeviceController) ClassBind_Add() {
  1290. // 验证登录
  1291. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1292. if !b_ {
  1293. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1294. c.ServeJSON()
  1295. return
  1296. }
  1297. T_class_id, _ := c.GetInt("T_class_id")
  1298. T_sn := c.GetString("T_sn")
  1299. T_id, _ := c.GetInt("T_id")
  1300. T_pid := admin_r.T_pid
  1301. if T_pid == 0 {
  1302. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1303. }
  1304. if T_class_id == 0 || T_id == 0 || len(T_sn) == 0 {
  1305. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1306. c.ServeJSON()
  1307. return
  1308. }
  1309. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1310. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  1311. c.ServeJSON()
  1312. return
  1313. }
  1314. R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
  1315. if err != nil {
  1316. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1317. c.ServeJSON()
  1318. return
  1319. }
  1320. if R_DeviceClass.T_pid != T_pid {
  1321. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1322. c.ServeJSON()
  1323. return
  1324. }
  1325. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_class_1(T_sn, T_id, T_class_id)
  1326. if len(DeviceSensor_class_list) > 0 {
  1327. c.Data["json"] = lib.JSONS{Code: 203, Msg: "重复绑定!"}
  1328. c.ServeJSON()
  1329. return
  1330. }
  1331. if err = Device.DeviceSensor_T_Class_Add(T_sn, T_id, T_class_id); err != nil {
  1332. c.Data["json"] = lib.JSONS{Code: 200, Msg: "添加失败"}
  1333. c.ServeJSON()
  1334. return
  1335. }
  1336. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "分类绑定操作", strconv.Itoa(T_class_id)+"+>"+T_sn+"-"+strconv.Itoa(T_id))
  1337. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1338. c.ServeJSON()
  1339. return
  1340. }
  1341. func (c *DeviceController) ClassBind_Del() {
  1342. // 验证登录
  1343. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1344. if !b_ {
  1345. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1346. c.ServeJSON()
  1347. return
  1348. }
  1349. T_class_id, _ := c.GetInt("T_class_id")
  1350. T_sn := c.GetString("T_sn")
  1351. T_id, _ := c.GetInt("T_id")
  1352. T_pid := admin_r.T_pid
  1353. if T_pid == 0 {
  1354. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1355. }
  1356. if T_class_id == 0 || T_id == 0 || len(T_sn) == 0 {
  1357. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1358. c.ServeJSON()
  1359. return
  1360. }
  1361. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1362. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  1363. c.ServeJSON()
  1364. return
  1365. }
  1366. R_DeviceClass, err := Company.Read_CompanyClass_ById(T_class_id)
  1367. if err != nil {
  1368. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1369. c.ServeJSON()
  1370. return
  1371. }
  1372. if R_DeviceClass.T_pid != T_pid {
  1373. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1374. c.ServeJSON()
  1375. return
  1376. }
  1377. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_class_1(T_sn, T_id, T_class_id)
  1378. if len(DeviceSensor_class_list) == 0 {
  1379. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1380. c.ServeJSON()
  1381. return
  1382. }
  1383. if err = Device.DeviceSensor_Bind_T_Class_Del(T_sn, T_id, T_class_id); err != nil {
  1384. c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除失败"}
  1385. c.ServeJSON()
  1386. return
  1387. }
  1388. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "分类删除绑定操作", strconv.Itoa(T_class_id)+"->"+T_sn+"-"+strconv.Itoa(T_id))
  1389. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1390. c.ServeJSON()
  1391. return
  1392. }
  1393. // 报警策略 ------------------------------------------
  1394. func (c *DeviceController) CompanyNotice_List() {
  1395. // 验证登录
  1396. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1397. if !b_ {
  1398. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1399. c.ServeJSON()
  1400. return
  1401. }
  1402. type R_JSONS struct {
  1403. //必须的大写开头
  1404. Data []Company.CompanyNotice_R
  1405. Num int64
  1406. Page int
  1407. Page_size int
  1408. }
  1409. var r_jsons R_JSONS
  1410. page, _ := c.GetInt("page")
  1411. println(page)
  1412. if page < 1 {
  1413. page = 1
  1414. }
  1415. page_z, _ := c.GetInt("page_z")
  1416. if page_z < 1 {
  1417. page_z = conf.Page_size
  1418. }
  1419. T_name := c.GetString("T_name")
  1420. T_pid := admin_r.T_pid
  1421. if T_pid == 0 {
  1422. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1423. }
  1424. r_jsons.Data, r_jsons.Num = Company.Read_CompanyNotice_List(T_pid, T_name, page, page_z)
  1425. r_jsons.Page = page
  1426. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1427. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1428. c.ServeJSON()
  1429. return
  1430. }
  1431. func (c *DeviceController) CompanyNotice_All() {
  1432. // 验证登录
  1433. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1434. if !b_ {
  1435. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1436. c.ServeJSON()
  1437. return
  1438. }
  1439. type R_JSONS struct {
  1440. //必须的大写开头
  1441. Data []Company.CompanyNotice_R
  1442. Num int64
  1443. Page int
  1444. Page_size int
  1445. }
  1446. var r_jsons R_JSONS
  1447. T_name := c.GetString("T_name")
  1448. T_pid := admin_r.T_pid
  1449. if T_pid == 0 {
  1450. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1451. }
  1452. r_jsons.Data = Company.Read_CompanyNotice_All(T_pid, T_name)
  1453. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1454. c.ServeJSON()
  1455. return
  1456. }
  1457. func (c *DeviceController) CompanyNotice_Get() {
  1458. // 验证登录
  1459. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1460. if !b_ {
  1461. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1462. c.ServeJSON()
  1463. return
  1464. }
  1465. id, _ := c.GetInt("T_id")
  1466. T_pid := admin_r.T_pid
  1467. if T_pid == 0 {
  1468. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1469. }
  1470. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
  1471. if err != nil {
  1472. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1473. c.ServeJSON()
  1474. return
  1475. }
  1476. if R_DeviceNotice.T_pid != T_pid {
  1477. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1478. c.ServeJSON()
  1479. return
  1480. }
  1481. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Company.CompanyNoticeToCompanyNotice_R(R_DeviceNotice)}
  1482. c.ServeJSON()
  1483. return
  1484. }
  1485. func (c *DeviceController) CompanyNotice_Add() {
  1486. // 验证登录
  1487. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1488. if !b_ {
  1489. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1490. c.ServeJSON()
  1491. return
  1492. }
  1493. T_name := c.GetString("T_name")
  1494. T_Notice_wx := c.GetString("T_Notice_wx")
  1495. T_Notice_wx2 := c.GetString("T_Notice_wx2")
  1496. T_Notice_phone := c.GetString("T_Notice_phone")
  1497. T_Notice_message := c.GetString("T_Notice_message")
  1498. T_Notice_mailbox := c.GetString("T_Notice_mailbox")
  1499. T_Notice_mechanism := c.GetString("T_Notice_mechanism")
  1500. T_pid := admin_r.T_pid
  1501. if T_pid == 0 {
  1502. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1503. }
  1504. t_c := Company.CompanyNotice{
  1505. T_pid: T_pid,
  1506. T_name: T_name,
  1507. T_Notice_wx: T_Notice_wx,
  1508. T_Notice_wx2: T_Notice_wx2,
  1509. T_Notice_phone: T_Notice_phone,
  1510. T_Notice_message: T_Notice_message,
  1511. T_Notice_mailbox: T_Notice_mailbox,
  1512. T_Notice_mechanism: T_Notice_mechanism,
  1513. T_State: 1,
  1514. }
  1515. Id, err := Company.Add_CompanyNotice(t_c)
  1516. if err != nil {
  1517. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  1518. c.ServeJSON()
  1519. return
  1520. }
  1521. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "报警策略添加", t_c)
  1522. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  1523. c.ServeJSON()
  1524. return
  1525. }
  1526. func (c *DeviceController) CompanyNotice_Edit() {
  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_name := c.GetString("T_name")
  1536. T_Notice_wx := c.GetString("T_Notice_wx")
  1537. T_Notice_wx2 := c.GetString("T_Notice_wx2")
  1538. T_Notice_phone := c.GetString("T_Notice_phone")
  1539. T_Notice_message := c.GetString("T_Notice_message")
  1540. T_Notice_mailbox := c.GetString("T_Notice_mailbox")
  1541. T_Notice_mechanism := c.GetString("T_Notice_mechanism")
  1542. T_pid := admin_r.T_pid
  1543. if T_pid == 0 {
  1544. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1545. }
  1546. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
  1547. if err != nil {
  1548. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1549. c.ServeJSON()
  1550. return
  1551. }
  1552. if R_DeviceNotice.T_pid != T_pid {
  1553. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1554. c.ServeJSON()
  1555. return
  1556. }
  1557. R_DeviceNotice.Id = id
  1558. if len(T_name) > 0 {
  1559. R_DeviceNotice.T_name = T_name
  1560. }
  1561. if len(T_Notice_wx) > 0 {
  1562. R_DeviceNotice.T_Notice_wx = T_Notice_wx
  1563. }
  1564. if len(T_Notice_wx2) > 0 {
  1565. R_DeviceNotice.T_Notice_wx2 = T_Notice_wx2
  1566. }
  1567. if len(T_Notice_phone) > 0 {
  1568. R_DeviceNotice.T_Notice_phone = T_Notice_phone
  1569. }
  1570. if len(T_Notice_message) > 0 {
  1571. R_DeviceNotice.T_Notice_message = T_Notice_message
  1572. }
  1573. if len(T_Notice_mailbox) > 0 {
  1574. R_DeviceNotice.T_Notice_mailbox = T_Notice_mailbox
  1575. }
  1576. if len(T_Notice_mechanism) > 0 {
  1577. R_DeviceNotice.T_Notice_mechanism = T_Notice_mechanism
  1578. }
  1579. if is := Company.Update_CompanyNotice(R_DeviceNotice, "T_name"); !is {
  1580. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  1581. c.ServeJSON()
  1582. return
  1583. }
  1584. System.Add_UserLogs_T(admin_r.T_uuid, "设备管理", "报警策略修改", R_DeviceNotice)
  1585. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1586. c.ServeJSON()
  1587. return
  1588. }
  1589. func (c *DeviceController) CompanyNotice_Del() {
  1590. // 验证登录
  1591. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1592. if !b_ {
  1593. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1594. c.ServeJSON()
  1595. return
  1596. }
  1597. id, _ := c.GetInt("T_id")
  1598. T_pid := admin_r.T_pid
  1599. if T_pid == 0 {
  1600. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1601. }
  1602. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(id)
  1603. if err != nil {
  1604. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  1605. c.ServeJSON()
  1606. return
  1607. }
  1608. if R_DeviceNotice.T_pid != T_pid {
  1609. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_pid Err!"}
  1610. c.ServeJSON()
  1611. return
  1612. }
  1613. if is := Company.Delete_CompanyNotice_ById(id); !is {
  1614. c.Data["json"] = lib.JSONS{Code: 203, Msg: "删除失败"}
  1615. c.ServeJSON()
  1616. return
  1617. }
  1618. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_Notice_Id(id)
  1619. for _, v := range DeviceSensor_class_list {
  1620. Device.DeviceSensor_Bind_T_Notice_Del(v.T_sn, v.T_id, id)
  1621. }
  1622. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "报警策略删除", strconv.Itoa(id))
  1623. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1624. c.ServeJSON()
  1625. return
  1626. }
  1627. // 设备通知绑定
  1628. func (c *DeviceController) DeviceNoticeBind_List() {
  1629. // 验证登录
  1630. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1631. if !b_ {
  1632. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1633. c.ServeJSON()
  1634. return
  1635. }
  1636. type R_JSONS struct {
  1637. //必须的大写开头
  1638. DeviceSensor_lite []Device.DeviceSensor_R
  1639. Num int
  1640. Page int
  1641. Page_size int
  1642. Pages []lib.Page_T
  1643. }
  1644. var r_jsons R_JSONS
  1645. page, _ := c.GetInt("page")
  1646. T_name := c.GetString("T_name")
  1647. T_sn := c.GetString("T_sn")
  1648. T_notice_id, _ := c.GetInt("T_notice_id")
  1649. page_z, _ := c.GetInt("page_z")
  1650. T_pid := admin_r.T_pid
  1651. if T_pid == 0 {
  1652. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1653. }
  1654. R_DeviceNotice, err := Company.Read_CompanyNotice_ById(T_notice_id)
  1655. if err != nil {
  1656. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1657. c.ServeJSON()
  1658. return
  1659. }
  1660. if R_DeviceNotice.T_pid != T_pid {
  1661. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1662. c.ServeJSON()
  1663. return
  1664. }
  1665. var cnt int64
  1666. Device_lite, cnt := Device.Read_DeviceSensor_Notice_ALL_1(T_pid, T_notice_id, page, page_z, T_sn, T_name, "")
  1667. r_jsons.DeviceSensor_lite = Device_lite
  1668. page_size := math.Ceil(float64(cnt) / float64(page_z))
  1669. r_jsons.Page = page
  1670. r_jsons.Page_size = int(page_size)
  1671. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  1672. r_jsons.Num = int(cnt)
  1673. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1674. c.ServeJSON()
  1675. return
  1676. }
  1677. func (c *DeviceController) NoticeBind_Add() {
  1678. // 验证登录
  1679. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1680. if !b_ {
  1681. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1682. c.ServeJSON()
  1683. return
  1684. }
  1685. T_notice_id, _ := c.GetInt("T_notice_id")
  1686. T_sn := c.GetString("T_sn")
  1687. T_id, _ := c.GetInt("T_id")
  1688. T_pid := admin_r.T_pid
  1689. if T_pid == 0 {
  1690. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1691. }
  1692. if T_notice_id == 0 || T_id == 0 || len(T_sn) == 0 {
  1693. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1694. c.ServeJSON()
  1695. return
  1696. }
  1697. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1698. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  1699. c.ServeJSON()
  1700. return
  1701. }
  1702. R_DeviceClass, err := Company.Read_CompanyNotice_ById(T_notice_id)
  1703. if err != nil {
  1704. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1705. c.ServeJSON()
  1706. return
  1707. }
  1708. if R_DeviceClass.T_pid != T_pid {
  1709. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1710. c.ServeJSON()
  1711. return
  1712. }
  1713. DeviceSensor_Notice_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn, T_id, T_notice_id)
  1714. if len(DeviceSensor_Notice_list) > 0 {
  1715. c.Data["json"] = lib.JSONS{Code: 203, Msg: "重复绑定!"}
  1716. c.ServeJSON()
  1717. return
  1718. }
  1719. if err = Company.Add_T_Notice_bind(T_sn, T_id, T_notice_id); err != nil {
  1720. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知绑定失败"}
  1721. c.ServeJSON()
  1722. return
  1723. }
  1724. if err = Device.DeviceSensor_T_Notice_Add(T_sn, T_id, T_notice_id); err != nil {
  1725. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知绑定失败"}
  1726. c.ServeJSON()
  1727. return
  1728. }
  1729. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "报警策略绑定添加", strconv.Itoa(T_notice_id)+"+>"+T_sn+"-"+strconv.Itoa(T_id))
  1730. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1731. c.ServeJSON()
  1732. return
  1733. }
  1734. func (c *DeviceController) NoticeBind_Del() {
  1735. // 验证登录
  1736. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  1737. if !b_ {
  1738. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  1739. c.ServeJSON()
  1740. return
  1741. }
  1742. T_notice_id, _ := c.GetInt("T_notice_id")
  1743. T_sn := c.GetString("T_sn")
  1744. T_id, _ := c.GetInt("T_id")
  1745. T_pid := admin_r.T_pid
  1746. if T_pid == 0 {
  1747. T_pid, _ = strconv.Atoi(c.Ctx.Request.Header.Get("T_pid"))
  1748. }
  1749. if T_notice_id == 0 || T_id == 0 || len(T_sn) == 0 {
  1750. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  1751. c.ServeJSON()
  1752. return
  1753. }
  1754. if _, err := Device.Read_Device_ByT_sn(T_sn); err != nil {
  1755. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  1756. c.ServeJSON()
  1757. return
  1758. }
  1759. R_DeviceClass, err := Company.Read_CompanyNotice_ById(T_notice_id)
  1760. if err != nil {
  1761. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_class_id Err!"}
  1762. c.ServeJSON()
  1763. return
  1764. }
  1765. if R_DeviceClass.T_pid != T_pid {
  1766. c.Data["json"] = lib.JSONS{Code: 205, Msg: "T_pid Err!"}
  1767. c.ServeJSON()
  1768. return
  1769. }
  1770. DeviceSensor_class_list := Device.Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn, T_id, T_notice_id)
  1771. if len(DeviceSensor_class_list) == 0 {
  1772. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1773. c.ServeJSON()
  1774. return
  1775. }
  1776. if err = Company.Delete_T_Notice_bind(T_sn, T_id, T_notice_id); err != nil {
  1777. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知删除绑定失败"}
  1778. c.ServeJSON()
  1779. return
  1780. }
  1781. if err = Device.DeviceSensor_Bind_T_Notice_Del(T_sn, T_id, T_notice_id); err != nil {
  1782. c.Data["json"] = lib.JSONS{Code: 200, Msg: "通知删除绑定失败"}
  1783. c.ServeJSON()
  1784. return
  1785. }
  1786. System.Add_UserLogs(admin_r.T_uuid, "设备管理", "通知删除绑定删除", strconv.Itoa(T_notice_id)+"->"+T_sn+"-"+strconv.Itoa(T_id))
  1787. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1788. c.ServeJSON()
  1789. return
  1790. }
  1791. // 设备类型
  1792. func (c *DeviceController) DeviceType_List_All() {
  1793. type R_JSONS struct {
  1794. //必须的大写开头
  1795. Data []Device.DeviceType
  1796. Num int64
  1797. Page int
  1798. Page_size int
  1799. }
  1800. var r_jsons R_JSONS
  1801. r_jsons.Data = Device.Read_DeviceType_List_All()
  1802. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1803. c.ServeJSON()
  1804. return
  1805. }