using DataEntity.Share;
|
using DataEntity.Sockets.Base;
|
using HxEnum;
|
using HxSocketImplement.Sockets.HxService;
|
using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net;
|
using System.Net.Sockets;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using XCommon.Event;
|
using XCommon.Log;
|
using XImagingXhandler.XDAL;
|
using static HxEnum.OperationTypeEnum;
|
using static HxEnum.SocketS.InfoTypeEnum;
|
using static HxEnum.StateEnum;
|
|
namespace HxSocketImplement.Sockets
|
{
|
/// <summary>
|
/// 内部服务Socket 服务端
|
/// </summary>
|
public class SocketInteriorService
|
{
|
#region 默认参数
|
private static Thread threadReceive = null; // 接收客户端发送消息的线程
|
private static Thread threadWatch = null; // 负责监听客户端的线程
|
public static Socket socketWatch = null; // 负责监听客户端的套接字
|
public static Socket clientConnection = null; // 负责和客户端通信的套接字
|
|
// 使用令牌取消线程: 从.NET 5 开始,以下 API(Thread.Abort) 被标记为过时 使用CancellationToken中止工作单元的处理,而不是调用Thread.Abort
|
static CancellationTokenSource cts = null;
|
// wdy
|
//public static System.Windows.Forms.ListView listView = new System.Windows.Forms.ListView();
|
|
/// <summary>
|
/// 实验状态 key:实验ID,value:线程启停控制
|
/// </summary>
|
public static Dictionary<string, ManualResetEvent> dicManualResetEvent = new Dictionary<string, ManualResetEvent>();
|
#endregion
|
|
#region 开启服务端监听
|
/// <summary>
|
/// 开启服务端监听
|
/// </summary>
|
public static void OpenService()
|
{
|
cts = new CancellationTokenSource();
|
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // AddressFamily地址类型,SocketType套接字类型,ProtocolType协议类型;
|
IPAddress iP = IPAddress.Parse(Shared.SoftwareInformation.ip); // 创建对象端口 (这里为ip类型地址)
|
IPEndPoint point = new IPEndPoint(iP, Convert.ToInt32(Shared.SoftwareInformation.port)); // 初始化IP及端口号
|
socketWatch.Bind(point); // 绑定端口
|
socketWatch.Listen(20); // 开启监听(参数代表排队等待连接的最大数量,不包含已连接数量)0表示无限
|
|
threadWatch = new Thread(Listen); //创建监听线程
|
threadWatch.IsBackground = true;
|
threadWatch.Start();
|
}
|
#endregion
|
|
#region 监听
|
/// <summary>
|
/// 监听
|
/// </summary>
|
public static void Listen()
|
{
|
try
|
{
|
// 持续不断监听客户端发来的请求
|
while (!cts.IsCancellationRequested)
|
{
|
clientConnection = socketWatch.Accept();
|
|
// wdy
|
//AddListView(new ListViewModel
|
//{
|
// Type = "客-》连接",
|
// Port = clientConnection.RemoteEndPoint.ToString(),
|
// receive_parameter = "",
|
// send_parameter = "",
|
// createtime = DateTime.Now.ToString()
|
//});
|
|
// 创建一个通信线程
|
ParameterizedThreadStart pts = new ParameterizedThreadStart(Received);
|
threadReceive = new Thread(pts);
|
threadReceive.IsBackground = true;
|
threadReceive.Start(clientConnection);
|
}
|
}
|
catch (Exception ex)
|
{
|
LoggerSocketHelper.ErrorLog($"监听Listen失败:{ex.Message}");
|
}
|
}
|
#endregion
|
|
#region 服务器端不停的接收客户端发来的消息
|
/// <summary>
|
/// 服务器端不停的接收客户端发来的消息
|
/// </summary>
|
/// <param name="socket"></param>
|
public static void Received(object socket)
|
{
|
try
|
{
|
//客户端连接服务器成功后,服务器接收客户端发送的消息
|
Socket socketSend = socket as Socket;
|
while (!cts.IsCancellationRequested)
|
{
|
object lockObject = new object();
|
lock (lockObject)
|
{
|
//创建一个内存缓冲区 其大小为1024*1024字节 即3M
|
byte[] buffer = new byte[1024 * 1024 * 3];
|
//实际接收到的有效字节数
|
int len = socketSend.Receive(buffer);
|
if (len == 0)
|
{
|
// wdy
|
//AddListView(new ListViewModel
|
//{
|
// Type = "客-》断开",
|
// Port = socketSend.RemoteEndPoint.ToString(),
|
// receive_parameter = "",
|
// send_parameter = "",
|
// createtime = DateTime.Now.ToString()
|
//});
|
break;
|
}
|
|
string msg = Encoding.UTF8.GetString(buffer, 0, len);
|
if (!string.IsNullOrWhiteSpace(msg))
|
{
|
var msgList = msg.Split(new string[] { CommonParameter.SpecifiedDisplay }, StringSplitOptions.None);
|
|
foreach (var item in msgList)
|
{
|
var receiveData = JsonConvert.DeserializeObject<HxSendBase>(item);
|
if (receiveData != null && !string.IsNullOrWhiteSpace(receiveData.method))
|
{
|
#region Del
|
//AddListView(new ListViewModel
|
//{
|
// Type = "接收-》客-》消息",
|
// Port = socketSend.RemoteEndPoint.ToString(),
|
// receive_parameter = msg,
|
// send_parameter = "",
|
// createtime = DateTime.Now.ToString()
|
//});
|
#endregion
|
|
//HxClientResponseObject hxClientResponseObject = new HxClientResponseObject();
|
//hxClientResponseObject.ResponseBase = new HxResponseBase
|
//{
|
// message_id = receiveData.message_id,
|
// message_type = receiveData.message_type,
|
// method = receiveData.method,
|
// equipment_id = receiveData.equipment_id,
|
// workflow_id = receiveData.workflow_id,
|
// experiment_id = receiveData.experiment_id,
|
// data = null,
|
// method_status = EnumManagement.GetEnumValue(StateEnum.StateEnum_Equipment.Completed),
|
// timestamp = DateTime.Now.ToString(),
|
// error = null,
|
//};
|
|
LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(receiveData.method)}指令【开始执行】------");
|
|
#region Del
|
//switch (EnumManagement.ToEnum<InfoTypeEnum_Enum>(receiveData.method))
|
//{
|
// case InfoTypeEnum_Enum.SendHeartbeatConnect: //发送心跳连接(接收到客户端发来的消息)
|
// #region 回复消息
|
// //if (ReplyClientMsg(socketSend, hxClientResponseObject))
|
// //{
|
// // Task.Run(() =>
|
// // {
|
// // //to do
|
// // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.SendHeartbeatConnect)}指令【成功】------");
|
// // });
|
// //}
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.SendServiceData: //客户端向服务端发送数据(接收到客户端发来的消息)
|
// #region 回复消息+处理逻辑
|
// //if (ReplyClientMsg(socketSend, hxClientResponseObject))
|
// //{
|
// // Task.Run(() =>
|
// // {
|
// // //to do
|
// // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.SendServiceData)}指令【成功】------");
|
// // });
|
// //}
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.SuspendExperiment: //暂停实验(接收到客户端发来的消息)
|
// #region 回复消息+暂停实验
|
// //if (ReplyClientMsg(socketSend, hxClientResponseObject))
|
// //{
|
// // //Task.Run(() =>
|
// // //{
|
// // // ResultHandle.instructCommonMethodSendModelByError = new InstructCommonMethodSendModel();
|
// // // ResultHandle.instructCommonMethodSendModelByError.SendData = new HxSendBase();
|
// // // EventBind.OperMark = new NodeOperationTypeEnum();// ExpOperationTypeEnum.ExpOperationTypeBEnum;
|
// // // Upt_DicTestState(scfm.SendData.experiment_id, ProcessTypeColorEnum_Equipment.OrangeColor, InfoTypeEnum_Enum.SuspendExperiment, ProcessLogicTypeEnum_Equipment.NoType);
|
// // // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.SuspendExperiment)}指令【成功】------");
|
// // //});
|
// //}
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.ContinueExperiment: //继续(跳过)实验
|
// #region 回复消息+继续实验
|
// //if (ReplyClientMsg(socketSend, hxClientResponseObject))
|
// //{
|
// // //Task.Run(() =>
|
// // //{
|
// // // ResultHandle.instructCommonMethodSendModelByError = new InstructCommonMethodSendModel();
|
// // // ResultHandle.instructCommonMethodSendModelByError.SendData = new HxSendBase();
|
// // // EventBind.OperMark = new NodeOperationTypeEnum();// ExpOperationTypeEnum.ExpOperationTypeBEnum;
|
// // // ErrorStateDB.Continue(scfm.SendData.experiment_id, StatusEnum.StatusCEnum, NodeOperationTypeEnum.Continue);
|
|
// // // Upt_DicTestState(scfm.SendData.experiment_id, ProcessTypeColorEnum_Equipment.GreenColor, InfoTypeEnum_Enum.ContinueExperiment, ProcessLogicTypeEnum_Equipment.NoType);
|
// // // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.ContinueExperiment)}指令【成功】------");
|
// // //});
|
// //}
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.SysErrorRetry: //系统报错后重试
|
// #region 系统报错后重试
|
// //if (ReplyClientMsg(socketSend, hxClientResponseObject))
|
// //{
|
// // //Task.Run(() =>
|
// // //{
|
// // // //新的参数重新赋值
|
// // // ResultHandle.instructCommonMethodSendModelByError = JsonConvert.DeserializeObject<InstructCommonMethodSendModel>(JsonConvert.SerializeObject(scfm.SendData.parameters));
|
// // // EventBind.OperMark = NodeOperationTypeEnum.Retry;
|
|
// // // ErrorStateDB.Continue(scfm.SendData.experiment_id, StatusEnum.StatusCEnum, NodeOperationTypeEnum.Retry);
|
|
// // // LoggerSocketHelper.DebugLog("运行方法【InfoTypeEnum_Enum.SysErrorRetry】重试参数" + JsonConvert.SerializeObject(scfm.SendData.parameters));
|
|
// // // Upt_DicTestState(scfm.SendData.experiment_id, ProcessTypeColorEnum_Equipment.GreenColor, InfoTypeEnum_Enum.ContinueExperiment, ProcessLogicTypeEnum_Equipment.NoType);
|
// // // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.SysErrorRetry)}指令【成功】------");
|
// // //});
|
// //}
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.TerminationExperiment: //终止实验
|
// #region 回复消息+终止实验
|
// //if (ReplyClientMsg(socketSend, hxClientResponseObject))
|
// //{
|
// // //Task.Run(() =>
|
// // //{
|
// // // ResultHandle.instructCommonMethodSendModelByError = new InstructCommonMethodSendModel();
|
// // // ResultHandle.instructCommonMethodSendModelByError.SendData = new HxSendBase();
|
// // // EventBind.OperMark = new NodeOperationTypeEnum();// ExpOperationTypeEnum.ExpOperationTypeBEnum;
|
|
// // // ErrorStateDB.Continue(scfm.SendData.experiment_id, StatusEnum.StatusFEnum, NodeOperationTypeEnum.Cancel);
|
|
// // // Upt_DicTestState(scfm.SendData.experiment_id, ProcessTypeColorEnum_Equipment.BlueColor, InfoTypeEnum_Enum.TerminationExperiment, ProcessLogicTypeEnum_Equipment.NoType);
|
// // // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.TerminationExperiment)}指令【成功】------");
|
// // //});
|
// //}
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.StartExperiment: //启动实验(接收到客户端发来的消息)
|
// #region 回复消息+启动实验
|
// //int count = dicManualResetEvent.Where(m => m.Key == scfm.SendData.experiment_id).Count();
|
// //if (count == 0)
|
// //{
|
// // Task.Run(() =>
|
// // {
|
// // dicManualResetEvent.Add(scfm.SendData.experiment_id, new ManualResetEvent(true));
|
// // var resultExp = InstructMethod.ImplementFlow(scfm.SendData.experiment_id);
|
// // if (resultExp)
|
// // {
|
// // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.StartExperiment)}指令【成功】------");
|
// // ReplyClientMsg(socketSend, hxClientResponseObject);
|
// // }
|
// // else
|
// // {
|
// // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.StartExperiment)}指令【失败】------");
|
// // }
|
// // });
|
// //}
|
// //else
|
// //{
|
// // Upt_DicTestState(scfm.SendData.experiment_id, ProcessTypeColorEnum_Equipment.GreenColor, InfoTypeEnum_Enum.ContinueExperiment, ProcessLogicTypeEnum_Equipment.NoType);
|
// // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.StartExperiment)}指令(统计为0)【成功】------");
|
// //}
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.DebuggingBySocket: //客户端Socket调试
|
// #region 客户端Socket调试
|
// //Task.Run(() =>
|
// //{
|
// // var model = JsonConvert.DeserializeObject<InstructCommonMethodSendModel>(scfm.SendData.parameters.ToString());
|
// // var resultInstructCommonMethod = ResultHandle.InstructCommonMethod(model);
|
// // string debuggingMsg = $"{EnumManagement.GetFieldText(InfoTypeEnum_Enum.DebuggingBySocket)}指令";
|
|
// // if (resultInstructCommonMethod != null && resultInstructCommonMethod.status == EnumManagement.GetEnumValue(StateEnum.StateEnum_Equipment.Completed))
|
// // {
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(resultInstructCommonMethod.msg) };
|
// // LoggerSocketHelper.DebugLog($"------{debuggingMsg}【成功】,返回内容:{JsonConvert.SerializeObject(resultInstructCommonMethod)}{",发送参数:" + JsonConvert.SerializeObject(scfm)}------");
|
// // }
|
// // else
|
// // {
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(resultInstructCommonMethod.msg) };
|
// // LoggerSocketHelper.DebugLog($"------{debuggingMsg}【失败】,返回内容:{JsonConvert.SerializeObject(resultInstructCommonMethod)}{",发送参数:" + JsonConvert.SerializeObject(scfm)}------");
|
// // }
|
// // ReplyClientMsg(socketSend, hxClientResponseObject);
|
// //});
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.DebuggingByModbus: //客户端Modbus调试
|
// #region 客户端Modbus调试
|
// //Task.Run(() =>
|
// //{
|
// // var model = JsonConvert.DeserializeObject<InstructCommonMethodSendModel>(scfm.SendData.parameters.ToString());
|
|
// // int countDebuggingByModbus = dicManualResetEvent.Where(m => m.Key == scfm.SendData.experiment_id).Count();
|
// // if (countDebuggingByModbus == 0)
|
// // {
|
// // dicManualResetEvent.Add(scfm.SendData.experiment_id, new ManualResetEvent(true));
|
// // }
|
// // var resultInstructCommonMethod = ResultHandle.InstructCommonMethod(model);
|
// // string debuggingMsg = $"{EnumManagement.GetFieldText(InfoTypeEnum_Enum.DebuggingByModbus)}指令";
|
// // if (resultInstructCommonMethod != null && resultInstructCommonMethod.status == EnumManagement.GetEnumValue(StateEnum.StateEnum_Equipment.Completed))
|
// // {
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(resultInstructCommonMethod) };
|
// // LoggerSocketHelper.DebugLog($"------{debuggingMsg}【成功】,返回内容:{JsonConvert.SerializeObject(resultInstructCommonMethod)}{",发送参数:" + JsonConvert.SerializeObject(scfm)}------");
|
// // }
|
// // else
|
// // {
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(resultInstructCommonMethod.msg) };
|
// // LoggerSocketHelper.DebugLog($"------{debuggingMsg}【失败】,返回内容:{JsonConvert.SerializeObject(resultInstructCommonMethod)}{",发送参数:" + JsonConvert.SerializeObject(scfm)}------");
|
// // }
|
// // ReplyClientMsg(socketSend, hxClientResponseObject);
|
// //});
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.DebuggingBySocketNonstandard: //客户端SocketNonstandard调试
|
// #region 客户端SocketNonstandard调试
|
// //Task.Run(() =>
|
// //{
|
// // var model = JsonConvert.DeserializeObject<InstructCommonMethodSendModel>(scfm.SendData.parameters.ToString());
|
// // var resultInstructCommonMethod = ResultHandle.InstructCommonMethod(model);
|
// // string debuggingMsg = $"{EnumManagement.GetFieldText(InfoTypeEnum_Enum.DebuggingBySocketNonstandard)}指令";
|
|
// // if (resultInstructCommonMethod != null && resultInstructCommonMethod.status == EnumManagement.GetEnumValue(StateEnum.StateEnum_Equipment.Completed))
|
// // {
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(resultInstructCommonMethod.msg) };
|
// // LoggerSocketHelper.DebugLog($"------{debuggingMsg}【成功】,返回内容:{JsonConvert.SerializeObject(resultInstructCommonMethod)}{",发送参数:" + JsonConvert.SerializeObject(scfm)}------");
|
// // }
|
// // else
|
// // {
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(resultInstructCommonMethod.msg) };
|
// // LoggerSocketHelper.DebugLog($"------{debuggingMsg}【失败】,返回内容:{JsonConvert.SerializeObject(resultInstructCommonMethod)}{",发送参数:" + JsonConvert.SerializeObject(scfm)}------");
|
// // }
|
// // ReplyClientMsg(socketSend, hxClientResponseObject);
|
// //});
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.DebuggingBySerialPort: //客户端DebuggingBySerialPort调试
|
// #region 客户端DebuggingBySerialPort调试
|
// //Task.Run(() =>
|
// //{
|
// // var model = JsonConvert.DeserializeObject<InstructCommonMethodSendModel>(scfm.SendData.parameters.ToString());
|
// // var resultInstructCommonMethod = ResultHandle.InstructCommonMethod(model);
|
// // string debuggingMsg = $"{EnumManagement.GetFieldText(InfoTypeEnum_Enum.DebuggingBySerialPort)}指令";
|
|
// // if (resultInstructCommonMethod != null && resultInstructCommonMethod.status == EnumManagement.GetEnumValue(StateEnum.StateEnum_Equipment.Completed))
|
// // {
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(resultInstructCommonMethod.msg) };
|
// // LoggerSocketHelper.DebugLog($"------{debuggingMsg}【成功】,返回内容:{JsonConvert.SerializeObject(resultInstructCommonMethod)}{",发送参数:" + JsonConvert.SerializeObject(scfm)}------");
|
// // }
|
// // else
|
// // {
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(resultInstructCommonMethod.msg) };
|
// // LoggerSocketHelper.DebugLog($"------{debuggingMsg}【失败】,返回内容:{JsonConvert.SerializeObject(resultInstructCommonMethod)}{",发送参数:" + JsonConvert.SerializeObject(scfm)}------");
|
// // }
|
// // ReplyClientMsg(socketSend, hxClientResponseObject);
|
// //});
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.CheckDeviceConnectState: //检查设备连接状态
|
// #region 检查设备连接状态
|
// //Task.Run(() =>
|
// //{
|
// // var getCheckDeviceConnectStateModel = EquConnect.GetCheckDeviceConnectState();
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(getCheckDeviceConnectStateModel) };
|
// // if (ReplyClientMsg(socketSend, hxClientResponseObject))
|
// // {
|
// // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.CheckDeviceConnectState)}指令【成功】------");
|
// // }
|
// //});
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.EquipmentStatus: //设备状态(是否忙碌 1空闲、2忙碌)
|
// #region 检查设备连接状态
|
// //Task.Run(() =>
|
// //{
|
// // var getEquipmentStatus = SocketParameterModel.Dic_EquipmentStatus;
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(getEquipmentStatus) };
|
// // if (ReplyClientMsg(socketSend, hxClientResponseObject))
|
// // {
|
// // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.EquipmentStatus)}指令【成功】------");
|
// // }
|
// //});
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.ServiceConnect: //服务连接
|
// #region 服务连接
|
// //Task.Run(() =>
|
// //{
|
// // var getServiceConnect = EquConnect.ConnectService();
|
// // hxClientResponseObject.ResponseBase.dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(getServiceConnect) };
|
// // if (ReplyClientMsg(socketSend, hxClientResponseObject))
|
// // {
|
// // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.ServiceConnect)}指令【成功】------");
|
// // }
|
// //});
|
// #endregion
|
// break;
|
// case InfoTypeEnum_Enum.EquipmentPointSyncData: //设备点位数据同步
|
// #region 设备点位数据同步
|
// //Task.Run(() =>
|
// //{
|
// // if (ReplyClientMsg(socketSend, hxClientResponseObject))
|
// // {
|
// // #region 执行命令
|
// // if (!string.IsNullOrWhiteSpace(scfm.SendData.experiment_id))
|
// // {
|
// // var deviceConfigModel = new DeviceConfigBLL().GetInfodById(scfm.SendData.experiment_id);
|
// // if (deviceConfigModel != null)
|
// // {
|
// // //InstructCommonMethodSendModel instructCommonMethodSendModel = HxSocketParameterService.GetInstructCommonMethodSendModel(new InstructCommonMethodSendModel()
|
// // //{
|
// // // EquipmentType = deviceConfigModel.Type,
|
// // // Ip = deviceConfigModel.Ip,
|
// // // Port = Convert.ToInt32(deviceConfigModel.Port),
|
// // // EquipmentId = deviceConfigModel.EquipmentId,
|
// // // SendData = new HxSendBase { experiment_id = HxSockServiceExecute.InstrunctionId(), parameters = "{}", method = "TrySyncAreas" }, //这个方法要写死,不然,前端也不知道传哪个。这个驱动接口他们都是写死的。
|
|
// // //});
|
// // //var resultEquipmentPointSyncData = ResultHandle.InstructCommonMethod(instructCommonMethodSendModel);
|
// // //if (resultEquipmentPointSyncData != null && resultEquipmentPointSyncData.status == EnumManagement.GetEnumValue(StateEnum.StateEnum_Equipment.Completed))
|
// // {
|
// // var areasList = new Robot_AreasBLL().GetAreasALL();
|
// // if (areasList != null && areasList.Count > 0)
|
// // {
|
// // foreach (var m in areasList)
|
// // {
|
// // var deviceconfigPointList = new DeviceConfigPointBLL().GetDeviceconfigPoint(new DeviceconfigPointModelSearchModel { DeviceconfigId = deviceConfigModel.Id, PointName = m.AreaName });
|
// // if (deviceconfigPointList == null || deviceconfigPointList.Count <= 0)
|
// // {
|
// // DeviceconfigPointModel deviceconfigPointModel = new DeviceconfigPointModel();
|
// // deviceconfigPointModel.Id = Guid.NewGuid().ToString();
|
// // deviceconfigPointModel.ProjectId = SharedServiceModel.ServiceConfigModel.Id;
|
// // deviceconfigPointModel.DeviceconfigId = deviceConfigModel.Id;
|
// // deviceconfigPointModel.Name = m.Description;
|
// // deviceconfigPointModel.PointName = m.AreaName;
|
// // deviceconfigPointModel.Remark = "数据同步" + DateTime.Now;
|
// // deviceconfigPointModel.CreateTime = DateTime.Now;
|
// // deviceconfigPointModel.CreatName = "系统同步";
|
// // var addResult = new DeviceConfigPointBLL().Add(deviceconfigPointModel);
|
// // LoggerSocketHelper.DebugLog($"------{EnumManagement.GetFieldText(InfoTypeEnum_Enum.EquipmentPointSyncData)}指令【成功】------");
|
// // }
|
// // }
|
// // }
|
// // }
|
// // }
|
// // }
|
// // #endregion
|
// // }
|
// //});
|
// #endregion
|
// break;
|
|
// default:
|
// break;
|
|
//}
|
#endregion
|
}
|
}
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
LoggerSocketHelper.ErrorLog($"Received 出错:{ex.Message}" + ex);
|
}
|
}
|
#endregion
|
|
#region 修改实验状态字典
|
/// <summary>
|
/// 修改实验状态字典
|
/// </summary>
|
/// <param name="expId">实验ID</param>
|
/// <param name="processType">处理类型颜色枚举(0不处理,1橙色,2蓝色,3绿色,4红色)</param>
|
/// <param name="infotypeEnumType">实验状态</param>
|
/// <param name="processLogicType">处理类型(0不处理,1处理)</param>
|
public static void Upt_DicTestState(string expId, ProcessTypeColorEnum_Equipment processType, InfoTypeEnum_Enum infotypeEnumType, ProcessLogicTypeEnum_Equipment processLogicType)
|
{
|
LoggerSocketHelper.DebugLog($"========================== 实验ID:{expId}状态更改:{EnumManagement.GetFieldText(infotypeEnumType)}, 处理类型颜色枚举={EnumManagement.GetFieldText(processType)}, 处理类型={EnumManagement.GetFieldText(processLogicType)} ========================== ");
|
|
int count = dicManualResetEvent.Where(m => m.Key == expId).Count();
|
if (count == 0)
|
{
|
dicManualResetEvent.Add(expId, new ManualResetEvent(true));
|
}
|
|
#region 三色灯
|
ProcessTypeColorSet(processType);
|
#endregion
|
|
if (EnumManagement.GetEnumValue(infotypeEnumType) == EnumManagement.GetEnumValue(InfoTypeEnum_Enum.SuspendExperiment) ||
|
EnumManagement.GetEnumValue(infotypeEnumType) == EnumManagement.GetEnumValue(InfoTypeEnum_Enum.TerminationExperiment)) //暂停+终止
|
{
|
if (EnumManagement.GetEnumValue(infotypeEnumType) == EnumManagement.GetEnumValue(InfoTypeEnum_Enum.SuspendExperiment)) //暂停
|
{
|
if (processLogicType == ProcessLogicTypeEnum_Equipment.YesType)
|
{
|
//暂停 业务逻辑(比如蜂鸣器开打,业务逻辑也可以写在三色灯里面)
|
}
|
}
|
else if (EnumManagement.GetEnumValue(infotypeEnumType) == EnumManagement.GetEnumValue(InfoTypeEnum_Enum.TerminationExperiment)) //终止
|
{
|
if (processLogicType == ProcessLogicTypeEnum_Equipment.YesType)
|
{
|
//终止 业务逻辑(比如蜂鸣器开打,业务逻辑也可以写在三色灯里面)
|
}
|
}
|
|
dicManualResetEvent[expId].Reset(); //堵塞线程
|
}
|
else if (EnumManagement.GetEnumValue(infotypeEnumType) == EnumManagement.GetEnumValue(InfoTypeEnum_Enum.ContinueExperiment)) //继续
|
{
|
if (processLogicType == ProcessLogicTypeEnum_Equipment.YesType)
|
{
|
//继续 业务逻辑(比如蜂鸣器关闭,业务逻辑也可以写在三色灯里面)
|
}
|
|
dicManualResetEvent[expId].Set(); //继续线程
|
}
|
}
|
|
/// <summary>
|
/// 处理类型颜色枚举
|
/// </summary>
|
/// <param name="processType">颜色枚举(0不处理,1橙色,2蓝色,3绿色,4红色)</param>
|
private static void ProcessTypeColorSet(ProcessTypeColorEnum_Equipment processType)
|
{
|
//#region 调用底层控制(DEMO)
|
//InstructCommonMethodSendModel instructCommonMethodSendModel = HxSocketParameterService.GetInstructCommonMethodSendModel(new InstructCommonMethodSendModel()
|
//{
|
// EquipmentType = getDeviceConfigModel.Type.Trim(),
|
// Ip = getDeviceConfigModel.Ip.Trim(),
|
// Port = Convert.ToInt32(getDeviceConfigModel.Port.Trim()),
|
// EquipmentId = getDeviceConfigModel.EquipmentId.Trim(),
|
// VirtualConnectionState = getDeviceConfigModel.VirtualConnectionState,
|
// CommunicateType = getDeviceConfigModel.CommunicateType,
|
// SendData = new HxSendBase { experiment_id = HxSockServiceExecute.InstrunctionId(), parameters = dictionaryList, method = getDeviceConfigMethod.ParameterName },
|
//});
|
////var model = JsonConvert.DeserializeObject<InstructCommonMethodSendModel>(scfm.SendData.parameters.ToString());
|
//var resultInstructCommonMethod = ResultHandle.InstructCommonMethod(instructCommonMethodSendModel);
|
//#endregion
|
|
#region 三色灯
|
switch (EnumManagement.ToEnum<ProcessTypeColorEnum_Equipment>(processType.ToString()))
|
{
|
case ProcessTypeColorEnum_Equipment.OrangeColor:
|
// 状态5 暂停 黄色
|
//MethodAction.Instance.StatusLamp(5);
|
//CommonBll.StatusLamp(5, isSimulator);
|
break;
|
case ProcessTypeColorEnum_Equipment.BlueColor:
|
// 状态1 链接成功 蓝亮
|
//MethodAction.Instance.StatusLamp(1);
|
break;
|
case ProcessTypeColorEnum_Equipment.GreenColor:
|
// 状态4 运行 绿色
|
//MethodAction.Instance.StatusLamp(4);
|
break;
|
case ProcessTypeColorEnum_Equipment.RedColor:
|
// 状态6 报错/急停 红闪/蜂鸣5S
|
//MethodAction.Instance.StatusLamp(6);
|
break;
|
|
default:
|
break;
|
}
|
#endregion
|
}
|
#endregion
|
|
#region 发送信息
|
/// <summary>
|
/// 发送信息
|
/// </summary>
|
/// <param name="msg">内容</param>
|
/// <returns></returns>
|
public static bool SendMessage(HxClientResponseObject model)
|
{
|
if (clientConnection != null && clientConnection.Connected)
|
{
|
byte[] sendMsg = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model.ResponseBase) + CommonParameter.SpecifiedDisplay);
|
var result = clientConnection.Send(sendMsg);
|
if (result > 0)
|
{
|
// wdy
|
//AddListView(new ListViewModel
|
//{
|
// Type = "服-》发送信息",
|
// Port = clientConnection.RemoteEndPoint.ToString(),
|
// receive_parameter = "",
|
// send_parameter = JsonConvert.SerializeObject(model),
|
// createtime = DateTime.Now.ToString()
|
//});
|
LoggerSocketHelper.DebugLog("运行方法【SendMessage】消息送出成功");
|
return true;
|
}
|
else
|
{
|
// wdy
|
//AddListView(new ListViewModel
|
//{
|
// Type = "服-》发送信息失败",
|
// Port = clientConnection.RemoteEndPoint.ToString(),
|
// receive_parameter = "",
|
// send_parameter = JsonConvert.SerializeObject(model),
|
// createtime = DateTime.Now.ToString()
|
//});
|
LoggerSocketHelper.DebugLog("运行方法【SendMessage】消息未送出");
|
return false;
|
}
|
}
|
else
|
{
|
LoggerSocketHelper.DebugLog("运行方法【SendMessage】断开");
|
return false;
|
}
|
}
|
#endregion
|
|
#region 停止服务端监听
|
/// <summary>
|
/// 停止服务端监听
|
/// </summary>
|
/// <returns></returns>
|
public static bool BreakConnect()
|
{
|
if (socketWatch != null)
|
{
|
// wdy
|
//AddListView(new ListViewModel
|
//{
|
// Type = "服-》停止监听",
|
// Port = $"{SharedServiceModel.ServiceConfigModel.Ip}:{SharedServiceModel.ServiceConfigModel.Port}",
|
// receive_parameter = "",
|
// send_parameter = "",
|
// createtime = DateTime.Now.ToString()
|
//});
|
|
cts.Cancel();//终止线程
|
if (clientConnection != null && clientConnection.Connected)
|
{
|
clientConnection.Shutdown(SocketShutdown.Both); // 禁用发送和接收
|
clientConnection.Close(); // 关闭同客户端的连接
|
}
|
socketWatch.Close(); // 关闭监听套节字
|
return true;
|
}
|
else
|
{
|
// wdy
|
//AddListView(new ListViewModel
|
//{
|
// Type = "服-》停止监听",
|
// Port = clientConnection.RemoteEndPoint.ToString(),
|
// receive_parameter = "",
|
// send_parameter = "网络未连接",
|
// createtime = DateTime.Now.ToString()
|
//});
|
}
|
return false;
|
}
|
#endregion
|
|
#region 添加列表数据
|
/// <summary>
|
/// 添加列表数据 wdy
|
/// </summary>
|
/// <param name="listView"></param>
|
/// <param name="lvm"></param>
|
//public static void AddListView(ListViewModel lvm)
|
//{
|
// int noCount = listView.Items.Count;
|
// ListViewItem lvi = new ListViewItem((noCount <= 0 ? noCount + 1 : noCount + 1).ToString());
|
// lvi.SubItems.Add(lvm.Type);
|
// lvi.SubItems.Add(lvm.Port);
|
// lvi.SubItems.Add(lvm.receive_parameter);
|
// lvi.SubItems.Add(lvm.send_parameter);
|
// lvi.SubItems.Add(lvm.createtime);
|
// listView.Items.Add(lvi);
|
|
// LoggerSocketHelper.DebugLog("服务端日志,添加显示到列表数据:" + JsonConvert.SerializeObject(lvm));
|
//}
|
#endregion
|
|
#region 回复客户端信息
|
/// <summary>
|
/// 回复客户端信息
|
/// </summary>
|
/// <param name="socketSend">socket</param>
|
/// <param name="hxClientResponseObject">返回数据</param>
|
/// <returns></returns>
|
public static bool ReplyClientMsg(Socket socketSend, HxClientResponseObject hxClientResponseObject)
|
{
|
#region 回复消息
|
string sendStr = JsonConvert.SerializeObject(hxClientResponseObject.ResponseBase) + CommonParameter.SpecifiedDisplay;
|
byte[] sendMsg = Encoding.UTF8.GetBytes(sendStr);
|
int result = socketSend.Send(sendMsg);
|
if (result > 0)
|
{
|
// wdy
|
//SocketInteriorService.AddListView(new ListViewModel
|
//{
|
// Type = "发送-》服-》消息",
|
// Port = socketSend.RemoteEndPoint.ToString(),
|
// receive_parameter = "",
|
// send_parameter = sendStr,
|
// createtime = DateTime.Now.ToString()
|
//});
|
return true;
|
}
|
#endregion
|
|
return false;
|
}
|
#endregion
|
|
#region 发送前端信息提示
|
/// <summary>
|
/// 发送前端信息提示
|
/// </summary>
|
/// <param name="expId">流程ID</param>
|
/// <param name="methodName">方法名称</param>
|
/// <param name="methodStatus">方法状态</param>
|
/// <param name="msgContent">消息内容</param>
|
/// <returns></returns>
|
public static bool SendMessagePrompt(string expId, string methodName, StateEnum_Equipment methodStatus, DealWithType_Enum dealwithtype, string msgContent)
|
{
|
InstructCommonMethodSendModel instructCommonMethodSendModel = new InstructCommonMethodSendModel();
|
instructCommonMethodSendModel.SendData = new HxSendBase { experiment_id = expId };
|
|
HxClientResponseObject hxClientResponseObject = new HxClientResponseObject();
|
hxClientResponseObject.ResponseBase = new HxResponseBase
|
{
|
message_id = expId,
|
message_type = 2,
|
method = methodName,
|
equipment_id = "",
|
workflow_id = "workflow_id_" + expId + "_" + methodName,
|
experiment_id = "experiment_id_" + expId,
|
dataResult = new DataResult { ResultJson = JsonConvert.SerializeObject(instructCommonMethodSendModel) },
|
method_status = EnumManagement.GetEnumValue(methodStatus),// EnumManagement.GetEnumValue(StateEnum.StateEnum_Equipment.Completed),
|
timestamp = DateTime.Now.ToString(),
|
error = new ErrorResult { error_text = msgContent, dealwithtype = EnumManagement.GetEnumValue(dealwithtype).ToString() },
|
};
|
var sendData = SocketInteriorService.SendMessage(hxClientResponseObject);
|
return sendData;
|
}
|
#endregion
|
}
|
}
|