/
luamodule.go
86 lines (73 loc) · 1.86 KB
/
luamodule.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
package helper
import (
"fmt"
"github.com/ibuilding-x/driver-box/driverbox/plugin"
lua "github.com/yuin/gopher-lua"
)
var LuaModuleInstance *LuaModule
// LuaModule go 实现 lua 模块,供脚本内使用
type LuaModule struct{}
// Loader 模块预加载
func (lm *LuaModule) Loader(L *lua.LState) int {
// 模块方法定义
mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
"setCache": lm.SetCache,
"getCache": lm.GetCache,
"writeToMsgBus": lm.WriteToMsgBus,
})
L.Push(mod)
return 1
}
// GetCache 获取缓存
func (lm *LuaModule) GetCache(L *lua.LState) int {
// 获取 lua 参数
key := L.ToString(1)
if key != "" {
if value, ok := PluginCacheMap.Load(key); ok {
v := fmt.Sprintf("%v", value)
// 结果返回
L.Push(lua.LString(v))
return 1
}
}
L.Push(lua.LNil)
return 1
}
// SetCache 设置缓存
func (lm *LuaModule) SetCache(L *lua.LState) int {
// 获取参数
key := L.ToString(1)
value := L.ToString(2)
PluginCacheMap.Store(key, value)
return 0
}
// WriteToMsgBus 上报设备数据
// deviceSn:设备SN,例如:sensor_1
// points:点位数据,例如(此时以json格式进行说明,lua实际入参格式为 table 类型):{"onOff":1, "voc": 23}
func (lm *LuaModule) WriteToMsgBus(L *lua.LState) int {
deviceSn := L.ToString(1) // 设备名称
points := L.ToTable(2) // 点位值
deviceData := plugin.DeviceData{
SN: deviceSn,
}
var pd []plugin.PointData
// 循环点位数据
points.ForEach(func(key lua.LValue, value lua.LValue) {
pd = append(pd, plugin.PointData{
PointName: key.String(),
Value: value.String(),
})
})
deviceData.Values = pd
PointCacheFilter(&deviceData)
// 发送数据
if len(deviceData.Values) > 0 {
for _, export := range Exports {
if export.IsReady() {
export.ExportTo(deviceData)
}
}
}
//WriteToMessageBus(deviceData)
return 0
}