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.
310 lines
12 KiB
C#
310 lines
12 KiB
C#
using CompressorXN_Model;
|
|
using CompressorXN_Model.ViewModel.Response;
|
|
using CompressorXN_Service;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using thinger.DataConvertLib;
|
|
|
|
namespace CompressorXN.Untils
|
|
{
|
|
public class AgteementHelper
|
|
{
|
|
private static readonly AgreementConfigService _agreementConfigService = new AgreementConfigService();
|
|
private static readonly AgreementMsgService _agreementMsgService = new AgreementMsgService();
|
|
private static readonly ParaService _paraService = new ParaService();
|
|
|
|
/// <summary>
|
|
/// 生成协议
|
|
/// </summary>
|
|
/// <param name="productTypeName">机型名称</param>
|
|
/// <param name="agreementName">协议名称</param>
|
|
public static bool GenerateAgreement(string productTypeName, string agreementName)
|
|
{
|
|
T_Agreement_Msg agreementMsg = new T_Agreement_Msg
|
|
{
|
|
ProductTypeName = productTypeName
|
|
};
|
|
//根据机型名称查询参数项
|
|
List<T_Para> paras = _paraService.QueryParaByProductTypeName(productTypeName)
|
|
.Where(m => m.ParaName == "转速[rmp]").ToList();
|
|
|
|
//根据协议名称查询协议配置规则
|
|
AgreementVM agreementVM = _agreementConfigService.QueryAgreementByName(agreementName);
|
|
|
|
//发送和接收报文明细规则
|
|
T_Agreement_Detail_Rule sendDetailRule = agreementVM.AgreementDetailRuleList.FirstOrDefault(m => m.MsgType == "send");
|
|
T_Agreement_Detail_Rule reciveDetailRule = agreementVM.AgreementDetailRuleList.FirstOrDefault(m => m.MsgType == "recive");
|
|
|
|
//辅助位
|
|
bool[] helperBitArr = new bool[512];
|
|
int i = 0;
|
|
foreach (var item in agreementVM.AgreementHelpRuleList)
|
|
{
|
|
helperBitArr[i] = item.Bit0;
|
|
helperBitArr[i + 1] = item.Bit1;
|
|
helperBitArr[i + 2] = item.Bit2;
|
|
helperBitArr[i + 3] = item.Bit3;
|
|
helperBitArr[i + 4] = item.Bit4;
|
|
helperBitArr[i + 5] = item.Bit5;
|
|
helperBitArr[i + 6] = item.Bit6;
|
|
helperBitArr[i + 7] = item.Bit7;
|
|
i += 8;
|
|
}
|
|
|
|
#region 空载
|
|
|
|
//T_Para idlingPara = paras.FirstOrDefault(m => m.ParaType == "空载");
|
|
//if (idlingPara != null && idlingPara.TargetVal.HasValue)
|
|
//{
|
|
// agreementMsg.IdlingSpeed = (int)idlingPara.TargetVal.Value;
|
|
// //发送值=设定值/精度-偏移量
|
|
// decimal tempSpeed = (agreementMsg.IdlingSpeed - sendDetailRule.Offset) / sendDetailRule.Precision+1;
|
|
// int speedVal = Convert.ToInt32(tempSpeed);
|
|
// string hex = GetHexData(agreementVM.AgreementConfig.MsgFormat, sendDetailRule.StartBit, sendDetailRule.Len, speedVal, helperBitArr);
|
|
// agreementMsg.IdlingHex = hex;
|
|
//}
|
|
|
|
#endregion
|
|
|
|
#region 一段升速
|
|
T_Para speed1UpPara = paras.FirstOrDefault(m => m.ParaType == "一段升速");
|
|
if (speed1UpPara != null && speed1UpPara.TargetVal.HasValue)
|
|
{
|
|
agreementMsg.Speed1 = (int)speed1UpPara.TargetVal.Value;
|
|
//发送值=设定值/精度-偏移量
|
|
decimal tempSpeed = (agreementMsg.Speed1 - sendDetailRule.Offset) / sendDetailRule.Precision;
|
|
int speedVal = Convert.ToInt32(tempSpeed);
|
|
string hex = GetHexData(agreementVM.AgreementConfig.MsgFormat, sendDetailRule.StartBit, sendDetailRule.Len, speedVal, helperBitArr);
|
|
agreementMsg.Speed1Hex = hex;
|
|
}
|
|
#endregion
|
|
|
|
#region 二段升速
|
|
T_Para speed2UpPara = paras.FirstOrDefault(m => m.ParaType == "二段升速");
|
|
if (speed2UpPara != null && speed2UpPara.TargetVal.HasValue)
|
|
{
|
|
agreementMsg.Speed2 = (int)speed2UpPara.TargetVal.Value;
|
|
//发送值=设定值/精度-偏移量
|
|
decimal tempSpeed = (agreementMsg.Speed2 - sendDetailRule.Offset) / sendDetailRule.Precision;
|
|
int speedVal = Convert.ToInt32(tempSpeed);
|
|
string hex = GetHexData(agreementVM.AgreementConfig.MsgFormat, sendDetailRule.StartBit, sendDetailRule.Len, speedVal, helperBitArr);
|
|
agreementMsg.Speed2Hex = hex;
|
|
}
|
|
#endregion
|
|
|
|
#region 三段升速
|
|
T_Para speed3UpPara = paras.FirstOrDefault(m => m.ParaType == "三段升速");
|
|
if (speed3UpPara != null && speed3UpPara.TargetVal.HasValue)
|
|
{
|
|
agreementMsg.Speed3 = (int)speed3UpPara.TargetVal.Value;
|
|
//发送值=设定值/精度-偏移量
|
|
decimal tempSpeed = (agreementMsg.Speed3 - sendDetailRule.Offset) / sendDetailRule.Precision;
|
|
int speedVal = Convert.ToInt32(tempSpeed);
|
|
string hex = GetHexData(agreementVM.AgreementConfig.MsgFormat, sendDetailRule.StartBit, sendDetailRule.Len, speedVal, helperBitArr);
|
|
agreementMsg.Speed3Hex = hex;
|
|
}
|
|
#endregion
|
|
|
|
#region 内漏
|
|
//T_Para endoleadPara = paras.FirstOrDefault(m => m.ParaType == "内漏");
|
|
//if (endoleadPara != null && endoleadPara.TargetVal.HasValue)
|
|
//{
|
|
// agreementMsg.EndoleadSpeed = (int)endoleadPara.TargetVal.Value;
|
|
// decimal tempSpeed = (agreementMsg.EndoleadSpeed - sendDetailRule.Offset) * sendDetailRule.Precision;
|
|
// int speedVal = Convert.ToInt32(tempSpeed);
|
|
// string hex = GetHexData(agreementVM.AgreementConfig.MsgFormat, sendDetailRule.StartBit, sendDetailRule.Len, speedVal, helperBitArr);
|
|
// agreementMsg.EndoleadHex = hex;
|
|
//}
|
|
#endregion
|
|
|
|
#region 单驱模式
|
|
//T_Para singleDriverPara = paras.FirstOrDefault(m => m.ParaType == "单驱模式");
|
|
//if (singleDriverPara != null && singleDriverPara.TargetVal.HasValue)
|
|
//{
|
|
// agreementMsg.SingleDriverSpeed = (int)singleDriverPara.TargetVal.Value;
|
|
// //发送值=设定值/精度-偏移量
|
|
// decimal tempSpeed = (agreementMsg.SingleDriverSpeed - sendDetailRule.Offset) / sendDetailRule.Precision + 1;
|
|
// int speedVal = Convert.ToInt32(tempSpeed);
|
|
// string hex = GetHexData(agreementVM.AgreementConfig.MsgFormat, sendDetailRule.StartBit, sendDetailRule.Len, speedVal, helperBitArr);
|
|
// agreementMsg.SingleDriverHex = hex;
|
|
//}
|
|
#endregion
|
|
|
|
return _agreementMsgService.SaveAgreementMsg(agreementMsg, productTypeName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取十六进制报文
|
|
/// </summary>
|
|
/// <param name="msgType"></param>
|
|
/// <param name="startBit"></param>
|
|
/// <param name="len"></param>
|
|
/// <param name="val"></param>
|
|
/// <param name="helperBitArr"></param>
|
|
/// <returns></returns>
|
|
private static string GetHexData(string msgType, int startBit, int len, int val, bool[] helperBitArr)
|
|
{
|
|
string hex = string.Empty;
|
|
switch (msgType)
|
|
{
|
|
case "Intel":
|
|
bool[] bitArr = GetIntel(startBit, len, val, helperBitArr);
|
|
//将bit数组转成十六进制字符串
|
|
var byteArr = ByteArrayLib.GetByteArrayFromBoolArray(bitArr);
|
|
hex = StringLib.GetHexStringFromByteArray(byteArr);
|
|
break;
|
|
case "Motorola(LSB)":
|
|
bitArr = GetMotoralaLsb(startBit, len, val, helperBitArr);
|
|
//将bit数组转成十六进制字符串
|
|
byteArr = ByteArrayLib.GetByteArrayFromBoolArray(bitArr);
|
|
hex = StringLib.GetHexStringFromByteArray(byteArr);
|
|
break;
|
|
case "Motorola(MSB)":
|
|
bitArr = GetIntel(startBit, len, val, helperBitArr);
|
|
//将bit数组转成十六进制字符串
|
|
byteArr = ByteArrayLib.GetByteArrayFromBoolArray(bitArr);
|
|
hex = StringLib.GetHexStringFromByteArray(byteArr);
|
|
break;
|
|
}
|
|
return hex;
|
|
}
|
|
|
|
#region 通用部分
|
|
|
|
/// <summary>
|
|
/// 获取Intel规则的发送报文
|
|
/// </summary>
|
|
/// <param name="startBit"></param>
|
|
/// <param name="len"></param>
|
|
/// <param name="val"></param>
|
|
/// <param name="baseBitArr"></param>
|
|
/// <returns></returns>
|
|
private static bool[] GetIntel(int startBit, int len, int val, bool[] baseBitArr)
|
|
{
|
|
var needBitArr = HexToBoolArr((ushort)val, len);
|
|
for (int j = 0; j < len; j++)
|
|
{
|
|
baseBitArr[startBit] = needBitArr[j];
|
|
|
|
startBit++;
|
|
}
|
|
return baseBitArr;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取MotoralaLsb规则的发送报文
|
|
/// </summary>
|
|
/// <param name="startBit"></param>
|
|
/// <param name="len"></param>
|
|
/// <param name="val"></param>
|
|
/// <param name="baseBitArr"></param>
|
|
/// <returns></returns>
|
|
private static bool[] GetMotoralaLsb(int startBit, int len, int val, bool[] baseBitArr)
|
|
{
|
|
var needBitArr = GetBitArrFromInt(val, len);
|
|
for (int j = len - 1; j >= 0; j--)
|
|
{
|
|
byte bits = (byte)(1 << (startBit % 8));
|
|
baseBitArr[startBit] = needBitArr[j];
|
|
startBit++;
|
|
if (bits == 0x80)
|
|
{
|
|
startBit -= 16;
|
|
}
|
|
}
|
|
return baseBitArr;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取MotoralaMsb规则的发送报文
|
|
/// </summary>
|
|
/// <param name="startBit"></param>
|
|
/// <param name="len"></param>
|
|
/// <param name="val"></param>
|
|
/// <param name="baseBitArr"></param>
|
|
/// <returns></returns>
|
|
private static bool[] GetMotoralaMsb(int startBit, int len, int val, bool[] baseBitArr)
|
|
{
|
|
var needBitArr = GetBitArrFromInt(val, len);
|
|
for (int j = len - 1; j >= 0; j--)
|
|
{
|
|
byte bits = (byte)(1 << (startBit % 8));
|
|
baseBitArr[startBit] = needBitArr[j];
|
|
startBit--;
|
|
if (bits == 0x01)
|
|
{
|
|
startBit += 16;
|
|
}
|
|
}
|
|
return baseBitArr;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将值按照IntelLsb规则获取bool数组
|
|
/// </summary>
|
|
/// <param name="val"></param>
|
|
/// <param name="len"></param>
|
|
/// <returns></returns>
|
|
private static bool[] HexToBoolArr(ushort val, int len)
|
|
{
|
|
bool[] boolArr = new bool[len];
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
boolArr[i] = (val & (1 << i)) != 0;
|
|
}
|
|
return boolArr;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从int数值获取指定长度的Bit数组
|
|
/// </summary>
|
|
/// <param name="val"></param>
|
|
/// <param name="length"></param>
|
|
/// <returns></returns>
|
|
private static bool[] GetBitArrFromInt(int val, int length)
|
|
{
|
|
string binaryString = Convert.ToString(val, 2);
|
|
List<bool> bits = new List<bool>();
|
|
|
|
// 补零操作,使得二进制位数达到指定长度
|
|
for (int i = 0; i < length - binaryString.Length; i++)
|
|
{
|
|
bits.Add(false);
|
|
}
|
|
|
|
// 将二进制字符串的每一位转换为bool值
|
|
foreach (char digit in binaryString)
|
|
{
|
|
bits.Add(digit == '1');
|
|
}
|
|
|
|
return bits.ToArray();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// 辅助位
|
|
/// </summary>
|
|
class HelperBit
|
|
{
|
|
public bool Bit7 { get; set; }
|
|
|
|
public bool Bit6 { get; set; }
|
|
|
|
public bool Bit5 { get; set; }
|
|
|
|
public bool Bit4 { get; set; }
|
|
|
|
public bool Bit3 { get; set; }
|
|
|
|
public bool Bit2 { get; set; }
|
|
|
|
public bool Bit1 { get; set; }
|
|
|
|
public bool Bit0 { get; set; }
|
|
}
|
|
}
|