mqtt_type.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. dataType := reflect.TypeOf(data).Kind()
  66. switch dataType {
  67. case reflect.Bool:
  68. b, _ := data.(bool)
  69. switch modelType {
  70. case "int":
  71. data = boolToInt(b)
  72. case "bool":
  73. return data, ""
  74. default:
  75. return data, fmt.Sprintf("not support modelType : %s", modelType)
  76. }
  77. case reflect.Int:
  78. i, _ := data.(int)
  79. switch modelType {
  80. case "int":
  81. return data, ""
  82. case "float":
  83. data = float64(i)
  84. case "boolean":
  85. data = intToBool(i)
  86. case "string":
  87. data = strconv.Itoa(i)
  88. default:
  89. return data, fmt.Sprintf("not support modelType : %s", modelType)
  90. }
  91. case reflect.String:
  92. s, _ := data.(string)
  93. switch modelType {
  94. case "string":
  95. return data, ""
  96. case "float":
  97. data, _ = strconv.ParseFloat(s, 64)
  98. case "int":
  99. data, _ = strconv.Atoi(s)
  100. default:
  101. return data, fmt.Sprintf("not support modelType : %s", modelType)
  102. }
  103. case reflect.Float64:
  104. f, _ := data.(float64)
  105. switch modelType {
  106. case "double", "float":
  107. return data, ""
  108. case "int":
  109. data = int(f)
  110. case "string":
  111. data = strconv.FormatFloat(f, 'f', -1, 64)
  112. default:
  113. return data, fmt.Sprintf("not support modelType : %s", modelType)
  114. }
  115. default:
  116. return data, fmt.Sprintf("not support type : %v", dataType)
  117. }
  118. return data, ""
  119. }
  120. func topicToDeviceid(topic string) string {
  121. sliStr := strings.Split(topic, `/`)
  122. if 4 > len(sliStr) {
  123. return ""
  124. }
  125. return sliStr[3]
  126. }
  127. func (this *deviceModel) checkType(m map[string]interface{}, topic string) []string {
  128. var sliErr []string
  129. strErr := ""
  130. for k, v := range m {
  131. deviceid := topicToDeviceid(topic)
  132. if 0 == len(deviceid) {
  133. sliErr = append(sliErr, fmt.Sprintf("not find deviceid : %s", topic))
  134. continue
  135. }
  136. modelType := this.findDataType(deviceid, k)
  137. if 0 == len(modelType) {
  138. continue
  139. }
  140. m[k], strErr = changeType(modelType, v)
  141. if 0 != len(strErr) {
  142. sliErr = append(sliErr, strErr)
  143. delete(m, k)
  144. }
  145. }
  146. return sliErr
  147. }