productconfig_test.go 3.1 KB

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