DeviceController.txt 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. package controllers
  2. import (
  3. "ColdP_server/conf"
  4. "ColdP_server/controllers/MqttServer"
  5. "ColdP_server/controllers/lib"
  6. "ColdP_server/logs"
  7. "ColdP_server/models/Company"
  8. "ColdP_server/models/Device"
  9. "ColdP_server/models/Warning"
  10. "encoding/json"
  11. "fmt"
  12. beego "github.com/beego/beego/v2/server/web"
  13. "io"
  14. "log"
  15. "math"
  16. "strconv"
  17. "strings"
  18. "sync"
  19. "time"
  20. )
  21. // DeviceController 设备管理页面
  22. type DeviceController struct {
  23. beego.Controller
  24. }
  25. // DeviceManagerHtml 返回页面
  26. func (c *DeviceController) DeviceManagerHtml() {
  27. //验证是否登录
  28. is, admin := lib.VerificationController(&c.Controller)
  29. if !is {
  30. return
  31. }
  32. //类名列表
  33. classList := Company.Read_CompanyClass_All(admin.T_pid, "")
  34. c.Data["Class_List"] = classList
  35. c.TplName = "Device/Device.html"
  36. }
  37. func (c *DeviceController) DeviceList() {
  38. tName := c.GetString("deviceName") // 设备名称
  39. page, _ := c.GetInt64("currentPage") // 当前页码
  40. tClassify, _ := c.GetInt("deviceClass") // 设备分类
  41. is, admin := lib.VerificationController(&c.Controller)
  42. fmt.Println("当前用户的PID为:", admin.T_pid)
  43. if !is {
  44. // 用户未登录
  45. c.Data["json"] = lib.JSONS{202, "身份认证失效", nil}
  46. c.ServeJSON()
  47. return
  48. }
  49. device_list, count := Device.Read_DeviceSensor_List_T_ClassOr(admin.T_pid, tClassify, tName, tName, -1, int(page), conf.Page_size)
  50. var pageCount int
  51. if (int(count) % conf.Page_size) != 0 {
  52. pageCount = int(count) / conf.Page_size
  53. pageCount++
  54. }
  55. var wg sync.WaitGroup
  56. for i := range device_list {
  57. wg.Add(1)
  58. go func(deviceList *Device.DeviceSensor_) {
  59. defer func() {
  60. if r := recover(); r != nil {
  61. logs.Debug("协程出现异常: %v", r)
  62. }
  63. }()
  64. defer wg.Done()
  65. t := Device.Read_DeviceData(deviceList.T_sn, deviceList.T_id)
  66. err2, m := Device.ReadDeviceSensorParameter(deviceList.T_Sp)
  67. if err2 != nil {
  68. logs.Error("Error getting device sensor parameter for SN: %s, %v", deviceList.T_sn, err2)
  69. }
  70. //topicSub := fmt.Sprintf("/sub/%s", deviceList.T_sn)
  71. //topicPub := fmt.Sprintf("/pub/%s", deviceList.T_sn)
  72. //mqttId := Device.ReadDeviceMqttId(deviceList.T_sn)
  73. //deviceList.T_tDeviation = 1201
  74. //deviceList.T_RhDeviation = 1201
  75. //ints := []int{deviceList.T_id}
  76. //timeout := 5 * time.Second
  77. //pubData, err := json.Marshal(MqttServer.Deviation_Pub{
  78. // Sn: deviceList.T_sn,
  79. // Type: 7,
  80. // Mid: time.Now().Unix(),
  81. // Data: ints,
  82. //})
  83. //if err != nil {
  84. // logs.Error("Error marshalling JSON for SN: %s, %v", deviceList.T_sn, err)
  85. // return
  86. //}
  87. //if len(mqttId) != 0 {
  88. // ack, res, err := MqttServer.SendAndWaitForAck(mqttId, topicPub, topicSub, pubData, timeout)
  89. // if ack {
  90. // if len(res.Data) > 0 {
  91. // for _, v := range res.Data {
  92. // if v.Id == deviceList.T_id {
  93. // deviceList.T_tDeviation = v.T
  94. // deviceList.T_RhDeviation = v.H
  95. // } else {
  96. // deviceList.T_tDeviation = 1201
  97. // deviceList.T_RhDeviation = 1201
  98. // }
  99. // }
  100. // }
  101. // } else {
  102. // logs.Error("连接失败", err)
  103. // deviceList.T_tDeviation = 1201
  104. // deviceList.T_RhDeviation = 1201
  105. // }
  106. //}
  107. coldp, err2 := MqttServer.FindClodpServerBySnAndId(deviceList.T_sn, deviceList.T_id)
  108. if err2 != nil {
  109. deviceList.T_tDeviation = 1201
  110. deviceList.T_RhDeviation = 1201
  111. } else {
  112. deviceList.T_tDeviation = coldp.T_t
  113. deviceList.T_RhDeviation = coldp.T_h
  114. }
  115. deviceList.T_Tlower = m.T_Tlower
  116. deviceList.T_Tupper = m.T_Tupper
  117. deviceList.T_RHlower = m.T_RHlower
  118. deviceList.T_RHupper = m.T_RHupper
  119. deviceList.T_t = t.T_t
  120. deviceList.T_rh = t.T_rh
  121. }(&device_list[i])
  122. }
  123. wg.Wait()
  124. c.Data["json"] = lib.PageHelper{int(count), pageCount, int(page), int(page) >= pageCount, page <= 1, device_list}
  125. c.ServeJSON()
  126. return
  127. }
  128. // CompanyClass 获取公司设备类目
  129. func (c *DeviceController) CompanyClass() {
  130. is, admin := lib.VerificationController(&c.Controller)
  131. if !is {
  132. c.Data["json"] = lib.JSONS{202, "用户未登录", nil}
  133. c.ServeJSON()
  134. return
  135. }
  136. //类名列表
  137. classList := Company.Read_CompanyClass_All(admin.T_pid, "")
  138. c.Data["json"] = classList
  139. c.ServeJSON()
  140. }
  141. // DataRepeat 数据重传
  142. func (c *DeviceController) DataRepeat() {
  143. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  144. if !b_ {
  145. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  146. c.ServeJSON()
  147. return
  148. }
  149. t := MqttServer.DataRepeat_C{}
  150. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  151. json.Unmarshal(bytes, &t)
  152. fmt.Println("浏览器接收数据:", t)
  153. s, _ := time.Parse("2006-01-02 15:04:05", t.StartTime)
  154. e, _ := time.Parse("2006-01-02 15:04:05", t.EndTime)
  155. var wg sync.WaitGroup
  156. failSn := make([]string, 0)
  157. // 发送MQTT
  158. for k, v := range t.Sns {
  159. wg.Add(1)
  160. log.Println("传感器参数", v)
  161. go func(k string, v []string) {
  162. defer wg.Done()
  163. if len(v[0]) > 0 {
  164. atoi, _ := strconv.Atoi(v[0])
  165. topicPub := fmt.Sprintf("/pub/%s", k)
  166. repeatPub := MqttServer.DataRepeat_Pub{
  167. Sn: k, Type: 9, Mid: time.Now().Unix(), Data: MqttServer.DataRepeat_Pub_Data{Start: s.Unix(), End: e.Unix(), Id: []int{atoi}}}
  168. msg, _ := json.Marshal(repeatPub)
  169. mqttId := strings.Split(Device.ReadDeviceMqttId(k), "\"")[0]
  170. topicSub := fmt.Sprintf("/sub/%s", k)
  171. timeout := 5 * time.Second
  172. if len(mqttId) != 0 {
  173. ack, _, err := MqttServer.SendAndWaitForAck(mqttId, topicPub, topicSub, msg, timeout)
  174. if ack {
  175. } else {
  176. logs.Error("连接失败", err)
  177. failSn = append(failSn, k)
  178. }
  179. }
  180. }
  181. }(k, v)
  182. }
  183. wg.Wait()
  184. if len(failSn) > 0 {
  185. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "部分重传失败", Data: failSn}
  186. } else {
  187. c.Data["json"] = lib.JSONS{Code: 200, Msg: "重传数据成功!", Data: nil}
  188. }
  189. c.ServeJSON()
  190. }
  191. func (c *DeviceController) ReadDeviation() {
  192. // 用户令牌验证
  193. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  194. if !b_ {
  195. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  196. c.ServeJSON()
  197. return
  198. }
  199. t := make(map[string][]int)
  200. bytes, err := io.ReadAll(c.Ctx.Request.Body)
  201. if err != nil {
  202. log.Printf("Error reading request body: %v", err)
  203. c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
  204. c.ServeJSON()
  205. return
  206. }
  207. if err := json.Unmarshal(bytes, &t); err != nil {
  208. log.Printf("Error unmarshalling JSON: %v", err)
  209. c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
  210. c.ServeJSON()
  211. return
  212. }
  213. fmt.Println("浏览器接收数据:", t)
  214. var errors []string
  215. for k, v := range t {
  216. topicSub := fmt.Sprintf("/sub/%s", k)
  217. topicPub := fmt.Sprintf("/pub/%s", k)
  218. mqttId := Device.ReadDeviceMqttId(k)
  219. timeout := 5 * time.Second
  220. // 订阅主题操作,设置超时控制
  221. pubData, err := json.Marshal(MqttServer.Deviation_Pub{
  222. Sn: k,
  223. Type: 7,
  224. Mid: time.Now().Unix(),
  225. Data: v,
  226. })
  227. if err != nil {
  228. log.Printf("Error marshalling JSON for SN: %s, %v", k, err)
  229. errors = append(errors, fmt.Sprintf("设备: %s 序列化消息失败 %v", k, err))
  230. continue
  231. }
  232. if len(mqttId) != 0 {
  233. ack, res, err := MqttServer.SendAndWaitForAck(mqttId, topicPub, topicSub, pubData, timeout)
  234. if ack {
  235. c.Data["json"] = lib.JSONS{Code: 200, Msg: "读取参数成功", Data: res}
  236. c.ServeJSON()
  237. return
  238. } else {
  239. logs.Error("连接失败", err)
  240. c.Data["json"] = lib.JSONS{Code: 500, Msg: "读取参数失败-无法通信", Data: errors}
  241. c.ServeJSON()
  242. return
  243. }
  244. }
  245. }
  246. }
  247. // WriteDeviation 设置偏差值
  248. func (c *DeviceController) WriteDeviation() {
  249. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  250. if !b_ {
  251. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  252. c.ServeJSON()
  253. return
  254. }
  255. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  256. fmt.Println("请求json:", string(bytes))
  257. data := make([]MqttServer.Deviation_Sub, 0)
  258. if err := json.Unmarshal(bytes, &data); err != nil {
  259. log.Printf("Error unmarshalling JSON: %v", err)
  260. c.Data["json"] = lib.JSONS{Code: 400, Msg: "mqtt请求失败"}
  261. c.ServeJSON()
  262. return
  263. }
  264. var wg sync.WaitGroup
  265. successfulSns := make(map[string]bool) // 记录成功的SN
  266. mutex := &sync.Mutex{} // 保护对successfulSns的访问
  267. failSn := make([]string, 0)
  268. handleResult := func(sn string, success bool) {
  269. mutex.Lock()
  270. defer mutex.Unlock()
  271. if success {
  272. successfulSns[sn] = true
  273. } else {
  274. failSn = append(failSn, sn)
  275. }
  276. }
  277. // 处理每个设备的数据
  278. for _, v := range data {
  279. wg.Add(1)
  280. go func(v MqttServer.Deviation_Sub) {
  281. defer wg.Done()
  282. v.Type = 8
  283. v.Mid = int64(int(time.Now().Unix()))
  284. mqttId := Device.ReadDeviceMqttId(v.Sn)
  285. topicSub := fmt.Sprintf("/sub/%s", v.Sn)
  286. topicPub := fmt.Sprintf("/pub/%s", v.Sn)
  287. msgBytes, err := json.Marshal(v)
  288. timeout := 5 * time.Second
  289. if err != nil {
  290. log.Printf("Error marshalling JSON for SN: %s, %v", v.Sn, err)
  291. handleResult(v.Sn, false)
  292. return
  293. }
  294. if len(mqttId) != 0 {
  295. ack, _, err := MqttServer.SendAndWaitForAck(mqttId, topicPub, topicSub, msgBytes, timeout)
  296. if ack {
  297. handleResult(v.Sn, true)
  298. } else {
  299. logs.Error("连接失败", err)
  300. failSn = append(failSn, v.Sn)
  301. }
  302. }
  303. }(v)
  304. }
  305. wg.Wait()
  306. if len(failSn) > 0 {
  307. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "部分设置失败", Data: failSn}
  308. } else {
  309. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置偏差值成功!", Data: nil}
  310. }
  311. c.ServeJSON()
  312. }
  313. // WriteDeviationAll 批量设置偏差值
  314. func (c *DeviceController) WriteDeviationAll() {
  315. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  316. if !b_ {
  317. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  318. c.ServeJSON()
  319. return
  320. }
  321. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  322. fmt.Println("请求json:", string(bytes))
  323. var data MqttServer.Deviation_SubAll
  324. if err := json.Unmarshal(bytes, &data); err != nil {
  325. log.Printf("Error unmarshalling JSON: %v", err)
  326. c.Data["json"] = lib.JSONS{Code: 400, Msg: "mqtt请求失败"}
  327. c.ServeJSON()
  328. return
  329. }
  330. var wg sync.WaitGroup
  331. successfulSns := make(map[string]bool) // 记录成功的SN
  332. mutex := &sync.Mutex{} // 保护对successfulSns的访问
  333. failSn := make([]string, 0)
  334. handleResult := func(sn string, success bool) {
  335. mutex.Lock()
  336. defer mutex.Unlock()
  337. if success {
  338. successfulSns[sn] = true
  339. } else {
  340. failSn = append(failSn, sn)
  341. }
  342. }
  343. // 处理每个设备的数据
  344. for i, sn := range data.Sn {
  345. subData := data.Data[i]
  346. wg.Add(1)
  347. go func(sn string, subData MqttServer.Deviation_Sub_Data) {
  348. defer wg.Done()
  349. sub_data := make([]MqttServer.Deviation_Sub_Data, 0)
  350. sub_data = append(sub_data, subData)
  351. var devi Device.Deviation
  352. v := MqttServer.Deviation_Sub{
  353. Type: 8,
  354. Sn: sn,
  355. Mid: int64(int(time.Now().Unix())),
  356. Data: sub_data,
  357. }
  358. mqttId := Device.ReadDeviceMqttId(v.Sn)
  359. topicSub := fmt.Sprintf("/sub/%s", v.Sn)
  360. topicPub := fmt.Sprintf("/pub/%s", v.Sn)
  361. msgBytes, err := json.Marshal(v)
  362. timeout := 5 * time.Second
  363. if err != nil {
  364. log.Printf("Error marshalling JSON for SN: %s, %v", v.Sn, err)
  365. handleResult(v.Sn, false)
  366. return
  367. }
  368. if len(mqttId) != 0 {
  369. ack, _, err := MqttServer.SendAndWaitForAck(mqttId, topicPub, topicSub, msgBytes, timeout)
  370. if ack {
  371. handleResult(v.Sn, true)
  372. } else {
  373. logs.Error("连接失败", err)
  374. failSn = append(failSn, v.Sn)
  375. }
  376. }
  377. if len(devi.Data) == 0 {
  378. failSn = append(failSn, v.Sn)
  379. }
  380. }(sn, subData)
  381. }
  382. wg.Wait()
  383. if len(failSn) > 0 {
  384. c.Data["json"] = lib.JSONS{Code: 1201, Msg: "部分设置失败", Data: failSn}
  385. } else {
  386. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置偏差值成功!", Data: nil}
  387. }
  388. c.ServeJSON()
  389. }
  390. // ReadSensor 读取偏差值
  391. func (c *DeviceController) ReadSensor() {
  392. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  393. if !b_ {
  394. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  395. c.ServeJSON()
  396. return
  397. }
  398. t := make(map[string][]int)
  399. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  400. if err := json.Unmarshal(bytes, &t); err != nil {
  401. log.Printf("Error unmarshalling JSON: %v", err)
  402. c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
  403. c.ServeJSON()
  404. return
  405. }
  406. fmt.Println("浏览器接收数据:", t)
  407. for k, v := range t {
  408. topicSub := fmt.Sprintf("/sub/%s", k)
  409. topicPub := fmt.Sprintf("/pub/%s", k)
  410. mqttId := Device.ReadDeviceMqttId(k)
  411. pubData, err := json.Marshal(MqttServer.Deviation_Pub{
  412. Sn: k,
  413. Type: 5,
  414. Mid: time.Now().Unix(),
  415. Data: v,
  416. })
  417. timeout := 5 * time.Second
  418. if err != nil {
  419. log.Printf("Error marshalling JSON for SN: %s, %v", k, err)
  420. return
  421. }
  422. if len(mqttId) != 0 {
  423. ack, _, err := MqttServer.SendAndWaitForAck(mqttId, topicPub, topicSub, pubData, timeout)
  424. if ack {
  425. } else {
  426. logs.Error("连接失败", err)
  427. }
  428. }
  429. }
  430. deviceRepeatData := make([]string, 0)
  431. fmt.Println("响应数据:", deviceRepeatData)
  432. c.Data["json"] = lib.JSONS{Code: 200, Msg: "偏差值上传成功", Data: deviceRepeatData}
  433. c.ServeJSON()
  434. return
  435. }
  436. // WriteSensor 设置偏差值
  437. func (c *DeviceController) WriteSensor() {
  438. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  439. if !b_ {
  440. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  441. c.ServeJSON()
  442. return
  443. }
  444. bytes, err := io.ReadAll(c.Ctx.Request.Body)
  445. if err != nil {
  446. log.Printf("Error reading request body: %v", err)
  447. c.Data["json"] = lib.JSONS{Code: 500, Msg: "Internal Server Error"}
  448. c.ServeJSON()
  449. return
  450. }
  451. fmt.Println("请求json:", string(bytes))
  452. data := make([]MqttServer.Sensor_Sub, 0)
  453. if err := json.Unmarshal(bytes, &data); err != nil {
  454. log.Printf("Error unmarshalling JSON: %v", err)
  455. c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
  456. c.ServeJSON()
  457. return
  458. }
  459. failSn := make([]string, 0)
  460. // 处理每个设备的数据
  461. for _, v := range data {
  462. go func(v MqttServer.Sensor_Sub) {
  463. v.Type = 6
  464. v.Mid = int(time.Now().Unix())
  465. mqttId := Device.ReadDeviceMqttId(v.Sn)
  466. topicSub := fmt.Sprintf("/sub/%s", v.Sn)
  467. topicPub := fmt.Sprintf("/pub/%s", v.Sn)
  468. timeout := 5 * time.Second
  469. msgBytes, err := json.Marshal(v)
  470. if err != nil {
  471. log.Printf("Error marshalling JSON for SN: %s, %v", v.Sn, err)
  472. return
  473. }
  474. if len(mqttId) != 0 {
  475. ack, _, err := MqttServer.SendAndWaitForAck(mqttId, topicPub, topicSub, msgBytes, timeout)
  476. if ack {
  477. } else {
  478. logs.Error("连接失败", err)
  479. failSn = append(failSn, v.Sn)
  480. }
  481. }
  482. }(v)
  483. }
  484. if len(failSn) != 0 {
  485. c.Data["json"] = lib.JSONS{Code: 201, Msg: "设置偏差值失败!", Data: nil}
  486. c.ServeJSON()
  487. }
  488. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置偏差值成功!", Data: nil}
  489. c.ServeJSON()
  490. return
  491. }
  492. // 列表 -
  493. func (c *DeviceController) DeviceWarning_List_html() {
  494. // 验证登录
  495. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  496. if !b_ {
  497. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  498. c.ServeJSON()
  499. return
  500. }
  501. c.Data["Admin_r"] = admin_r
  502. page, _ := c.GetInt("page")
  503. println(page)
  504. if page < 1 {
  505. page = 1
  506. }
  507. c.Data["Admin_r"] = admin_r
  508. bindSN := c.GetStrings("bindSN")
  509. tpList := c.GetStrings("tpList")
  510. T_sn := c.GetString("T_sn")
  511. if len(T_sn) != 0 {
  512. bindSN = append(bindSN, T_sn)
  513. c.Data["T_sn"] = T_sn
  514. }
  515. Time_start := c.GetString("Time_start")
  516. Time_end := c.GetString("Time_end")
  517. if len(Time_start) == 0 && len(Time_end) == 0 {
  518. Time_start = time.Now().Format("2006-01-02") + " 00:00:00"
  519. Time_end = time.Now().Format("2006-01-02") + " 23:59:59"
  520. }
  521. c.Data["Time_start"] = Time_start
  522. c.Data["Time_end"] = Time_end
  523. //c.Data["Class_List"] = Device.Read_DeviceWarningList_All_1()
  524. //T_Title := ""
  525. //if Class_1 > 0 {
  526. // T_Title = Device.Read_DeviceWarningList_ById(Class_1).T_name
  527. //}
  528. pageSize := c.GetString("pageSize", "100")
  529. pageSizeInt, _ := strconv.Atoi(pageSize)
  530. //atoi, _ := strconv.Atoi(pageSizes.PageSize)
  531. getString := c.GetString("t_tp")
  532. t_tp, _ := strconv.Atoi(getString)
  533. var cnt int64
  534. DeviceWarning_List, cnt := Warning.Read_Warning_List(admin_r.T_pid, bindSN, tpList, Time_start, Time_end, t_tp, page, pageSizeInt)
  535. c.Data["List"] = DeviceWarning_List
  536. page_size := math.Ceil(float64(cnt) / float64(pageSizeInt))
  537. c.Data["Page"] = page
  538. c.Data["Page_size"] = page_size
  539. c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
  540. c.Data["cnt"] = cnt
  541. // 将sync.Map中的数据转存到切片中
  542. c.TplName = "Device/DeviceWarning.html"
  543. }
  544. // GetWarningtype 获取报警类型
  545. func (c *DeviceController) GetWarningtype() {
  546. var results []struct {
  547. Key int
  548. Value string
  549. }
  550. Warning.WarningType_list.Range(func(key, value any) bool {
  551. // 确保类型断言成功
  552. if k, ok := key.(int); ok {
  553. if v, ok := value.(string); ok {
  554. // 创建匿名结构体实例并添加到切片
  555. results = append(results, struct {
  556. Key int
  557. Value string
  558. }{Key: k, Value: v})
  559. } else {
  560. fmt.Println("Value is not of type string")
  561. }
  562. } else {
  563. fmt.Println("Key is not of type int")
  564. }
  565. return true // 继续遍历
  566. })
  567. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: results}
  568. c.ServeJSON()
  569. log.Println(results)
  570. }
  571. func (c *DeviceController) DeviceWarning_() {
  572. // 验证登录
  573. //b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  574. //if !b_ {
  575. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  576. // c.ServeJSON()
  577. // return
  578. //}
  579. c.Data["WarningType"] = Warning.Read_WarningType_All()
  580. c.TplName = "Device/DeviceWarning-.html"
  581. }
  582. // DeviceWarning_Post 添加报警
  583. func (c *DeviceController) DeviceWarning_Post() {
  584. // 验证登录
  585. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  586. if !b_ {
  587. c.Ctx.Redirect(302, "Login")
  588. return
  589. }
  590. T_tp, _ := c.GetInt("T_tp")
  591. T_sn := c.GetString("T_sn")
  592. T_id, _ := c.GetInt("T_id")
  593. T_Ut := c.GetString("T_Ut")
  594. T_Remark := c.GetString("T_Remark")
  595. r_Device, err := Device.Read_Device_ByT_sn(T_sn)
  596. if err != nil {
  597. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "SN 错误!!!"}
  598. c.ServeJSON()
  599. return
  600. }
  601. // 获取 传感器 参数
  602. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, T_id)
  603. if !is {
  604. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "编号 错误!!!"}
  605. c.ServeJSON()
  606. return
  607. }
  608. t1, _ := time.ParseInLocation("2006-01-02 15:04:05", T_Ut, time.Local) // +8 时差
  609. t_c := Warning.Warning{
  610. T_pid: admin_r.T_pid,
  611. T_tp: T_tp,
  612. T_sn: T_sn,
  613. T_D_name: r_Device.T_devName,
  614. T_id: T_id,
  615. T_DS_name: DeviceSensor_r.T_name,
  616. T_Ut: t1,
  617. T_State: 1,
  618. T_Remark: T_Remark,
  619. }
  620. Warning.Add_Warning(t_c)
  621. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  622. c.ServeJSON()
  623. return
  624. }
  625. // DeviceWarning_Del 删除报警
  626. func (c *DeviceController) DeviceWarning_Del() {
  627. // 验证登录
  628. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  629. if !b_ {
  630. c.Ctx.Redirect(302, "Login")
  631. return
  632. }
  633. Id, _ := c.GetInt("Id")
  634. if Id > 1000 {
  635. Warning.Delete_Warning(admin_r.T_pid, Id)
  636. }
  637. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  638. c.ServeJSON()
  639. return
  640. }
  641. // DeviceWarning_DelS 批量删除
  642. func (c *DeviceController) DeviceWarning_DelS() {
  643. // 验证登录
  644. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  645. if !b_ {
  646. c.Ctx.Redirect(302, "Login")
  647. return
  648. }
  649. type SelectedItem struct {
  650. Id string `json:"id"`
  651. Ut string `json:"ut"` // 假设"ut"字段是时间戳字符串
  652. }
  653. type SelectedItems struct {
  654. SelectedIds []SelectedItem `json:"selectedIds"`
  655. }
  656. var SelectedIds SelectedItems
  657. //var selectedIds map[string]any
  658. err := json.Unmarshal(c.Ctx.Input.RequestBody, &SelectedIds)
  659. log.Println(SelectedIds.SelectedIds)
  660. for _, v := range SelectedIds.SelectedIds {
  661. Warning.Delete_Warning_List(v.Id, v.Ut, admin_r.T_pid)
  662. }
  663. if err != nil {
  664. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "数据格式错误!!!"}
  665. c.ServeJSON()
  666. } else {
  667. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  668. c.ServeJSON()
  669. }
  670. }
  671. // Device_Copy 复制并添加
  672. func (c *DeviceController) Device_Copy() {
  673. // 验证登录
  674. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  675. if !b_ {
  676. c.Ctx.Redirect(302, "Login")
  677. return
  678. }
  679. type T_data struct {
  680. T_sn string `json:"t_sn"`
  681. T_id int `json:"t_id"`
  682. T_Rh any `json:"t_rh"`
  683. T_Site string `json:"t_site"`
  684. T_T any `json:"t_t"`
  685. CreateTime string `json:"createTime"`
  686. }
  687. var data T_data
  688. json.Unmarshal(c.Ctx.Input.RequestBody, &data)
  689. r := Device.Read_DeviceParameter_SNNo(data.T_sn)
  690. parse, _ := time.Parse("2006-01-02 15:04:05", data.CreateTime)
  691. data.CreateTime = parse.Add(time.Duration(r[0].T_saveT) * time.Second).Format("2006-01-02 15:04:05")
  692. itoa := strconv.Itoa(data.T_id)
  693. Device.Copy_DeviceData(data.T_sn, itoa, data.T_Rh, data.T_T, data.T_Site, data.CreateTime)
  694. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: data}
  695. c.ServeJSON()
  696. }
  697. // DeviceWarningUpdate 修改报警
  698. func (c *DeviceController) DeviceWarningUpdate() {
  699. // 验证登录
  700. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  701. if !b_ {
  702. c.Ctx.Redirect(302, "Login")
  703. return
  704. }
  705. var data struct {
  706. ColumnName string `json:"columnName"`
  707. NewValue string `json:"newValue"`
  708. RowId string `json:"rowId"`
  709. T_Ut string `json:"T_Ut"`
  710. SN string `json:"sn"`
  711. }
  712. json.Unmarshal(c.Ctx.Input.RequestBody, &data)
  713. log.Println(admin_r.T_pid)
  714. Warning.Update_DeviceParameter_Warning(data.ColumnName, data.NewValue, data.RowId, data.T_Ut)
  715. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: data}
  716. c.ServeJSON()
  717. }
  718. // GetDeviceALLSN 根据pid获取所有sn
  719. func (c *DeviceController) GetDeviceALLSN() {
  720. // 验证登录
  721. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  722. if !b_ {
  723. c.Ctx.Redirect(302, "Login")
  724. return
  725. }
  726. r := Device.Read_Device_All_SN(admin_r.T_pid)
  727. log.Println(r)
  728. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r}
  729. c.ServeJSON()
  730. }
  731. // GetDeviceALLTID 根据SN获取所有探头
  732. func (c *DeviceController) GetDeviceALLTID() {
  733. // 验证登录
  734. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  735. if !b_ {
  736. c.Ctx.Redirect(302, "Login")
  737. return
  738. }
  739. getString := c.GetString("sn")
  740. r := Device.Get_DeviceSensor_Tid_ByT_sn(getString)
  741. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: r}
  742. c.ServeJSON()
  743. }
  744. // DeviceWarningAdd 复制添加报警
  745. func (c *DeviceController) DeviceWarningAdd() {
  746. // 验证登录
  747. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  748. if !b_ {
  749. c.Ctx.Redirect(302, "Login")
  750. return
  751. }
  752. var data struct {
  753. T_tp_name string `json:"column1"`
  754. T__d_name string `json:"column2"`
  755. T_DS_name string `json:"column3"`
  756. T_Remark string `json:"column4"`
  757. T_State string `json:"column5"`
  758. T_Ut string `json:"column6"`
  759. RowId string `json:"rowId"`
  760. SN string `json:"sn"`
  761. T_id string `json:"t_id"`
  762. }
  763. json.Unmarshal(c.Ctx.Input.RequestBody, &data)
  764. atoi, _ := strconv.Atoi(data.T_id)
  765. r_Device, err := Device.Read_Device_ByT_sn(data.SN)
  766. if err != nil {
  767. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "SN 错误!!!"}
  768. c.ServeJSON()
  769. return
  770. }
  771. T_tp := Warning.Read_WarningType(data.T_tp_name)
  772. t1, _ := time.ParseInLocation("2006-01-02 15:04:05", data.T_Ut, time.Local) // +8 时差
  773. t_c := Warning.Warning{
  774. T_pid: admin_r.T_pid,
  775. T_tp: T_tp,
  776. T_sn: data.SN,
  777. T_D_name: r_Device.T_devName,
  778. T_id: atoi,
  779. T_DS_name: data.T_DS_name,
  780. T_Ut: t1,
  781. T_State: 1,
  782. T_Remark: data.T_Remark,
  783. }
  784. Warning.Add_Warning(t_c)
  785. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  786. c.ServeJSON()
  787. return
  788. }
  789. // SiftWarningType 根据报警类型筛选
  790. func (c *DeviceController) SiftWarningType() {
  791. // 验证登录
  792. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  793. if !b_ {
  794. c.Ctx.Redirect(302, "Login")
  795. return
  796. }
  797. }