mqtt_type.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package extensions
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strconv"
  6. "strings"
  7. )
  8. type (
  9. property struct {
  10. Name string `json:"name"`
  11. DataType string `json:"dataType"`
  12. }
  13. device struct {
  14. Name string `json:"name"`
  15. Properties []*property `json:"properties"`
  16. }
  17. deviceModel struct {
  18. Devices []*device `json:"deviceModels"`
  19. }
  20. )
  21. type modelVersion interface {
  22. checkType(map[string]interface{}, string) []string
  23. }
  24. func modelFactory(version string) modelVersion {
  25. return new(deviceModel)
  26. }
  27. func (this *property) getName() string {
  28. return this.Name
  29. }
  30. func (this *property) getDataType() string {
  31. return this.DataType
  32. }
  33. func (this *device) getName() string {
  34. return this.Name
  35. }
  36. func (this *device) findDataType(name string) string {
  37. for _, v := range this.Properties {
  38. if strings.ToLower(v.getName()) == strings.ToLower(name) {
  39. return v.getDataType()
  40. }
  41. }
  42. return ""
  43. }
  44. func (this *deviceModel) findDataType(deviceId, dataName string) string {
  45. for _, v := range this.Devices {
  46. if strings.ToLower(v.getName()) == strings.ToLower(deviceId) {
  47. return v.findDataType(dataName)
  48. }
  49. }
  50. return ""
  51. }
  52. func boolToInt(b bool) int {
  53. if b {
  54. return 1
  55. }
  56. return 0
  57. }
  58. func intToBool(i int) bool {
  59. if 0 == i {
  60. return false
  61. }
  62. return true
  63. }
  64. func changeType(modelType string, data interface{}) (interface{}, string) {
  65. var err error
  66. dataType := reflect.TypeOf(data).Kind()
  67. switch dataType {
  68. case reflect.Bool:
  69. b, _ := data.(bool)
  70. switch modelType {
  71. case "int":
  72. data = boolToInt(b)
  73. case "bool":
  74. return data, ""
  75. default:
  76. return data, fmt.Sprintf("not support modelType : %s", modelType)
  77. }
  78. case reflect.Int:
  79. i, _ := data.(int)
  80. switch modelType {
  81. case "int":
  82. return data, ""
  83. case "float":
  84. data = float64(i)
  85. case "boolean":
  86. data = intToBool(i)
  87. case "string":
  88. data = strconv.Itoa(i)
  89. default:
  90. return data, fmt.Sprintf("not support modelType : %s", modelType)
  91. }
  92. case reflect.String:
  93. s, _ := data.(string)
  94. switch modelType {
  95. case "string":
  96. return data, ""
  97. case "float":
  98. data, err = strconv.ParseFloat(s, 64)
  99. if nil != err {
  100. return data, fmt.Sprintf("%v", err)
  101. }
  102. case "int":
  103. data, err = strconv.Atoi(s)
  104. if nil != err {
  105. return data, fmt.Sprintf("%v", err)
  106. }
  107. default:
  108. return data, fmt.Sprintf("not support modelType : %s", modelType)
  109. }
  110. case reflect.Float64:
  111. f, _ := data.(float64)
  112. switch modelType {
  113. case "double", "float":
  114. return data, ""
  115. case "int":
  116. data = int(f)
  117. case "string":
  118. data = strconv.FormatFloat(f, 'f', -1, 64)
  119. default:
  120. return data, fmt.Sprintf("not support modelType : %s", modelType)
  121. }
  122. default:
  123. return data, fmt.Sprintf("not support type : %v", dataType)
  124. }
  125. return data, ""
  126. }
  127. func topicToDeviceid(topic string) string {
  128. sliStr := strings.Split(topic, `/`)
  129. if 4 > len(sliStr) {
  130. return ""
  131. }
  132. return sliStr[3]
  133. }
  134. func (this *deviceModel) checkType(m map[string]interface{}, topic string) []string {
  135. var sliErr []string
  136. strErr := ""
  137. for k, v := range m {
  138. deviceid := topicToDeviceid(topic)
  139. if 0 == len(deviceid) {
  140. sliErr = append(sliErr, fmt.Sprintf("not find deviceid : %s", topic))
  141. continue
  142. }
  143. modelType := this.findDataType(deviceid, k)
  144. if 0 == len(modelType) {
  145. continue
  146. }
  147. m[k], strErr = changeType(modelType, v)
  148. if 0 != len(strErr) {
  149. sliErr = append(sliErr, strErr)
  150. delete(m, k)
  151. }
  152. }
  153. return sliErr
  154. }