123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package gateway
- import (
- errors "confrontation-training/err"
- "confrontation-training/global"
- deviceModel "confrontation-training/models/gateway"
- "confrontation-training/response"
- deviceService "confrontation-training/service/device"
- "fmt"
- "github.com/gin-gonic/gin"
- "strings"
- )
- type DeviceService struct {
- deviceService.DeviceService
- }
- func GetDeviceService() *DeviceService {
- return &DeviceService{}
- }
- // DeviceAdd
- // PingExample confrontation-training
- // @Summary 新增设备
- // @Schemes
- // @Description 新增设备
- // @Tags 设备管理
- // @Param device body string true "type:类型 0脑电1心电;mac:设备MAC地址"
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/add [post]
- func (d *DeviceService) DeviceAdd(c *gin.Context) {
- var param deviceModel.DeviceAddParam
- err := c.ShouldBindJSON(¶m)
- if err != nil {
- fmt.Printf("参数格式化异常:%s", err.Error())
- response.Failed(errors.ParamInvalid, c)
- return
- }
- if _, count := d.FindDeviceByMac(param.Mac); count > 0 {
- response.Failed(errors.DeviceAddFailed, c)
- return
- }
- if result := d.CreateDevice(param); result.Error == nil {
- response.Success(errors.DeviceAddSuccess, result.RowsAffected, c)
- return
- } else {
- response.Failed(errors.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
- return
- }
- }
- // DeviceRemove
- // PingExample confrontation-training
- // @Summary 移除设备
- // @Schemes
- // @Description 移除设备
- // @Tags 设备管理
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/:mac/remove [delete]
- func (d *DeviceService) DeviceRemove(c *gin.Context) {
- mac := c.Param("mac")
- count := d.RemoveDevice(mac)
- if count == 0 {
- response.Failed(errors.DeviceRemoveFailed, c)
- } else {
- response.Success(errors.DeviceRemoveSuccess, count, c)
- }
- return
- }
- // DeviceList
- // PingExample confrontation-training
- // @Summary 设备列表
- // @Schemes
- // @Description 设备列表
- // @Tags 设备管理
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/list/:type [get]
- func (d *DeviceService) DeviceList(c *gin.Context) {
- deviceInfos, _ := d.FindDeviceByType(c.Param("type"))
- response.Success(errors.FindSuccess, deviceInfos, c)
- }
- // DeviceConnected 连接列表
- // PingExample confrontation-training
- // @Summary 连接列表
- // @Schemes
- // @Description 连接列表
- // @Tags 设备管理
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/connected [get]
- func (d *DeviceService) DeviceConnected(c *gin.Context) {
- url := global.Config.Gateway.BaseUrl + global.Config.Gateway.ConnectedUrl
- list, err := d.DeviceConnectedList(url)
- if err != nil {
- response.Failed("查询失败", c)
- }
- response.Success(errors.FindSuccess, list, c)
- return
- }
- // Disconnect
- // PingExample confrontation-training
- // @Summary 断开连接
- // @Schemes
- // @Description 断开连接
- // @Tags 设备管理
- // @Accept json
- // @Produce json
- // @Success 200 {string} string "ok"
- // @Router /v1/device/:mac/disconnect [get]
- func (d *DeviceService) Disconnect(c *gin.Context) {
- mac := c.Param("mac")
- disconnectUrl := global.Config.Gateway.BaseUrl + global.Config.Gateway.DisconnectUrl
- disconnectUrl = strings.Replace(disconnectUrl, "MAC", mac, -1)
- resp := d.DisconnectDevice(disconnectUrl)
- if resp.StatusCode != 200 {
- fmt.Println("断开连接失败")
- response.Failed("断开连接失败", c)
- } else {
- response.Success("断开连接", mac, c)
- }
- return
- }
|