RULEX:Golang嵌入式边缘网关开发框架

wwhai · · 4186 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
## 简介 闲来无事,试着用Golang来搞Linux嵌入式应用级开发。发现效果还挺好的,总结出来了个框架,分享一下:https://github.com/i4de/rulex 。 ## 入门 ```go package main import ( "os" "os/signal" "syscall" "github.com/i4de/rulex/core" "github.com/i4de/rulex/engine" "github.com/i4de/rulex/glogger" httpserver "github.com/i4de/rulex/plugin/http_server" "github.com/i4de/rulex/typex" ) /* * * Test 485 sensor gateway * */ func main() { mainConfig := core.InitGlobalConfig("rulex.ini") glogger.StartGLogger(true, core.GlobalConfig.LogPath) glogger.StartLuaLogger(core.GlobalConfig.LuaLogPath) core.StartStore(core.GlobalConfig.MaxQueueSize) core.SetLogLevel() core.SetPerformance() c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGINT, syscall.SIGABRT, syscall.SIGTERM) engine := engine.NewRuleEngine(mainConfig) engine.Start() hh := httpserver.NewHttpApiServer() // HttpApiServer loaded default if err := engine.LoadPlugin("plugin.http_server", hh); err != nil { glogger.GLogger.Fatal("Rule load failed:", err) } // RTU485_THER Inend RTU485Device := typex.NewDevice("RTU485_THER", "温湿度采集器", "温湿度采集器", "", map[string]interface{}{ "slaverIds": []uint8{1, 2}, "timeout": 5, "frequency": 5, "config": map[string]interface{}{ "uart": "/dev/ttyUSB0", "dataBits": 8, "parity": "N", "stopBits": 1, "baudRate": 4800, }, "registers": []map[string]interface{}{ { "tag": "node1", "function": 3, "slaverId": 1, "address": 0, "quantity": 2, }, { "tag": "node2", "function": 3, "slaverId": 2, "address": 0, "quantity": 2, }, }, }) RTU485Device.UUID = "RTU485Device1" if err := engine.LoadDevice(RTU485Device); err != nil { glogger.GLogger.Error("RTU485Device load failed:", err) } mqttOutEnd := typex.NewOutEnd( "MQTT", "MQTT桥接", "MQTT桥接", map[string]interface{}{ "Host": "127.0.0.1", "Port": 1883, "ClientId": "test-485-thgw1", "Username": "test-485-thgw1", "Password": "test-485-thgw1", "PubTopic": "MQTT/upstream/test-485-thgw1", "SubTopic": "MQTT/downstream/test-485-thgw1", }, ) mqttOutEnd.UUID = "mqttOutEnd-MQTT" if err := engine.LoadOutEnd(mqttOutEnd); err != nil { glogger.GLogger.Fatal("mqttOutEnd load failed:", err) } rule := typex.NewRule(engine, "uuid", "数据推送至MQTT", "数据推送至MQTT", []string{}, []string{RTU485Device.UUID}, // 数据来自网关设备,所以这里需要配置设备ID `function Success() print("[LUA Success Callback]=> OK") end`, ` Actions = {function(data) for tag, v in pairs(rulexlib:J2T(data)) do local ts = rulexlib:TsUnixNano() local value = rulexlib:J2T(v['value']) value['tag']= tag; local jsont = { method = 'report', requestId = ts, timestamp = ts, params = value } print('mqttOutEnd-MQTT', rulexlib:T2J(jsont)) rulexlib:DataToMqtt('mqttOutEnd-MQTT', rulexlib:T2J(jsont)) end return true, data end} `, `function Failed(error) print("[LUA Failed Callback]", error) end`) if err := engine.LoadRule(rule); err != nil { glogger.GLogger.Fatal(err) } s := <-c glogger.GLogger.Warn("Received stop signal:", s) engine.Stop() os.Exit(0) } ```

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

4186 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传