| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package dto
- import (
- "MeterService/data"
- "MeterService/dataStruct"
- "encoding/json"
- )
- type Device struct {
- ID int `json:"id"` // 设备ID
- Enable bool `json:"enable"` // 是否启用
- SN string `json:"sn"` // 设备编码SN
- Name string `json:"name"` // 设备名称
- SimId string `json:"simId"` // sim卡ID
- IP string `json:"ip"` // 平台IP
- Port int `json:"port"` // 平台端口
- Protocol string `json:"protocol"` // 平台协议
- Pw string `json:"pw"` // 平台密码
- Mn string `json:"mn"` // 设备MN
- Secs int `json:"secs"` // 上报周期
- St string `json:"st"` // 设备ST
- Cn string `json:"cn"` // 设备CN
- Others string `json:"others"` // 其他
- Slave []DeviceSlave `json:"slave"` // 从机
- }
- type DeviceSlave struct {
- Addr int `json:"addr"` //设备串口地址
- NO string `json:"no"` //设备编号
- LvRef float32 `json:"lvRef"` //线电压基准(220)
- PvRef float32 `json:"pvRef"` //相电压基准(380)
- MType string `json:"mType"` //设备类型
- BmYz map[string]string `json:"bmYz"` //编码因子
- }
- // AddOrUpdate 添加或更新设备
- func (d *Device) AddOrUpdate() error {
- var dtuSlave []*dataStruct.DtuSlave
- for _, slave := range d.Slave {
- dtuSlave = append(dtuSlave, &dataStruct.DtuSlave{
- Addr: slave.Addr,
- BmYz: slave.BmYz,
- LvRef: slave.LvRef,
- MType: slave.MType,
- PvRef: slave.PvRef,
- NO: slave.NO,
- })
- }
- dtuConfig := &dataStruct.DtuConfig{
- Enable: d.Enable,
- ID: d.ID,
- Name: d.Name,
- IP: d.IP,
- Secs: d.Secs,
- St: d.St,
- Cn: d.Cn,
- Others: d.Others,
- Port: d.Port,
- Protocol: d.Protocol,
- Pw: d.Pw,
- Mn: d.Mn,
- Slave: dtuSlave,
- }
- dtuConfigStr, err := json.Marshal(dtuConfig)
- if err != nil {
- return err
- }
- dtuDevice := &dataStruct.DTUDevice{
- Configs: string(dtuConfigStr),
- Name: d.Name,
- SimId: d.SimId,
- SN: d.SN,
- }
- deviceState := &dataStruct.DTUDeviceState{
- Info: dtuDevice,
- Online: false,
- PwrOff: false,
- }
- data.DtuMap.Add(d.SN, deviceState)
- return err
- }
- // Delete 删除设备
- func (d *Device) Delete() error {
- data.DtuMap.Remove(d.SN)
- return nil
- }
|