# lua_opcua **Repository Path**: srdgame/lua_opcua ## Basic Information - **Project Name**: lua_opcua - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-03-18 - **Last Updated**: 2026-03-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # lua_open62541 **Pure Lua OPC UA Protocol Implementation** - 教学和研究用的纯 Lua OPC UA 协议实现 [![License: MPLv2.0](https://img.shields.io/badge/License-MPLv2.0-brightgreen.svg)](LICENSE) [![Lua Version](https://img.shields.io/badge/Lua-5.4+-blue.svg)](https://www.lua.org/) [![Tests](https://img.shields.io/badge/Tests-243%20Passed-success.svg)](tests/) ## 📋 目录 - [特性](#-特性) - [安装](#-安装) - [快速开始](#-快速开始) - [功能概览](#-功能概览) - [使用示例](#-使用示例) - [项目结构](#-项目结构) - [测试](#-测试) - [文档](#-文档) - [许可证](#-许可证) - [贡献](#-贡献) ## ✨ 特性 ### 核心功能 - ✅ **纯 Lua 实现** - 无 C 依赖,完全可移植 - ✅ **OPC UA 协议** - 完整的客户端和服务端实现 - ✅ **虚拟传输层** - 支持 LuaSocket 和 Skynet 两种后端 - ✅ **类型系统** - 完整的 OPC UA 类型系统和编码 - ✅ **安全加密** - 支持多种加密算法和安全策略 - ✅ **方法调用** - 远程方法调用(RPC)支持 - ✅ **历史数据** - 历史数据读取和更新服务 - ✅ **订阅监控** - 数据变化订阅和通知 ### 技术特性 - 🚀 **高性能** - 优化的二进制编码和传输 - 🔒 **安全性** - 多种安全策略(None, Basic128Rsa15, Basic256, Basic256Sha256) - 🔌 **可扩展** - 模块化设计,易于扩展 - 📚 **教学友好** - 代码清晰,注释完善 - 🧪 **测试覆盖** - 243 个测试,100% 通过率 - 🎯 **类型安全** - 完整的 Variant 类型系统 ## 📦 安装 ### 环境要求 - **Lua 5.4+** (推荐使用 Lua 5.4) - **LuaRocks** (用于包管理) - **busted** (测试框架,可选) ### 使用 LuaRocks 安装 ```bash # 从本地 rockspec 安装 luarocks install lua_open62541-0.1.0-1.rockspec # 或从源码安装 git clone https://github.com/yourusername/lua_open62541.git cd lua_open62541 luarocks make ``` ### 手动安装 ```bash # 复制源码到 Lua 路径 cp -r src/opcua /usr/local/share/lua/5.4/ ``` ## 🚀 快速开始 ### 客户端示例 ```lua local opcua = require("opcua") -- 创建客户端 local client = opcua.Client:new() -- 连接到服务器 local ok, err = client:connect("opc.tcp://localhost:4840") if not ok then print("连接失败:", err) return end -- 创建会话 ok, err = client:create_session("MySession") if not ok then print("创建会话失败:", err) return end -- 激活会话 ok, err = client:activate_session() if not ok then print("激活会话失败:", err) return end -- 读取节点值 local value, err = client:read("ns=2;s=Demo.Variable") if value then print("节点值:", value) end -- 写入节点值 ok, err = client:write("ns=2;s=Demo.Variable", 42) if ok then print("写入成功") end -- 断开连接 client:disconnect_all() ``` ### 服务端示例 ```lua local opcua = require("opcua") -- 创建服务器 local server = opcua.Server:new() server.port = 4840 -- 启动服务器 server:start() -- 注册简单方法 server:register_method_simple( "ns=2;i=100", -- 对象 ID "ns=2;i=200", -- 方法 ID "MultiplyBy2", -- 方法名 function(input) -- 方法实现 return input * 2 end ) -- 运行服务器 server:run() ``` ## 📚 功能概览 ### 1. 类型系统 完整的 OPC UA 类型系统实现: ```lua local NodeId = require("opcua.types.nodeid") local Variant = require("opcua.types.variant") -- 创建 NodeId local id = NodeId.numeric(2, 100) -- ns=2;i=100 local id2 = NodeId.string(2, "Demo.Var") -- ns=2;s=Demo.Var -- 创建 Variant local v1 = Variant.new(42) -- Int32 local v2 = Variant.new("hello") -- String local v3 = Variant.array({1,2,3}, "Int32") -- Int32数组 ``` ### 2. 客户端 API 高级客户端 API: ```lua local client = opcua.Client:new() -- 连接和会话 client:connect_and_session("opc.tcp://localhost:4840") -- 读写操作 local value = client:read("ns=2;i=100") client:write("ns=2;i=100", 123) -- 批量读取 local values = client:read_multiple({ "ns=2;i=100", "ns=2;i=101", "ns=2;i=102" }) -- 浏览节点 local children = client:browse("ns=2;i=100") -- 方法调用 local result = client:call_method( "ns=2;i=100", -- 对象 ID "ns=2;i=200", -- 方法 ID {42} -- 输入参数 ) -- 历史数据读取 local history = client.services.history.read_raw( client, "ns=2;i=100", os.time() - 3600, -- 开始时间 os.time() -- 结束时间 ) ``` ### 3. 服务端 API 服务端功能: ```lua local server = opcua.Server:new() -- 启动服务器 server:start() -- 添加节点 local node = { node_id = "ns=2;i=100", node_class = 1, -- Variable browse_name = "MyVariable", display_name = "My Variable", value = 42 } server:add_node(node) -- 注册方法 server:register_method_simple( "ns=2;i=100", "ns=2;i=200", "MyMethod", function(x, y) return x + y end ) -- 运行服务器 server:run() ``` ### 4. 安全加密 安全策略和加密支持: ```lua local security = require("opcua.security") -- 安全策略 local policy = security.security_policy.Basic256Sha256 -- 证书生成 local cert = security.certificate.generate_self_signed_cert( "CN=MyServer", 365 -- 有效期(天) ) -- 加密数据 local encrypted = security.encryption.encrypt_aes( plaintext, key, security.security_policy.Basic256 ) ``` ### 5. 传输层 虚拟传输层支持: ```lua -- 使用 LuaSocket(默认) local client = opcua.Client:new() local transport = require("opcua.transport.luasocket") -- 使用 Skynet local skynet_transport = require("opcua.transport.skynet") local skynet_client = opcua.Client:new(skynet_transport) ``` ## 💡 使用示例 ### 示例 1: 读写数据 ```lua local opcua = require("opcua") local client = opcua.Client:new() -- 连接 client:connect_and_session("opc.tcp://localhost:4840") -- 读取当前值 local temp = client:read("ns=2;s=Temperature") print(string.format("当前温度: %.1f°C", temp)) -- 写入新值 client:write("ns=2;s=SetPoint", 25.5) -- 断开 client:disconnect_all() ``` ### 示例 2: 订阅监控 ```lua local client = opcua.Client:new() client:connect_and_session("opc.tcp://localhost:4840") -- 创建订阅 local subscription = client:create_subscription(1000) -- 添加监控项 local monitored_item = subscription:add_monitored_item( "ns=2;s=Temperature", function(value) print(string.format("温度变化: %.1f°C", value)) end ) -- 保持运行... -- monitored_item:remove() -- subscription:delete() ``` ### 示例 3: 方法调用 ```lua -- 服务端 local server = opcua.Server:new() server:start() -- 注册计算方法 server:register_method_simple( "ns=2;i=100", "ns=2;i=200", "Add", function(a, b) return a + b end ) server:run() -- 客户端 local client = opcua.Client:new() client:connect_and_session("opc.tcp://localhost:4840") -- 调用方法 local result = client:call_method( "ns=2;i=100", "ns=2;i=200", {10, 20} ) print("10 + 20 =", result[1]) -- 输出: 10 + 20 = 30 ``` ### 示例 4: 历史数据 ```lua local client = opcua.Client:new() client:connect_and_session("opc.tcp://localhost:4840") -- 读取过去1小时的历史数据 local history = client.services.history.read_raw( client, "ns=2;s=Temperature", os.time() - 3600, os.time(), 0 -- 0 = 所有值 ) -- 处理历史数据 for i, data in ipairs(history[1].data_values) do print(string.format("[%d] 值: %.1f, 时间: %s", i, data.value:get_value(), os.date("%Y-%m-%d %H:%M:%S", data.source_time) )) end ``` ## 📁 项目结构 ``` lua_open62541/ ├── src/opcua/ # 源代码 │ ├── init.lua # 模块入口 │ ├── util/ # 工具模块 │ │ ├── logger.lua # 日志工具 │ │ ├── tools.lua # 字节/位操作 │ │ └── middleclass.lua # OOP框架 │ ├── types/ # 类型系统 │ │ ├── builtin.lua # 内置类型 │ │ ├── nodeid.lua # NodeId │ │ ├── variant.lua # Variant │ │ └── data_value.lua # DataValue │ ├── encoding/ # 编码层 │ │ └── binary.lua # 二进制编码 │ ├── transport/ # 传输层 │ │ ├── interface.lua # 传输接口 │ │ ├── luasocket.lua # LuaSocket实现 │ │ └── skynet.lua # Skynet实现 │ ├── protocol/ # 协议层 │ │ ├── header.lua # 消息头 │ │ ├── secure_channel.lua # 安全通道 │ │ └── session.lua # 会话管理 │ ├── services/ # 服务层 │ │ ├── discovery.lua # 发现服务 │ │ ├── session_service.lua # 会话服务 │ │ ├── monitored.lua # 监控服务 │ │ ├── method_service.lua # 方法调用 │ │ └── history_service.lua # 历史数据 │ ├── client/ # 客户端 │ │ ├── client.lua # 客户端主类 │ │ ├── highlevel.lua # 高级API │ │ └── subscription.lua # 订阅管理 │ ├── server/ # 服务端 │ │ ├── server.lua # 服务端主类 │ │ ├── nodestore.lua # 节点存储 │ │ ├── service_handler.lua # 服务处理器 │ │ └── subscription_manager.lua # 订阅管理器 │ └── security/ # 安全层 │ ├── security_policy.lua # 安全策略 │ ├── encryption.lua # 加密功能 │ └── certificate.lua # 证书管理 ├── tests/ # 测试文件 │ ├── test_init.lua │ ├── test_logger.lua │ ├── test_types.lua │ ├── test_client.lua │ ├── test_server.lua │ ├── test_method.lua │ ├── test_history.lua │ ├── test_security.lua │ └── ... ├── examples/ # 示例代码 │ ├── client_basic.lua │ ├── server_basic.lua │ ├── client_subscription.lua │ └── feature_showcase.lua ├── docs/ # 文档 │ ├── SECURITY.md # 安全文档 │ └── SKYNET_TRANSPORT.md # Skynet文档 ├── README.md # 项目说明 ├── PLAN.md # 实现计划 ├── LICENSE # MPLv2.0许可证 └── lua_open62541-0.1.0-1.rockspec # LuaRocks配置 ``` ## 🧪 测试 ### 运行所有测试 ```bash # 使用 busted 运行测试 busted # 或使用 Lua 5.4 lua5.4 test_runner.lua ``` ### 测试覆盖 当前测试统计: - **总测试数**: 243 - **通过率**: 100% - **测试模块**: - 基础模块测试 (test_init.lua, test_logger.lua, test_tools.lua) - 类型系统测试 (test_types.lua) - 编码测试 (test_encoding.lua) - 传输层测试 (test_transport.lua) - Skynet测试 (test_skynet.lua) - 客户端测试 (test_client.lua) - 服务端测试 (test_server.lua) - 方法调用测试 (test_method.lua) - 历史数据测试 (test_history.lua) - 安全加密测试 (test_security.lua) - 集成测试 (test_integration.lua) ## 📖 文档 ### 详细文档 - [PLAN.md](PLAN.md) - 完整的实现计划和进度 - [docs/SECURITY.md](docs/SECURITY.md) - 安全加密文档 - [docs/SKYNET_TRANSPORT.md](docs/SKYNET_TRANSPORT.md) - Skynet传输层文档 ### API 文档 ```lua -- 查看模块信息 local opcua = require("opcua") print(opcua._VERSION) -- 版本号 print(opcua._NAME) -- 模块名 print(opcua._DESCRIPTION) -- 描述 -- 查看子模块 print(opcua.util.logger) -- 日志工具 print(opcua.types.variant) -- Variant类型 print(opcua.client.highlevel) -- 高级客户端 ``` ## 📄 许可证 本项目采用 **MPLv2.0** 许可证 - 详见 [LICENSE](LICENSE) 文件 ## 🤝 贡献 欢迎贡献!请遵循以下步骤: 1. Fork 本仓库 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`) 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) 4. 推送到分支 (`git push origin feature/AmazingFeature`) 5. 开启 Pull Request ### 代码规范 - 遵循 Lua 5.4 最佳实践 - 添加单元测试 - 更新相关文档 - 保持代码清晰易读 ## 📞 联系方式 - 问题反馈: [GitHub Issues](https://github.com/yourusername/lua_open62541/issues) - 文档: [Wiki](https://github.com/yourusername/lua_open62541/wiki) ## 🙏 致谢 - [open62541](https://github.com/open62541/open62541) - 优秀的 OPC UA C 实现,提供了参考 - OPC Foundation - OPC UA 规范 - Lua 社区 - 提供了优秀的工具和库 --- **Made with ❤️ for the OPC UA community**