天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.0 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using QMAPP.FJC.Entity.Basic;
using QMFrameWork.Common.Serialization;
namespace OpcProcessWinService
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
//[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class OpcService : IOpcService
{
IControlCallback callback = null;
static object syncObj = new Object();
/// <summary>
/// 工控机注册服务端
/// </summary>
/// <param name="machineNo"></param>
/// <returns></returns>
public int Register(string machineNo)
{
//建立双通道通信
lock (syncObj)//线程的同步性,同步访问多个线程的任何变量,利用lock(独占锁),确保数据访问的唯一性。
{
if (ChatEventHelper.chatters.ContainsKey(machineNo) == false)
{
//添加客户信息
ChatEventHelper.chatters.Add(machineNo, ReturnProductCode);
//添加客户端时间标记
//ChatEventHelper.chatterTimes.Add(machineNo, System.DateTime.Now);
Console.WriteLine(machineNo + ":Register");
}
else
{
//如果包含了客户端信息,先删除客户端信息
ChatEventHelper.chatters.Remove(machineNo);
//ChatEventHelper.chatterTimes.Remove(machineNo);
//删除后再次添加
ChatEventHelper.chatters.Add(machineNo, ReturnProductCode);
//删除后再次添加客户端时间标记
//ChatEventHelper.chatterTimes.Add(machineNo, System.DateTime.Now);
Console.WriteLine(machineNo + ":Register");
}
callback = OperationContext.Current.GetCallbackChannel<IControlCallback>();
}
return 1;
}
/// <summary>
/// 退出登录
/// </summary>
/// <param name="machineNo"></param>
/// <returns></returns>
public int Logout(string machineNo)
{
if (ChatEventHelper.chatters.ContainsKey(machineNo))
{
lock (syncObj)
{
//删除客户端
ChatEventHelper.chatters.Remove(machineNo);
}
}
return 1;
}
/// <summary>
/// 向向设备发送可操作标记
/// </summary>
/// <param name="order"></param>
public void SendOperateOrder(string paraStr)
{
ParameterConfig config = new ParameterConfig();
List<ParameterConfig> configList = new List<ParameterConfig>();
configList = JsonConvertHelper.GetDeserialize<List<ParameterConfig>>(paraStr);
ParaInit.SendOperateReadyOrder(configList);
}
/// <summary>
/// 更新客户端连接服务器
/// </summary>
/// <param name="machineNo"></param>
/// <returns></returns>
public int UpdateClientInfo(string machineNo)
{
//建立双通道通信
//线程的同步性,同步访问多个线程的任何变量,利用lock(独占锁),确保数据访问的唯一性。
lock (syncObj)
{
if (ChatEventHelper.chatters.ContainsKey(machineNo))
{
ChatEventHelper.chatters.Remove(machineNo);
ChatEventHelper.chatterTimes.Remove(machineNo);
}
ChatEventHelper.chatters.Add(machineNo, ReturnProductCode);
ChatEventHelper.chatterTimes.Add(machineNo, System.DateTime.Now);
callback = OperationContext.Current.GetCallbackChannel<IControlCallback>();
}
return 1;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ReturnProductCode(object sender, ChatEventArgs e)
{
ReturnMessage obj = new ReturnMessage();
obj.MessageContent = e.MessageContent;
obj.MessageType = e.MessageType;
//callback.ReturnResult(obj);
callback.ReturnResult(e.MessageType,e.MessageContent);
}
public void ReturnProductCodeToMachine(ChatEventArgs e)
{
try
{
//初始化传送参数
ChatEventHelper.ChatEventHandler chatEvent;
if (ChatEventHelper.chatters.Keys.Contains(e.MachineCode))
{
lock (syncObj)
{
//通过工控机名称获取事件信息
chatEvent = ChatEventHelper.chatters[e.MachineCode];
//查找成员字典中,找到要接收者的委托调用
}
//向工控机发送条码信息
chatEvent.BeginInvoke(this, e, new AsyncCallback(EndAsync), null);//异步方式调用接收者的委托调用
}
}
catch (KeyNotFoundException ex)
{
}
}
private void EndAsync(IAsyncResult ar)
{
ChatEventHelper.ChatEventHandler d = null;
try
{
//封装异步委托上的异步操作结果
System.Runtime.Remoting.Messaging.AsyncResult asres = (System.Runtime.Remoting.Messaging.AsyncResult)ar;
d = ((ChatEventHelper.ChatEventHandler)asres.AsyncDelegate);
d.EndInvoke(ar);
}
catch
{
//ChatEvent -= d;
}
}
}
}