123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package admin
- import (
- "fmt"
- "server/dao"
- "server/model/common/request"
- "server/model/common/response"
- "server/utils/protocol"
- "strconv"
- )
- type DeviceService struct{}
- func (ds *DeviceService) QueryAllDevices() ([]dao.Device, error) {
- return dao.QueryAllDevices()
- }
- func (ds *DeviceService) QueryDeviceList(info request.DeviceSearch) ([]dao.Device, int64, error) {
- limit := info.PageInfo.PageSize
- offset := info.PageInfo.PageSize * (info.PageInfo.Page - 1)
- return dao.QueryDeviceList(info.Sn, info.Name, info.Genre, limit, offset)
- }
- func (ds *DeviceService) GenerateDeviceFile(id int) (data response.DeviceFileData, err error) {
- devices, err := dao.QueryDeviceByTunnelId(id)
- if err != nil {
- return data, err
- }
- for _, d := range devices {
- if d.DeviceGenre.Type == "环境设备" {
- envDev := response.EnvDev{
- Sn: d.Sn,
- Name: d.Name,
- Address: d.Address,
- TaskTime: d.TaskTime,
- WaitTime: d.WaitTime,
- SerialId: d.SerialId,
- }
- data.EnvDev = append(data.EnvDev, envDev)
- } else if d.DeviceGenre.Type == "开关设备" {
- lampDev := response.LampDev{
- Sn: d.Sn,
- Name: d.Name,
- Address: d.Address,
- RadarId: d.RadarId,
- SerialId: d.SerialId,
- }
- data.LampDev = append(data.LampDev, lampDev)
- }
- }
- return data, nil
- }
- func (ds *DeviceService) DeviceSwitch(info request.SwitchTunnel) error {
- device, err := dao.QueryDeviceByTunnelSnAndRadarId(info.TunnelSn, info.RadarId)
- if err != nil {
- return err
- }
- err = dao.UpdateRelayState(int(device.ID), info.RelayId, info.State)
- if err != nil {
- return err
- }
- var i int
- if info.State {
- i = 1
- } else {
- i = 0
- }
- data := strconv.Itoa(info.RadarId) + strconv.Itoa(info.RelayId) + strconv.Itoa(i)
- err = MqttService.Publish(MqttService.GetTopic(info.TunnelSn, protocol.TopicSwitchControl), []byte(data))
- if err != nil {
- return fmt.Errorf("error updating: %v", err)
- }
- return nil
- }
- func (ds *DeviceService) CreateDevice(device dao.Device) error {
- tunnel, err := dao.QueryTunnelById(device.TunnelId)
- if err != nil {
- return err
- }
- genre, err := dao.QueryDeviceGenreByID(device.Genre)
- if err != nil {
- return err
- }
- if genre.Name == tunnel.SwitchType && genre.Type == "开关设备" {
- return fmt.Errorf("开关设备需安装隧道需求添加")
- }
- return device.CreateDevice()
- }
- func (ds *DeviceService) UpdateDevice(device dao.Device) error {
- return device.UpdateDevice()
- }
- func (ds *DeviceService) DeleteDevice(id int) error {
- return dao.DeleteDevice(id)
- }
|