Files
6078/src/StandardDomeNewApp/Communication/SweepCodeGun/ScanCode_ClientSocket.cs

346 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using StandardDomeNewApp.Communication.Sockets;
using StandardDomeNewApp.Mappering;
using StandardDomeNewApp.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace StandardDomeNewApp.Communication.SweepCodeGun
{
public class ScanCode_ClientSocket : IScanCode
{
/// <summary>
/// 是否连接
/// </summary>
public override bool IsConnect
{
get => base.IsConnect;
protected set
{
if (base.IsConnect != value)
{
base.IsConnect = value;
if (configModel == null)
{
return;
}
using (var dbContext = new DataBaseMappering())
{
var find = dbContext.TSweepCodeGunConfigs.Where(o => o.PrimaryKey == configModel.PrimaryKey);
if (find != null)
{
var list = find.ToList();
if (list.Count > 0)
{
var one = list.ElementAt(0);
//修改这个one
one.IsConnect = value ? 1 : 0;
dbContext.SaveChanges();
}
}
}
}
}
}
/// <summary>
/// 虚拟码
/// </summary>
public override int Vcode
{
get => base.Vcode;
set
{
if (base.Vcode != value)
{
base.Vcode = value;
if (configModel == null)
{
return;
}
using (var dbContext = new DataBaseMappering())
{
var find = dbContext.TSweepCodeGunConfigs.Where(o => o.PrimaryKey == configModel.PrimaryKey);
if (find != null)
{
var list = find.ToList();
if (list.Count > 0)
{
var one = list.ElementAt(0);
//修改这个one
one.VCode = value;
dbContext.SaveChanges();
}
}
}
}
}
}
/// <summary>
/// 读取操作的锁
/// </summary>
private object readopLock { set; get; } = new object();
/// <summary>
/// 扫码枪配置参数
/// </summary>
private SweepCodeGunConfigModel configModel { set; get; }
/// <summary>
/// 网口客户端对象
/// </summary>
private ProvidOrdinaryClient providOrdinaryClient { set; get; }
/// <summary>
/// 正则对象
/// </summary>
private Regex regex { set; get; }
/// <summary>
/// 一个客户端接收数据也是一个个来的
/// </summary>
private string dataPack { set; get; }
/// <summary>
/// 是否允许开始接收了
/// </summary>
private bool IsAllowReceive { set; get; } = false;
/// <summary>
/// 条码的长度
/// </summary>
private int QRcodeLength = int.Parse(Login.GetParaValue("QRCodeLength"));
/// <summary>
/// 建立对象
/// </summary>
/// <param name="config"></param>
public override void Build(object config)
{
if (config is SweepCodeGunConfigModel model)
{
configModel = model;
KeyName = configModel.PrimaryKey;
if (configModel.MaxVCode == -1 || configModel.MinVCode == -1)
{
IsUserVCode = false;
}
MaxVCode = configModel.MaxVCode;
MinVCode = configModel.MinVCode;
Vcode = configModel.VCode;
if (configModel.ErrorCode != null)
{
var errorcodes = configModel.ErrorCode.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in errorcodes)
{
ErrorCodes.Add(item);
}
}
if (!string.IsNullOrEmpty(configModel.CodeRegex))
{
regex = new Regex(configModel.CodeRegex);
}
}
}
/// <summary>
/// 连接
/// </summary>
/// <returns></returns>
public override bool Connect()
{
IsConnect = false;
if (configModel == null)
{
ShowMessageAction?.Invoke("请先Build后再尝试连接", false, Core.SysEnumInfon.MessageLogType.Info);
Util.CommonHelper.Fatal("ScanCode_ClientSocket","Error 1:"+ "请先Build后再尝试连接");
return false;
}
providOrdinaryClient = contentCache.GetContent<ProvidOrdinaryClient>(configModel.ProtocolKey);
if (providOrdinaryClient == null)
{
ShowMessageAction?.Invoke($"无法找到[{configModel.ProtocolKey}]对应的网口客户端对象!", false, Core.SysEnumInfon.MessageLogType.Info);
Util.CommonHelper.Fatal("ScanCode_ClientSocket", "Error 2:" + $"无法找到[{configModel.ProtocolKey}]对应的网口客户端对象!");
return false;
}
providOrdinaryClient.IsconnectserverAction = b =>
{
IsConnect = b;
ShowMessageAction?.Invoke($"扫码枪对象[{KeyName}]连接状态:{IsConnect}", false, Core.SysEnumInfon.MessageLogType.Info);
Util.CommonHelper.Fatal("ScanCode_ClientSocket", "Error 3:" + $"扫码枪对象[{KeyName}]连接状态:{IsConnect}");
};
providOrdinaryClient.GetMessages = RawData_Receive;
IsConnect = providOrdinaryClient.IsconnectServer;
oneEncoding = providOrdinaryClient.encoding;
ShowMessageAction?.Invoke($"扫码枪对象[{KeyName}]连接状态:{IsConnect}", false, Core.SysEnumInfon.MessageLogType.Info);
Util.CommonHelper.Fatal("ScanCode_ClientSocket", "Error 4:" + $"扫码枪对象[{KeyName}]连接状态:{IsConnect}");
return IsConnect;
}
/// <summary>
/// 读取条码 (同步完成的) 自动扫码
/// </summary>
/// <returns></returns>
public Tuple<string,int,bool> ReadCode3()//去掉了 override
{
lock (readopLock)
{
IsBusy = true;
string dataPackResult = "";
for (int i = 0; i < MaxReadCount; i++)
{
//发送命令
if (!string.IsNullOrEmpty(configModel.Command))
{
var array = configModel.Command.Split('+');
var command = array[0];
if (array.Length > 1 && array[1] == "NewLine")
{
providOrdinaryClient.SendCommand(command + Environment.NewLine);
}
else
{
providOrdinaryClient.SendCommand(command);
}
}
//开始允许接收
IsAllowReceive = true;
//记录开始的时间
DateTime dateTime = DateTime.Now;
while (true)
{
if ((DateTime.Now - dateTime).TotalMilliseconds > configModel.OutTime)
{
//超过预定的时间跳出等待
break;
}
//正则表达式,基本用不到
//直接拿过来
if (!string.IsNullOrEmpty(dataPack))
{
//只要有数据来就进来解析
dataPackResult = dataPack;//dataPack在RawData_Receive函数接收
var find = ErrorCodes.Find(o => dataPackResult.Contains(o));
if (find == null)
{
//如果读到了数据就跳出来,以下语句挑出两层循环
goto OutPutData;
}
}
Thread.Sleep(5);
}
}
OutPutData:
//读取完成无论成功与否都结束接收
IsAllowReceive = false;
//清除缓存
dataPack = string.Empty;
IsBusy = false;
//这个时候拿出(返回)数据包
return Tuple.Create(dataPackResult,-1,true);
}
}
/// <summary>
/// 手动扫码
/// </summary>
/// <returns></returns>
public override Tuple<string, int, bool> ReadCode()
{
lock (readopLock)
{
IsBusy = true;
string dataPackResult = "";
//开始允许接收
//IsAllowReceive = true;
//正则表达式,基本用不到
//直接拿过来
if (!string.IsNullOrEmpty(dataPack))
{
//只要有数据来就进来解析
dataPackResult = dataPack;//dataPack在RawData_Receive函数接收
var find = ErrorCodes.Find(o => dataPackResult.Contains(o));
if (find == null)
{
//如果读到了数据就跳出来,以下语句挑出两层循环
goto OutPutData;
}
}
OutPutData:
//读取完成无论成功与否都结束接收
IsAllowReceive = false;
//清除缓存
dataPack = string.Empty;
IsBusy = false;
//这个时候拿出(返回)数据包
bool flag = false;
if (dataPackResult.Length>=3)
{
flag = true;
}
return Tuple.Create(dataPackResult, -1, flag);
}
}
public override void ReConnect()
{
providOrdinaryClient?.ReConnect();
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public override bool UnConnect()
{
if (providOrdinaryClient == null)
{
return false;
}
providOrdinaryClient.CloseConnect();
IsConnect = false;
return true;
}
/// <summary>
/// 原始数据接收
/// </summary>
/// <param name="data"></param>
/// <param name="encoding"></param>
private void RawData_Receive(byte[] data, Encoding encoding)
{
GetOriginalData?.Invoke(data, encoding);
//if (!IsAllowReceive)
//{
// return;
//}
dataPack += encoding.GetString(data);
//if (encoding.BodyName=="utf-8")// addwang 读取刷卡机
//{
// StringBuilder builder = new StringBuilder();
// for (int i = 0; i < data.Length; i++)
// {
// builder.Append(string.Format("{0:X2} ", data[i]));
// }
// dataPack = builder.ToString().Trim();
//}
//else
//{
// dataPack += encoding.GetString(data);
//}
}
/// <summary>
///
/// </summary>
/// <param name="byteDatas"></param>
/// <returns></returns>
}
}