DeviceController.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. package controllers
  2. import (
  3. conf "ColdP_server/conf"
  4. "ColdP_server/controllers/MqttServer"
  5. "ColdP_server/controllers/lib"
  6. "ColdP_server/models/Company"
  7. "ColdP_server/models/Device"
  8. "ColdP_server/models/Warning"
  9. "context"
  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. "time"
  19. )
  20. // DeviceController 设备管理页面
  21. type DeviceController struct {
  22. beego.Controller
  23. }
  24. // DeviceManagerHtml 返回页面
  25. func (c *DeviceController) DeviceManagerHtml() {
  26. //验证是否登录
  27. is, admin := lib.VerificationController(&c.Controller)
  28. if !is {
  29. return
  30. }
  31. //类名列表
  32. classList := Company.Read_CompanyClass_All(admin.T_pid, "")
  33. c.Data["Class_List"] = classList
  34. c.TplName = "Device/Device.html"
  35. }
  36. // DeviceList 获取设备列表
  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. c.Data["json"] = lib.PageHelper{int(count), pageCount, int(page), int(page) >= pageCount, page <= 1, device_list}
  56. c.ServeJSON()
  57. return
  58. }
  59. // CompanyClass 获取公司设备类目
  60. func (c *DeviceController) CompanyClass() {
  61. is, admin := lib.VerificationController(&c.Controller)
  62. if !is {
  63. c.Data["json"] = lib.JSONS{202, "用户未登录", nil}
  64. c.ServeJSON()
  65. return
  66. }
  67. //类名列表
  68. classList := Company.Read_CompanyClass_All(admin.T_pid, "")
  69. c.Data["json"] = classList
  70. c.ServeJSON()
  71. }
  72. // DataRepeat 数据重传
  73. func (c *DeviceController) DataRepeat() {
  74. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  75. if !b_ {
  76. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  77. c.ServeJSON()
  78. return
  79. }
  80. t := MqttServer.DataRepeat_C{}
  81. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  82. json.Unmarshal(bytes, &t)
  83. fmt.Println("浏览器接收数据:", t)
  84. s, _ := time.Parse("2006-01-02 15:04:05", t.StartTime)
  85. e, _ := time.Parse("2006-01-02 15:04:05", t.EndTime)
  86. // 发送MQTT
  87. for k, v := range t.Sns {
  88. topic := fmt.Sprintf("/pub/%s", k)
  89. repeatPub := MqttServer.DataRepeat_Pub{Sn: k, Type: 9, Mid: time.Now().Unix(), Data: MqttServer.DataRepeat_Pub_Data{Start: s.Unix(), End: e.Unix(), Id: v}}
  90. msg, _ := json.Marshal(repeatPub)
  91. mqttId := strings.Split(Device.ReadDeviceMqttId(k), "\"")[0]
  92. client, err := MqttServer.GetMqttClient(mqttId)
  93. if err != nil {
  94. log.Printf("Error getting MQTT client for SN: %s, %v", k, err)
  95. continue
  96. }
  97. for i := 0; i < 3; i++ {
  98. time.Sleep(time.Second * time.Duration(i+1))
  99. if err := MqttServer.PubMqttMessage(client, topic, msg); err != nil {
  100. log.Printf("Error publishing MQTT message for SN: %s, %v", k, err)
  101. c.Data["json"] = lib.JSONS{Code: 0, Msg: "设置失败", Data: k}
  102. return
  103. }
  104. }
  105. client.Disconnect()
  106. client.Terminate()
  107. }
  108. c.Data["json"] = lib.JSONS{Code: 200, Msg: "数据重传成功", Data: nil}
  109. c.ServeJSON()
  110. return
  111. }
  112. // ReadDeviation 读取偏差值
  113. func (c *DeviceController) ReadDeviation() {
  114. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  115. if !b_ {
  116. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  117. c.ServeJSON()
  118. return
  119. }
  120. t := make(map[string][]int)
  121. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  122. if err := json.Unmarshal(bytes, &t); err != nil {
  123. log.Printf("Error unmarshalling JSON: %v", err)
  124. c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
  125. c.ServeJSON()
  126. return
  127. }
  128. fmt.Println("浏览器接收数据:", t)
  129. // MQTT发送
  130. fmt.Println("发送MQTT t:", t)
  131. deviation := make(chan string, 10)
  132. var count = 0
  133. for k, v := range t {
  134. topicSub := fmt.Sprintf("/sub/%s", k)
  135. topicPub := fmt.Sprintf("/pub/%s", k)
  136. mqttId := Device.ReadDeviceMqttId(k)
  137. client, err := MqttServer.GetMqttClient(mqttId)
  138. if err != nil {
  139. log.Printf("Error getting MQTT client for SN: %s, %v", k, err)
  140. continue
  141. }
  142. // 订阅主题
  143. if err := MqttServer.Subscript(client, topicSub, deviation, "\"type\":7,"); err != nil {
  144. log.Printf("Error subscribing to MQTT topic for SN: %s, %v", k, err)
  145. c.Data["json"] = lib.JSONS{Code: 1, Msg: "mqtt订阅连接失败", Data: nil}
  146. return
  147. continue
  148. }
  149. pubData, _ := json.Marshal(MqttServer.Deviation_Pub{
  150. Sn: k,
  151. Type: 7,
  152. Mid: time.Now().Unix(),
  153. Data: v,
  154. })
  155. if err := MqttServer.PubMqttMessage(client, topicPub, pubData); err != nil {
  156. log.Printf("Error publishing MQTT message for SN: %s, %v", k, err)
  157. c.Data["json"] = lib.JSONS{Code: 1, Msg: "mqtt发送失败", Data: k}
  158. continue
  159. }
  160. count++
  161. }
  162. deviceRepeatData := make([]string, 0)
  163. timeout := time.After(10 * time.Second) // 设置超时时间
  164. for count > 0 {
  165. select {
  166. case v := <-deviation:
  167. fmt.Println("channel收到数据:", v)
  168. fmt.Println("count:", count)
  169. deviceRepeatData = append(deviceRepeatData, v)
  170. count--
  171. case <-timeout:
  172. log.Println("Timeout waiting for MQTT responses")
  173. c.Data["json"] = lib.JSONS{Code: 500, Msg: "Timeout waiting for MQTT responses"}
  174. c.ServeJSON()
  175. return
  176. }
  177. }
  178. close(deviation)
  179. fmt.Println("响应数据:", deviceRepeatData)
  180. c.Data["json"] = lib.JSONS{Code: 200, Msg: "偏差值上传成功", Data: deviceRepeatData}
  181. c.ServeJSON()
  182. return
  183. }
  184. // WriteDeviation 设置偏差值
  185. func (c *DeviceController) WriteDeviation() {
  186. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  187. if !b_ {
  188. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  189. c.ServeJSON()
  190. return
  191. }
  192. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  193. fmt.Println("请求json:", string(bytes))
  194. data := make([]MqttServer.Deviation_Sub, 0)
  195. if err := json.Unmarshal(bytes, &data); err != nil {
  196. log.Printf("Error unmarshalling JSON: %v", err)
  197. c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
  198. c.ServeJSON()
  199. return
  200. }
  201. // 处理每个设备的数据
  202. for _, v := range data {
  203. go func(v MqttServer.Deviation_Sub) {
  204. v.Type = 8
  205. v.Mid = int(time.Now().Unix())
  206. mqttId := Device.ReadDeviceMqttId(v.Sn)
  207. client, err := MqttServer.GetMqttClient(mqttId)
  208. if err != nil {
  209. log.Printf("Error getting MQTT client for SN: %s, %v", v.Sn, err)
  210. return
  211. }
  212. msgBytes, err := json.Marshal(v)
  213. if err != nil {
  214. log.Printf("Error marshalling JSON for SN: %s, %v", v.Sn, err)
  215. client.Disconnect()
  216. client.Terminate()
  217. return
  218. }
  219. for i := 0; i < 3; i++ {
  220. time.Sleep(time.Second * time.Duration(i+1))
  221. if err := MqttServer.PubMqttMessage(client, fmt.Sprintf("/pub/%s", v.Sn), msgBytes); err != nil {
  222. log.Printf("Error publishing MQTT message for SN: %s, %v", v.Sn, err)
  223. c.Data["json"] = lib.JSONS{Code: 0, Msg: "设置失败", Data: v.Sn}
  224. return
  225. }
  226. }
  227. client.Disconnect()
  228. client.Terminate()
  229. }(v)
  230. }
  231. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置偏差值成功!", Data: nil}
  232. c.ServeJSON()
  233. return
  234. }
  235. // ReadSensor 读取偏差值
  236. func (c *DeviceController) ReadSensor() {
  237. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  238. if !b_ {
  239. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  240. c.ServeJSON()
  241. return
  242. }
  243. t := make(map[string][]int)
  244. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  245. if err := json.Unmarshal(bytes, &t); err != nil {
  246. log.Printf("Error unmarshalling JSON: %v", err)
  247. c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
  248. c.ServeJSON()
  249. return
  250. }
  251. fmt.Println("浏览器接收数据:", t)
  252. // MQTT发送
  253. fmt.Println("发送MQTT t:", t)
  254. deviation := make(chan string, 10)
  255. var count = 0
  256. for k, v := range t {
  257. topicSub := fmt.Sprintf("/sub/%s", k)
  258. topicPub := fmt.Sprintf("/pub/%s", k)
  259. mqttId := Device.ReadDeviceMqttId(k)
  260. client, err := MqttServer.GetMqttClient(mqttId)
  261. if err != nil {
  262. log.Printf("Error getting MQTT client for SN: %s, %v", k, err)
  263. continue
  264. }
  265. // 订阅主题
  266. if err := MqttServer.Subscript(client, topicSub, deviation, "\"type\":5,"); err != nil {
  267. log.Printf("Error subscribing to MQTT topic for SN: %s, %v", k, err)
  268. continue
  269. }
  270. pubData, _ := json.Marshal(MqttServer.Deviation_Pub{
  271. Sn: k,
  272. Type: 5,
  273. Mid: time.Now().Unix(),
  274. Data: v,
  275. })
  276. if err := MqttServer.PubMqttMessage(client, topicPub, pubData); err != nil {
  277. log.Printf("Error publishing MQTT message for SN: %s, %v", k, err)
  278. c.Data["json"] = lib.JSONS{Code: 1, Msg: "mqtt发送失败", Data: k}
  279. continue
  280. }
  281. count++
  282. }
  283. deviceRepeatData := make([]string, 0)
  284. timeout := time.After(10 * time.Second) // 设置超时时间
  285. for count > 0 {
  286. select {
  287. case v := <-deviation:
  288. fmt.Println("channel收到数据:", v)
  289. fmt.Println("count:", count)
  290. deviceRepeatData = append(deviceRepeatData, v)
  291. count--
  292. case <-timeout:
  293. log.Println("Timeout waiting for MQTT responses")
  294. c.Data["json"] = lib.JSONS{Code: 500, Msg: "Timeout waiting for MQTT responses"}
  295. c.ServeJSON()
  296. return
  297. }
  298. }
  299. close(deviation)
  300. fmt.Println("响应数据:", deviceRepeatData)
  301. c.Data["json"] = lib.JSONS{Code: 200, Msg: "偏差值上传成功", Data: deviceRepeatData}
  302. c.ServeJSON()
  303. return
  304. }
  305. // WriteSensor 设置偏差值
  306. func (c *DeviceController) WriteSensor() {
  307. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  308. if !b_ {
  309. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  310. c.ServeJSON()
  311. return
  312. }
  313. bytes, err := io.ReadAll(c.Ctx.Request.Body)
  314. if err != nil {
  315. log.Printf("Error reading request body: %v", err)
  316. c.Data["json"] = lib.JSONS{Code: 500, Msg: "Internal Server Error"}
  317. c.ServeJSON()
  318. return
  319. }
  320. fmt.Println("请求json:", string(bytes))
  321. data := make([]MqttServer.Sensor_Sub, 0)
  322. if err := json.Unmarshal(bytes, &data); err != nil {
  323. log.Printf("Error unmarshalling JSON: %v", err)
  324. c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
  325. c.ServeJSON()
  326. return
  327. }
  328. // 设置上下文超时
  329. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  330. defer cancel()
  331. // 处理每个设备的数据
  332. for _, v := range data {
  333. go func(v MqttServer.Sensor_Sub) {
  334. select {
  335. case <-ctx.Done():
  336. log.Printf("Context canceled or timeout for SN: %s", v.Sn)
  337. return
  338. default:
  339. }
  340. v.Type = 6
  341. v.Mid = int(time.Now().Unix())
  342. mqttId := Device.ReadDeviceMqttId(v.Sn)
  343. client, err := MqttServer.GetMqttClient(mqttId)
  344. if err != nil {
  345. log.Printf("Error getting MQTT client for SN: %s, %v", v.Sn, err)
  346. return
  347. }
  348. msgBytes, err := json.Marshal(v)
  349. if err != nil {
  350. log.Printf("Error marshalling JSON for SN: %s, %v", v.Sn, err)
  351. client.Disconnect()
  352. client.Terminate()
  353. return
  354. }
  355. for i := 0; i < 3; i++ {
  356. time.Sleep(time.Second * time.Duration(i+1))
  357. select {
  358. case <-ctx.Done():
  359. log.Printf("Context canceled or timeout for SN: %s", v.Sn)
  360. client.Disconnect()
  361. client.Terminate()
  362. return
  363. default:
  364. }
  365. if err := MqttServer.PubMqttMessage(client, fmt.Sprintf("/pub/%s", v.Sn), msgBytes); err != nil {
  366. log.Printf("Error publishing MQTT message for SN: %s, %v", v.Sn, err)
  367. c.Data["json"] = lib.JSONS{Code: 0, Msg: "设置失败", Data: v.Sn}
  368. return
  369. }
  370. }
  371. client.Disconnect()
  372. client.Terminate()
  373. }(v)
  374. }
  375. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置偏差值成功!", Data: nil}
  376. c.ServeJSON()
  377. return
  378. }
  379. // 列表 -
  380. func (c *DeviceController) DeviceWarning_List_html() {
  381. // 验证登录
  382. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  383. if !b_ {
  384. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  385. c.ServeJSON()
  386. return
  387. }
  388. c.Data["Admin_r"] = admin_r
  389. page, _ := c.GetInt("page")
  390. println(page)
  391. if page < 1 {
  392. page = 1
  393. }
  394. c.Data["Admin_r"] = admin_r
  395. bindSN := c.GetStrings("bindSN")
  396. tpList := c.GetStrings("tpList")
  397. T_sn := c.GetString("T_sn")
  398. if len(T_sn) != 0 {
  399. bindSN = append(bindSN, T_sn)
  400. c.Data["T_sn"] = T_sn
  401. }
  402. Time_start := c.GetString("Time_start")
  403. Time_end := c.GetString("Time_end")
  404. if len(Time_start) == 0 && len(Time_end) == 0 {
  405. Time_start = time.Now().Format("2006-01-02") + " 00:00:00"
  406. Time_end = time.Now().Format("2006-01-02") + " 23:59:59"
  407. }
  408. c.Data["Time_start"] = Time_start
  409. c.Data["Time_end"] = Time_end
  410. //c.Data["Class_List"] = Device.Read_DeviceWarningList_All_1()
  411. //T_Title := ""
  412. //if Class_1 > 0 {
  413. // T_Title = Device.Read_DeviceWarningList_ById(Class_1).T_name
  414. //}
  415. pageSize := c.GetString("pageSize", "100")
  416. pageSizeInt, _ := strconv.Atoi(pageSize)
  417. //atoi, _ := strconv.Atoi(pageSizes.PageSize)
  418. getString := c.GetString("t_tp")
  419. t_tp, _ := strconv.Atoi(getString)
  420. var cnt int64
  421. DeviceWarning_List, cnt := Warning.Read_Warning_List(admin_r.T_pid, bindSN, tpList, Time_start, Time_end, t_tp, page, pageSizeInt)
  422. c.Data["List"] = DeviceWarning_List
  423. page_size := math.Ceil(float64(cnt) / float64(pageSizeInt))
  424. c.Data["Page"] = page
  425. c.Data["Page_size"] = page_size
  426. c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
  427. c.Data["cnt"] = cnt
  428. // 将sync.Map中的数据转存到切片中
  429. c.TplName = "Device/DeviceWarning.html"
  430. }
  431. // GetWarningtype 获取报警类型
  432. func (c *DeviceController) GetWarningtype() {
  433. var results []struct {
  434. Key int
  435. Value string
  436. }
  437. Warning.WarningType_list.Range(func(key, value any) bool {
  438. // 确保类型断言成功
  439. if k, ok := key.(int); ok {
  440. if v, ok := value.(string); ok {
  441. // 创建匿名结构体实例并添加到切片
  442. results = append(results, struct {
  443. Key int
  444. Value string
  445. }{Key: k, Value: v})
  446. } else {
  447. fmt.Println("Value is not of type string")
  448. }
  449. } else {
  450. fmt.Println("Key is not of type int")
  451. }
  452. return true // 继续遍历
  453. })
  454. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: results}
  455. c.ServeJSON()
  456. log.Println(results)
  457. }
  458. func (c *DeviceController) DeviceWarning_() {
  459. // 验证登录
  460. //b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  461. //if !b_ {
  462. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  463. // c.ServeJSON()
  464. // return
  465. //}
  466. c.Data["WarningType"] = Warning.Read_WarningType_All()
  467. c.TplName = "Device/DeviceWarning-.html"
  468. }
  469. // DeviceWarning_Post 添加报警
  470. func (c *DeviceController) DeviceWarning_Post() {
  471. // 验证登录
  472. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  473. if !b_ {
  474. c.Ctx.Redirect(302, "Login")
  475. return
  476. }
  477. T_tp, _ := c.GetInt("T_tp")
  478. T_sn := c.GetString("T_sn")
  479. T_id, _ := c.GetInt("T_id")
  480. T_Ut := c.GetString("T_Ut")
  481. T_Remark := c.GetString("T_Remark")
  482. r_Device, err := Device.Read_Device_ByT_sn(T_sn)
  483. if err != nil {
  484. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "SN 错误!!!"}
  485. c.ServeJSON()
  486. return
  487. }
  488. // 获取 传感器 参数
  489. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, T_id)
  490. if !is {
  491. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "编号 错误!!!"}
  492. c.ServeJSON()
  493. return
  494. }
  495. t1, _ := time.ParseInLocation("2006-01-02 15:04:05", T_Ut, time.Local) // +8 时差
  496. t_c := Warning.Warning{
  497. T_pid: admin_r.T_pid,
  498. T_tp: T_tp,
  499. T_sn: T_sn,
  500. T_D_name: r_Device.T_devName,
  501. T_id: T_id,
  502. T_DS_name: DeviceSensor_r.T_name,
  503. T_Ut: t1,
  504. T_State: 1,
  505. T_Remark: T_Remark,
  506. }
  507. Warning.Add_Warning(t_c)
  508. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  509. c.ServeJSON()
  510. return
  511. }
  512. // DeviceWarning_Del 删除报警
  513. func (c *DeviceController) DeviceWarning_Del() {
  514. // 验证登录
  515. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  516. if !b_ {
  517. c.Ctx.Redirect(302, "Login")
  518. return
  519. }
  520. Id, _ := c.GetInt("Id")
  521. if Id > 1000 {
  522. Warning.Delete_Warning(admin_r.T_pid, Id)
  523. }
  524. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  525. c.ServeJSON()
  526. return
  527. }
  528. // DeviceWarning_DelS 批量删除
  529. func (c *DeviceController) DeviceWarning_DelS() {
  530. // 验证登录
  531. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  532. if !b_ {
  533. c.Ctx.Redirect(302, "Login")
  534. return
  535. }
  536. type SelectedItem struct {
  537. Id string `json:"id"`
  538. Ut string `json:"ut"` // 假设"ut"字段是时间戳字符串
  539. }
  540. type SelectedItems struct {
  541. SelectedIds []SelectedItem `json:"selectedIds"`
  542. }
  543. var SelectedIds SelectedItems
  544. //var selectedIds map[string]any
  545. err := json.Unmarshal(c.Ctx.Input.RequestBody, &SelectedIds)
  546. log.Println(SelectedIds.SelectedIds)
  547. for _, v := range SelectedIds.SelectedIds {
  548. Warning.Delete_Warning_List(v.Id, v.Ut, admin_r.T_pid)
  549. }
  550. if err != nil {
  551. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "数据格式错误!!!"}
  552. c.ServeJSON()
  553. } else {
  554. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  555. c.ServeJSON()
  556. }
  557. }
  558. // Device_Copy 复制并添加
  559. func (c *DeviceController) Device_Copy() {
  560. // 验证登录
  561. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  562. if !b_ {
  563. c.Ctx.Redirect(302, "Login")
  564. return
  565. }
  566. type T_data struct {
  567. T_sn string `json:"t_sn"`
  568. T_id int `json:"t_id"`
  569. T_Rh any `json:"t_rh"`
  570. T_Site string `json:"t_site"`
  571. T_T any `json:"t_t"`
  572. CreateTime string `json:"createTime"`
  573. }
  574. var data T_data
  575. json.Unmarshal(c.Ctx.Input.RequestBody, &data)
  576. r := Device.Read_DeviceParameter_SNNo(data.T_sn)
  577. parse, _ := time.Parse("2006-01-02 15:04:05", data.CreateTime)
  578. data.CreateTime = parse.Add(time.Duration(r[0].T_saveT) * time.Second).Format("2006-01-02 15:04:05")
  579. itoa := strconv.Itoa(data.T_id)
  580. Device.Copy_DeviceData(data.T_sn, itoa, data.T_Rh, data.T_T, data.T_Site, data.CreateTime)
  581. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: data}
  582. c.ServeJSON()
  583. }
  584. // DeviceWarningUpdate 修改报警
  585. func (c *DeviceController) DeviceWarningUpdate() {
  586. // 验证登录
  587. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  588. if !b_ {
  589. c.Ctx.Redirect(302, "Login")
  590. return
  591. }
  592. var data struct {
  593. ColumnName string `json:"columnName"`
  594. NewValue string `json:"newValue"`
  595. RowId string `json:"rowId"`
  596. T_Ut string `json:"T_Ut"`
  597. SN string `json:"sn"`
  598. }
  599. json.Unmarshal(c.Ctx.Input.RequestBody, &data)
  600. log.Println(admin_r.T_pid)
  601. Warning.Update_DeviceParameter_Warning(data.ColumnName, data.NewValue, data.RowId, data.T_Ut)
  602. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: data}
  603. c.ServeJSON()
  604. }
  605. // GetDeviceALLSN 根据pid获取所有sn
  606. func (c *DeviceController) GetDeviceALLSN() {
  607. // 验证登录
  608. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  609. if !b_ {
  610. c.Ctx.Redirect(302, "Login")
  611. return
  612. }
  613. r := Device.Read_Device_All_SN(admin_r.T_pid)
  614. log.Println(r)
  615. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r}
  616. c.ServeJSON()
  617. }
  618. // GetDeviceALLTID 根据SN获取所有探头
  619. func (c *DeviceController) GetDeviceALLTID() {
  620. // 验证登录
  621. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  622. if !b_ {
  623. c.Ctx.Redirect(302, "Login")
  624. return
  625. }
  626. getString := c.GetString("sn")
  627. r := Device.Get_DeviceSensor_Tid_ByT_sn(getString)
  628. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: r}
  629. c.ServeJSON()
  630. }
  631. // DeviceWarningAdd 复制添加报警
  632. func (c *DeviceController) DeviceWarningAdd() {
  633. // 验证登录
  634. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  635. if !b_ {
  636. c.Ctx.Redirect(302, "Login")
  637. return
  638. }
  639. var data struct {
  640. T_tp_name string `json:"column1"`
  641. T__d_name string `json:"column2"`
  642. T_DS_name string `json:"column3"`
  643. T_Remark string `json:"column4"`
  644. T_State string `json:"column5"`
  645. T_Ut string `json:"column6"`
  646. RowId string `json:"rowId"`
  647. SN string `json:"sn"`
  648. T_id string `json:"t_id"`
  649. }
  650. json.Unmarshal(c.Ctx.Input.RequestBody, &data)
  651. atoi, _ := strconv.Atoi(data.T_id)
  652. r_Device, err := Device.Read_Device_ByT_sn(data.SN)
  653. if err != nil {
  654. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "SN 错误!!!"}
  655. c.ServeJSON()
  656. return
  657. }
  658. T_tp := Warning.Read_WarningType(data.T_tp_name)
  659. t1, _ := time.ParseInLocation("2006-01-02 15:04:05", data.T_Ut, time.Local) // +8 时差
  660. t_c := Warning.Warning{
  661. T_pid: admin_r.T_pid,
  662. T_tp: T_tp,
  663. T_sn: data.SN,
  664. T_D_name: r_Device.T_devName,
  665. T_id: atoi,
  666. T_DS_name: data.T_DS_name,
  667. T_Ut: t1,
  668. T_State: 1,
  669. T_Remark: data.T_Remark,
  670. }
  671. Warning.Add_Warning(t_c)
  672. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  673. c.ServeJSON()
  674. return
  675. }
  676. // SiftWarningType 根据报警类型筛选
  677. func (c *DeviceController) SiftWarningType() {
  678. // 验证登录
  679. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  680. if !b_ {
  681. c.Ctx.Redirect(302, "Login")
  682. return
  683. }
  684. }