Device.go 55 KB

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