183 lines
7.2 KiB
C#
183 lines
7.2 KiB
C#
using Face.Domain.Entities;
|
|
using Face.Services.Extensions;
|
|
using Face.Services.Manager;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Web;
|
|
using WebSocketToolsConsole;
|
|
using System.Messaging;
|
|
|
|
namespace Face.Web.Areas.App.Models
|
|
{
|
|
public static class UdpCommunication
|
|
{
|
|
static object lockobject = new object();
|
|
public static UdpClient client = null;
|
|
public static IPEndPoint remotePoint = null;
|
|
public static UdpClient client1 = null;
|
|
public static IPEndPoint remotePoint1 = null;
|
|
|
|
|
|
|
|
//Queue for send message to console web socket server.
|
|
public static string queueName = @".\private$\facesvrcmd";
|
|
public static MessageQueue mq = null;
|
|
|
|
/// <summary>
|
|
/// UDP传输发送数据到控制台
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
public static void UdpState(string str)
|
|
{
|
|
//Logs.WriteLog(str);
|
|
try
|
|
{
|
|
byte[] sendData = null;//要发送的字节数组
|
|
//IPAddress remoteIP = IPAddress.Parse("172.30.53.136");
|
|
IPAddress remoteIP = IPAddress.Parse("127.0.0.1");
|
|
int remotePort = 11001;
|
|
remotePoint = new IPEndPoint(remoteIP, remotePort);//实例化一个远程端点
|
|
sendData = Encoding.Default.GetBytes(str);
|
|
client = new UdpClient();
|
|
client.Send(sendData, sendData.Length, remotePoint);//将数据发送到远程端点
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new CustomException(ex.Message);
|
|
}
|
|
|
|
//client.Close();//关闭连接
|
|
}
|
|
/// <summary>
|
|
/// 接收控制台返回数据
|
|
/// </summary>
|
|
public static void UdpStateinfo()
|
|
{
|
|
Entity.msgx msgx = new Entity.msgx();
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
string receiveString = null;
|
|
byte[] receiveData = null;
|
|
//IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
|
|
|
|
//client.Client.ReceiveTimeout = 500;
|
|
//client.Client.Blocking = false; //设置为非阻塞模式
|
|
receiveData = client.Receive(ref remotePoint);//接收数据
|
|
receiveString = Encoding.Default.GetString(receiveData);
|
|
Entity.msgx Elist = JsonConvert.DeserializeObject<Entity.msgx>(receiveString);//josn转换实体类
|
|
|
|
using (var dr=SqlSugarBase.GesmartDb())
|
|
{
|
|
transferFace sx = dr.Queryable<transferFace>().First(x=>x.infoid==Elist.msgid);
|
|
sx.faultState = int.Parse(Elist.cmd);
|
|
FaceIssue issue = dr.Queryable<FaceIssue>().First(x => x.messageid == Elist.msgid);
|
|
if (Elist.cmd == "2")
|
|
{
|
|
issue.issuestate = 2;
|
|
var assed = dr.Queryable<DeviceManage>().First(x=>x.SerialNo==sx.faceSN);
|
|
SqlSugarBase.Db.Updateable<DeviceManage>().SetColumns(it => it.maintainStatus == 1).Where(it => it.SerialNo == sx.faceSN).ExecuteCommand();
|
|
SqlSugarBase.Db.Updateable<FaceIssue>().SetColumns(it => it.issuestate ==2).Where(x => x.messageid == Elist.msgid).ExecuteCommand();
|
|
}
|
|
SqlSugarBase.Db.Updateable<transferFace>().SetColumns(it => it.faultState == int.Parse(Elist.cmd)).Where(x => x.infoid == Elist.msgid).ExecuteCommand();
|
|
}
|
|
//client.Close();//关闭连接
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//Logs.WriteTimingPlanLog(ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
public static void UdpState2(string str)
|
|
{
|
|
try
|
|
{
|
|
byte[] sendData = null;//要发送的字节数组
|
|
IPAddress remoteIP = IPAddress.Parse("172.30.53.136");
|
|
int remotePort = 11002;
|
|
remotePoint1 = new IPEndPoint(remoteIP, remotePort);//实例化一个远程端点
|
|
sendData = Encoding.Default.GetBytes(str);
|
|
client1 = new UdpClient();
|
|
client1.Send(sendData, sendData.Length, remotePoint1);//将数据发送到远程端点
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new CustomException(ex.Message);
|
|
throw;
|
|
}
|
|
|
|
//client.Close();//关闭连接
|
|
}
|
|
//public static string UdpStateinfo2()
|
|
//{
|
|
// Entity.msgx msgx = new Entity.msgx();
|
|
// while (true)
|
|
// {
|
|
// try
|
|
// {
|
|
// string receiveString = null;
|
|
// byte[] receiveData = null;
|
|
// //IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
|
|
|
|
// //client.Client.ReceiveTimeout = 500;
|
|
// //client.Client.Blocking = false; //设置为非阻塞模式
|
|
// receiveData = client1.Receive(ref remotePoint1);//接收数据
|
|
// receiveString = Encoding.Default.GetString(receiveData);
|
|
// //Entity.msgx_person Elist = JsonConvert.DeserializeObject<Entity.msgx_person>(receiveString);//josn转换实体类
|
|
// //Logs.WriteLog(receiveString);
|
|
// return receiveString;
|
|
// //client.Close();//关闭连接
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// //Logs.WriteTimingPlanLog(ex.ToString());
|
|
// //throw new CustomException(ex.Message);
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
public static string QueueSend(string sendMsg)
|
|
{
|
|
try
|
|
{
|
|
if (mq == null)
|
|
{
|
|
if (MessageQueue.Exists(queueName))
|
|
{
|
|
mq = new MessageQueue(queueName);
|
|
}
|
|
else
|
|
{
|
|
mq = MessageQueue.Create(queueName);
|
|
}
|
|
mq.SetPermissions("Administrator", MessageQueueAccessRights.FullControl);
|
|
mq.SetPermissions("ANONYMOUS LOGON", MessageQueueAccessRights.FullControl);
|
|
mq.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);
|
|
}
|
|
|
|
Message msgTx = new Message();
|
|
msgTx.Body = sendMsg;
|
|
msgTx.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
|
|
mq.Send(msgTx);
|
|
return "Succeed push msg to MQ !";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
mq = null;
|
|
|
|
Logs.WriteErrorLog(ex);
|
|
return ex.ToString();
|
|
}
|
|
}
|
|
|
|
}
|
|
} |