Device.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package controllers
  2. import (
  3. "Yunlot/Handle"
  4. "Yunlot/lib"
  5. "Yunlot/logs"
  6. "Yunlot/models/Device"
  7. "Yunlot/models/Product"
  8. beego "github.com/beego/beego/v2/server/web"
  9. "time"
  10. )
  11. type DeviceController struct {
  12. beego.Controller
  13. }
  14. func (c *DeviceController) List() {
  15. PageIndex, _ := c.GetInt("PageIndex", 0)
  16. PageSize, _ := c.GetInt("PageSize", 10)
  17. Devicer := Device.Device{}
  18. c.ParseForm(&Devicer)
  19. ProductTyper := Product.ProductType{T_ProductID: Devicer.T_ProductID}
  20. if !ProductTyper.Read() {
  21. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID E!"}
  22. c.ServeJSON()
  23. return
  24. }
  25. Device_r, Total := Devicer.Lists(PageIndex, PageSize)
  26. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: lib.C_Page(Device_r, PageIndex, PageSize, Total)}
  27. c.ServeJSON()
  28. return
  29. }
  30. func (c *DeviceController) Add() {
  31. // 验证秘钥
  32. ProductKey := c.GetString("ProductKey")
  33. ProductID := c.GetString("ProductID")
  34. ProductType_r := Product.ProductType{T_ProductID: ProductID}
  35. if !ProductType_r.Read() {
  36. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductID!"}
  37. c.ServeJSON()
  38. return
  39. }
  40. if ProductType_r.T_akey != ProductKey {
  41. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "ProductKey!"}
  42. c.ServeJSON()
  43. return
  44. }
  45. // 创建sn
  46. T_sn := c.GetString("T_sn")
  47. Device_r := Device.Device{T_ProductID: ProductID, T_sn: T_sn}
  48. //
  49. CreateNum, _ := c.GetInt("CreateNum", 1)
  50. if CreateNum == 1 || len(Device_r.T_sn) >= 10 {
  51. // 单个
  52. if !Device_r.CreateSn(1) {
  53. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_sn 重复!"}
  54. c.ServeJSON()
  55. return
  56. }
  57. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_r}
  58. c.ServeJSON()
  59. return
  60. } else {
  61. // 多个
  62. var Device_List []Device.Device
  63. for i := 1; i <= CreateNum; i++ {
  64. Device_rr := Device_r
  65. Device_rr.CreateSn(i)
  66. Device_List = append(Device_List, Device_rr)
  67. }
  68. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Device_List}
  69. c.ServeJSON()
  70. return
  71. }
  72. }
  73. func (c *DeviceController) Update() {
  74. Devicer := Device.Device{T_sn: c.GetString("T_sn")}
  75. Devicer.Read_Tidy()
  76. c.ParseForm(&Devicer)
  77. if !Devicer.Update("T_state", "T_project") {
  78. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  79. c.ServeJSON()
  80. return
  81. }
  82. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  83. c.ServeJSON()
  84. return
  85. }
  86. //
  87. //func (c *DeviceController) Delete() {
  88. // Devicer := Device.Device{}
  89. // c.ParseForm(&Devicer)
  90. //
  91. // if !Devicer.Delete() {
  92. // c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "E!"}
  93. // c.ServeJSON()
  94. // return
  95. // }
  96. //
  97. // c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  98. // c.ServeJSON()
  99. // return
  100. //}
  101. func (c *DeviceController) Get() {
  102. Devicer := Device.Device{}
  103. c.ParseForm(&Devicer)
  104. if !Devicer.Read_Tidy() {
  105. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "SN E!"}
  106. c.ServeJSON()
  107. return
  108. }
  109. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: Devicer}
  110. c.ServeJSON()
  111. return
  112. }
  113. func (c *DeviceController) Push() {
  114. Devicer := Device.Device{}
  115. c.ParseForm(&Devicer)
  116. T_data := c.GetString("T_data")
  117. if len(T_data) == 0 {
  118. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "T_data E!"}
  119. c.ServeJSON()
  120. return
  121. }
  122. if !Devicer.Read_Tidy() {
  123. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "SN E!"}
  124. c.ServeJSON()
  125. return
  126. }
  127. Handle.PushHandle(&Devicer, Devicer.T_sn, T_data)
  128. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!"}
  129. c.ServeJSON()
  130. return
  131. }
  132. func (c *DeviceController) DataList() {
  133. DeviceData_Form := Device.DeviceData_Form{}
  134. c.ParseForm(&DeviceData_Form)
  135. if len(DeviceData_Form.T_sn) < 1 {
  136. c.Data["json"] = lib.JSONS{Code: lib.Error, Msg: "T_sn !"}
  137. c.ServeJSON()
  138. return
  139. }
  140. if len(DeviceData_Form.T_jointTab) < 1 {
  141. c.Data["json"] = lib.JSONS{Code: lib.Error, Msg: "T_jointTab !"}
  142. c.ServeJSON()
  143. return
  144. }
  145. JSONR_r := Device.Data_List(DeviceData_Form.T_sn+"_"+DeviceData_Form.T_jointTab, DeviceData_Form.T_jsonFind, DeviceData_Form.T_jsonSort, DeviceData_Form.PageIndex, DeviceData_Form.PageSize)
  146. c.Data["json"] = JSONR_r
  147. c.ServeJSON()
  148. return
  149. }
  150. func (c *DeviceController) GetLog() {
  151. Devicer := Device.Device{}
  152. c.ParseForm(&Devicer)
  153. if !Devicer.Read_Tidy() {
  154. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "SN E!"}
  155. c.ServeJSON()
  156. return
  157. }
  158. v, is := logs.DeviceRealLogMap[Devicer.T_sn]
  159. if !is {
  160. logs.DeviceRealLogMap[Devicer.T_sn] = logs.DeviceRealLogR{Time: time.Now(), Data: []string{}}
  161. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: []string{}}
  162. c.ServeJSON()
  163. return
  164. }
  165. v.Time = time.Now()
  166. logs.DeviceRealLogMap[Devicer.T_sn] = v
  167. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: v.Data}
  168. c.ServeJSON()
  169. return
  170. }