-
Notifications
You must be signed in to change notification settings - Fork 1
/
conventions.go
221 lines (194 loc) · 7.45 KB
/
conventions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package wbgong
import (
"log"
)
const (
//
// Format strings for fmt.Printf to form topic names
// Use '+' as values to form subscription topics
// Control value topic format string
// Parameters:
// 1. Device name
// 2. Control name
CONV_CONTROL_VALUE_FMT = "/devices/%s/controls/%s"
// Control 'on' value topic format string
// Used to pass new value to device from external software
// Parameters:
// 1. Device name
// 2. Control name
CONV_CONTROL_ON_VALUE_FMT = CONV_CONTROL_VALUE_FMT + "/on"
// Device meta info topic format string
// Parameters:
// 1. Device name
// 2. Meta subtopic name
CONV_DEVICE_META_FMT = "/devices/%s/meta/%s"
// Device meta info v2 topic format string
// Parameters:
// 1. Device name
CONV_DEVICE_META_V2_FMT = "/devices/%s/meta"
// Device driver topic format string
// Parameters:
// 1. Device name
CONV_DEVICE_META_DRIVER_FMT = "/devices/%s/meta/driver"
// Device control meta info topic format string
// Parameters:
// 1. Device name
// 2. Control name
// 3. Meta subtopic name
CONV_CONTROL_META_FMT = "/devices/%s/controls/%s/meta/%s"
// Device control meta info v2 topic format string
// Parameters:
// 1. Device name
// 2. Control name
CONV_CONTROL_META_V2_FMT = "/devices/%s/controls/%s/meta"
// Device control all meta info topic format string
// Parameters:
// 1. Device name
// 2. Control name
CONV_CONTROL_ALL_META_FMT = "/devices/%s/controls/%s/meta/+"
//
// Meta information subtopics
CONV_META_SUBTOPIC_DRIVER = "driver" // for /devices/+/meta/driver
CONV_META_SUBTOPIC_TITLE = "name" // for /devices/+/meta/name ('name' is legacy)
CONV_META_SUBTOPIC_TITLE_V2 = "title" // for /devices/+/meta/title
CONV_META_SUBTOPIC_ERROR = "error" // for /devices/+/controls/+/meta/error and /devices/+/meta/error
CONV_META_SUBTOPIC_ORDER = "order" // for /devices/+/controls/+/meta/order
CONV_META_SUBTOPIC_TYPE = "type" // for /devices/+/controls/+/meta/type
CONV_META_SUBTOPIC_UNITS = "units" // for /devices/+/controls/+/meta/units
CONV_META_SUBTOPIC_MAX = "max" // for /devices/+/controls/+/meta/max
CONV_META_SUBTOPIC_MIN = "min" // for /devices/+/controls/+/meta/min
CONV_META_SUBTOPIC_PRECISION = "precision" // for /devices/+/controls/+/meta/precision
CONV_META_SUBTOPIC_DESCRIPTION = "description" // for /devices/+/controls/+/meta/description
CONV_META_SUBTOPIC_CONTROL_TITLE = "title" // for /devices/+/controls/+/meta/title
CONV_META_SUBTOPIC_READONLY = "readonly" // for /devices/+/controls/+/meta/readonly
CONV_META_SUBTOPIC_CONTROL_ENUM = "enum" // for /devices/+/controls/+/meta/enum
// Type names
CONV_TYPE_SWITCH = "switch"
CONV_TYPE_ALARM = "alarm"
CONV_TYPE_PUSHBUTTON = "pushbutton"
CONV_TYPE_RANGE = "range"
CONV_TYPE_RGB = "rgb"
CONV_TYPE_TEXT = "text"
CONV_TYPE_VALUE = "value"
// Meta types (types derived from 'value')
CONV_TYPE_TEMPERATURE = "temperature"
CONV_TYPE_REL_HUMIDITY = "rel_humidity"
CONV_TYPE_ATMOSPHERIC_PRESSURE = "atmospheric_pressure"
CONV_TYPE_RAINFALL = "rainfall"
CONV_TYPE_WIND_SPEED = "wind_speed"
CONV_TYPE_POWER = "power"
CONV_TYPE_POWER_CONSUMPTION = "power_consumption"
CONV_TYPE_VOLTAGE = "voltage"
CONV_TYPE_WATER_FLOW = "water_flow"
CONV_TYPE_WATER_CONSUMPTION = "water_consumption"
CONV_TYPE_RESISTANCE = "resistance"
CONV_TYPE_CONCENTRATION = "concentration"
CONV_TYPE_PRESSURE = "pressure"
CONV_TYPE_ILLUMINANCE = "lux"
CONV_TYPE_SOUND_LEVEL = "sound_level"
CONV_TYPE_HEAT_POWER = "heat_power"
CONV_TYPE_HEAT_ENERGY = "heat_energy"
CONV_TYPE_CURRENT = "current"
// Default data type for unknown meta type
CONV_DEFAULT_DATATYPE = CONV_DATATYPE_STRING
CONV_META_BOOL_TRUE = "1"
CONV_META_BOOL_FALSE = "0"
// Special values for types
CONV_SWITCH_VALUE_TRUE = CONV_META_BOOL_TRUE
CONV_SWITCH_VALUE_FALSE = CONV_META_BOOL_FALSE
CONV_ALARM_VALUE_TRUE = CONV_META_BOOL_TRUE
CONV_ALARM_VALUE_FALSE = CONV_META_BOOL_FALSE
CONV_SWITCH_DEFAULT_VALUE = CONV_SWITCH_VALUE_FALSE
CONV_ALARM_DEFAULT_VALUE = CONV_ALARM_VALUE_FALSE
CONV_SUBTOPIC_ALL = "+"
)
// ControlDataType is a real data types used in representations
type ControlDataType int
const (
CONV_DATATYPE_STRING ControlDataType = iota
CONV_DATATYPE_BOOLEAN
CONV_DATATYPE_FLOAT
CONV_DATATYPE_BUTTON
)
var (
funcToTypedValue func(string, string) (interface{}, error)
funcRawValueToDataTyped func(string, ControlDataType) (interface{}, error)
funcToRawValue func(interface{}, string, ...interface{}) (string, error)
funcDataTypedToRawValue func(interface{}, ControlDataType, ...interface{}) (string, error)
funcGetDefaultValue func(string) (string, error)
)
func ToTypedValue(rawValue, typestr string) (interface{}, error) {
if funcToTypedValue != nil {
return funcToTypedValue(rawValue, typestr)
}
funcSym, errSym := plug.Lookup("ToTypedValue")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcToTypedValue, okResolve = funcSym.(func(string, string) (interface{}, error))
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcToTypedValue(rawValue, typestr)
}
func RawValueToDataTyped(rawValue string, dataType ControlDataType) (interface{}, error) {
if funcRawValueToDataTyped != nil {
return funcRawValueToDataTyped(rawValue, dataType)
}
funcSym, errSym := plug.Lookup("RawValueToDataTyped")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcRawValueToDataTyped, okResolve = funcSym.(func(string, ControlDataType) (interface{}, error))
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcRawValueToDataTyped(rawValue, dataType)
}
func ToRawValue(value interface{}, typestr string, args ...interface{}) (raw string, err error) {
if funcToRawValue != nil {
return funcToRawValue(value, typestr, args...)
}
funcSym, errSym := plug.Lookup("ToRawValue")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcToRawValue, okResolve = funcSym.(func(interface{}, string, ...interface{}) (string, error))
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcToRawValue(value, typestr, args...)
}
func DataTypedToRawValue(value interface{}, dataType ControlDataType, args ...interface{}) (raw string, err error) {
if funcDataTypedToRawValue != nil {
return funcDataTypedToRawValue(value, dataType, args...)
}
funcSym, errSym := plug.Lookup("DataTypedToRawValue")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcDataTypedToRawValue, okResolve = funcSym.(func(interface{}, ControlDataType, ...interface{}) (string, error))
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcDataTypedToRawValue(value, dataType, args...)
}
func GetDefaultValue(typestr string) (raw string, err error) {
if funcGetDefaultValue != nil {
return funcGetDefaultValue(typestr)
}
funcSym, errSym := plug.Lookup("GetDefaultValue")
if errSym != nil {
log.Fatalf("Error in lookup symbol: %s", errSym)
}
var okResolve bool
funcGetDefaultValue, okResolve = funcSym.(func(string) (string, error))
if !okResolve {
log.Fatal("Wrong sign on resolving func")
}
return funcGetDefaultValue(typestr)
}