You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.2 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2026 WenJY 保留所有权利。
* CLR版本4.0.30319.42000
* 机器名称Mr.Wen's MacBook Pro
* 命名空间Sln.IntelliBelt.Business.base
* 唯一标识6DE5D45A-D61B-4F49-ABE8-C98677E1CB69
*
* 创建者WenJY
* 电子邮箱:
* 创建时间2026-04-24 16:09:42
* 版本V1.0.0
* 描述:
*
*--------------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using Newtonsoft.Json.Linq;
using Sln.IntelliBelt.Common;
using Sln.IntelliBelt.Config;
using Sln.IntelliBelt.Serilog;
using Sln.IntelliBelt.Socket.Adapter;
using Sln.IntelliBelt.WebSocket;
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace Sln.IntelliBelt.Business.@base;
public abstract class BaseBusiness
{
public SerilogHelper _logger;
public AppConfig _appConfig;
public StringChange _stringChange;
public WebSocketHelper _webSocket;
public BaseBusiness(SerilogHelper logger,AppConfig appConfig,StringChange stringChange, WebSocketHelper webSocket)
{
_logger = logger;
_appConfig = appConfig;
_stringChange = stringChange;
_webSocket = webSocket;
}
/// <summary>
/// 指令解析方法
/// </summary>
/// <param name="client"></param>
/// <param name="requestInfo"></param>
public abstract void BufferAnalysis(TcpSessionClient client,BufferRequestInfo requestInfo,long bodyLength);
/// <summary>
/// 应答响应
/// </summary>
/// <param name="client"></param>
/// <param name="requestInfo"></param>
public abstract void ResponseHandle(TcpSessionClient client,BufferRequestInfo requestInfo);
/// <summary>
/// 封装回复指令
/// </summary>
/// <param name="SendMessagePackInfo"></param>
/// <param name="buffer"></param>
public byte[] GetMessagePack(ref ResponsePack packInfo)
{
int bufferLength = packInfo.contentLen + 10 ;
int index = 0;
byte[] buffer = new byte[bufferLength];
buffer[index++] = packInfo.header ;
buffer[index++] = packInfo.terminalType ;
Array.Copy(packInfo.terminalAddr, 0, buffer, index, 2);
index += 2;
buffer[index++] = packInfo.serialNumber ;
buffer[index++] = packInfo.identCode ;
buffer[index++] = packInfo.objType ;
buffer[index++] = packInfo.contentLen ;
Array.Copy(packInfo.content, 0, buffer, index, packInfo.contentLen);
index += packInfo.contentLen;
Byte[] checkBitBytes = new Byte[bufferLength-2];
Array.Copy(buffer, 0, checkBitBytes, 0, checkBitBytes.Length);
buffer[index++] = _stringChange.CalculateChecksum(checkBitBytes);
buffer[index++] = packInfo.tail ;
return buffer;
}
public void GetJsonStr(string clientId, BufferRequestInfo requestInfo,out string jsonStr)
{
// {
// "terminalAddr": "0001", //终端地址
// "identCode":30 //功能标识码
// "objType": 0, //对象类型0-终端1-1#读写器2-2#读写器
// "content": "123456789ABC", //读写器指令
// "clientId": "01-00-00-00" //客户端Id终端链接后的 Id
// }
JObject jObj = new JObject();
jObj["clientId"] = clientId;
jObj["terminalAddr"] = requestInfo.terminalAddr;
jObj["identCode"] = requestInfo.identCode;
jObj["objType"] = requestInfo.objType;
jObj["contentLen"] = requestInfo.contentLen;
jObj["content"] = requestInfo.content;
jsonStr = jObj.ToString();
}
/// <summary>
/// 对象类别
/// </summary>
/// <param name="objTypeByte"></param>
/// <param name="objTypeStr"></param>
public void GetObjectTypeStr(int objTypeByte,out string objTypeStr)
{
if (objTypeByte == 0x00)
{
objTypeStr = "终端设备";
}
else
{
objTypeStr = $"{(int)objTypeByte}#读写器";
}
}
}