添加设备点位监控

main
xiaoguo 2 years ago
parent 6cf243d09c
commit 6539a9fdbe

Binary file not shown.

@ -1,12 +0,0 @@
using PVDEMCS.Common.DI;
using PVDEMCS.Services;
namespace PVDEMCS.Common.Devices
{
public interface IDeviceConfiguration : IDependency
{
}
}

@ -0,0 +1,21 @@
using PVDEMCS.Common.DI;
using PVDEMCS.Services;
namespace PVDEMCS.Common.Devices
{
/// <summary>
/// 设备运行
/// </summary>
public interface IDeviceRun : IDependency
{
/// <summary>
/// 开始运行
/// </summary>
void Run();
/// <summary>
/// 停止运行
/// </summary>
void Stop();
}
}

@ -1,50 +0,0 @@
using PVDEMCS.Services;
using System.Threading;
namespace PVDEMCS.Common.Devices.Impl
{
public class DeviceConfiguration : IDeviceConfiguration
{
private IDeviceService deviceService;
private List<DeviceMonitor> monitors = new List<DeviceMonitor>();
public DeviceConfiguration(IDeviceService device)
{
this.deviceService = device;
}
public void Load()
{
monitors.Clear();
var result = this.deviceService.GetDeviceInfoList("", "", true);
if (result.IsSuccess)
{
var devices = result.Content;
foreach (var device in devices)
{
var deviceMonitor = new DeviceMonitor(device);
var devicePoints = deviceService.GetDevicePointList(device.DeviceName);
if (devicePoints.IsSuccess && devicePoints.Content.Count>0)
{
deviceMonitor.Load(devicePoints.Content.ToArray());
}
deviceMonitor.ErrorMessage += DeviceMonitor_ErrorMessage;
deviceMonitor.PointChnage += DeviceMonitor_PointChnage;
monitors.Add(deviceMonitor);
Task<bool> task = deviceMonitor.StartAsync();
}
}
}
private void DeviceMonitor_PointChnage(object sender, Services.Models.DevicePoint e)
{
}
private void DeviceMonitor_ErrorMessage(object sender, string e)
{
}
}
}

@ -39,6 +39,26 @@ namespace PVDEMCS.Common.Devices.Impl
return plcCommunicationService; return plcCommunicationService;
} }
} }
/// <summary>
/// 连接状态
/// </summary>
public Boolean isConnected
{
get
{
return plcCommunicationService.isConnected;
}
}
/// <summary>
/// 监控是否运行
/// </summary>
public Boolean IsRun
{
get { return IsRun; }
}
/// <summary> /// <summary>
/// 当前点位集合 /// 当前点位集合
/// </summary> /// </summary>

@ -0,0 +1,155 @@
using AngleSharp.Css.Dom;
using Masuit.Tools;
using PVDEMCS.Common.Constant;
using PVDEMCS.Services;
using PVDEMCS.Services.Models;
using System.Threading;
namespace PVDEMCS.Common.Devices.Impl
{
/// <summary>
/// 设备运行
/// </summary>
public class DeviceRun : IDeviceRun
{
private IDeviceService deviceService;
private IEquipmentRecordService equipmentRecordService;
private List<DeviceMonitor> monitors = new List<DeviceMonitor>();
private object lockObj = new object();
private object lockObj1 = new object();
public DeviceRun(IDeviceService deviceService, IEquipmentRecordService equipmentRecordService)
{
this.deviceService = deviceService;
this.equipmentRecordService = equipmentRecordService;
TaskRun();
}
/// <summary>
/// 开始运行
/// </summary>
public void Run()
{
Stop();
monitors.Clear();
var result = this.deviceService.GetDeviceInfoList("", "", true);
if (result.IsSuccess)
{
var devices = result.Content;
foreach (var device in devices)
{
var deviceMonitor = new DeviceMonitor(device);
var devicePoints = deviceService.GetDevicePointList(device.DeviceName);
if (devicePoints.IsSuccess && devicePoints.Content.Count > 0)
{
deviceMonitor.Load(devicePoints.Content.ToArray());
}
deviceMonitor.ErrorMessage += DeviceMonitor_ErrorMessage;
deviceMonitor.PointChnage += DeviceMonitor_PointChnage; ;
monitors.Add(deviceMonitor);
Task<bool> task = deviceMonitor.StartAsync();
}
}
}
/// <summary>
/// 停止运行
/// </summary>
public void Stop()
{
foreach (var item in monitors)
{
var task = item.StartAsync();
}
}
private void TaskRun()
{
Task.Run(() =>
{
while (true)
{
lock (lockObj)
{
RunEquipmentRecord();
Thread.Sleep(1000);
}
}
});
Task.Run(() =>
{
while (true)
{
lock (lockObj1)
{
RunEquipmentRecordTotal();
Thread.Sleep(1000 * 60);
}
}
});
}
//记录设备数据
private void RunEquipmentRecord()
{
foreach (var monitor in monitors)
{
if (!monitor.IsRun)
{
continue;
}
var points = monitor.Points;
if (!points.IsNullOrEmpty())
{
continue;
}
var equipmentIds = points.Select(f => f.EquipmentId).Distinct();
foreach (var id in equipmentIds)
{
var list = points.Where(f => f.EquipmentId == id).ToList();
if (list.Count >= 2)
{
var startStop = list.Where(f => f.ActionType == ActionType.StartStop).FirstOrDefault();
var fault = list.Where(f => f.ActionType == ActionType.Fault).FirstOrDefault();
if (!startStop.IsNullOrEmpty() && !fault.IsNullOrEmpty())
{
var state = EquipmentState.Run;
if (fault.GetValue<bool>())
{
state = EquipmentState.Alarm;
}
else if (!startStop.GetValue<bool>())
{
state = EquipmentState.Stop;
}
this.equipmentRecordService.AddUpdateEquipmentRecord(id, state, DateTime.Now);
}
}
}
}
}
//记录设备数据统计
private void RunEquipmentRecordTotal()
{
equipmentRecordService.RunEquipmentRecordTotal(DateTime.Now);
}
private void DeviceMonitor_PointChnage(object sender, DevicePoint e)
{
}
private void DeviceMonitor_ErrorMessage(object sender, string e)
{
Console.WriteLine(e);
}
}
}

@ -52,8 +52,10 @@ app.MapControllers();
app.Lifetime.ApplicationStarted.Register(() => app.Lifetime.ApplicationStarted.Register(() =>
{ {
var deviceConfiguration = app.Services.GetService<IDeviceConfiguration>(); var deviceRun = app.Services.GetService<IDeviceRun>();
//deviceConfiguration
deviceRun.Run();
}); });
app.Run(); app.Run();

@ -2,6 +2,8 @@
#nullable disable #nullable disable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace PVDEMCS.Services.Models; namespace PVDEMCS.Services.Models;
@ -47,6 +49,11 @@ public partial class DevicePoint
/// </summary> /// </summary>
public string EquipmentType { get; set; } public string EquipmentType { get; set; }
/// <summary>
/// 点位所属动作启动停止start_stop故障fault
/// </summary>
public string ActionType { get; set; }
/// <summary> /// <summary>
/// 点位编号 /// 点位编号
/// </summary> /// </summary>
@ -79,4 +86,13 @@ public partial class DevicePoint
public Object ObjectValue { get; set; } public Object ObjectValue { get; set; }
public T GetValue<T>()
{
if (ObjectValue is T)
{
return (T)ObjectValue;
}
return default;
}
} }

@ -279,6 +279,7 @@ namespace PVDEMCS.Services.Repositories.Impl
PointName = point.PointName, PointName = point.PointName,
Address = point.Address, Address = point.Address,
DataType = point.DataType, DataType = point.DataType,
ActionType = point.ActionType,
Remark = point.Remark, Remark = point.Remark,
}; };
if (!deviceName.IsNullOrEmpty()) if (!deviceName.IsNullOrEmpty())

@ -110,11 +110,6 @@ namespace PVDEMCS.Services.Repositories.Impl
/// <returns></returns> /// <returns></returns>
public Result AddUpdateEquipmentRecord(string equipmentId, string state, DateTime dateTime) public Result AddUpdateEquipmentRecord(string equipmentId, string state, DateTime dateTime)
{ {
if (!EquipmentState.HaveState(state))
{
return new Result($"【{state}】状态不正确!");
}
using (var context = new EFContext()) using (var context = new EFContext())
{ {
var entity = context.EquipmentRecords.Where(f => f.EquipmentId == equipmentId).OrderByDescending(f => f.StartTime).FirstOrDefault(); var entity = context.EquipmentRecords.Where(f => f.EquipmentId == equipmentId).OrderByDescending(f => f.StartTime).FirstOrDefault();
@ -162,7 +157,7 @@ namespace PVDEMCS.Services.Repositories.Impl
var result = new Result(); var result = new Result();
using (var context = new EFContext()) using (var context = new EFContext())
{ {
var query = context.EquipmentRecords.Where(f => f.StartTime >= begDate && f.StartTime <= endDate); var query = context.EquipmentRecords.Where(f => f.StartTime >= begDate && f.StartTime <= endDate).ToList();
//获取状态统计时间 //获取状态统计时间
var total = query.GroupBy(f => new { f.EquipmentId, f.State }) var total = query.GroupBy(f => new { f.EquipmentId, f.State })
.Select(f => new TotalRecord .Select(f => new TotalRecord

@ -59,6 +59,21 @@
报警 报警
</summary> </summary>
</member> </member>
<member name="T:PVDEMCS.Common.Devices.IDeviceRun">
<summary>
设备运行
</summary>
</member>
<member name="M:PVDEMCS.Common.Devices.IDeviceRun.Run">
<summary>
开始运行
</summary>
</member>
<member name="M:PVDEMCS.Common.Devices.IDeviceRun.Stop">
<summary>
停止运行
</summary>
</member>
<member name="E:PVDEMCS.Common.Devices.Impl.DeviceMonitor.PointChnage"> <member name="E:PVDEMCS.Common.Devices.Impl.DeviceMonitor.PointChnage">
<summary> <summary>
PLC 点位改变触发 PLC 点位改变触发
@ -74,6 +89,16 @@
间隔时间 间隔时间
</summary> </summary>
</member> </member>
<member name="P:PVDEMCS.Common.Devices.Impl.DeviceMonitor.isConnected">
<summary>
连接状态
</summary>
</member>
<member name="P:PVDEMCS.Common.Devices.Impl.DeviceMonitor.IsRun">
<summary>
监控是否运行
</summary>
</member>
<member name="P:PVDEMCS.Common.Devices.Impl.DeviceMonitor.Points"> <member name="P:PVDEMCS.Common.Devices.Impl.DeviceMonitor.Points">
<summary> <summary>
当前点位集合 当前点位集合
@ -118,6 +143,21 @@
<param name="point"></param> <param name="point"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="T:PVDEMCS.Common.Devices.Impl.DeviceRun">
<summary>
设备运行
</summary>
</member>
<member name="M:PVDEMCS.Common.Devices.Impl.DeviceRun.Run">
<summary>
开始运行
</summary>
</member>
<member name="M:PVDEMCS.Common.Devices.Impl.DeviceRun.Stop">
<summary>
停止运行
</summary>
</member>
<member name="T:PVDEMCS.Common.Devices.Impl.S7CommunicationService"> <member name="T:PVDEMCS.Common.Devices.Impl.S7CommunicationService">
<summary> <summary>
PLC通信服务 SiemensS7 smart200 实现 PLC通信服务 SiemensS7 smart200 实现
@ -1515,6 +1555,11 @@
设备类型IonbondBalzersCemecon 设备类型IonbondBalzersCemecon
</summary> </summary>
</member> </member>
<member name="P:PVDEMCS.Services.Models.DevicePoint.ActionType">
<summary>
点位所属动作启动停止start_stop故障fault
</summary>
</member>
<member name="P:PVDEMCS.Services.Models.DevicePoint.PointCode"> <member name="P:PVDEMCS.Services.Models.DevicePoint.PointCode">
<summary> <summary>
点位编号 点位编号

@ -1 +1 @@
343174067cd832fbade3d0f003640c7839b8168f 78dedf078fa663772dbe506cad079b136782b2c8

@ -59,6 +59,21 @@
报警 报警
</summary> </summary>
</member> </member>
<member name="T:PVDEMCS.Common.Devices.IDeviceRun">
<summary>
设备运行
</summary>
</member>
<member name="M:PVDEMCS.Common.Devices.IDeviceRun.Run">
<summary>
开始运行
</summary>
</member>
<member name="M:PVDEMCS.Common.Devices.IDeviceRun.Stop">
<summary>
停止运行
</summary>
</member>
<member name="E:PVDEMCS.Common.Devices.Impl.DeviceMonitor.PointChnage"> <member name="E:PVDEMCS.Common.Devices.Impl.DeviceMonitor.PointChnage">
<summary> <summary>
PLC 点位改变触发 PLC 点位改变触发
@ -74,6 +89,16 @@
间隔时间 间隔时间
</summary> </summary>
</member> </member>
<member name="P:PVDEMCS.Common.Devices.Impl.DeviceMonitor.isConnected">
<summary>
连接状态
</summary>
</member>
<member name="P:PVDEMCS.Common.Devices.Impl.DeviceMonitor.IsRun">
<summary>
监控是否运行
</summary>
</member>
<member name="P:PVDEMCS.Common.Devices.Impl.DeviceMonitor.Points"> <member name="P:PVDEMCS.Common.Devices.Impl.DeviceMonitor.Points">
<summary> <summary>
当前点位集合 当前点位集合
@ -118,6 +143,21 @@
<param name="point"></param> <param name="point"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="T:PVDEMCS.Common.Devices.Impl.DeviceRun">
<summary>
设备运行
</summary>
</member>
<member name="M:PVDEMCS.Common.Devices.Impl.DeviceRun.Run">
<summary>
开始运行
</summary>
</member>
<member name="M:PVDEMCS.Common.Devices.Impl.DeviceRun.Stop">
<summary>
停止运行
</summary>
</member>
<member name="T:PVDEMCS.Common.Devices.Impl.S7CommunicationService"> <member name="T:PVDEMCS.Common.Devices.Impl.S7CommunicationService">
<summary> <summary>
PLC通信服务 SiemensS7 smart200 实现 PLC通信服务 SiemensS7 smart200 实现
@ -1515,6 +1555,11 @@
设备类型IonbondBalzersCemecon 设备类型IonbondBalzersCemecon
</summary> </summary>
</member> </member>
<member name="P:PVDEMCS.Services.Models.DevicePoint.ActionType">
<summary>
点位所属动作启动停止start_stop故障fault
</summary>
</member>
<member name="P:PVDEMCS.Services.Models.DevicePoint.PointCode"> <member name="P:PVDEMCS.Services.Models.DevicePoint.PointCode">
<summary> <summary>
点位编号 点位编号

Loading…
Cancel
Save