device.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package api
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "server/dao"
  5. "server/model"
  6. "server/service"
  7. "server/utils"
  8. )
  9. func LoadData(c *gin.Context) {
  10. data, err := utils.LoadData()
  11. if err != nil {
  12. model.FailWithMessage(err.Error(), c)
  13. return
  14. }
  15. model.OkWithData(data, c)
  16. }
  17. func SaveDevice(c *gin.Context) {
  18. var regions []dao.Region
  19. err := c.ShouldBindJSON(&regions)
  20. if err != nil {
  21. model.FailWithMessage(err.Error(), c)
  22. return
  23. }
  24. err = service.SaveData(regions)
  25. if err != nil {
  26. model.FailWithMessage(err.Error(), c)
  27. return
  28. }
  29. model.Ok(c)
  30. }
  31. // DeviceLoopSwitch 指定设备回路进行开关灯操作
  32. func DeviceLoopSwitch(c *gin.Context) {
  33. var deviceLoop dao.DeviceLoop
  34. err := c.ShouldBindJSON(&deviceLoop)
  35. if err != nil {
  36. model.FailWithMessage(err.Error(), c)
  37. return
  38. }
  39. err = service.DeviceLoopSwitch(deviceLoop)
  40. if err != nil {
  41. model.FailWithMessage(err.Error(), c)
  42. return
  43. }
  44. model.Ok(c)
  45. }
  46. // DeviceSwitch 指定设备进行开关灯操作
  47. func DeviceSwitch(c *gin.Context) {
  48. var req model.DeviceRequest
  49. err := c.ShouldBindJSON(&req)
  50. if err != nil {
  51. model.FailWithMessage(err.Error(), c)
  52. return
  53. }
  54. err = service.DeviceSwitch(req)
  55. if err != nil {
  56. model.FailWithMessage(err.Error(), c)
  57. return
  58. }
  59. model.Ok(c)
  60. }
  61. // BatchDeviceSwitch 设备批量开关
  62. func BatchDeviceSwitch(c *gin.Context) {
  63. var req model.DevicesRequest
  64. err := c.ShouldBindJSON(&req)
  65. if err != nil {
  66. model.FailWithMessage(err.Error(), c)
  67. return
  68. }
  69. err = service.DeviceBatchSwitch(req)
  70. if err != nil {
  71. model.FailWithMessage(err.Error(), c)
  72. return
  73. }
  74. model.Ok(c)
  75. }
  76. func GetOnlineDevice(c *gin.Context) {
  77. total, online := service.GetOnlineDevice()
  78. model.OkWithDetailed(gin.H{
  79. "total": total,
  80. "online": online,
  81. }, "ok", c)
  82. }
  83. func GetSunDevices(c *gin.Context) {
  84. devices, err := service.GetSunDevices()
  85. if err != nil {
  86. model.FailWithMessage(err.Error(), c)
  87. return
  88. }
  89. model.OkWithData(devices, c)
  90. }
  91. func UpdateRegisterAddress(c *gin.Context) {
  92. var id string
  93. err := c.ShouldBindJSON(&id)
  94. if err != nil {
  95. model.FailWithMessage(err.Error(), c)
  96. return
  97. }
  98. err = service.UpdateRegisterAddress(id)
  99. if err != nil {
  100. model.FailWithMessage(err.Error(), c)
  101. return
  102. }
  103. model.Ok(c)
  104. }