You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
14 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using AucmaEms.Common;
using AucmaEms.Entity;
using AucmaEms.SqlSugar;
using AucmaEms.SqlSugar.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using TouchSocket.Core.Config;
using TouchSocket.Sockets;
using TcpClient = TouchSocket.Sockets.TcpClient;
namespace AucmaEms
{
internal class Program
{
private static TcpServer server = TcpServer.Instance;
private static LogHelper logHelper = LogHelper.Instance;
private static AppConfig appConfig = AppConfig.Instance;
static void Main(string[] args)
{
printMessage("服务启动");
Thread.Sleep(3000);
while (true)
{
CollectEmsData_2Q();
Thread.Sleep(1000 * 60 * appConfig.ReadIntervel);
}
}
/// <summary>
/// 一期采集读取现场电表数据
/// </summary>
static async void CollectEmsData_1Q()
{
Business business = Business.Instance;
List<BaseMonitorInfo> monitorInfos = await business.monitorInfos("PDS");
if (monitorInfos == null) return;
int monitorInfosLength = monitorInfos.Count;
try
{
for (int i = 0; i < monitorInfosLength; i++)
{
BaseMonitorInfo info = monitorInfos[i];
if (info != null)
{
TcpClient m_tcpClient = new TcpClient();
try
{
string title = info.monitorName + DateTime.Now.ToString("MM-dd HH:mm:ss");
Console.WriteLine("-------------------------" + title.PadRight(25,'-') + "-----------------------");
//声明配置
TouchSocketConfig config = new TouchSocketConfig();
config.SetRemoteIPHost(new IPHost(info.networkAddress+":502")).UsePlugin().SetBufferLength(1024 * 10);
//载入配置
m_tcpClient.Setup(config);
m_tcpClient.Connect();
ScadaEmsElectricLoss scadaEmsElectric = new ScadaEmsElectricLoss();
byte[] zxyg_Bytes = business.packageBuffer(new byte[]{ Convert.ToByte(info.monitorId), 0x03, 0x04, 0x4B, 0x00, 0x02 });
logHelper.SemaphoreLog(String.Format("{0}{1}发送获取正向有功指令:{2}",info.monitorName,info.networkAddress,StringChange.bytesToHexStr(zxyg_Bytes, zxyg_Bytes.Length)));
byte[] zxyg_buffer = m_tcpClient.GetWaitingClient(WaitingOptions.AllAdapter).SendThenReturn(zxyg_Bytes, 2000);
logHelper.SemaphoreLog(String.Format("{0}{1}接收正向有功指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(zxyg_buffer, zxyg_buffer.Length)));
scadaEmsElectric.zxyg = business.byteToFloat(zxyg_buffer.Skip(3).Take(4).ToArray(), 100);
printMessage(String.Format("{0}正向有功:{1}", info.monitorName, scadaEmsElectric.zxyg));
//取三项电压
byte[] v_Byte = business.packageBuffer(new byte[] { Convert.ToByte(info.monitorId), 0x03, 0x03, 0xE7, 0x00, 0x06 });
logHelper.SemaphoreLog(String.Format("{0}{1}发送获取三相电压指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(v_Byte, v_Byte.Length)));
byte[] v_Buffer = m_tcpClient.GetWaitingClient(WaitingOptions.AllAdapter).SendThenReturn(v_Byte, 2000);
logHelper.SemaphoreLog(String.Format("{0}{1}接收三相电压指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(v_Buffer, v_Buffer.Length)));
scadaEmsElectric.va = business.byteToFloat(v_Buffer.Skip(3).Take(4).ToArray(), 1000);
scadaEmsElectric.vb = business.byteToFloat(v_Buffer.Skip(7).Take(4).ToArray(), 1000);
scadaEmsElectric.vc = business.byteToFloat(v_Buffer.Skip(11).Take(4).ToArray(), 1000);
printMessage(String.Format("{0}A项电压{1}B项电压{2}C项电压{3}", info.monitorName, scadaEmsElectric.va, scadaEmsElectric.vb, scadaEmsElectric.vc));
//取三项电流
byte[] i_Byte = business.packageBuffer(new byte[] { Convert.ToByte(info.monitorId), 0x03, 0x03, 0xF3, 0x00, 0x06 });
logHelper.SemaphoreLog(String.Format("{0}{1}发送获取三相电流指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(i_Byte, i_Byte.Length)));
byte[] i_Buffer = m_tcpClient.GetWaitingClient(WaitingOptions.AllAdapter).SendThenReturn(i_Byte, 2000);
logHelper.SemaphoreLog(String.Format("{0}{1}接收三相电流指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(i_Buffer, i_Buffer.Length)));
scadaEmsElectric.ia = business.byteToFloat(i_Buffer.Skip(3).Take(4).ToArray(), 1000);
scadaEmsElectric.ib = business.byteToFloat(i_Buffer.Skip(7).Take(4).ToArray(), 1000);
scadaEmsElectric.ic = business.byteToFloat(i_Buffer.Skip(11).Take(4).ToArray(), 1000);
printMessage(String.Format("{0}A项电流{1}B项电流{2}C项电流{3}", info.monitorName, scadaEmsElectric.ia, scadaEmsElectric.ib, scadaEmsElectric.ic));
scadaEmsElectric.collectTime = DateTime.Now;
scadaEmsElectric.monitorId = info.monitorId;
scadaEmsElectric.uuid = System.Guid.NewGuid().ToString("N");
business.SaveDate(info, scadaEmsElectric);
//断开连接
if (m_tcpClient.Online)
{
m_tcpClient.Shutdown(System.Net.Sockets.SocketShutdown.Both);
m_tcpClient.Close();
m_tcpClient.SafeDispose();
}
}
catch (TimeoutException)
{
printMessage(info.monitorName+"等待超时");
if (m_tcpClient.Online)
{
m_tcpClient.Shutdown(System.Net.Sockets.SocketShutdown.Both);
m_tcpClient.Close();
m_tcpClient.SafeDispose();
}
}
}
}
}
catch (Exception ex)
{
logHelper.Error("数据采集异常", ex);
}
}
/// <summary>
/// 二期采集读取现场电表数据
/// </summary>
static async void CollectEmsData_2Q()
{
Business business = Business.Instance;
List<BaseMonitorInfo> monitorInfos = await business.monitorInfos("2QPDG");
if (monitorInfos == null) return;
int monitorInfosLength = monitorInfos.Count;
try
{
for (int i = 0; i < monitorInfosLength; i++)
{
BaseMonitorInfo info = monitorInfos[i];
if (info != null)
{
string monitorId = info.monitorId.Replace("2Q_", "");
TcpClient m_tcpClient = new TcpClient();
try
{
string title = info.monitorName + DateTime.Now.ToString("MM-dd HH:mm:ss");
Console.WriteLine("-------------------------" + title.PadRight(25, '-') + "-----------------------");
//声明配置
TouchSocketConfig config = new TouchSocketConfig();
config.SetRemoteIPHost(new IPHost(info.networkAddress + ":502")).UsePlugin().SetBufferLength(1024 * 10);
//载入配置
m_tcpClient.Setup(config);
m_tcpClient.Connect();
ScadaEmsElectricLoss scadaEmsElectric = new ScadaEmsElectricLoss();
byte[] zxyg_Bytes = business.packageBuffer(new byte[] { Convert.ToByte(monitorId), 0x03, 0x04, 0x4B, 0x00, 0x02 });
logHelper.SemaphoreLog(String.Format("{0}{1}发送获取正向有功指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(zxyg_Bytes, zxyg_Bytes.Length)));
byte[] zxyg_buffer = m_tcpClient.GetWaitingClient(WaitingOptions.AllAdapter).SendThenReturn(zxyg_Bytes, 2000);
logHelper.SemaphoreLog(String.Format("{0}{1}接收正向有功指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(zxyg_buffer, zxyg_buffer.Length)));
scadaEmsElectric.zxyg = business.byteToFloat(zxyg_buffer.Skip(3).Take(4).ToArray(), 100);
printMessage(String.Format("{0}正向有功:{1}", info.monitorName, scadaEmsElectric.zxyg));
//取三项电压
byte[] v_Byte = business.packageBuffer(new byte[] { Convert.ToByte(monitorId), 0x03, 0x03, 0xE7, 0x00, 0x06 });
logHelper.SemaphoreLog(String.Format("{0}{1}发送获取三相电压指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(v_Byte, v_Byte.Length)));
byte[] v_Buffer = m_tcpClient.GetWaitingClient(WaitingOptions.AllAdapter).SendThenReturn(v_Byte, 2000);
logHelper.SemaphoreLog(String.Format("{0}{1}接收三相电压指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(v_Buffer, v_Buffer.Length)));
scadaEmsElectric.va = business.byteToFloat(v_Buffer.Skip(3).Take(4).ToArray(), 1000);
scadaEmsElectric.vb = business.byteToFloat(v_Buffer.Skip(7).Take(4).ToArray(), 1000);
scadaEmsElectric.vc = business.byteToFloat(v_Buffer.Skip(11).Take(4).ToArray(), 1000);
printMessage(String.Format("{0}A项电压{1}B项电压{2}C项电压{3}", info.monitorName, scadaEmsElectric.va, scadaEmsElectric.vb, scadaEmsElectric.vc));
//取三项电流
byte[] i_Byte = business.packageBuffer(new byte[] { Convert.ToByte(monitorId), 0x03, 0x03, 0xF3, 0x00, 0x06 });
logHelper.SemaphoreLog(String.Format("{0}{1}发送获取三相电流指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(i_Byte, i_Byte.Length)));
byte[] i_Buffer = m_tcpClient.GetWaitingClient(WaitingOptions.AllAdapter).SendThenReturn(i_Byte, 2000);
logHelper.SemaphoreLog(String.Format("{0}{1}接收三相电流指令:{2}", info.monitorName, info.networkAddress, StringChange.bytesToHexStr(i_Buffer, i_Buffer.Length)));
scadaEmsElectric.ia = business.byteToFloat(i_Buffer.Skip(3).Take(4).ToArray(), 1000);
scadaEmsElectric.ib = business.byteToFloat(i_Buffer.Skip(7).Take(4).ToArray(), 1000);
scadaEmsElectric.ic = business.byteToFloat(i_Buffer.Skip(11).Take(4).ToArray(), 1000);
printMessage(String.Format("{0}A项电流{1}B项电流{2}C项电流{3}", info.monitorName, scadaEmsElectric.ia, scadaEmsElectric.ib, scadaEmsElectric.ic));
scadaEmsElectric.collectTime = DateTime.Now;
scadaEmsElectric.monitorId = info.monitorId;
scadaEmsElectric.uuid = System.Guid.NewGuid().ToString("N");
business.SaveDate(info, scadaEmsElectric);
//断开连接
if (m_tcpClient.Online)
{
m_tcpClient.Shutdown(System.Net.Sockets.SocketShutdown.Both);
m_tcpClient.Close();
m_tcpClient.SafeDispose();
}
}
catch (TimeoutException)
{
printMessage(info.monitorName + "等待超时");
if (m_tcpClient.Online)
{
m_tcpClient.Shutdown(System.Net.Sockets.SocketShutdown.Both);
m_tcpClient.Close();
m_tcpClient.SafeDispose();
}
}
}
}
}
catch (Exception ex)
{
logHelper.Error("数据采集异常", ex);
}
}
private static void printMessage(string message)
{
Console.WriteLine(message);
logHelper.Info(message);
}
}
}