productconfig_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package productconfig
  2. import (
  3. "encoding/json"
  4. "sparrow/pkg/protocol"
  5. "sparrow/pkg/tlv"
  6. "reflect"
  7. "testing"
  8. )
  9. func testStatus(c *ProductConfig, t *testing.T) {
  10. status :=
  11. `
  12. {
  13. "switch": [1]
  14. }
  15. `
  16. var v interface{}
  17. err := json.Unmarshal([]byte(status), &v)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. for label, onedata := range v.(map[string]interface{}) {
  22. params := onedata.([]interface{})
  23. obj, realParams, err := c.ValidateStatus(label, params)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. t.Log(obj)
  28. t.Log(realParams)
  29. }
  30. one, err := tlv.MakeTLV(uint8(1))
  31. if err != nil {
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. }
  36. params := []tlv.TLV{*one}
  37. teststatus := []protocol.SubData{protocol.SubData{
  38. Head: protocol.SubDataHead{
  39. SubDeviceid: uint16(1),
  40. PropertyNum: uint16(1),
  41. ParamsCount: uint16(1),
  42. },
  43. Params: params,
  44. }}
  45. res, err := c.StatusToMap(teststatus)
  46. if err != nil {
  47. t.Error(err)
  48. }
  49. t.Log(res)
  50. m := make(map[string]interface{})
  51. m["switch"] = []interface{}{float64(1)}
  52. _, err = c.MapToStatus(m)
  53. if err != nil {
  54. t.Error(err)
  55. }
  56. }
  57. func testEvent(c *ProductConfig, t *testing.T) {
  58. want := `{"alarm":["test"]}`
  59. testev := &protocol.Event{}
  60. testev.Head.No = 1
  61. testev.Head.SubDeviceid = 1
  62. params, err := tlv.MakeTLVs([]interface{}{"test"})
  63. if err != nil {
  64. t.Error(err)
  65. }
  66. testev.Params = params
  67. m, err := c.EventToMap(testev)
  68. if err != nil {
  69. t.Error(err)
  70. }
  71. result, err := json.Marshal(m)
  72. if err != nil {
  73. t.Error(err)
  74. }
  75. got := string(result)
  76. if got != want {
  77. t.Errorf("event to map error: want: %v, got : %v", want, got)
  78. }
  79. }
  80. func testCommand(c *ProductConfig, t *testing.T) {
  81. input := `{"switch":[1,2]}`
  82. v := make(map[string]interface{})
  83. err := json.Unmarshal([]byte(input), &v)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. params, err := tlv.MakeTLVs([]interface{}{uint8(1), uint8(2)})
  88. want := &protocol.Command{}
  89. want.Head.No = 1
  90. want.Head.SubDeviceid = 1
  91. want.Head.ParamsCount = 2
  92. want.Params = params
  93. got, err := c.MapToCommand(v)
  94. if !reflect.DeepEqual(want, got) {
  95. t.Errorf("map to command error: want: %v, got %v", want, got)
  96. }
  97. }
  98. func TestParseProductConfig(t *testing.T) {
  99. config :=
  100. `
  101. {
  102. "objects": [{
  103. "no": 1,
  104. "label": "switch",
  105. "part": 1,
  106. "status": [{
  107. "value_type": 7,
  108. "name": "onoff"
  109. }]
  110. }],
  111. "commands": [{
  112. "no": 1,
  113. "part": 1,
  114. "name": "switch",
  115. "priority": 0,
  116. "params": [{
  117. "value_type": 7,
  118. "name": "p1"
  119. },{
  120. "value_type": 7,
  121. "name": "p2"
  122. }]
  123. }],
  124. "events": [{
  125. "no": 1,
  126. "part": 1,
  127. "name": "alarm",
  128. "priority": 0,
  129. "params": [{
  130. "value_type": 12,
  131. "name": "text"
  132. }]
  133. }]
  134. }
  135. `
  136. c, err := New(config)
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. testStatus(c, t)
  141. testEvent(c, t)
  142. testCommand(c, t)
  143. }