123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package service
- import (
- "fmt"
- "server/dao"
- "server/modbus"
- "server/model"
- "server/utils"
- )
- func GetDeviceList() {
- fmt.Println("Get device list.")
- }
- func SaveData(data interface{}) error {
- err := utils.SaveData("static/data.json", data)
- if err != nil {
- return err
- }
- return err
- }
- func DeviceLoopSwitch(deviceLoop dao.DeviceLoop) error {
- data := modbus.DeviceLoopSwitch(deviceLoop.ID, deviceLoop.State)
- if model.ConnectionMap[deviceLoop.DeviceId] == nil {
- return fmt.Errorf("设备连接丢失")
- }
- _, dev, _ := utils.GetDataByDeviceId(deviceLoop.DeviceId)
- if dev.State == 0 {
- return fmt.Errorf("设备离线无法操作")
- }
- err := utils.WriteDevice(data, model.ConnectionMap[deviceLoop.DeviceId])
- if err != nil {
- fmt.Printf("Write device error: %v\n", err)
- return err
- }
- return nil
- }
- func DeviceSwitch(req model.DeviceRequest) error {
- data := modbus.DeviceSwitch(req.Device.LoopNumber, req.State)
- if model.ConnectionMap[req.Device.Sn] == nil {
- return fmt.Errorf("设备连接丢失")
- }
- err := utils.WriteDevice(data, model.ConnectionMap[req.Device.Sn])
- if err != nil {
- fmt.Printf("Write device error: %v\n", err)
- return err
- }
- 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)
- if model.ConnectionMap[device.Sn] == nil {
- return fmt.Errorf("设备连接丢失")
- }
- err := utils.WriteDevice(data, model.ConnectionMap[device.Sn])
- if err != nil {
- fmt.Printf("Write device error: %v\n", err)
- return err
- }
- }
- return nil
- }
- // GetOnlineDevice 获取在线设备 总共设备 在线率
- func GetOnlineDevice() (total, online int) {
- devices, err := utils.GetOnlineDevices()
- if err != nil {
- fmt.Printf("GetOnlineDevices err = %s\n", err.Error())
- return
- }
- allDevices, err := utils.GetAllDevices()
- if err != nil {
- fmt.Printf("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 {
- err := utils.WriteDevice(modbus.UpdateRegisterAddress(), model.ConnectionMap[deviceId])
- if err != nil {
- fmt.Printf("UpdateRegisterAddress error: %v\n", err)
- return err
- }
- return nil
- }
|