Skip to content

Commit

Permalink
版本升级
Browse files Browse the repository at this point in the history
  • Loading branch information
18298001496 committed Oct 25, 2022
1 parent cb073c6 commit b5df5c8
Show file tree
Hide file tree
Showing 13 changed files with 508 additions and 80 deletions.
4 changes: 4 additions & 0 deletions src/IOTCS.EdgeGateway.Application/Imps/DataLocationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public async Task<IEnumerable<DataLocationDto>> GetNodeByIdAsync(string id)
results.Add(dataLocation);
}
}
else
{
results.AddRange(node);
}
return results;
}

Expand Down
23 changes: 3 additions & 20 deletions src/IOTCS.EdgeGateway.CmdHandler/SystemNotificationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using IOTCS.EdgeGateway.WsHandler;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -19,19 +18,18 @@ public class SystemNotificationHandler : INotificationHandler<SystemNotification
public SystemNotificationHandler(WsMessageHandler messageHandler)
{
_logger = IocManager.Instance.GetService<ILoggerFactory>().CreateLogger("WebSocket");
_webSocket = messageHandler;//IocManager.Instance.GetService<WsMessageHandler>();
_webSocket = messageHandler;
}

public async Task Handle(SystemNotification notification, CancellationToken cancellationToken)
{
try
{
//var strString = HandleMessage(notification);
{
await Send(notification);
}
catch (Exception e)
{
_logger.Error($"OPCUA 系统消息解析异常,错误消息 => {e.Message},错误位置 => {e.StackTrace},");
_logger.Error($"系统消息通知异常,错误消息 => {e.Message},错误位置 => {e.StackTrace},");
}
}

Expand All @@ -51,20 +49,5 @@ await _webSocket.SendMessageToAllAsync(new WebSocketManager.Common.Message
_logger.Error($"系统消息发送WebSocket信息到UI异常,异常信息 => {e.Message},位置 => {e.StackTrace}");
}
}

//private string HandleMessage(SystemNotification message)
//{
// var result = string.Empty;
// var socketObject = new WebSocketProtocol<dynamic>();

// if (message != null)
// {
// socketObject.RequestType = message.MsgType;
// socketObject.Data = message.Message;
// result = JsonConvert.SerializeObject(socketObject);
// }

// return result;
//}
}
}
5 changes: 3 additions & 2 deletions src/IOTCS.EdgeGateway.Freesql/DBSessionContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FreeSql;
using Microsoft.Extensions.Options;
using System;
using System.IO;

namespace IOTCS.EdgeGateway.Freesql
{
Expand Down Expand Up @@ -68,9 +69,9 @@ private DataType GetOptions(Extensions.DbContextOptions options, out string conn
dbType = DataType.Sqlite;
string dbPath = string.Empty;
if (options.Debug)
dbPath = System.IO.Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName + "\\Database\\iotcs.db";
dbPath = Path.Combine(System.IO.Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName, "Database", "iotcs.db");
else
dbPath = System.IO.Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory) + "\\Database\\iotcs.db";
dbPath = Path.Combine(System.IO.Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).FullName, "Database", "iotcs.db");
connectionStrings = $"Data Source={dbPath}";
break;
default:
Expand Down
142 changes: 138 additions & 4 deletions src/IOTCS.EdgeGateway.Plugins/BACNetDriver/BACNetDriver.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,158 @@
using System;
using IOTCS.EdgeGateway.Core;
using IOTCS.EdgeGateway.Core.Collections;
using IOTCS.EdgeGateway.Diagnostics;
using IOTCS.EdgeGateway.Domain.ValueObject;
using Microsoft.Extensions.DependencyInjection;
using IOTCS.EdgeGateway.Domain.ValueObject.Device;
using IOTCS.EdgeGateway.Logging;
using System;
using System.Collections.Generic;
using System.IO.BACnet;
using System.Text;
using System.Linq;
using Newtonsoft.Json;
using System.Text.RegularExpressions;

namespace IOTCS.EdgeGateway.Plugins.BACNetDriver
{
public class BACNetDriver : IBACNetDriver
{
private readonly ILogger _logger;
private readonly ISystemDiagnostics _diagnostics;
private int _bacnetDeviceId = 0;
private BacnetClient _bacneClient;
private BacnetAddress _bacneAddr = null;
private IConcurrentList<DeviceConfigDto> _deviceConfig = null;
private IConcurrentList<DataLocationDto> _dataLocations = null;
public BACNetDriver()
{
_logger = IocManager.Instance.GetService<ILoggerFactory>().CreateLogger("Monitor");
_deviceConfig = IocManager.Instance.GetService<IConcurrentList<DeviceConfigDto>>();
_diagnostics = IocManager.Instance.GetService<ISystemDiagnostics>();
_dataLocations = IocManager.Instance.GetService<IConcurrentList<DataLocationDto>>();
}
public bool Connect(string deviceID)
{
throw new NotImplementedException();
var result = false;
try
{
if (!string.IsNullOrEmpty(deviceID))
{
//获取配置
var config = _deviceConfig.Where(w => w.DeviceId == deviceID).FirstOrDefault();
if (config != null && !string.IsNullOrEmpty(config.ConfigJson))
{
var djson = JsonConvert.DeserializeObject<dynamic>(config.ConfigJson);
_bacnetDeviceId = Convert.ToInt32(djson.Host);
if (Convert.ToInt32(djson.Port) != 0)
_bacneClient = new BacnetClient(new BacnetIpUdpProtocolTransport(Convert.ToInt32(djson.Port), false));
else//使用默认端口
_bacneClient = new BacnetClient(new BacnetIpUdpProtocolTransport(0xBAC0, false));
_bacneClient.Start();
_bacneClient.OnIam += new BacnetClient.IamHandler(Handler_OnIam);
_bacneClient.WhoIs();
}
else
{
var msg = $"BACNet连接失败!设备ID => {deviceID},没有找到对应的设备配置信息。";
_logger.Error(msg);
_diagnostics.PublishDiagnosticsInfo(msg);
}
}
}
catch (Exception ex)
{
result = false;
var msg = $"BACNet连接失败!设备ID => {deviceID},信息 => {ex.Message},位置 => {ex.StackTrace}";
_logger.Error(msg);
_diagnostics.PublishDiagnosticsInfo(msg);
}
return result;
}

public bool IsAviable()
{
throw new NotImplementedException();
return _bacneAddr != null;
}

public string Run(string deviceID, string groupID)
{
throw new NotImplementedException();
var result = string.Empty;
if (IsAviable())
{
//var nodeValues = new Dictionary<string, BacnetValue>();
var locations = _dataLocations.Where(w => w.ParentId == groupID);
var curLocationValues = from l in locations
select new DataNodeDto
{
FieldName = l.DisplayName,
NodeId = l.NodeAddress
};
List<DataNodeDto> list = new List<DataNodeDto>();
DataNodeDto dNode = null;
foreach (var l in locations)
{
BacnetValue currNodeValue;
dNode = curLocationValues.Where(w => w.NodeId == l.Id).FirstOrDefault();
var ObjName = Regex.Replace(l.NodeAddress, "[a-z]", "", RegexOptions.IgnoreCase);
var ObjAddr = Convert.ToUInt32(Regex.Replace(l.NodeAddress, "[0-9]", "", RegexOptions.IgnoreCase));
var readRet = ReadScalarValue(new BacnetObjectId(ConvertBacnetType(ObjName), ObjAddr), BacnetPropertyIds.PROP_PRESENT_VALUE, out currNodeValue);
dNode.NodeValue = (string)currNodeValue.Value;
dNode.StatusCode = readRet ? "good" : "error";
list.Add(dNode);
// nodeValues.Add(l.Id, currNodeValue);
}
result = JsonConvert.SerializeObject(list);
list.Clear();
}
else
{
//设备重连
Connect(deviceID);
}
return result;
}
private void Handler_OnIam(BacnetClient sender, BacnetAddress adr, uint device_id, uint max_apdu, BacnetSegmentations segmentation, ushort vendor_id)
{
if (device_id == _bacnetDeviceId)
_bacneAddr = adr;
}

private bool ReadScalarValue(BacnetObjectId BacnetObjet, BacnetPropertyIds Propriete, out BacnetValue Value)
{
IList<BacnetValue> NoScalarValue;

Value = new BacnetValue(null);

// Property Read
if (_bacneClient.ReadPropertyRequest(_bacneAddr, BacnetObjet, Propriete, out NoScalarValue) == false)
return false;

Value = NoScalarValue[0];
return true;
}

private BacnetObjectTypes ConvertBacnetType(string objName)
{
if (objName.Equals("AI"))
return BacnetObjectTypes.OBJECT_ANALOG_INPUT;
if (objName.Equals("AO"))
return BacnetObjectTypes.OBJECT_ANALOG_OUTPUT;
if (objName.Equals("AV"))
return BacnetObjectTypes.OBJECT_ANALOG_VALUE;
if (objName.Equals("BI"))
return BacnetObjectTypes.OBJECT_BINARY_INPUT;
if (objName.Equals("BO"))
return BacnetObjectTypes.OBJECT_BINARY_OUTPUT;
if (objName.Equals("BV"))
return BacnetObjectTypes.OBJECT_BINARY_VALUE;
if (objName.Equals("MSI"))
return BacnetObjectTypes.OBJECT_MULTI_STATE_INPUT;
if (objName.Equals("MSO"))
return BacnetObjectTypes.OBJECT_MULTI_STATE_OUTPUT;
if (objName.Equals("MSV"))
return BacnetObjectTypes.OBJECT_MULTI_STATE_VALUE;
return BacnetObjectTypes.OBJECT_ANALOG_INPUT;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ namespace IOTCS.EdgeGateway.Plugins.BACNetDriver
{
public interface IBACNetDriver : IDriver
{

}
}
9 changes: 7 additions & 2 deletions src/IOTCS.EdgeGateway.Plugins/Executor/Collector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ private void LoadDriver()
siemens_s7_1500.Connect(e.Id);
_equipments.TryAdd(e.Id, siemens_s7_1500);
break;
case "bacnet/ip":
var bacnet_ip = new BACNetDriver.BACNetDriver();
bacnet_ip.Connect(e.Id);
_equipments.TryAdd(e.Id, bacnet_ip);
break;
}
}
else
Expand All @@ -215,7 +220,7 @@ private string BuildingJsonObject(string data, string deviceID, string groupID)
var result = string.Empty;
var retResult = new List<dynamic>();
IEnumerable<DataNodeDto> list = JsonConvert.DeserializeObject<IEnumerable<DataNodeDto>>(data);
Dictionary<string, string> keyValues = new Dictionary<string, string>();
Dictionary<string, dynamic> keyValues = new Dictionary<string, dynamic>();
try
{
var device = _device.Where(w => w.Id == deviceID).FirstOrDefault();
Expand All @@ -226,7 +231,7 @@ private string BuildingJsonObject(string data, string deviceID, string groupID)
keyValues.Add("DeviceID", deviceID);
keyValues.Add("GroupID", groupID);
keyValues.Add("Timestamp", DateTime.Now.ToString());
foreach (var e in list)
foreach (var e in list)
{
keyValues.Add(e.FieldName, e.NodeValue);
}
Expand Down
Loading

0 comments on commit b5df5c8

Please sign in to comment.