productconfig.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package productconfig
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "sparrow/pkg/protocol"
  7. "sparrow/pkg/tlv"
  8. )
  9. // CommandOrEventParam command or a event param struct
  10. type CommandOrEventParam struct {
  11. ValueType int32 `json:"value_type"`
  12. Name string
  13. }
  14. // ProductCommandOrEvent ``
  15. type ProductCommandOrEvent struct {
  16. No int
  17. Part int
  18. Name string
  19. Priority int
  20. Params []CommandOrEventParam
  21. }
  22. // StatusParam 上报类型的参数
  23. type StatusParam struct {
  24. ValueType int32 `json:"value_type"`
  25. Name string `json:"name"`
  26. ID uint `json:"pid"` //protocal id 与后台对应
  27. }
  28. // ProductObject 产品对象
  29. type ProductObject struct {
  30. Id int
  31. No int
  32. Label string
  33. Part int
  34. Status []StatusParam
  35. }
  36. // ProductConfig product config parses the JSON product config string.
  37. type ProductConfig struct {
  38. Objects []ProductObject `json:"objects"`
  39. Commands []ProductCommandOrEvent `json:"commands"`
  40. Events []ProductCommandOrEvent `json:"events"`
  41. }
  42. // New create a new productConfig
  43. func New(config string) (*ProductConfig, error) {
  44. v := &ProductConfig{}
  45. err := json.Unmarshal([]byte(config), v)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return v, nil
  50. }
  51. // ValidateStatus validate the status
  52. func (config *ProductConfig) ValidateStatus(label string, params []interface{}) (*ProductObject, []interface{}, error) {
  53. // search for status name
  54. var paramInfo []StatusParam
  55. var status *ProductObject
  56. found := false
  57. for _, obj := range config.Objects {
  58. if obj.Label == label {
  59. paramInfo = obj.Status
  60. status = &obj
  61. found = true
  62. break
  63. }
  64. }
  65. if found == false {
  66. return nil, []interface{}{}, errors.New("object not found.")
  67. }
  68. if len(paramInfo) != len(params) {
  69. return nil, []interface{}{}, errors.New("wrong status parameters.")
  70. }
  71. realParams := make([]interface{}, len(params))
  72. for idx, para := range paramInfo {
  73. realParams[idx] = tlv.CastTLV(params[idx], para.ValueType)
  74. }
  75. return status, realParams, nil
  76. }
  77. // ValidateCommandOrEvent validate command or event
  78. func (config *ProductConfig) ValidateCommandOrEvent(name string, params []interface{}, typ string) (*ProductCommandOrEvent, []interface{}, error) {
  79. var target []ProductCommandOrEvent
  80. if typ == "command" {
  81. target = config.Commands
  82. } else if typ == "event" {
  83. target = config.Events
  84. } else {
  85. return nil, []interface{}{}, errors.New("wrong target type.")
  86. }
  87. // search for name
  88. var paramInfo []CommandOrEventParam
  89. var coe *ProductCommandOrEvent
  90. found := false
  91. for _, one := range target {
  92. if one.Name == name {
  93. paramInfo = one.Params
  94. coe = &one
  95. found = true
  96. break
  97. }
  98. }
  99. if found == false {
  100. return nil, []interface{}{}, errors.New("command or event not found.")
  101. }
  102. if len(paramInfo) != len(params) {
  103. return nil, []interface{}{}, errors.New("wrong parameters.")
  104. }
  105. realParams := make([]interface{}, len(params))
  106. for idx, para := range paramInfo {
  107. realParams[idx] = tlv.CastTLV(params[idx], para.ValueType)
  108. }
  109. return coe, realParams, nil
  110. }
  111. // StatusToMap struct to map
  112. func (config *ProductConfig) StatusToMap(status []protocol.SubData) (map[string]interface{}, error) {
  113. result := make(map[string]interface{})
  114. for _, sub := range status {
  115. val, err := tlv.ReadTLVs(sub.Params)
  116. if err != nil {
  117. return nil, err
  118. }
  119. label := ""
  120. values := make(map[string]interface{})
  121. for _, obj := range config.Objects {
  122. if obj.No == int(sub.Head.PropertyNum) {
  123. label = obj.Label
  124. }
  125. for k, v := range obj.Status {
  126. values[v.Name] = val[k]
  127. }
  128. }
  129. result[label] = values
  130. result["device_id"] = sub.Head.ExternalDeviceId
  131. }
  132. return result, nil
  133. }
  134. // EventToMap event ot map
  135. func (config *ProductConfig) EventToMap(event *protocol.Event) (map[string][]interface{}, error) {
  136. result := make(map[string][]interface{})
  137. name := ""
  138. for _, ev := range config.Events {
  139. if ev.No == int(event.Head.No) {
  140. name = ev.Name
  141. }
  142. }
  143. val, err := tlv.ReadTLVs(event.Params)
  144. if err != nil {
  145. return nil, err
  146. }
  147. result[name] = val
  148. return result, nil
  149. }
  150. // MapToStatus map to status
  151. func (config *ProductConfig) MapToStatus(data map[string]interface{}) ([]protocol.SubData, error) {
  152. result := []protocol.SubData{}
  153. for label, one := range data {
  154. params, ok := one.([]interface{})
  155. if !ok {
  156. return nil, fmt.Errorf("status format error: %v", one)
  157. }
  158. obj, realParams, err := config.ValidateStatus(label, params)
  159. if err != nil {
  160. return nil, err
  161. }
  162. tlvs, err := tlv.MakeTLVs(realParams)
  163. if err != nil {
  164. return nil, err
  165. }
  166. result = append(result, protocol.SubData{
  167. Head: protocol.SubDataHead{
  168. SubDeviceid: uint16(obj.Part),
  169. PropertyNum: uint16(obj.No),
  170. ParamsCount: uint16(len(realParams)),
  171. },
  172. Params: tlvs,
  173. })
  174. }
  175. return result, nil
  176. }
  177. func (config *ProductConfig) MapToCommand(cmd map[string]interface{}) (*protocol.Command, error) {
  178. result := &protocol.Command{}
  179. for name, one := range cmd {
  180. params, ok := one.([]interface{})
  181. if !ok {
  182. return nil, fmt.Errorf("command format error: %v", one)
  183. }
  184. c, realParams, err := config.ValidateCommandOrEvent(name, params, "command")
  185. if err != nil {
  186. return nil, err
  187. }
  188. tlvs, err := tlv.MakeTLVs(realParams)
  189. if err != nil {
  190. return nil, err
  191. }
  192. result.Head.No = uint16(c.No)
  193. result.Head.Priority = uint16(c.Priority)
  194. result.Head.SubDeviceid = uint16(c.Part)
  195. result.Head.ParamsCount = uint16(len(realParams))
  196. result.Params = tlvs
  197. }
  198. return result, nil
  199. }