Device.go 56 KB

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