Skip to content

Commit

Permalink
BACnet协议,楼宇自动化
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Apr 5, 2022
1 parent b238620 commit 7203ff6
Show file tree
Hide file tree
Showing 22 changed files with 547 additions and 1,194 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/publish-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@ name: publish-beta

on:
push:
branches: [ master ]
branches:
- master
- main
paths:
- 'NewLife.BACnet/**'
workflow_dispatch:

jobs:
build-publish:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x
- name: Restore
run: |
dotnet restore NewLife.Delta/NewLife.Delta.csproj
dotnet restore NewLife.BACnet/NewLife.BACnet.csproj
- name: Build
run: |
dotnet pack --no-restore --version-suffix $(date "+%Y.%m%d-beta%H%M") -c Release -o out NewLife.Delta/NewLife.Delta.csproj
dotnet pack --no-restore --version-suffix $(date "+%Y.%m%d-beta%H%M") -c Release -o out NewLife.BACnet/NewLife.BACnet.csproj
- name: Publish
run: |
dotnet nuget push ./out/*.nupkg --skip-duplicate --source https://nuget.pkg.github.com/NewLifeX/index.json --api-key ${{ github.token }}
dotnet nuget push ./out/*.nupkg --skip-duplicate --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.nugetKey }}
10 changes: 5 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ name: publish
on:
push:
tags: [ v* ]
workflow_dispatch:

jobs:
build-publish:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x
- name: Restore
run: |
dotnet restore NewLife.Delta/NewLife.Delta.csproj
dotnet restore NewLife.BACnet/NewLife.BACnet.csproj
- name: Build
run: |
dotnet pack --no-restore --version-suffix $(date "+%Y.%m%d") -c Release -o out NewLife.Delta/NewLife.Delta.csproj
dotnet pack --no-restore --version-suffix $(date "+%Y.%m%d") -c Release -o out NewLife.BACnet/NewLife.BACnet.csproj
- name: Publish
run: |
dotnet nuget push ./out/*.nupkg --skip-duplicate --source https://nuget.pkg.github.com/NewLifeX/index.json --api-key ${{ github.token }}
dotnet nuget push ./out/*.nupkg --skip-duplicate --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.nugetKey }}
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
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
40 changes: 40 additions & 0 deletions NewLife.BACnet.sln
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
124 changes: 124 additions & 0 deletions NewLife.BACnet/Drivers/BACnetDriver.cs
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
}
15 changes: 15 additions & 0 deletions NewLife.BACnet/Drivers/BACnetNode.cs
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; }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.1</TargetFrameworks>
<AssemblyTitle>加拿大Delta控制</AssemblyTitle>
<Description>加拿大Delta控制,BACnet楼宇自动化</Description>
<AssemblyTitle>BACnet协议</AssemblyTitle>
<Description>BACnet楼宇自动化</Description>
<Company>新生命开发团队</Company>
<Copyright>©2002-2022 新生命开发团队</Copyright>
<VersionPrefix>1.0</VersionPrefix>
Expand All @@ -22,9 +22,9 @@
<Authors>$(Company)</Authors>
<PackageProjectUrl>https://www.yuque.com/smartstone/nx</PackageProjectUrl>
<PackageIcon>leaf.png</PackageIcon>
<RepositoryUrl>https://github.com/NewLifeX/NewLife.Modbus</RepositoryUrl>
<RepositoryUrl>https://github.com/NewLifeX/NewLife.BACnet</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>物联网;IoT;边缘计算;Edge;新生命团队;NewLife;PLC;$(AssemblyName)</PackageTags>
<PackageTags>物联网;IoT;边缘计算;Edge;新生命团队;NewLife;PLC;BACnet;$(AssemblyName)</PackageTags>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
Expand All @@ -42,8 +42,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="9.0.2022.304" />
<PackageReference Include="NewLife.IoT" Version="1.0.2022.314-beta0125" />
<PackageReference Include="NewLife.Core" Version="9.0.2022.401" />
<PackageReference Include="NewLife.IoT" Version="1.0.2022.402" />
<PackageReference Include="Talk.BACnet" Version="1.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 7203ff6

Please sign in to comment.