productconfig.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. for k, v := range obj.Status {
  125. values[v.Name] = val[k]
  126. }
  127. }
  128. }
  129. result[label] = values
  130. result["sub_device_id"] = sub.Head.SubDeviceid
  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. val, err := tlv.ReadTLVs(event.Params)
  138. if err != nil {
  139. return nil, err
  140. }
  141. label := ""
  142. values := make(map[string]interface{})
  143. for _, obj := range config.Events {
  144. if obj.No == int(event.Head.No) {
  145. label = obj.Name
  146. for k, v := range obj.Params {
  147. values[v.Name] = val[k]
  148. }
  149. }
  150. }
  151. result[label] = values
  152. result["device_id"] = event.Head.SubDeviceid
  153. return result, nil
  154. }
  155. // MapToStatus map to status
  156. func (config *ProductConfig) MapToStatus(data map[string]interface{}) ([]protocol.SubData, error) {
  157. var result []protocol.SubData
  158. for label, one := range data {
  159. params, ok := one.([]interface{})
  160. if !ok {
  161. return nil, fmt.Errorf("status format error: %v", one)
  162. }
  163. obj, realParams, err := config.ValidateStatus(label, params)
  164. if err != nil {
  165. return nil, err
  166. }
  167. tlvs, err := tlv.MakeTLVs(realParams)
  168. if err != nil {
  169. return nil, err
  170. }
  171. result = append(result, protocol.SubData{
  172. Head: protocol.SubDataHead{
  173. SubDeviceid: uint16(obj.Part),
  174. PropertyNum: uint16(obj.No),
  175. ParamsCount: uint16(len(realParams)),
  176. },
  177. Params: tlvs,
  178. })
  179. }
  180. return result, nil
  181. }
  182. func (config *ProductConfig) MapToCommand(cmd map[string]interface{}) (*protocol.Command, error) {
  183. result := &protocol.Command{}
  184. for name, one := range cmd {
  185. params, ok := one.([]interface{})
  186. if !ok {
  187. return nil, fmt.Errorf("command format error: %v", one)
  188. }
  189. c, realParams, err := config.ValidateCommandOrEvent(name, params, "command")
  190. if err != nil {
  191. return nil, err
  192. }
  193. tlvs, err := tlv.MakeTLVs(realParams)
  194. if err != nil {
  195. return nil, err
  196. }
  197. result.Head.No = uint16(c.No)
  198. result.Head.Priority = uint16(c.Priority)
  199. result.Head.SubDeviceid = uint16(c.Part)
  200. result.Head.ParamsCount = uint16(len(realParams))
  201. result.Params = tlvs
  202. }
  203. return result, nil
  204. }