Device.go 54 KB

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