-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
547 additions
and
1,194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: test | ||
|
||
on: | ||
push: | ||
branches: [ '*' ] | ||
pull_request: | ||
branches: [ '*' ] | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v2 | ||
with: | ||
dotnet-version: 6.0.x | ||
- name: Build | ||
run: dotnet build -c Release | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
needs: build | ||
|
||
steps: | ||
- name: Test | ||
run: dotnet test -c Release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.32112.339 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{719C113A-6804-4150-8473-BF889F06DFD6}" | ||
ProjectSection(SolutionItems) = preProject | ||
.editorconfig = .editorconfig | ||
.github\workflows\publish-beta.yml = .github\workflows\publish-beta.yml | ||
.github\workflows\publish.yml = .github\workflows\publish.yml | ||
Readme.MD = Readme.MD | ||
.github\workflows\test.yml = .github\workflows\test.yml | ||
EndProjectSection | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewLife.BACnet", "NewLife.BACnet\NewLife.BACnet.csproj", "{C83D59DF-58AA-4ECE-9AAE-0FE618771AEF}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest", "UnitTest\UnitTest.csproj", "{7A1FDA0B-5B02-4B75-8334-87EB9049BDE2}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{C83D59DF-58AA-4ECE-9AAE-0FE618771AEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{C83D59DF-58AA-4ECE-9AAE-0FE618771AEF}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{C83D59DF-58AA-4ECE-9AAE-0FE618771AEF}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{C83D59DF-58AA-4ECE-9AAE-0FE618771AEF}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{7A1FDA0B-5B02-4B75-8334-87EB9049BDE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{7A1FDA0B-5B02-4B75-8334-87EB9049BDE2}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{7A1FDA0B-5B02-4B75-8334-87EB9049BDE2}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{7A1FDA0B-5B02-4B75-8334-87EB9049BDE2}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {323831A1-A95B-40AB-B9AD-36A0BC10C2CB} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using NewLife.BACnet.Protocols; | ||
using NewLife.IoT.Drivers; | ||
using NewLife.IoT.ThingModels; | ||
using NewLife.Log; | ||
|
||
namespace NewLife.BACnet.Drivers; | ||
|
||
/// <summary> | ||
/// BACnet协议封装 | ||
/// </summary> | ||
[Driver("BACnet")] | ||
public class BACnetDriver : DisposeBase, IDriver | ||
{ | ||
private BACnetClient _client; | ||
private Int32 _nodes; | ||
|
||
#region 构造 | ||
#endregion | ||
|
||
#region 方法 | ||
/// <summary> | ||
/// 打开通道。一个BACnet设备可能分为多个通道读取,需要共用Tcp连接,以不同节点区分 | ||
/// </summary> | ||
/// <param name="channel">通道</param> | ||
/// <param name="parameters">参数</param> | ||
/// <returns></returns> | ||
public INode Open(IChannel channel, IDictionary<String, Object> parameters) | ||
{ | ||
var node = new BACnetNode | ||
{ | ||
Address = parameters["Address"] as String, | ||
Channel = channel | ||
}; | ||
|
||
// 实例化一次Tcp连接 | ||
if (_client == null) | ||
{ | ||
lock (this) | ||
{ | ||
if (_client == null) | ||
{ | ||
var client = new BACnetClient(); | ||
|
||
// 外部已指定通道时,打开连接 | ||
if (channel != null) client.Open(); | ||
|
||
_client = client; | ||
} | ||
} | ||
} | ||
|
||
Interlocked.Increment(ref _nodes); | ||
|
||
return node; | ||
} | ||
|
||
/// <summary> | ||
/// 关闭设备驱动 | ||
/// </summary> | ||
/// <param name="node"></param> | ||
public void Close(INode node) | ||
{ | ||
if (Interlocked.Decrement(ref _nodes) <= 0) | ||
{ | ||
_client.TryDispose(); | ||
_client = null; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 读取数据 | ||
/// </summary> | ||
/// <param name="node">节点对象,可存储站号等信息,仅驱动自己识别</param> | ||
/// <param name="points">点位集合</param> | ||
/// <returns></returns> | ||
public virtual IDictionary<String, Object> Read(INode node, IPoint[] points) | ||
{ | ||
if (points == null || points.Length == 0) return null; | ||
|
||
// 加锁,避免冲突 | ||
lock (_client) | ||
{ | ||
var device = _client.GetNode((node as BACnetNode).Address); | ||
var dic = _client.Read(device, points); | ||
|
||
return dic; | ||
} | ||
|
||
} | ||
|
||
/// <summary> | ||
/// 写入数据 | ||
/// </summary> | ||
/// <param name="node">节点对象,可存储站号等信息,仅驱动自己识别</param> | ||
/// <param name="point">点位</param> | ||
/// <param name="value">数值</param> | ||
public virtual Object Write(INode node, IPoint point, Object value) | ||
{ | ||
var n = node as BACnetNode; | ||
return _client.Write(n.Address, point, value); | ||
} | ||
|
||
/// <summary> | ||
/// 控制设备,特殊功能使用 | ||
/// </summary> | ||
/// <param name="node"></param> | ||
/// <param name="parameters"></param> | ||
/// <exception cref="NotImplementedException"></exception> | ||
public void Control(INode node, IDictionary<String, Object> parameters) => throw new NotImplementedException(); | ||
#endregion | ||
|
||
#region 日志 | ||
/// <summary>日志</summary> | ||
public ILog Log { get; set; } | ||
|
||
/// <summary>写日志</summary> | ||
/// <param name="format"></param> | ||
/// <param name="args"></param> | ||
public void WriteLog(String format, params Object[] args) => Log?.Info(format, args); | ||
|
||
/// <summary>性能追踪器</summary> | ||
public ITracer Tracer { get; set; } | ||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using NewLife.IoT.Drivers; | ||
|
||
namespace NewLife.BACnet.Drivers; | ||
|
||
/// <summary> | ||
/// BACnet节点 | ||
/// </summary> | ||
public class BACnetNode : INode | ||
{ | ||
/// <summary>地址</summary> | ||
public String Address { get; set; } | ||
|
||
/// <summary>通道</summary> | ||
public IChannel Channel { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.