123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- package service
- import (
- "fmt"
- "net"
- "server/dao"
- "server/logger"
- "server/modbus"
- "server/model"
- "server/utils"
- )
- func SaveData(data []dao.Region) error {
- if len(data) == 0 {
- return fmt.Errorf("数据不能为空")
- }
- utils.SaveRegions(data)
- //err := utils.SaveData("static/data.json", data)
- //if err != nil {
- // return err
- //}
- // Remove the redundant return err statement.
- return nil // Return nil to indicate success
- }
- func DeviceSave(device dao.Device) error {
- regions, _ := utils.LoadData()
- for i, region := range regions {
- for j, d := range region.Devices {
- if device.Sn == d.Sn {
- regions[i].Devices[j] = device
- }
- }
- }
- err := SaveData(regions)
- if err != nil {
- return err
- }
- // Remove the redundant return err statement.
- return nil // Return nil to indicate success
- }
- func DevicesSave(device []dao.Device) error {
- regions, _ := utils.LoadData()
- regions[0].Devices = device
- err := SaveData(regions)
- if err != nil {
- return err
- }
- // Remove the redundant return err statement.
- return nil // Return nil to indicate success
- }
- func DeviceLoopSwitch(deviceLoop dao.DeviceLoop) error {
- data := modbus.DeviceLoopSwitch(deviceLoop.ID, deviceLoop.State)
- if conn1, ok := model.ConnectionMap1.Load(deviceLoop.DeviceId); ok {
- // 成功找到连接
- netConn := conn1.(net.Conn)
- _, dev, _ := utils.GetDataByDeviceId(deviceLoop.DeviceId)
- if dev.State == 0 {
- return fmt.Errorf("设备离线无法操作")
- }
- err := utils.WriteDevice(data, netConn)
- if err != nil {
- fmt.Printf("Write device error: %v\n", err)
- return err
- }
- } else {
- // 没有找到对应的连接
- logger.Get().Printf("Connection for key %s not found", deviceLoop.DeviceId)
- }
- return nil
- }
- func DeviceSwitch(req model.DeviceRequest) error {
- data := modbus.DeviceSwitch(req.Device.LoopNumber, req.State)
- if conn1, ok := model.ConnectionMap1.Load(req.Device.Sn); ok {
- // 成功找到连接
- netConn := conn1.(net.Conn)
- modbus.OperationCommand = data
- err := utils.WriteDevice(data, netConn)
- if err != nil {
- logger.Get().Errorf("Write device error: %v\n", err)
- return err
- }
- } else {
- // 没有找到对应的连接
- logger.Get().Printf("Connection for key %s not found", req.Device.Sn)
- }
- return nil
- }
- func DeviceBatchSwitch(req model.DevicesRequest) error {
- for _, device := range req.Devices {
- if device.State == 0 {
- continue
- }
- data := modbus.DeviceSwitch(device.LoopNumber, req.State)
- modbus.OperationCommand = data
- if conn1, ok := model.ConnectionMap1.Load(device.Sn); ok {
- // 成功找到连接
- netConn := conn1.(net.Conn)
- err := utils.WriteDevice(data, netConn)
- if err != nil {
- fmt.Printf("Write device error: %v\n", err)
- return err
- }
- } else {
- // 没有找到对应的连接
- logger.Get().Printf("Connection for key %s not found", device.Sn)
- }
- }
- return nil
- }
- // GetOnlineDevice 获取在线设备 总共设备 在线率
- func GetOnlineDevice() (total, online int) {
- devices, err := utils.GetOnlineDevices()
- if err != nil {
- logger.Get().Errorf("GetOnlineDevices err = %s\n", err.Error())
- return
- }
- allDevices, err := utils.GetAllDevices()
- if err != nil {
- logger.Get().Errorf("GetAllDevices err = %s\n", err.Error())
- return
- }
- return len(allDevices), len(devices)
- }
- func GetSunDevices() (devices []dao.Device, err error) {
- allDevices, err := utils.GetAllDevices()
- if err != nil {
- return nil, err
- }
- for _, device := range allDevices {
- if device.IsSun {
- devices = append(devices, device)
- }
- }
- return
- }
- func UpdateRegisterAddress(deviceId string) error {
- if conn1, ok := model.ConnectionMap1.Load(deviceId); ok {
- // 成功找到连接
- netConn := conn1.(net.Conn)
- err := utils.WriteDevice(modbus.UpdateRegisterAddress(), netConn)
- if err != nil {
- logger.Get().Errorf("UpdateRegisterAddress error: %v\n", err)
- return err
- }
- } else {
- // 没有找到对应的连接
- logger.Get().Printf("Connection for key %s not found", deviceId)
- }
- return nil
- }
|