device.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package dto
  2. import (
  3. "MeterService/data"
  4. "MeterService/dataStruct"
  5. "encoding/json"
  6. )
  7. type Device struct {
  8. ID int `json:"id"` // 设备ID
  9. Enable bool `json:"enable"` // 是否启用
  10. SN string `json:"sn"` // 设备编码SN
  11. Name string `json:"name"` // 设备名称
  12. SimId string `json:"simId"` // sim卡ID
  13. IP string `json:"ip"` // 平台IP
  14. Port int `json:"port"` // 平台端口
  15. Protocol string `json:"protocol"` // 平台协议
  16. Pw string `json:"pw"` // 平台密码
  17. Mn string `json:"mn"` // 设备MN
  18. Secs int `json:"secs"` // 上报周期
  19. St string `json:"st"` // 设备ST
  20. Cn string `json:"cn"` // 设备CN
  21. Others string `json:"others"` // 其他
  22. Slave []DeviceSlave `json:"slave"` // 从机
  23. }
  24. type DeviceSlave struct {
  25. Addr int `json:"addr"` //设备串口地址
  26. NO string `json:"no"` //设备编号
  27. LvRef float32 `json:"lvRef"` //线电压基准(220)
  28. PvRef float32 `json:"pvRef"` //相电压基准(380)
  29. MType string `json:"mType"` //设备类型
  30. BmYz map[string]string `json:"bmYz"` //编码因子
  31. }
  32. // AddOrUpdate 添加或更新设备
  33. func (d *Device) AddOrUpdate() error {
  34. var dtuSlave []*dataStruct.DtuSlave
  35. for _, slave := range d.Slave {
  36. dtuSlave = append(dtuSlave, &dataStruct.DtuSlave{
  37. Addr: slave.Addr,
  38. BmYz: slave.BmYz,
  39. LvRef: slave.LvRef,
  40. MType: slave.MType,
  41. PvRef: slave.PvRef,
  42. NO: slave.NO,
  43. })
  44. }
  45. dtuConfig := &dataStruct.DtuConfig{
  46. Enable: d.Enable,
  47. ID: d.ID,
  48. Name: d.Name,
  49. IP: d.IP,
  50. Secs: d.Secs,
  51. St: d.St,
  52. Cn: d.Cn,
  53. Others: d.Others,
  54. Port: d.Port,
  55. Protocol: d.Protocol,
  56. Pw: d.Pw,
  57. Mn: d.Mn,
  58. Slave: dtuSlave,
  59. }
  60. dtuConfigStr, err := json.Marshal(dtuConfig)
  61. if err != nil {
  62. return err
  63. }
  64. dtuDevice := &dataStruct.DTUDevice{
  65. Configs: string(dtuConfigStr),
  66. Name: d.Name,
  67. SimId: d.SimId,
  68. SN: d.SN,
  69. }
  70. deviceState := &dataStruct.DTUDeviceState{
  71. Info: dtuDevice,
  72. Online: false,
  73. PwrOff: false,
  74. }
  75. data.DtuMap.Add(d.SN, deviceState)
  76. return err
  77. }
  78. // Delete 删除设备
  79. func (d *Device) Delete() error {
  80. data.DtuMap.Remove(d.SN)
  81. return nil
  82. }