package iotMeter import ( "IotAdmin/core/tools/utils" iotInterface "IotAdmin/iot/interface" iotMap "IotAdmin/iot/map" iotProtocol "IotAdmin/iot/protocol" iotProtocolHandler "IotAdmin/iot/protocol/handler" iotRtuService "IotAdmin/iot/service/rtuService" iotStruct "IotAdmin/iot/struct" "errors" "fmt" ) func SetMeterAddress(sn, protocol string, addr, newAddr int) error { client, handler, rtu, err := getClientHandle(sn, protocol, addr) if err != nil { return err } client.MLock.Lock() defer client.MLock.Unlock() if newAddr == addr { return errors.New("新地址不能与旧地址相同") } if newAddr < 1 || newAddr > 255 { return errors.New("新地址范围1-255") } if err = handler.SetAddress(rtu, newAddr); err != nil { return fmt.Errorf("表计地址设置失败: %s", err.Error()) } return nil } func SetMeterConfig(sn, protocol string, addr int, data *string) error { client, handler, rtu, err := getClientHandle(sn, protocol, addr) if err != nil { return err } client.MLock.Lock() defer client.MLock.Unlock() if err = handler.SetConfig(rtu, data); err != nil { return fmt.Errorf("表计配置设置失败: %s", err.Error()) } return nil } func GetMeterConfig(sn, protocol string, addr int) (data interface{}, err error) { client, handler, rtu, err := getClientHandle(sn, protocol, addr) if err != nil { return } client.MLock.Lock() defer client.MLock.Unlock() if data, err = handler.GetConfig(rtu); err != nil { err = fmt.Errorf("表计配置获取失败: %s", err.Error()) } return } func getClientHandle(sn, protocol string, addr int) (client *iotStruct.DtuClient, handle iotInterface.MeterHandler, rtu iotRtuService.RtuNetPgr, err error) { if !iotProtocol.VerifyMeterProtocol(protocol) { err = errors.New("表计协议不支持") return } if addr < 1 || addr > 255 { err = errors.New("地址范围1-255") return } _, ok := iotMap.MapDtuDevice.Get(sn) if !ok { err = errors.New("表计不存在") return } client, ok = iotMap.MapDtuClient.Get(sn) if !ok { err = errors.New("表计正在通信或已离线,请稍后再试!") return } rtu = iotRtuService.NewRtuNetPgr(utils.IntoByte(addr)) rtu.SetClientState(client) rtu.SetSerialNumber(sn) handle, err = iotProtocolHandler.GetMeterHandler(protocol) return } func IsOnline(sn string) bool { _, ok := iotMap.MapDtuClient.Get(sn) return ok }