DeviceController.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. "encoding/json"
  10. "fmt"
  11. beego "github.com/beego/beego/v2/server/web"
  12. "io"
  13. "io/ioutil"
  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, _ := ioutil.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 := MqttServer.GetMqttClient(mqttId)
  93. for i := 0; i < 3; i++ {
  94. time.Sleep(time.Second * time.Duration(i+1))
  95. MqttServer.PubMqttMessage(client, topic, msg)
  96. }
  97. client.Disconnect()
  98. client.Terminate()
  99. }
  100. c.Data["json"] = lib.JSONS{200, "数据重传成功", nil}
  101. c.ServeJSON()
  102. return
  103. }
  104. // ReadDeviation 读取偏差值
  105. func (c *DeviceController) ReadDeviation() {
  106. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  107. if !b_ {
  108. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  109. c.ServeJSON()
  110. return
  111. }
  112. t := make(map[string][]int)
  113. bytes, _ := ioutil.ReadAll(c.Ctx.Request.Body)
  114. json.Unmarshal(bytes, &t)
  115. fmt.Println("浏览器接收数据:", t)
  116. //MQTT发送
  117. fmt.Println("发送MQTT t:", t)
  118. deviation := make(chan string, 10)
  119. var count = 0
  120. for k, v := range t {
  121. topicSub := fmt.Sprintf("/sub/%s", k)
  122. fmt.Println(v)
  123. topicPub := fmt.Sprintf("/pub/%s", k)
  124. mqttId := Device.ReadDeviceMqttId(k)
  125. client := MqttServer.GetMqttClient(mqttId)
  126. MqttServer.Subscript(client, topicSub, deviation, "\"type\":7,")
  127. pubData, _ := json.Marshal(MqttServer.Deviation_Pub{
  128. Sn: k,
  129. Type: 7,
  130. Mid: time.Now().Unix(),
  131. Data: v,
  132. })
  133. MqttServer.PubMqttMessage(client, topicPub, pubData)
  134. count++
  135. }
  136. deviceRepeatData := make([]string, 0)
  137. select {
  138. case v := <-deviation:
  139. fmt.Println("channel收到数据:", v)
  140. fmt.Println("count:", count)
  141. deviceRepeatData = append(deviceRepeatData, v)
  142. count--
  143. if count <= 0 {
  144. close(deviation)
  145. break
  146. }
  147. }
  148. fmt.Println("响应数据:", deviceRepeatData)
  149. c.Data["json"] = lib.JSONS{200, "偏差值上传成功", deviceRepeatData}
  150. c.ServeJSON()
  151. return
  152. }
  153. // WriteDeviation 设置偏差值
  154. func (c *DeviceController) WriteDeviation() {
  155. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  156. if !b_ {
  157. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  158. c.ServeJSON()
  159. return
  160. }
  161. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  162. data := make([]MqttServer.Deviation_Sub, 0)
  163. fmt.Println("请求json:", string(bytes))
  164. json.Unmarshal(bytes, &data)
  165. for _, v := range data {
  166. go func(v MqttServer.Deviation_Sub) {
  167. v.Type = 8
  168. v.Mid = int(time.Now().Unix())
  169. mqttid := Device.ReadDeviceMqttId(v.Sn)
  170. cli := MqttServer.GetMqttClient(mqttid)
  171. bytes, _ := json.Marshal(v)
  172. for i := 0; i < 3; i++ {
  173. time.Sleep(time.Second * time.Duration(i+1))
  174. MqttServer.PubMqttMessage(cli, fmt.Sprintf("/pub/%s", v.Sn), bytes)
  175. }
  176. cli.Disconnect()
  177. cli.Terminate()
  178. }(v)
  179. }
  180. c.Data["json"] = lib.JSONS{200, "设置偏差值成功!", nil}
  181. c.ServeJSON()
  182. return
  183. }
  184. // ReadSensor 读取偏差值
  185. func (c *DeviceController) ReadSensor() {
  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. t := make(map[string][]int)
  193. bytes, _ := ioutil.ReadAll(c.Ctx.Request.Body)
  194. json.Unmarshal(bytes, &t)
  195. fmt.Println("浏览器接收数据:", t)
  196. //MQTT发送
  197. fmt.Println("发送MQTT t:", t)
  198. deviation := make(chan string, 10)
  199. var count = 0
  200. for k, v := range t {
  201. topicSub := fmt.Sprintf("/sub/%s", k)
  202. fmt.Println(v)
  203. topicPub := fmt.Sprintf("/pub/%s", k)
  204. mqttId := Device.ReadDeviceMqttId(k)
  205. client := MqttServer.GetMqttClient(mqttId)
  206. MqttServer.Subscript(client, topicSub, deviation, "\"type\":5,")
  207. pubData, _ := json.Marshal(MqttServer.Deviation_Pub{
  208. Sn: k,
  209. Type: 5,
  210. Mid: time.Now().Unix(),
  211. Data: v,
  212. })
  213. MqttServer.PubMqttMessage(client, topicPub, pubData)
  214. count++
  215. }
  216. deviceRepeatData := make([]string, 0)
  217. select {
  218. case v := <-deviation:
  219. fmt.Println("channel收到数据:", v)
  220. fmt.Println("count:", count)
  221. deviceRepeatData = append(deviceRepeatData, v)
  222. count--
  223. if count <= 0 {
  224. close(deviation)
  225. break
  226. }
  227. }
  228. fmt.Println("响应数据:", deviceRepeatData)
  229. c.Data["json"] = lib.JSONS{200, "偏差值上传成功", deviceRepeatData}
  230. c.ServeJSON()
  231. return
  232. }
  233. // WriteSensor WriteDeviation 设置偏差值
  234. func (c *DeviceController) WriteSensor() {
  235. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  236. if !b_ {
  237. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  238. c.ServeJSON()
  239. return
  240. }
  241. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  242. data := make([]MqttServer.Sensor_Sub, 0)
  243. fmt.Println("请求json:", string(bytes))
  244. json.Unmarshal(bytes, &data)
  245. for _, v := range data {
  246. go func(v MqttServer.Sensor_Sub) {
  247. v.Type = 6
  248. v.Mid = int(time.Now().Unix())
  249. mqttid := Device.ReadDeviceMqttId(v.Sn)
  250. cli := MqttServer.GetMqttClient(mqttid)
  251. bytes, _ := json.Marshal(v)
  252. for i := 0; i < 3; i++ {
  253. time.Sleep(time.Second * time.Duration(i+1))
  254. MqttServer.PubMqttMessage(cli, fmt.Sprintf("/pub/%s", v.Sn), bytes)
  255. }
  256. cli.Disconnect()
  257. cli.Terminate()
  258. }(v)
  259. }
  260. c.Data["json"] = lib.JSONS{200, "设置偏差值成功!", nil}
  261. c.ServeJSON()
  262. return
  263. }
  264. // 列表 -
  265. func (c *DeviceController) DeviceWarning_List_html() {
  266. // 验证登录
  267. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  268. if !b_ {
  269. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  270. c.ServeJSON()
  271. return
  272. }
  273. c.Data["Admin_r"] = admin_r
  274. page, _ := c.GetInt("page")
  275. println(page)
  276. if page < 1 {
  277. page = 1
  278. }
  279. c.Data["Admin_r"] = admin_r
  280. bindSN := c.GetStrings("bindSN")
  281. tpList := c.GetStrings("tpList")
  282. T_sn := c.GetString("T_sn")
  283. if len(T_sn) != 0 {
  284. bindSN = append(bindSN, T_sn)
  285. c.Data["T_sn"] = T_sn
  286. }
  287. Time_start := c.GetString("Time_start")
  288. Time_end := c.GetString("Time_end")
  289. if len(Time_start) == 0 && len(Time_end) == 0 {
  290. Time_start = time.Now().Format("2006-01-02") + " 00:00:00"
  291. Time_end = time.Now().Format("2006-01-02") + " 23:59:59"
  292. }
  293. c.Data["Time_start"] = Time_start
  294. c.Data["Time_end"] = Time_end
  295. //c.Data["Class_List"] = Device.Read_DeviceWarningList_All_1()
  296. //T_Title := ""
  297. //if Class_1 > 0 {
  298. // T_Title = Device.Read_DeviceWarningList_ById(Class_1).T_name
  299. //}
  300. pageSize := c.GetString("pageSize", "100")
  301. pageSizeInt, _ := strconv.Atoi(pageSize)
  302. //atoi, _ := strconv.Atoi(pageSizes.PageSize)
  303. getString := c.GetString("t_tp")
  304. t_tp, _ := strconv.Atoi(getString)
  305. var cnt int64
  306. DeviceWarning_List, cnt := Warning.Read_Warning_List(admin_r.T_pid, bindSN, tpList, Time_start, Time_end, t_tp, page, pageSizeInt)
  307. c.Data["List"] = DeviceWarning_List
  308. page_size := math.Ceil(float64(cnt) / float64(pageSizeInt))
  309. c.Data["Page"] = page
  310. c.Data["Page_size"] = page_size
  311. c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
  312. c.Data["cnt"] = cnt
  313. // 将sync.Map中的数据转存到切片中
  314. c.TplName = "Device/DeviceWarning.html"
  315. }
  316. // GetWarningtype 获取报警类型
  317. func (c *DeviceController) GetWarningtype() {
  318. var results []struct {
  319. Key int
  320. Value string
  321. }
  322. Warning.WarningType_list.Range(func(key, value any) bool {
  323. // 确保类型断言成功
  324. if k, ok := key.(int); ok {
  325. if v, ok := value.(string); ok {
  326. // 创建匿名结构体实例并添加到切片
  327. results = append(results, struct {
  328. Key int
  329. Value string
  330. }{Key: k, Value: v})
  331. } else {
  332. fmt.Println("Value is not of type string")
  333. }
  334. } else {
  335. fmt.Println("Key is not of type int")
  336. }
  337. return true // 继续遍历
  338. })
  339. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: results}
  340. c.ServeJSON()
  341. log.Println(results)
  342. }
  343. func (c *DeviceController) DeviceWarning_() {
  344. // 验证登录
  345. //b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  346. //if !b_ {
  347. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  348. // c.ServeJSON()
  349. // return
  350. //}
  351. c.Data["WarningType"] = Warning.Read_WarningType_All()
  352. c.TplName = "Device/DeviceWarning-.html"
  353. }
  354. // DeviceWarning_Post 添加报警
  355. func (c *DeviceController) DeviceWarning_Post() {
  356. // 验证登录
  357. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  358. if !b_ {
  359. c.Ctx.Redirect(302, "Login")
  360. return
  361. }
  362. T_tp, _ := c.GetInt("T_tp")
  363. T_sn := c.GetString("T_sn")
  364. T_id, _ := c.GetInt("T_id")
  365. T_Ut := c.GetString("T_Ut")
  366. T_Remark := c.GetString("T_Remark")
  367. r_Device, err := Device.Read_Device_ByT_sn(T_sn)
  368. if err != nil {
  369. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "SN 错误!!!"}
  370. c.ServeJSON()
  371. return
  372. }
  373. // 获取 传感器 参数
  374. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, T_id)
  375. if !is {
  376. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "编号 错误!!!"}
  377. c.ServeJSON()
  378. return
  379. }
  380. t1, _ := time.ParseInLocation("2006-01-02 15:04:05", T_Ut, time.Local) // +8 时差
  381. t_c := Warning.Warning{
  382. T_pid: admin_r.T_pid,
  383. T_tp: T_tp,
  384. T_sn: T_sn,
  385. T_D_name: r_Device.T_devName,
  386. T_id: T_id,
  387. T_DS_name: DeviceSensor_r.T_name,
  388. T_Ut: t1,
  389. T_State: 1,
  390. T_Remark: T_Remark,
  391. }
  392. Warning.Add_Warning(t_c)
  393. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  394. c.ServeJSON()
  395. return
  396. }
  397. // DeviceWarning_Del 删除报警
  398. func (c *DeviceController) DeviceWarning_Del() {
  399. // 验证登录
  400. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  401. if !b_ {
  402. c.Ctx.Redirect(302, "Login")
  403. return
  404. }
  405. Id, _ := c.GetInt("Id")
  406. if Id > 1000 {
  407. Warning.Delete_Warning(admin_r.T_pid, Id)
  408. }
  409. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  410. c.ServeJSON()
  411. return
  412. }
  413. // DeviceWarning_DelS 批量删除
  414. func (c *DeviceController) DeviceWarning_DelS() {
  415. // 验证登录
  416. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  417. if !b_ {
  418. c.Ctx.Redirect(302, "Login")
  419. return
  420. }
  421. type SelectedItem struct {
  422. Id string `json:"id"`
  423. Ut string `json:"ut"` // 假设"ut"字段是时间戳字符串
  424. }
  425. type SelectedItems struct {
  426. SelectedIds []SelectedItem `json:"selectedIds"`
  427. }
  428. var SelectedIds SelectedItems
  429. //var selectedIds map[string]any
  430. err := json.Unmarshal(c.Ctx.Input.RequestBody, &SelectedIds)
  431. log.Println(SelectedIds.SelectedIds)
  432. for _, v := range SelectedIds.SelectedIds {
  433. Warning.Delete_Warning_List(v.Id, v.Ut, admin_r.T_pid)
  434. }
  435. if err != nil {
  436. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "数据格式错误!!!"}
  437. c.ServeJSON()
  438. } else {
  439. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  440. c.ServeJSON()
  441. }
  442. }
  443. // Device_Copy 复制并添加
  444. func (c *DeviceController) Device_Copy() {
  445. // 验证登录
  446. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  447. if !b_ {
  448. c.Ctx.Redirect(302, "Login")
  449. return
  450. }
  451. type T_data struct {
  452. T_sn string `json:"t_sn"`
  453. T_id int `json:"t_id"`
  454. T_Rh any `json:"t_rh"`
  455. T_Site string `json:"t_site"`
  456. T_T any `json:"t_t"`
  457. CreateTime string `json:"createTime"`
  458. }
  459. var data T_data
  460. json.Unmarshal(c.Ctx.Input.RequestBody, &data)
  461. r := Device.Read_DeviceParameter_SNNo(data.T_sn)
  462. parse, _ := time.Parse("2006-01-02 15:04:05", data.CreateTime)
  463. data.CreateTime = parse.Add(time.Duration(r[0].T_saveT) * time.Second).Format("2006-01-02 15:04:05")
  464. itoa := strconv.Itoa(data.T_id)
  465. Device.Copy_DeviceData(data.T_sn, itoa, data.T_Rh, data.T_T, data.T_Site, data.CreateTime)
  466. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: data}
  467. c.ServeJSON()
  468. }
  469. // DeviceWarningUpdate 修改报警
  470. func (c *DeviceController) DeviceWarningUpdate() {
  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. var data struct {
  478. ColumnName string `json:"columnName"`
  479. NewValue string `json:"newValue"`
  480. RowId string `json:"rowId"`
  481. T_Ut string `json:"T_Ut"`
  482. SN string `json:"sn"`
  483. }
  484. json.Unmarshal(c.Ctx.Input.RequestBody, &data)
  485. log.Println(admin_r.T_pid)
  486. Warning.Update_DeviceParameter_Warning(data.ColumnName, data.NewValue, data.RowId, data.T_Ut)
  487. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: data}
  488. c.ServeJSON()
  489. }
  490. // GetDeviceALLSN 根据pid获取所有sn
  491. func (c *DeviceController) GetDeviceALLSN() {
  492. // 验证登录
  493. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  494. if !b_ {
  495. c.Ctx.Redirect(302, "Login")
  496. return
  497. }
  498. r := Device.Read_Device_All_SN(admin_r.T_pid)
  499. log.Println(r)
  500. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r}
  501. c.ServeJSON()
  502. }
  503. // GetDeviceALLTID 根据SN获取所有探头
  504. func (c *DeviceController) GetDeviceALLTID() {
  505. // 验证登录
  506. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  507. if !b_ {
  508. c.Ctx.Redirect(302, "Login")
  509. return
  510. }
  511. getString := c.GetString("sn")
  512. r := Device.Get_DeviceSensor_Tid_ByT_sn(getString)
  513. c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置成功", Data: r}
  514. c.ServeJSON()
  515. }
  516. // DeviceWarningAdd 复制添加报警
  517. func (c *DeviceController) DeviceWarningAdd() {
  518. // 验证登录
  519. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  520. if !b_ {
  521. c.Ctx.Redirect(302, "Login")
  522. return
  523. }
  524. var data struct {
  525. T_tp_name string `json:"column1"`
  526. T__d_name string `json:"column2"`
  527. T_DS_name string `json:"column3"`
  528. T_Remark string `json:"column4"`
  529. T_State string `json:"column5"`
  530. T_Ut string `json:"column6"`
  531. RowId string `json:"rowId"`
  532. SN string `json:"sn"`
  533. T_id string `json:"t_id"`
  534. }
  535. json.Unmarshal(c.Ctx.Input.RequestBody, &data)
  536. atoi, _ := strconv.Atoi(data.T_id)
  537. r_Device, err := Device.Read_Device_ByT_sn(data.SN)
  538. if err != nil {
  539. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "SN 错误!!!"}
  540. c.ServeJSON()
  541. return
  542. }
  543. T_tp := Warning.Read_WarningType(data.T_tp_name)
  544. t1, _ := time.ParseInLocation("2006-01-02 15:04:05", data.T_Ut, time.Local) // +8 时差
  545. t_c := Warning.Warning{
  546. T_pid: admin_r.T_pid,
  547. T_tp: T_tp,
  548. T_sn: data.SN,
  549. T_D_name: r_Device.T_devName,
  550. T_id: atoi,
  551. T_DS_name: data.T_DS_name,
  552. T_Ut: t1,
  553. T_State: 1,
  554. T_Remark: data.T_Remark,
  555. }
  556. Warning.Add_Warning(t_c)
  557. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  558. c.ServeJSON()
  559. return
  560. }
  561. // SiftWarningType 根据报警类型筛选
  562. func (c *DeviceController) SiftWarningType() {
  563. // 验证登录
  564. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  565. if !b_ {
  566. c.Ctx.Redirect(302, "Login")
  567. return
  568. }
  569. }