Files
6078/src/StandardDomeNewApp/Communication/SweepCodeGun/backup/ScanCode_ServerSocket.cs

317 lines
11 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.Concurrent;
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_ServerSocket : 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.tbl_SweepCodeGunConfigs.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.tbl_SweepCodeGunConfigs.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 SingleSocketServer singleSocketServer { set; get; }
/// <summary>
/// 正则对象
/// </summary>
private Regex regex { set; get; }
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);
}
}
}
public override bool Connect()
{
IsConnect = false;
if (configModel == null)
{
ShowMessageAction?.Invoke("请先Build后再尝试连接", false, Core.SysEnumInfon.MessageLogType.Info);
return false;
}
singleSocketServer = contentCache.GetContent<SingleSocketServer>(configModel.ProtocolKey);
if (singleSocketServer == null)
{
ShowMessageAction?.Invoke($"无法找到[{configModel.ProtocolKey}]对应的串口对象!", false, Core.SysEnumInfon.MessageLogType.Info);
return false;
}
if (singleSocketServer.RequestReceivedAction == null)
{
singleSocketServer.RequestReceivedAction = RequestReceived;
}
if (singleSocketServer.OnSessionAccept == null)
{
singleSocketServer.OnSessionAccept = SessionAccept;
}
if (singleSocketServer.OnSessionClose == null)
{
singleSocketServer.OnSessionClose = SessionClose;
}
oneEncoding = singleSocketServer.encoding;
IsConnect = true;
return true;
}
public override Tuple<string, int, bool> ReadCode()
{
lock (readopLock)
{
IsBusy = true;
string datapack = "";
int vcode = -1;
bool isok = false;
var id = singleSocketServer.GetSessionID(configModel.Reserve);
for (int i = 0; i < MaxReadCount; i++)
{
bool flag = false;
//发送命令
if (!string.IsNullOrEmpty(configModel.Command))
{
var array = configModel.Command.Split('+');
var command = array[0];
if (array.Length > 1 && array[1] == "NewLine")
{
singleSocketServer.SessionSend(command + Environment.NewLine, id);
}
else
{
singleSocketServer.SessionSend(command, id);
}
}
//开始允许接收
singleSocketServer.IsAllowReceiveDic[id] = true;
//记录开始的时间
DateTime dateTime = DateTime.Now;
while (true)
{
if ((DateTime.Now - dateTime).TotalMilliseconds > configModel.OutTime)
{
//超过预定的时间跳出等待
break;
}
if (regex == null)
{
//直接拿过来
if (!string.IsNullOrEmpty(singleSocketServer.SessionDataDic[id]))
{
//只要有数据来就进来解析
datapack = singleSocketServer.SessionDataDic[id];
var find = ErrorCodes.Find(o => o == datapack);
if (find == null)
{
isok = true;
}
}
}
else
{
var result = regex.Match(singleSocketServer.SessionDataDic[id]);
if (result.Success)
{
//去头去尾
string str = result.Value;
var arrayconnand = configModel.Command.Split(new string[] { ".*" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in arrayconnand)
{
str = str.Replace(item, "");
}
var find = ErrorCodes.Find(o => o == str);
if (find == null)
{
datapack = str;
isok = true;
}
}
}
if (isok)
{
//收到数据了
flag = true;
break;
}
//这个是为了破除最大线程数 防止死循环卡死的
Thread.Sleep(5);
}
if (flag)
{
//如果接收到了数据就跳出尝试循环
break;
}
}
if (IsUserVCode)
{
Vcode++;
if (Vcode > MaxVCode)
{
Vcode = MinVCode;
}
vcode = Vcode;
}
//读取完成无论成功与否都结束接收
singleSocketServer.IsAllowReceiveDic[id] = false;
//清除缓存
singleSocketServer.SessionDataDic[id] = string.Empty;
IsBusy = false;
//这个时候拿出(返回)数据包
return Tuple.Create(datapack, vcode, isok);
}
}
public override void ReConnect()
{
//服务不需要
}
public override bool UnConnect()
{
if (singleSocketServer == null)
{
return false;
}
singleSocketServer.StopServer();
IsConnect = false;
return true;
}
private void RequestReceived(string id, string data)
{
if (singleSocketServer.IsAllowReceiveDic.ContainsKey(id))
{
if (!singleSocketServer.IsAllowReceiveDic[id])
{
//其中一个会话返回
return;
}
if (singleSocketServer.SessionDataDic.ContainsKey(id))
{
singleSocketServer.SessionDataDic[id] += data;
}
}
}
private void SessionAccept(string id)
{
singleSocketServer.IsAllowReceiveDic[id] = false;
singleSocketServer.SessionDataDic[id] = string.Empty;
}
private void SessionClose(string id)
{
if (singleSocketServer.SessionDataDic.ContainsKey(id))
{
singleSocketServer.SessionDataDic.TryRemove(id, out string str);
}
if (singleSocketServer.IsAllowReceiveDic.ContainsKey(id))
{
singleSocketServer.IsAllowReceiveDic.TryRemove(id, out bool state);
}
}
}
}