using System;
using System.Text;
using JTNE.Protocol.Enums;
using JTNE.Protocol.Interfaces;
using JTNE.Protocol.Internal;
using JTNE.Protocol.MessageBody;
namespace JTNE.Protocol {
///
///
///
public class JTNEGlobalConfigs {
private static readonly Lazy instance = new Lazy (() => new JTNEGlobalConfigs ());
private JTNEGlobalConfigs () {
Encoding.RegisterProvider (CodePagesEncodingProvider.Instance);
Encoding = Encoding.UTF8;
SkipCRCCode = false;
DeviceMsgSNDistributed = new DefaultDeviceMsgSNDistributedImpl ();
PlatformMsgSNDistributed = new DefaultPlatformMsgSNDistributedImpl ();
}
///
/// 字符串编码
///
public Encoding Encoding { get; set; }
///
///
///
public static JTNEGlobalConfigs Instance {
get {
return instance.Value;
}
}
///
/// 设备流水号
///
public IDeviceMsgSNDistributed DeviceMsgSNDistributed { get; private set; }
///
/// 平台流水号
///
public IPlatformMsgSNDistributed PlatformMsgSNDistributed { get; private set; }
///
/// 跳过校验码
/// 测试的时候需要手动修改值,避免验证
/// 默认:false
///
public bool SkipCRCCode { get; private set; }
///
/// 消息数据体加密算法
/// RSA=>IJTNEEncryptImpl
/// AES=>IJTNEEncryptImpl
///
public Func DataBodiesEncrypt { get; private set; }
///
/// 平台登入加密算法
/// RSA=>IJTNEEncryptImpl
/// AES=>IJTNEEncryptImpl
///
public Func PlatformLoginEncrypt { get; private set; }
///
/// 注册自定义消息
///
///
///
///
public JTNEGlobalConfigs Register_CustomMsgId (byte customMsgId)
where TJTNEBodies : JTNEBodies {
JTNEMsgIdFactory.SetMap (customMsgId);
return instance.Value;
}
///
/// 重写消息
///
///
///
///
public JTNEGlobalConfigs Overwrite_MsgId (byte overwriteMsgId)
where TJTNEBodies : JTNEBodies {
JTNEMsgIdFactory.ReplaceMap (overwriteMsgId);
return instance.Value;
}
///
///
///
/// 自定义类型编码
/// 继承JTNE.Protocol.MessageBody.JTNE_0x02_CustomBody
///
public JTNEGlobalConfigs Register_JTNE0x02CustomBody (byte typeCode, Type type) {
if (!JTNE_0x02_CustomBody.CustomTypeCodes.ContainsKey (typeCode)) {
JTNE_0x02_CustomBody.CustomTypeCodes.Add (typeCode, type);
}
return instance.Value;
}
///
///
///
/// 自定义类型编码
/// 继承JTNE.Protocol.MessageBody.JTNE_0x81_Body
///
public JTNEGlobalConfigs Register_JTNE0x81CustomBody (byte typeCode, Type type) {
if (!JTNE_0x81_Body.JTNE_0x81Method.ContainsKey (typeCode)) {
JTNE_0x81_Body.JTNE_0x81Method.Add (typeCode, type);
}
return instance.Value;
}
///
///
///
/// 自定义类型编码
/// 继承JTNE.Protocol.MessageBody.JTNE_0x81_Body
///
public JTNEGlobalConfigs Register_JTNE0x81CustomDepenedBody (byte dependerId, byte dependedId) {
if (!JTNE_0x81_Body.JTNE_0x81LengthOfADependOnValueOfB.ContainsKey (dependerId)) {
JTNE_0x81_Body.JTNE_0x81LengthOfADependOnValueOfB.Add (dependerId, dependedId);
}
return instance.Value;
}
///
///
///
/// 自定义类型编码
/// 继承JTNE.Protocol.MessageBody.JTNE_0x81_Body
///
public JTNEGlobalConfigs Register_JTNE0x82CustomBody (byte typeCode, Type type) {
if (!JTNE_0x82_Body.JTNE_0x82Method.ContainsKey (typeCode)) {
JTNE_0x82_Body.JTNE_0x82Method.Add (typeCode, type);
}
return instance.Value;
}
///
/// 设置跳过校验码
/// 场景:测试的时候,可能需要手动改数据,所以测试的时候有用
///
///
///
public JTNEGlobalConfigs SetSkipCRCCode (bool skipCRCCode) {
instance.Value.SkipCRCCode = skipCRCCode;
return instance.Value;
}
///
/// 设置设备流水号
///
///
///
public JTNEGlobalConfigs SetDeviceMsgSNDistributed (IDeviceMsgSNDistributed deviceMsgSNDistributed) {
instance.Value.DeviceMsgSNDistributed = deviceMsgSNDistributed;
return instance.Value;
}
///
/// 设置平台流水号
///
///
///
public JTNEGlobalConfigs SetPlatformMsgSNDistributed (IPlatformMsgSNDistributed platformMsgSNDistributed) {
instance.Value.PlatformMsgSNDistributed = platformMsgSNDistributed;
return instance.Value;
}
///
/// 设置消息数据体加密算法
///
///
///
public JTNEGlobalConfigs SetDataBodiesEncrypt (Func dataBodiesEncrypt) {
instance.Value.DataBodiesEncrypt = dataBodiesEncrypt;
return instance.Value;
}
///
/// 设置平台登入加密算法
///
///
///
public JTNEGlobalConfigs SetPlatformLoginEncrypt (Func platformLoginEncrypt) {
instance.Value.PlatformLoginEncrypt = platformLoginEncrypt;
return instance.Value;
}
}
}