123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package gateway
- import (
- errors "confrontation-training/err"
- "confrontation-training/global"
- deviceModel "confrontation-training/models/gateway"
- gatewaym "confrontation-training/models/gateway"
- "confrontation-training/response"
- deviceService "confrontation-training/service/device"
- "fmt"
- "github.com/gin-gonic/gin"
- "reflect"
- "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())
- global.Log4J.Info("参数格式化异常:" + 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 {
- service := GetDeviceService()
- d, i := service.FindDeviceByType("")
- global.DeviceMap = make(map[string]gatewaym.DeviceInfo)
- if i > 0 {
- for j := 0; j < len(d); j++ {
- global.DeviceMap[d[j].Mac] = d[j]
- }
- }
- 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)
- }
- service := GetDeviceService()
- device, i := service.FindDeviceByType("")
- global.DeviceMap = make(map[string]gatewaym.DeviceInfo)
- if i > 0 {
- for j := 0; j < len(device); j++ {
- global.DeviceMap[device[j].Mac] = device[j]
- }
- }
- 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.Query("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
- listResult, err := d.DeviceConnectedList(url)
- if err != nil {
- response.Failed("查询失败", c)
- return
- }
- a := make(map[string]interface{})
- a = listResult
- b := a["nodes"]
- var list []map[string]interface{}
- var connectedList []deviceModel.ConnectedDevice
- if reflect.ValueOf(b).Kind() == reflect.Slice {
- s := reflect.ValueOf(b)
- for i := 0; i < s.Len(); i++ {
- ele := s.Index(i)
- list = append(list, ele.Interface().(map[string]interface{}))
- }
- for _, m := range list {
- mm := make(map[string]interface{})
- mm = m
- mac := mm["id"]
- if device, count := d.FindDeviceByMac(mac.(string)); count > 0 {
- var connDevice = deviceModel.ConnectedDevice{
- MAC: mac.(string),
- DeviceType: device.Type,
- Name: device.Name,
- }
- if connDevice.Name == "" {
- if device.Type == "1" {
- connDevice.Name = "BW-EGC-01"
- } else if device.Type == "0" {
- connDevice.Name = "MIND" + strings.ReplaceAll(connDevice.MAC[len(connDevice.MAC)-6:], ":", "")
- }
- }
- connectedList = append(connectedList, connDevice)
- }
- }
- }
- response.Success(errors.FindSuccess, connectedList, 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("断开连接失败")
- global.Log4J.Info("断开连接失败")
- response.Failed("断开连接失败", c)
- } else {
- response.Success("断开连接", mac, c)
- }
- return
- }
|