一个高性能、易部署的 IoT 开发框架,采用 Core-Edge 分布式架构。
- 🚀 高性能: 内存存储 + NSON 序列化,查询响应达微秒级
- 📦 易部署: 纯 Go 实现,无 CGO 依赖,单一二进制文件
- 🔌 灵活扩展: Wire/Pin 模型适配各种 IoT 设备
- 🎯 标准化: Cluster 系统促进设备定义统一
- 💾 低资源: Edge 端可运行在资源受限的嵌入式设备
- 🔒 安全: 支持 TLS 双向认证,节点级别 Secret
┌─────────────┐ gRPC/TLS ┌─────────────┐
│ Core 端 │ ◄─────────────────────► │ Edge 端 │
│ │ │ │
│ • 节点管理 │ │ • 本地设备 │
│ • 数据汇聚 │ │ • 数据采集 │
│ • API 服务 │ │ • 配置同步 │
│ │ │ • 离线缓存 │
└─────────────┘ └─────────────┘
│ │
▼ ▼
┌──────────┐ ┌──────────┐
│ Badger │ │ Badger │
│ (持久化) │ │ (持久化) │
└──────────┘ └──────────┘
- Go 1.24.0 或更高版本
- Protocol Buffers Compiler (可选,仅用于开发)
go get github.com/snple/beacon@latestpackage main
import (
"github.com/dgraph-io/badger/v4"
"github.com/snple/beacon/core"
"google.golang.org/grpc"
"net"
)
func main() {
// 打开数据库
opts := badger.DefaultOptions("/data/beacon/core")
db, _ := badger.Open(opts)
defer db.Close()
// 创建 Core 服务
cs, _ := core.Core(db)
cs.Start()
defer cs.Stop()
// 启动 gRPC 服务器
server := grpc.NewServer()
cs.Register(server)
lis, _ := net.Listen("tcp", ":50051")
server.Serve(lis)
}完整示例见 bin/core/main.go
package main
import (
"github.com/snple/beacon/edge"
"time"
)
func main() {
// 创建 Edge 服务
edgeOpts := []edge.EdgeOption{
edge.WithNodeID("edge-001", "secret"),
edge.WithNode(edge.NodeOptions{
Enable: true,
Addr: "core-server:50051",
}),
edge.WithSync(edge.SyncOptions{
Interval: time.Minute,
Realtime: false,
}),
}
es, _ := edge.Edge(edgeOpts...)
es.Start()
defer es.Stop()
// ... 实现设备驱动逻辑
}完整示例见 bin/edge/main.go
代表一个 IoT 设备或边缘节点。
代表设备的一个功能端点,例如:
- GPIO 端口组
代表具体的数据点或属性,例如:
- 温度传感器读数
- LED 开关状态
- 电机转速
Pin 的逻辑分组,用于标准化设备定义,例如:
OnOff- 开关控制LevelControl- 亮度/级别控制TemperatureMeasurement- 温度测量
import "github.com/snple/beacon/device"
// 使用预定义模板
result := device.BuildDimmableLightWire("bedroom_light")
// 设置类型和标签
result.Wire.Type = "DimmableLight"
result.Wire.Tags = []string{"category:light", "room:bedroom"}
// 配置 Pin 地址
for _, pin := range result.Pins {
switch pin.Name {
case "onoff":
pin.Addr = "40001" // Modbus 地址
case "level":
pin.Addr = "40002"
}
}
// 保存到数据库或发送到 Core
// ...import "github.com/snple/beacon/pb"
// 读取值
value, updated, err := storage.GetPinValue(nodeID, pinID)
// 写入值
nsonValue := &pb.NsonValue{
Value: &pb.NsonValue_Bool{Bool: true},
}
err = storage.SetPinValue(ctx, nodeID, pinID, nsonValue, time.Now())// 定时采集温度
ticker := time.NewTicker(time.Second)
for range ticker.C {
// 从传感器读取温度
temp := readTemperature()
// 保存到 Pin
value := &pb.NsonValue{
Value: &pb.NsonValue_F32{F32: temp},
}
storage.SetPinValue(ctx, pinID, value, time.Now())
}git clone https://github.com/snple/beacon.git
cd beacongo mod downloadmake gengo test ./...# Core 端
go build -o bin/core/core ./bin/core
# Edge 端
go build -o bin/edge/edge ./bin/edge- ✅ 工业物联网数据采集
- ✅ 智能家居系统
- ✅ 边缘计算平台
- ✅ 设备管理系统
- ✅ 中小规模 IoT 项目
- ❌ 超大规模系统 (百万级设备)
- ❌ 需要复杂 SQL 查询的场景
- ❌ 强一致性要求的场景 (目前是最终一致性)
欢迎贡献代码、报告问题或提出建议!
- Fork 项目
- 创建功能分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'feat: add some amazing feature') - 推送到分支 (
git push origin feature/AmazingFeature) - 提交 Pull Request
请参阅 开发指南 了解更多详情。
本项目采用 Apache 2.0 许可证。详见 LICENSE 文件。
- Issues: GitHub Issues
- Discussions: GitHub Discussions
⭐ 如果这个项目对你有帮助,请给个 Star!