struct.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package dataStruct
  2. import (
  3. "log"
  4. "time"
  5. "unicode/utf8"
  6. )
  7. // DTU管理命令
  8. const (
  9. CmdRegister = iota // 注册
  10. CmdHeartBeat // 心跳
  11. CmdCollect // 采集
  12. )
  13. type DTUManageCmd struct {
  14. SN string `json:"sn"`
  15. Cmd int `json:"cmd"`
  16. Data []byte `json:"data"`
  17. }
  18. // DmcUnmarshal 解析DTU上报
  19. // 注册和心跳格式:注册包@202402200322 心跳包 $202402200322
  20. // SN长度为8-16个字符
  21. func (dmc *DTUManageCmd) DmcUnmarshal(data []byte) {
  22. size := utf8.RuneCount(data)
  23. log.Println("DTU采集:", data)
  24. // SN 长度在 8-16个字符 (注册包在9-17个字符)
  25. if size > 8 && size < 18 {
  26. if data[0] == '@' {
  27. dmc.Cmd = CmdRegister
  28. dmc.SN = string(data[1:])
  29. return
  30. } else if data[0] == '$' {
  31. dmc.Cmd = CmdHeartBeat
  32. dmc.SN = string(data[1:])
  33. return
  34. }
  35. }
  36. dmc.Data = data
  37. dmc.Cmd = CmdCollect
  38. }
  39. type DtuRegisterChanMsg struct {
  40. Sn string
  41. Value *ClientState
  42. }
  43. type DateTime struct {
  44. Year int
  45. Month time.Month
  46. Day int
  47. Hour int
  48. Minute int
  49. Second int
  50. }
  51. // DataToTimeStamp 日期转时间戳
  52. func (dt *DateTime) DataToTimeStamp() int64 {
  53. return time.Date(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0, time.Local).Unix()
  54. }
  55. // TimeStampToData 时间戳转日期
  56. func (dt *DateTime) TimeStampToData(t int64) *DateTime {
  57. v := time.Unix(t, 0)
  58. m := &DateTime{}
  59. m.Year = v.Year()
  60. m.Month = v.Month()
  61. m.Day = v.Day()
  62. m.Hour = v.Hour()
  63. m.Minute = v.Minute()
  64. m.Second = v.Second()
  65. return m
  66. }
  67. type MeterRef struct {
  68. PvRef float32
  69. LvRef float32
  70. }
  71. type MeterRatio struct {
  72. Ct int `json:"ct"`
  73. Pt int `json:"pt"`
  74. }
  75. type MeterPMC350BParam struct {
  76. PrimaryVolt int `json:"primary_volt"`
  77. SecondVolt int `json:"second_volt"`
  78. PrimaryCurrent int `json:"primary_current"`
  79. SecondCurrent int `json:"second_current"`
  80. CType int `json:"ct_type"`
  81. }