| | |
| | | "apsClient/utils/file" |
| | | "encoding/json" |
| | | "fmt" |
| | | "github.com/mitchellh/mapstructure" |
| | | "github.com/spf13/cast" |
| | | "gorm.io/gorm" |
| | | "strings" |
| | |
| | | Topic: slf.Topic, |
| | | Message: data, |
| | | } |
| | | if len(resp.KeyData) == 0 || len(resp.AddressData) == 0 { |
| | | return nil |
| | | } |
| | | //写入到文件 |
| | | err = file.WriteFile(fmt.Sprintf("%s%s", constvar.PlcAddressDataPath, constvar.PlcAddressDataKeyFileName), resp.KeyData) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = file.WriteFile(fmt.Sprintf("%s%s", constvar.PlcAddressDataPath, constvar.PlcAddressDataValueFileName), resp.AddressData) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | //写入到内存 |
| | | keyString := string(resp.KeyData) |
| | | addressString := string(resp.AddressData) |
| | | if len(resp.KeyData) != 0 && len(resp.AddressData) != 0 { |
| | | //写入到文件 |
| | | err = file.WriteFile(fmt.Sprintf("%s%s", constvar.PlcAddressDataPath, constvar.PlcAddressDataKeyFileName), resp.KeyData) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = file.WriteFile(fmt.Sprintf("%s%s", constvar.PlcAddressDataPath, constvar.PlcAddressDataValueFileName), resp.AddressData) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | //写入到内存 |
| | | keyString := string(resp.KeyData) |
| | | addressString := string(resp.AddressData) |
| | | |
| | | keys := strings.Split(keyString, "\n") |
| | | addresses := strings.Split(addressString, "\n") |
| | | if len(keys) != len(addresses) { |
| | | logx.Error("plc address message error: key length not equal address length") |
| | | return nil |
| | | keys := strings.Split(keyString, "\n") |
| | | addresses := strings.Split(addressString, "\n") |
| | | if len(keys) != len(addresses) { |
| | | logx.Error("plc address message error: key length not equal address length") |
| | | return nil |
| | | } |
| | | for i := 0; i < len(keys); i++ { |
| | | key := strings.ReplaceAll(keys[i], "\r", "") |
| | | address := cast.ToInt(strings.ReplaceAll(addresses[i], "\r", "")) |
| | | plc_address.Set(key, address) |
| | | logx.Infof("plc address set ok: key:%v, address:%v", key, address) |
| | | } |
| | | } |
| | | for i := 0; i < len(keys); i++ { |
| | | key := strings.ReplaceAll(keys[i], "\r", "") |
| | | address := cast.ToInt(strings.ReplaceAll(addresses[i], "\r", "")) |
| | | plc_address.Set(key, address) |
| | | logx.Infof("plc address set ok: key:%v, address:%v", key, address) |
| | | |
| | | var record model.DevicePlc |
| | | err = mapstructure.Decode(resp.PlcConfig, &record) |
| | | if err != nil { |
| | | logx.Infof("decode from PlcAddressList to DevicePlc Details err:%v", err) |
| | | } |
| | | err = mapstructure.Decode(resp.PlcAddressList, &record.Details) |
| | | if err != nil { |
| | | logx.Infof("decode from PlcAddressList to DevicePlc Details err:%v", err) |
| | | } |
| | | detail, err := json.Marshal(record.Details) |
| | | record.Detail = string(detail) |
| | | record.Id = 1 |
| | | err = model.NewDevicePlcSearch().SetId(record.Id).Save(&record) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return nil |
| | | } |