DeviceController.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. "math"
  15. "strings"
  16. "time"
  17. )
  18. // DeviceController 设备管理页面
  19. type DeviceController struct {
  20. beego.Controller
  21. }
  22. // DeviceManagerHtml 返回页面
  23. func (c *DeviceController) DeviceManagerHtml() {
  24. //验证是否登录
  25. is, admin := lib.VerificationController(&c.Controller)
  26. if !is {
  27. return
  28. }
  29. //类名列表
  30. classList := Company.Read_CompanyClass_All(admin.T_pid, "")
  31. c.Data["Class_List"] = classList
  32. c.TplName = "Device/Device.html"
  33. }
  34. // DeviceList 获取设备列表
  35. func (c *DeviceController) DeviceList() {
  36. tName := c.GetString("deviceName") //设备名称
  37. page, _ := c.GetInt64("currentPage") //当前页码
  38. tClassify, _ := c.GetInt("deviceClass") //设备分类
  39. is, admin := lib.VerificationController(&c.Controller)
  40. fmt.Println("当前用户的PID为:", admin.T_pid)
  41. if !is {
  42. //用户未登录
  43. c.Data["json"] = lib.JSONS{202, "身份认证失效", nil}
  44. c.ServeJSON()
  45. return
  46. }
  47. device_list, count := Device.Read_DeviceSensor_List_T_ClassOr(admin.T_pid, tClassify, tName, tName, -1, int(page), conf.Page_size)
  48. var pageCount int
  49. if (int(count) % conf.Page_size) != 0 {
  50. pageCount = int(count) / conf.Page_size
  51. pageCount++
  52. }
  53. c.Data["json"] = lib.PageHelper{int(count), pageCount, int(page), int(page) >= pageCount, page <= 1, device_list}
  54. c.ServeJSON()
  55. return
  56. }
  57. // CompanyClass 获取公司设备类目
  58. func (c *DeviceController) CompanyClass() {
  59. is, admin := lib.VerificationController(&c.Controller)
  60. if !is {
  61. c.Data["json"] = lib.JSONS{202, "用户未登录", nil}
  62. c.ServeJSON()
  63. return
  64. }
  65. //类名列表
  66. classList := Company.Read_CompanyClass_All(admin.T_pid, "")
  67. c.Data["json"] = classList
  68. c.ServeJSON()
  69. }
  70. // DataRepeat 数据重传
  71. func (c *DeviceController) DataRepeat() {
  72. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  73. if !b_ {
  74. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  75. c.ServeJSON()
  76. return
  77. }
  78. t := MqttServer.DataRepeat_C{}
  79. bytes, _ := ioutil.ReadAll(c.Ctx.Request.Body)
  80. json.Unmarshal(bytes, &t)
  81. fmt.Println("浏览器接收数据:", t)
  82. s, _ := time.Parse("2006-01-02 15:04:05", t.StartTime)
  83. e, _ := time.Parse("2006-01-02 15:04:05", t.EndTime)
  84. //发送MQTT
  85. for k, v := range t.Sns {
  86. topic := fmt.Sprintf("/pub/%s", k)
  87. 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}}
  88. msg, _ := json.Marshal(repeatPub)
  89. mqttId := strings.Split(Device.ReadDeviceMqttId(k), "\"")[0]
  90. client := MqttServer.GetMqttClient(mqttId)
  91. for i := 0; i < 3; i++ {
  92. time.Sleep(time.Second * time.Duration(i+1))
  93. MqttServer.PubMqttMessage(client, topic, msg)
  94. }
  95. client.Disconnect()
  96. client.Terminate()
  97. }
  98. c.Data["json"] = lib.JSONS{200, "数据重传成功", nil}
  99. c.ServeJSON()
  100. return
  101. }
  102. // ReadDeviation 读取偏差值
  103. func (c *DeviceController) ReadDeviation() {
  104. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  105. if !b_ {
  106. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  107. c.ServeJSON()
  108. return
  109. }
  110. t := make(map[string][]int)
  111. bytes, _ := ioutil.ReadAll(c.Ctx.Request.Body)
  112. json.Unmarshal(bytes, &t)
  113. fmt.Println("浏览器接收数据:", t)
  114. //MQTT发送
  115. fmt.Println("发送MQTT t:", t)
  116. deviation := make(chan string, 10)
  117. var count = 0
  118. for k, v := range t {
  119. topicSub := fmt.Sprintf("/sub/%s", k)
  120. fmt.Println(v)
  121. topicPub := fmt.Sprintf("/pub/%s", k)
  122. mqttId := Device.ReadDeviceMqttId(k)
  123. client := MqttServer.GetMqttClient(mqttId)
  124. MqttServer.Subscript(client, topicSub, deviation, "\"type\":7,")
  125. pubData, _ := json.Marshal(MqttServer.Deviation_Pub{
  126. Sn: k,
  127. Type: 7,
  128. Mid: time.Now().Unix(),
  129. Data: v,
  130. })
  131. MqttServer.PubMqttMessage(client, topicPub, pubData)
  132. count++
  133. }
  134. deviceRepeatData := make([]string, 0)
  135. select {
  136. case v := <-deviation:
  137. fmt.Println("channel收到数据:", v)
  138. fmt.Println("count:", count)
  139. deviceRepeatData = append(deviceRepeatData, v)
  140. count--
  141. if count <= 0 {
  142. close(deviation)
  143. break
  144. }
  145. }
  146. fmt.Println("响应数据:", deviceRepeatData)
  147. c.Data["json"] = lib.JSONS{200, "偏差值上传成功", deviceRepeatData}
  148. c.ServeJSON()
  149. return
  150. }
  151. // WriteDeviation 设置偏差值
  152. func (c *DeviceController) WriteDeviation() {
  153. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  154. if !b_ {
  155. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  156. c.ServeJSON()
  157. return
  158. }
  159. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  160. data := make([]MqttServer.Deviation_Sub, 0)
  161. fmt.Println("请求json:", string(bytes))
  162. json.Unmarshal(bytes, &data)
  163. for _, v := range data {
  164. go func(v MqttServer.Deviation_Sub) {
  165. v.Type = 8
  166. v.Mid = int(time.Now().Unix())
  167. mqttid := Device.ReadDeviceMqttId(v.Sn)
  168. cli := MqttServer.GetMqttClient(mqttid)
  169. bytes, _ := json.Marshal(v)
  170. for i := 0; i < 3; i++ {
  171. time.Sleep(time.Second * time.Duration(i+1))
  172. MqttServer.PubMqttMessage(cli, fmt.Sprintf("/pub/%s", v.Sn), bytes)
  173. }
  174. cli.Disconnect()
  175. cli.Terminate()
  176. }(v)
  177. }
  178. c.Data["json"] = lib.JSONS{200, "设置偏差值成功!", nil}
  179. c.ServeJSON()
  180. return
  181. }
  182. // ReadSensor 读取偏差值
  183. func (c *DeviceController) ReadSensor() {
  184. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  185. if !b_ {
  186. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  187. c.ServeJSON()
  188. return
  189. }
  190. t := make(map[string][]int)
  191. bytes, _ := ioutil.ReadAll(c.Ctx.Request.Body)
  192. json.Unmarshal(bytes, &t)
  193. fmt.Println("浏览器接收数据:", t)
  194. //MQTT发送
  195. fmt.Println("发送MQTT t:", t)
  196. deviation := make(chan string, 10)
  197. var count = 0
  198. for k, v := range t {
  199. topicSub := fmt.Sprintf("/sub/%s", k)
  200. fmt.Println(v)
  201. topicPub := fmt.Sprintf("/pub/%s", k)
  202. mqttId := Device.ReadDeviceMqttId(k)
  203. client := MqttServer.GetMqttClient(mqttId)
  204. MqttServer.Subscript(client, topicSub, deviation, "\"type\":5,")
  205. pubData, _ := json.Marshal(MqttServer.Deviation_Pub{
  206. Sn: k,
  207. Type: 5,
  208. Mid: time.Now().Unix(),
  209. Data: v,
  210. })
  211. MqttServer.PubMqttMessage(client, topicPub, pubData)
  212. count++
  213. }
  214. deviceRepeatData := make([]string, 0)
  215. select {
  216. case v := <-deviation:
  217. fmt.Println("channel收到数据:", v)
  218. fmt.Println("count:", count)
  219. deviceRepeatData = append(deviceRepeatData, v)
  220. count--
  221. if count <= 0 {
  222. close(deviation)
  223. break
  224. }
  225. }
  226. fmt.Println("响应数据:", deviceRepeatData)
  227. c.Data["json"] = lib.JSONS{200, "偏差值上传成功", deviceRepeatData}
  228. c.ServeJSON()
  229. return
  230. }
  231. // WriteDeviation 设置偏差值
  232. func (c *DeviceController) WriteSensor() {
  233. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  234. if !b_ {
  235. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  236. c.ServeJSON()
  237. return
  238. }
  239. bytes, _ := io.ReadAll(c.Ctx.Request.Body)
  240. data := make([]MqttServer.Sensor_Sub, 0)
  241. fmt.Println("请求json:", string(bytes))
  242. json.Unmarshal(bytes, &data)
  243. for _, v := range data {
  244. go func(v MqttServer.Sensor_Sub) {
  245. v.Type = 6
  246. v.Mid = int(time.Now().Unix())
  247. mqttid := Device.ReadDeviceMqttId(v.Sn)
  248. cli := MqttServer.GetMqttClient(mqttid)
  249. bytes, _ := json.Marshal(v)
  250. for i := 0; i < 3; i++ {
  251. time.Sleep(time.Second * time.Duration(i+1))
  252. MqttServer.PubMqttMessage(cli, fmt.Sprintf("/pub/%s", v.Sn), bytes)
  253. }
  254. cli.Disconnect()
  255. cli.Terminate()
  256. }(v)
  257. }
  258. c.Data["json"] = lib.JSONS{200, "设置偏差值成功!", nil}
  259. c.ServeJSON()
  260. return
  261. }
  262. // 列表 -
  263. func (c *DeviceController) DeviceWarning_List_html() {
  264. // 验证登录
  265. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  266. if !b_ {
  267. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  268. c.ServeJSON()
  269. return
  270. }
  271. c.Data["Admin_r"] = admin_r
  272. page, _ := c.GetInt("page")
  273. println(page)
  274. if page < 1 {
  275. page = 1
  276. }
  277. c.Data["Admin_r"] = admin_r
  278. bindSN := c.GetStrings("bindSN")
  279. tpList := c.GetStrings("tpList")
  280. T_sn := c.GetString("T_sn")
  281. if len(T_sn) != 0 {
  282. bindSN = append(bindSN, T_sn)
  283. c.Data["T_sn"] = T_sn
  284. }
  285. Time_start := c.GetString("Time_start")
  286. Time_end := c.GetString("Time_end")
  287. if len(Time_start) == 0 && len(Time_end) == 0 {
  288. Time_start = time.Now().Format("2006-01-02") + " 00:00:00"
  289. Time_end = time.Now().Format("2006-01-02") + " 23:59:59"
  290. }
  291. c.Data["Time_start"] = Time_start
  292. c.Data["Time_end"] = Time_end
  293. //c.Data["Class_List"] = Device.Read_DeviceWarningList_All_1()
  294. //T_Title := ""
  295. //if Class_1 > 0 {
  296. // T_Title = Device.Read_DeviceWarningList_ById(Class_1).T_name
  297. //}
  298. var cnt int64
  299. DeviceWarning_List, cnt := Warning.Read_Warning_List(admin_r.T_pid, bindSN, tpList, Time_start, Time_end, page, 10)
  300. c.Data["List"] = DeviceWarning_List
  301. page_size := math.Ceil(float64(cnt) / float64(conf.Page_size))
  302. c.Data["Page"] = page
  303. c.Data["Page_size"] = page_size
  304. c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
  305. c.Data["cnt"] = cnt
  306. c.TplName = "Device/DeviceWarning.html"
  307. }
  308. func (c *DeviceController) DeviceWarning_() {
  309. // 验证登录
  310. //b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  311. //if !b_ {
  312. // c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  313. // c.ServeJSON()
  314. // return
  315. //}
  316. c.Data["WarningType"] = Warning.Read_WarningType_All()
  317. c.TplName = "Device/DeviceWarning-.html"
  318. }
  319. func (c *DeviceController) DeviceWarning_Post() {
  320. // 验证登录
  321. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  322. if !b_ {
  323. c.Ctx.Redirect(302, "Login")
  324. return
  325. }
  326. T_tp, _ := c.GetInt("T_tp")
  327. T_sn := c.GetString("T_sn")
  328. T_id, _ := c.GetInt("T_id")
  329. T_Ut := c.GetString("T_Ut")
  330. T_Remark := c.GetString("T_Remark")
  331. r_Device, err := Device.Read_Device_ByT_sn(T_sn)
  332. if err != nil {
  333. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "SN 错误!!!"}
  334. c.ServeJSON()
  335. return
  336. }
  337. // 获取 传感器 参数
  338. DeviceSensor_r, is := Device.Read_DeviceSensor_ByT_sn(r_Device.T_sn, T_id)
  339. if !is {
  340. c.Data["json"] = lib.JSONS{Code: 201, Msg: "E!", Data: "编号 错误!!!"}
  341. c.ServeJSON()
  342. return
  343. }
  344. t1, _ := time.ParseInLocation("2006-01-02 15:04:05", T_Ut, time.Local) // +8 时差
  345. t_c := Warning.Warning{
  346. T_pid: admin_r.T_pid,
  347. T_tp: T_tp,
  348. T_sn: T_sn,
  349. T_D_name: r_Device.T_devName,
  350. T_id: T_id,
  351. T_DS_name: DeviceSensor_r.T_name,
  352. T_Ut: t1,
  353. T_State: 1,
  354. T_Remark: T_Remark,
  355. }
  356. Warning.Add_Warning(t_c)
  357. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  358. c.ServeJSON()
  359. return
  360. }
  361. func (c *DeviceController) DeviceWarning_Del() {
  362. // 验证登录
  363. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  364. if !b_ {
  365. c.Ctx.Redirect(302, "Login")
  366. return
  367. }
  368. Id, _ := c.GetInt("Id")
  369. if Id > 1000 {
  370. Warning.Delete_Warning(admin_r.T_pid, Id)
  371. }
  372. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  373. c.ServeJSON()
  374. return
  375. }