Bladeren bron

调整说明:增加伽乐美 外设控制协议

样式分支
fyf 2 jaren geleden
bovenliggende
commit
0e1ad6636d
10 gewijzigde bestanden met toevoegingen van 382 en 48 verwijderingen
  1. +258
    -2
      BPASmartClient.KLMCoffee/CoffeeMachine.cs
  2. +12
    -0
      BPASmartClient.KLMCoffee/Protocal/FaultMessage.cs
  3. +7
    -7
      BPASmartClient.KLMCoffee/Protocal/K95Command.cs
  4. +2
    -1
      BPASmartClient.KLMCoffee/Protocal/K95commandEnumExtensions.cs
  5. +2
    -1
      BPASmartClient.KLMCoffee/Protocal/RecipeModel.cs
  6. +49
    -32
      BPASmartClient.KLMCoffee/Protocal/SystemStatusModel.cs
  7. +12
    -0
      BPASmartClient.KLMCoffee/Protocal/UpkeepMessage.cs
  8. +1
    -1
      BPASmartClient.Model/咖啡机/Enum/DrinkType.cs
  9. +32
    -4
      BPASmartClient.Model/咖啡机/KLMCoffeeEvent.cs
  10. +7
    -0
      BPASmartClient/DeviceInfo.xml

+ 258
- 2
BPASmartClient.KLMCoffee/CoffeeMachine.cs Bestand weergeven

@@ -1,14 +1,25 @@
using BPASmartClient.Helper;
using BPASmartClient.EventBus;
using BPASmartClient.Helper;
using BPASmartClient.KLMCoffee.Protocal;
using BPASmartClient.Message;
using BPASmartClient.Model;
using BPASmartClient.Model.咖啡机.Enum;
using BPASmartClient.Peripheral;
using BPASmartClient.SerialPort;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static BPASmartClient.EventBus.EventBus;

namespace BPASmartClient.KLMCoffee
{
public class CoffeeMachine
/// <summary>
/// 伽乐美咖啡机
/// </summary>
public class CoffeeMachine :BasePeripheral
{
//通讯代理
SerialPortClient commProxy = null;
@@ -26,9 +37,254 @@ namespace BPASmartClient.KLMCoffee
private DateTime lastRefreshTime = DateTime.MinValue;
//是否在线
public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
//命令
public K95Command command = new K95Command();

public CoffeeMachine()
{
cmdAsk = new K95Command().ReturnsStatusInquire();
}

/// <summary>
/// 主线程开始运行
/// </summary>
public override void Start()
{
try
{
commProxy.Start();
IsConnected = true;
free = false;
MainLoop();
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
}

/// <summary>
/// 停止运行
/// </summary>
public override void Stop()
{
try
{
commProxy.Stop();
IsConnected = false;
free = true;
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
}

/// <summary>
/// 主循环,循环询问状态
/// </summary>
private void MainLoop()
{
ThreadManage.GetInstance.StartLong(new Action(() =>
{
if (!free)
{
commProxy.SendData(cmdAsk);
}
Thread.Sleep(200);
}),"咖啡机询问线程");

ThreadManage.GetInstance.StartLong(new Action(() =>
{
ResolveMsg();
}),"咖啡机解析线程");
}

private void ResolveMsg()
{
List<byte> temp = new List<byte>();
//一系列解包
while (dataStorage.GetSize() > 0)
{
byte item = dataStorage.GetData();
List<byte> data = new List<byte>() { item };
if (Encoding.ASCII.GetString(data.ToArray()) == ":")
{
temp.Add(item);
while (temp.Count < 32)
{
temp.Add(dataStorage.GetData());
}
List<byte> vs = new List<byte>() { temp[temp.Count - 4],temp[temp.Count - 3],temp[temp.Count - 2],temp[temp.Count - 1] };

//帧尾
if (Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\\r\\n" || Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\r\n")
{
var package = Encoding.ASCII.GetString(temp.ToArray());
ProcessMsg(package);
}
}
continue;
}
Thread.Sleep(5);
}

public void ProcessMsg(string data)
{
try
{
SystemStatusModel systemStatus = new K95Command().StateResolution(data);
if (systemStatus != null)
{
status["temStatus"] = systemStatus.temStatus;
status["drinkType"] = systemStatus.drinkType;
status["taskIndex"] = systemStatus.taskIndex;
status["progress"] = systemStatus.progress;
status["faultMessage"] = systemStatus.faultMessage;
status["upkeepMessage"] = systemStatus.upkeepMessage;
if (systemStatus.faultMessage.IsFault() || systemStatus.upkeepMessage.IsUpkeep())
IsWork=false;
else
IsWork=true;
}

}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
}

protected override void InitStatus()
{
status["temStatus"] = K95SysTemStatus.空闲状态;
status["drinkType"] = DrinkType.意式;
status["taskIndex"] = TaskIndex.无任务;
status["progress"] = 0;
status["faultMessage"] = new FaultMessage(0x00,0x00);
status["upkeepMessage"] = new UpkeepMessage(0x00);

}

public override void Init()
{
commProxy = new SerialPortClient(PortName,(BaudRates)Enum.Parse(typeof(BaudRates),BaudRate));
commProxy.SetDataStorage(dataStorage);

//string sdas = ":010510000000000000000000F3A\r\n";
//byte[] sdsd = new K95Command().ascii2Hex(sdas);
//dataStorage.PutData(sdsd);
//ResolveMsg();

//伽乐美咖啡机制作
EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_MakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
{
try
{
free = true;
Thread.Sleep(200);
byte[] data=command.ReturnsCommandData(K95CommandEnum.配方咖啡制作.GetString(),new RecipeModel().Packe(((KLMCoffee_MakeCoffeeEvent)@event).DrinkCode));
commProxy.SendData(data);
Thread.Sleep(200);
free = false;
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
});

//伽乐美咖啡机取消制作咖啡
EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CancelMakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
{
try
{
free = true;
Thread.Sleep(200);
byte[] data = command.ReturnsCancelMake();
commProxy.SendData(data);
Thread.Sleep(200);
free = false;
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
});

//伽乐美咖啡机清洗冲泡器
EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashCPJEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
{
try
{
free = true;
Thread.Sleep(200);
byte[] data = command.ReturnsWashCPJ();
commProxy.SendData(data);
Thread.Sleep(200);
free = false;
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
});

//伽乐美咖啡机放杯确认
EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CupIsOKEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
{
try
{
free = true;
Thread.Sleep(200);
byte[] data = command.ReturnsCupIsOK();
commProxy.SendData(data);
Thread.Sleep(200);
free = false;
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
});


//伽乐美咖啡机清洗奶沫器
EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
{
try
{
free = true;
Thread.Sleep(200);
byte[] data = command.ReturnsWashNMJ();
commProxy.SendData(data);
Thread.Sleep(200);
free = false;
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
});


//伽乐美咖啡机清洗奶沫器确认
EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJIsOKEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
{
try
{
free = true;
Thread.Sleep(200);
byte[] data = command.ReturnsWashNMJIsOK();
commProxy.SendData(data);
Thread.Sleep(200);
free = false;
}
catch (Exception ex)
{
MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
});
}




+ 12
- 0
BPASmartClient.KLMCoffee/Protocal/FaultMessage.cs Bestand weergeven

@@ -92,5 +92,17 @@ namespace BPASmartClient.KLMCoffee.Protocal
ERROR6 = GetBitValue(valueH,6);
牛奶已用尽 = GetBitValue(valueH,7);
}

public bool IsFault()
{
foreach (System.Reflection.PropertyInfo info in this.GetType().GetProperties())
{
if ((bool)info.GetValue(this) == true)
{
return true;
}
}
return false;
}
}
}

+ 7
- 7
BPASmartClient.KLMCoffee/Protocal/K95Command.cs Bestand weergeven

@@ -1,4 +1,5 @@
using System;
using BPASmartClient.Model.咖啡机.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
@@ -84,7 +85,7 @@ namespace BPASmartClient.KLMCoffee.Protocal
/// <summary>
/// 返回放杯确认
/// </summary>
public byte[] ReturnsIsOK()
public byte[] ReturnsCupIsOK()
{
try
{
@@ -127,7 +128,7 @@ namespace BPASmartClient.KLMCoffee.Protocal
}

/// <summary>
/// 清洗奶沫器
/// 清洗奶沫器确认
/// </summary>
public byte[] ReturnsWashNMJIsOK()
{
@@ -164,16 +165,15 @@ namespace BPASmartClient.KLMCoffee.Protocal
/// :010510000000000000000000F3A\r\n
/// </summary>
/// <param name="message"></param>
public void StateResolution(string message)
public SystemStatusModel StateResolution(string message)
{
try
{
SystemStatusModel systemStatus = new SystemStatusModel(message);
return new SystemStatusModel(message);
}
catch (Exception)
{

throw;
return null;
}
}
#endregion


+ 2
- 1
BPASmartClient.KLMCoffee/Protocal/K95commandEnumExtensions.cs Bestand weergeven

@@ -1,4 +1,5 @@
using System;
using BPASmartClient.Model.咖啡机.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


+ 2
- 1
BPASmartClient.KLMCoffee/Protocal/RecipeModel.cs Bestand weergeven

@@ -1,4 +1,5 @@
using System;
using BPASmartClient.Model.咖啡机.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


+ 49
- 32
BPASmartClient.KLMCoffee/Protocal/SystemStatusModel.cs Bestand weergeven

@@ -1,4 +1,5 @@
using System;
using BPASmartClient.Model.咖啡机.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -46,45 +47,61 @@ namespace BPASmartClient.KLMCoffee.Protocal
public SystemStatusModel(string cmd)
{
// 去掉字符串首尾处的空格
if (string.IsNullOrEmpty(cmd) || cmd.Length < 21)
if (string.IsNullOrEmpty(cmd) || cmd.Length < 32)
return;
string str = cmd.Trim().Substring(5,cmd.Trim().Length - 7).Replace(@"\R","").Replace("\r","").Replace(@"\n","").Replace(@"\N","");
string systemStatus = str.Substring(0,4);//系统状态
string makeSta = str.Substring(4,4);//制作状态
string makeProgress = str.Substring(8,4);//制作进度
string makeProgress1 = str.Substring(12,4);//制作进度
byte ErrorH = byte.Parse(str.Substring(16,2));//故障信息
byte ErrorL = byte.Parse(str.Substring(18,2));//故障信息
byte InFormation = byte.Parse(str.Substring(20,1));//保养信息

foreach (var item in Enum.GetNames(typeof(K95SysTemStatus)))
string str = cmd.Trim().Replace(@"\","").Replace("r","").Replace(@"R","").Replace(@"n","").Replace(@"N","");
int length = str.Length;
str = str.Substring(5,length - 7);
try
{
if (systemStatus.Substring(0,1) == ((K95SysTemStatus)Enum.Parse(typeof(K95SysTemStatus),item)).GetString())
temStatus = ((K95SysTemStatus)Enum.Parse(typeof(K95SysTemStatus),item));
}
string systemStatus = str.Substring(0,4);//系统状态
string makeSta = str.Substring(4,4);//制作状态
string makeProgress = str.Substring(8,4);//制作进度
string makeProgress1 = str.Substring(12,4);//制作进度
byte ErrorH = (byte)Convert.ToInt32(str.Substring(16,2),16);//故障信息
byte ErrorL = (byte)Convert.ToInt32(str.Substring(18,2),16);//故障信息
byte InFormation = (byte)Convert.ToInt32(str.Substring(20,1),16);//保养信息

foreach (var item in Enum.GetNames(typeof(DrinkType)))
{
if ("0" + makeSta.Substring(0,1) == ((DrinkType)Enum.Parse(typeof(DrinkType),item)).GetString())
drinkType = ((DrinkType)Enum.Parse(typeof(DrinkType),item));
}
System.Array values = System.Enum.GetValues(typeof(K95SysTemStatus));
string[] nameList = System.Enum.GetNames(typeof(K95SysTemStatus));

foreach (var item in Enum.GetNames(typeof(MakeStatus)))
{
if (makeSta.Substring(1,1) == ((MakeStatus)Enum.Parse(typeof(MakeStatus),item)).GetString())
makeStatus = ((MakeStatus)Enum.Parse(typeof(MakeStatus),item));
}
string[] sds = Enum.GetNames(typeof(K95SysTemStatus));

foreach (var item in Enum.GetNames(typeof(K95SysTemStatus)))
{
if (systemStatus.Substring(0,1) == ((K95SysTemStatus)Enum.Parse(typeof(K95SysTemStatus),item)).GetString())
temStatus = ((K95SysTemStatus)Enum.Parse(typeof(K95SysTemStatus),item));
}

foreach (var item in Enum.GetNames(typeof(TaskIndex)))
foreach (var item in Enum.GetNames(typeof(DrinkType)))
{
if ("0" + makeSta.Substring(0,1) == ((DrinkType)Enum.Parse(typeof(DrinkType),item)).GetString())
drinkType = ((DrinkType)Enum.Parse(typeof(DrinkType),item));
}

foreach (var item in Enum.GetNames(typeof(MakeStatus)))
{
if (makeSta.Substring(1,1) == ((MakeStatus)Enum.Parse(typeof(MakeStatus),item)).GetString())
makeStatus = ((MakeStatus)Enum.Parse(typeof(MakeStatus),item));
}

foreach (var item in Enum.GetNames(typeof(TaskIndex)))
{
if (makeSta.Substring(2,1) == ((TaskIndex)Enum.Parse(typeof(TaskIndex),item)).GetString())
taskIndex = ((TaskIndex)Enum.Parse(typeof(TaskIndex),item));
}

if (makeProgress1 != "0000")
progress = Convert.ToInt32(makeProgress,16) / Convert.ToInt32(makeProgress1,16);
faultMessage = new FaultMessage(ErrorL,ErrorH);
upkeepMessage = new UpkeepMessage(InFormation);
}
catch (Exception ex)
{
if (makeSta.Substring(2,1) == ((TaskIndex)Enum.Parse(typeof(TaskIndex),item)).GetString())
taskIndex = ((TaskIndex)Enum.Parse(typeof(TaskIndex),item));

}

if (makeProgress1 != "0000")
progress = Convert.ToInt32(makeProgress,16) / Convert.ToInt32(makeProgress1,16);
faultMessage = new FaultMessage(ErrorL,ErrorH);
upkeepMessage = new UpkeepMessage(InFormation);
}
}
}

+ 12
- 0
BPASmartClient.KLMCoffee/Protocal/UpkeepMessage.cs Bestand weergeven

@@ -32,5 +32,17 @@ namespace BPASmartClient.KLMCoffee.Protocal
咖啡机需要除垢 = GetBitValue(value,1);
需要更换滤芯 = GetBitValue(value,0);
}

public bool IsUpkeep()
{
foreach (System.Reflection.PropertyInfo info in this.GetType().GetProperties())
{
if ((bool)info.GetValue(this) == true)
{
return true;
}
}
return false;
}
}
}

BPASmartClient.KLMCoffee/Protocal/DrinkType.cs → BPASmartClient.Model/咖啡机/Enum/DrinkType.cs Bestand weergeven

@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.KLMCoffee.Protocal
namespace BPASmartClient.Model.咖啡机.Enum
{
/// <summary>
/// 饮品类型

+ 32
- 4
BPASmartClient.Model/咖啡机/KLMCoffeeEvent.cs Bestand weergeven

@@ -1,4 +1,5 @@
using System;
using BPASmartClient.Model.咖啡机.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -17,7 +18,10 @@ namespace BPASmartClient.Model
/// </summary>
public class KLMCoffee_MakeCoffeeEvent :BaseEvent
{

/// <summary>
/// 饮品类型
/// </summary>
public DrinkType DrinkCode { get; set; }
}

/// <summary>
@@ -29,9 +33,33 @@ namespace BPASmartClient.Model
}

/// <summary>
/// 伽乐美咖啡机模式设置
/// 伽乐美咖啡机清洗冲泡器
/// </summary>
public class KLMCoffee_WashCPJEvent :BaseEvent
{

}

/// <summary>
/// 伽乐美咖啡机放杯确认
/// </summary>
public class KLMCoffee_CupIsOKEvent :BaseEvent
{

}

/// <summary>
/// 伽乐美咖啡机清洗奶沫器
/// </summary>
public class KLMCoffee_WashNMJEvent :BaseEvent
{

}

/// <summary>
/// 伽乐美咖啡机清洗奶沫器确认
/// </summary>
public class KLMCoffee_CoffeeCommEvent :BaseEvent
public class KLMCoffee_WashNMJIsOKEvent :BaseEvent
{

}


+ 7
- 0
BPASmartClient/DeviceInfo.xml Bestand weergeven

@@ -21,6 +21,13 @@
<BaudRate>9600</BaudRate>
</Parameters>
</Peripheral>
<Peripheral Module="BPASmartClient.KLMCoffee.CoffeeMachine">
<Parameters>
<PortName>COM5</PortName>
<BaudRate>9600</BaudRate>
</Parameters>
</Peripheral>
</Peripherals>
</Device>
</BPADevices>

Laden…
Annuleren
Opslaan