using BPASmartClient.Model.咖啡机.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace BPASmartClient.KLMCoffee.Protocal
{
///
/// K95 咖啡机
///
///
public class K95Command
{
//响应说明:00 代表发送成功
// 01 代表机器正忙
// 02 代表串口指令错误
#region 私有变量
private string address = "01";//地址,默认01
private string command;//控制命令
private string content;//控制内容
private string lrc;//校验位
#endregion
#region 构造
///
/// 构造函数
///
/// 控制命令
/// 控制内容
/// 地址,默认“01”
public K95Command(string _command,string _content,string _address = "01")
{
command = _command;
content = _content;
address = _address;
lrc = calLrcCommon("01" + _command + _content);
}
///
/// 提供无参数构造
///
public K95Command()
{
}
#endregion
#region 外部调用场景与命令
///
/// 发送通用常规命令
///
public byte[] ReturnsCommandData(string _command,string _content)
{
try
{
command = _command;
content = _content;
lrc = calLrcCommon("01" + _command + _content);//生成校验位
string commandStr = $":01{command}{content}{lrc}\r\n";//生成控制命令
return ascii2Hex(commandStr);
}
catch (Exception ex)
{
return new byte[0];
}
}
///
/// 返回取消制作
///
public byte[] ReturnsCancelMake()
{
try
{
return ReturnsCommandData("03","0001000C");
}
catch (Exception ex)
{
return new byte[0];
}
}
///
/// 返回放杯确认
///
public byte[] ReturnsCupIsOK()
{
try
{
return ReturnsCommandData("06","0001");
}
catch (Exception ex)
{
return new byte[0];
}
}
///
/// 清洗冲泡器
///
public byte[] ReturnsWashCPJ()
{
try
{
return ReturnsCommandData("04","0001010A");
}
catch (Exception ex)
{
return new byte[0];
}
}
///
/// 清洗奶沫器
///
public byte[] ReturnsWashNMJ()
{
try
{
return ReturnsCommandData("04","0001040A");
}
catch (Exception ex)
{
return new byte[0];
}
}
///
/// 清洗奶沫器确认
///
public byte[] ReturnsWashNMJIsOK()
{
try
{
return ReturnsCommandData("06","0001000C");
}
catch (Exception ex)
{
return new byte[0];
}
}
///
/// 状态查询
///
public byte[] ReturnsStatusInquire()
{
try
{
return ReturnsCommandData("05","0000000D");
}
catch (Exception ex)
{
return new byte[0];
}
}
///
/// 返回状态信息解析
/// :0105 AD10(系统状态) 0000(制作状态)
/// 0000(制作进度)0000(制作进度)
/// 0000(故障信息) B(保养信息) 3A\r\n
/// :010510000000000000000000F3A\r\n
///
///
public SystemStatusModel StateResolution(string message)
{
try
{
return new SystemStatusModel(message);
}
catch (Exception)
{
return null;
}
}
#endregion
#region 获取所有命令或类型
///
/// 获取所有命令
///
public List GetCommand()
{
List vs = new List();
try
{
//var obj = new K95CommandEnum();
//var type = obj.GetType();
//foreach (FieldInfo field in type.GetFields())
//{
// vs.Add(field.Name);
//}
vs = System.Enum.GetNames(typeof(K95CommandEnum))?.ToList();
}
catch (Exception ex)
{
return vs;
}
return vs;
}
///
/// 根据命令获取值
///
public string GetCommandValue(string name)
{
try
{
//var obj = new K95CommandEnum();
//var type = obj.GetType();
//foreach (FieldInfo field in type.GetFields())
//{
// if (field.Name == name) return (string)field.GetValue(obj);
//}
return ToEnumValue(name).GetString();
}
catch (Exception ex)
{
return string.Empty;
}
return string.Empty;
}
///
/// 获取所有咖啡类型
///
public List GetCoffeeType()
{
List vs = new List();
try
{
//var obj = new K95deFaultCoffeeEnum();
//var type = obj.GetType();
//foreach (FieldInfo field in type.GetFields())
//{
// vs.Add(field.Name);
//}
vs = System.Enum.GetNames(typeof(K95deFaultCoffeeEnum))?.ToList();
}
catch (Exception ex)
{
return vs;
}
return vs;
}
///
/// 根据咖啡类型获取值
///
public string GetCoffeeTypeValue(string name)
{
try
{
//var obj = new K95deFaultCoffeeEnum();
//var type = obj.GetType();
//foreach (FieldInfo field in type.GetFields())
//{
// if (field.Name == name) return (string)field.GetValue(obj);
//}
return ToEnumValue(name).GetString();
}
catch (Exception ex)
{
return string.Empty;
}
return string.Empty;
}
#endregion
#region 常规命令生成函数
///
/// 附校验码生成代码
///
/// 地址+控制命令+控制内容
///
public static string calLrcCommon(string data)
{
try
{
if (data.Length % 2 != 0)
{
data = data + "0";
}
int total = 0;
int len = data.Length;
int num = 0;
while (num < len)
{
string s = data.Substring(num,2);
total += Convert.ToInt32(s,16);
num += 2;
}
total = ~total + 1;
string checkSum = (total & 255).ToString("x").ToUpper();
while (checkSum.Length < 2)
{
checkSum = "0" + checkSum;
}
return checkSum;
}
catch (Exception)
{
}
return "";
}
///
/// 返回控制协议ASCII
///
///
public string toUartString()
{
string redata = string.Empty;
try
{
redata = $":01{command}{content}{lrc}\r\n";
return redata;
}
catch (Exception ex)
{
return "";
}
}
///
/// ASCII转换成16进制
///
///
///
public byte[] ascii2Hex(string str)
{
try
{
string str1 = str.Trim(); // 去掉字符串首尾处的空格
char[] charBuf = str1.ToArray(); // 将字符串转换为字符数组
ASCIIEncoding charToASCII = new ASCIIEncoding();
byte[] TxdBuf = new byte[charBuf.Length + 4]; // 定义发送缓冲区;
byte[] Buf = charToASCII.GetBytes(charBuf); // 转换为各字符对应的ASCII
for (int i = 0; i < Buf.Length; i++)
{
TxdBuf[i] = Buf[i];
}
TxdBuf[charBuf.Length] = 0x5c;
TxdBuf[charBuf.Length + 1] = 0x72;
TxdBuf[charBuf.Length + 2] = 0x5c;
TxdBuf[charBuf.Length + 3] = 0x6e;
return TxdBuf;
}
catch (Exception ex)
{
return new byte[0];
}
}
///
/// Int 转 16进制字符串 (保留几位)
///
public string IntToHexString(int data,int length = 2)
{
try
{
return data.ToString("X").PadLeft(length,'0');
}
catch (Exception ex)
{
return string.Empty;
}
}
///
/// 将一条十六进制字符串转换为ASCII
///
/// 一条十六进制字符串
/// 返回一条ASCII码
public static string HexStringToASCII(string hexstring)
{
byte[] bt = HexStringToBinary(hexstring);
string lin = "";
for (int i = 0; i < bt.Length; i++)
{
lin = lin + bt[i] + " ";
}
string[] ss = lin.Trim().Split(new char[] { ' ' });
char[] c = new char[ss.Length];
int a;
for (int i = 0; i < c.Length; i++)
{
a = Convert.ToInt32(ss[i]);
c[i] = Convert.ToChar(a);
}
string b = new string(c);
return b;
}
///
/// 16进制字符串转换为二进制数组
///
/// 用空格切割字符串
/// 返回一个二进制字符串
public static byte[] HexStringToBinary(string hexstring)
{
string[] tmpary = hexstring.Trim().Split(' ');
byte[] buff = new byte[tmpary.Length];
for (int i = 0; i < buff.Length; i++)
{
buff[i] = Convert.ToByte(tmpary[i],16);
}
return buff;
}
///
/// 根据枚举名称获取枚举值
///
public T ToEnumValue(string name)
{
return (T)Enum.Parse(typeof(T),name);
}
#endregion
}
}