From 776a1ddce2c380aa753054a1284b95615f42466c Mon Sep 17 00:00:00 2001
From: maqiao <625215135@qq.com>
Date: Fri, 30 Aug 2024 17:25:07 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0SharpPromise=E5=BC=95?=
=?UTF-8?q?=E7=94=A8=E5=B9=B6=E4=BF=AE=E6=94=B9=E5=BC=80=E6=8A=BD=E5=B1=89?=
=?UTF-8?q?=E5=BE=AA=E7=8E=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
DM_Weight/App.config | 2 +-
DM_Weight/DM_Weight - Backup.csproj | 127 ++++++++
DM_Weight/DM_Weight.csproj | 1 +
DM_Weight/Models/ChannelStock.cs | 3 +
DM_Weight/Models/OrderTakeVo.cs | 32 ++
DM_Weight/Port/PortUtil.cs | 6 +-
DM_Weight/Port/PromiseUtil.cs | 44 +++
.../ViewModels/OrderTakeDialogViewModel.cs | 275 +++++++++++++++++-
DM_Weight/Views/Dialog/OrderTakeDialog.xaml | 2 +-
9 files changed, 483 insertions(+), 9 deletions(-)
create mode 100644 DM_Weight/DM_Weight - Backup.csproj
create mode 100644 DM_Weight/Models/OrderTakeVo.cs
create mode 100644 DM_Weight/Port/PromiseUtil.cs
diff --git a/DM_Weight/App.config b/DM_Weight/App.config
index ddc7c74..d435570 100644
--- a/DM_Weight/App.config
+++ b/DM_Weight/App.config
@@ -36,7 +36,7 @@
-
+
diff --git a/DM_Weight/DM_Weight - Backup.csproj b/DM_Weight/DM_Weight - Backup.csproj
new file mode 100644
index 0000000..feb0e7b
--- /dev/null
+++ b/DM_Weight/DM_Weight - Backup.csproj
@@ -0,0 +1,127 @@
+
+
+
+ WinExe
+ net6.0-windows
+ enable
+ true
+
+ 毒麻管理程序
+ Images\favicon.ico
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ tlbimp
+ 0
+ 1
+ fe9ded34-e159-408e-8490-b720a5e632c7
+ 0
+ false
+ False
+
+
+ tlbimp
+ 0
+ 6
+ 4018f953-1bfe-441e-8a04-dc8ba1ff060e
+ 0
+ false
+ False
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Always
+
+
+
+
+
+ Never
+
+
+ Always
+
+
+ Always
+
+
+ Always
+
+
+ Always
+
+
+ Always
+
+
+ Always
+
+
+ Always
+
+
+
+
+
+ $(DefaultXamlRuntime)
+
+
+
+
+
+
+
+
diff --git a/DM_Weight/DM_Weight.csproj b/DM_Weight/DM_Weight.csproj
index a7f1eb6..feb0e7b 100644
--- a/DM_Weight/DM_Weight.csproj
+++ b/DM_Weight/DM_Weight.csproj
@@ -70,6 +70,7 @@
+
diff --git a/DM_Weight/Models/ChannelStock.cs b/DM_Weight/Models/ChannelStock.cs
index 8bcd816..ad044eb 100644
--- a/DM_Weight/Models/ChannelStock.cs
+++ b/DM_Weight/Models/ChannelStock.cs
@@ -185,5 +185,8 @@ namespace DM_Weight.Models
[Navigate(NavigateType.ManyToOne, nameof(Chnguid))]
public ChannelList ChannelLst { get => _channelList; set => SetProperty(ref _channelList, value); }
+ [SugarColumn(IsIgnore = true)]
+ public int DrawerState { get; set; } = 0;
+
}
}
\ No newline at end of file
diff --git a/DM_Weight/Models/OrderTakeVo.cs b/DM_Weight/Models/OrderTakeVo.cs
new file mode 100644
index 0000000..b848a49
--- /dev/null
+++ b/DM_Weight/Models/OrderTakeVo.cs
@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DM_Weight.Models
+{
+ public class OrderTakeVo
+ {
+ public DrugInfo Drug { get; set; }
+
+ public OrderDetail OrderDetail { get; set; }
+
+ public ChannelStock ChannelStock { get; set; }
+
+ public int StockQuantity { get; set; }
+
+ public int Quantity { get; set; }
+
+ public int GetQuantity { get; set; } = 0;
+
+ public int Status { get; set; } = 0;
+
+ public int[] BeforeQuantity { get; set; } = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+
+ public int[] AfterQuantity { get; set; } = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+
+ public int AddQuantity { get; set; } = 0;
+
+ }
+}
diff --git a/DM_Weight/Port/PortUtil.cs b/DM_Weight/Port/PortUtil.cs
index 134d1f3..80537a0 100644
--- a/DM_Weight/Port/PortUtil.cs
+++ b/DM_Weight/Port/PortUtil.cs
@@ -71,7 +71,7 @@ namespace DM_Weight.Port
private int[] AfterQuantity { get; set; } = new int[] { };
// 整体流程状态
- private int statue { get; set; } = 0;
+ public int statue { get; set; } = 0;
// 是否正在操作中
@@ -696,7 +696,7 @@ namespace DM_Weight.Port
return await GetBufferByPort(drawerSerial, 11);
}
- private bool DrawerState(int[] r)
+ public bool DrawerState(int[] r)
{
int index = DrawerNo > 8 ? DrawerNo - 7 : DrawerNo + 1;
return r[index] == 0;
@@ -769,7 +769,7 @@ namespace DM_Weight.Port
canBusSerial.Write(buffer, 0, 8);
}
- private int[] CheckStorageStatus(int[] data)
+ public int[] CheckStorageStatus(int[] data)
{
int a = data[0];
int b = data[1];
diff --git a/DM_Weight/Port/PromiseUtil.cs b/DM_Weight/Port/PromiseUtil.cs
new file mode 100644
index 0000000..e192517
--- /dev/null
+++ b/DM_Weight/Port/PromiseUtil.cs
@@ -0,0 +1,44 @@
+using SharpPromise;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DM_Weight.Port
+{
+ public class PromiseUtil
+ {
+
+ public int _delay { get; set; }
+
+ public T? _data { get; set; }
+
+ public async Task taskAsyncLoop(int delay, T data, Action, Action, Action> action)
+ {
+ _data = data;
+ _delay = 0;
+ while (_delay >= 0)
+ {
+ await new Promise(async (Action onResolve, Action onReject) =>
+ {
+ await Task.Delay(_delay);
+ try
+ {
+ await Task.Run(() => action(this, onResolve, onReject));
+ }
+ catch (Exception ex)
+ {
+ onReject();
+ }
+ }).Then(() =>
+ {
+ _delay = delay;
+ }).Catch((Exception e) =>
+ {
+ _delay = -1;
+ });
+ }
+ }
+ }
+}
diff --git a/DM_Weight/ViewModels/OrderTakeDialogViewModel.cs b/DM_Weight/ViewModels/OrderTakeDialogViewModel.cs
index 112f0d3..bc8bea2 100644
--- a/DM_Weight/ViewModels/OrderTakeDialogViewModel.cs
+++ b/DM_Weight/ViewModels/OrderTakeDialogViewModel.cs
@@ -21,6 +21,7 @@ using DM_Weight.Port;
using DM_Weight.select;
using DM_Weight.util;
using System.Threading;
+using System.Windows.Markup;
namespace DM_Weight.ViewModels
{
@@ -226,7 +227,7 @@ namespace DM_Weight.ViewModels
DialogParameters dialogParameters = new DialogParameters();
dialogParameters.Add("msgInfo", msg);
DialogServiceExtensions.ShowDialogHost(_dialogService, "ShowMessageDialog", dialogParameters, "RootDialog");
-
+
}
else
{
@@ -258,7 +259,264 @@ namespace DM_Weight.ViewModels
});
}
+ public DelegateCommand OpenDrawer_New
+ {
+ get => new DelegateCommand(async () =>
+ {
+ OpenDrawerAction();
+ });
+ }
+ async Task OpenDrawerAction()
+ {
+ this.Status = 1;
+ // 解析需要打开的抽屉列表
+ //List drawerNos = this.data.GroupBy(it => it.ChannelStock.DrawerNo).Select(it => it.First()).ToList();
+
+ // 根据抽屉类型来决定打开前是否需要查询数量
+ var promiseUtil = new PromiseUtil();
+ enumerable = ChannelStocks.GroupBy(cs => cs.DrawerNo, cs => cs);
+ enumerator = enumerable.GetEnumerator();
+
+ _portUtil.WindowName = "OrderTakeDrugWindow";
+ _portUtil.BoardType = ChannelStocks.Count > 0 ? ChannelStocks[0].BoardType : 1;
+ _portUtil.ColNos = ChannelStocks.Select(it => it.ColNo).ToArray();
+ enumerator.MoveNext();
+
+ await promiseUtil.taskAsyncLoop(500, 0, async (options, next, stop) =>
+ {
+ //var orderTakeVo = drawerNos[options._data];
+ //orderTakeVo.ChannelStock.DrawerNo;
+ IGrouping groupingStock = enumerator.Current;
+ var drawerNo = groupingStock.Key;
+ _portUtil.DrawerNo = groupingStock.Key;
+ int[] BeforeQuantity = new int[] { };
+ List channelStocks = groupingStock.ToList();
+ try
+ {
+ if (this.Status == 0)
+ {
+ stop();
+ }
+ // 开启抽屉
+ else if (this.Status == 1)
+ {
+ // 储物箱直接开
+ if (channelStocks[0].BoardType == 4)
+ {
+ if (channelStocks[0].DrawerState == 0)
+ {
+ byte[] resultOpen = await _portUtil.OpenStorage();
+
+ int[] rOpen = resultOpen.Select(it => Convert.ToInt32(it)).ToArray();
+ if (rOpen[4] != 0)
+ {
+ logger.Info($"储物箱使能成功");
+ channelStocks[0].DrawerState = 1;
+ next();
+ }
+ else
+ {
+ string _WindowName = "OrderTakeDialog";
+ // 重新初始化数据
+ _portUtil.ResetData();
+ // 指令发送错误,未打开
+ // 返回消息 抽屉打开失败
+ _eventAggregator.GetEvent().Publish(new util.DeviceMsg()
+ {
+ EventType = util.EventType.OPENERROR,
+ WindowName = "OrderTakeDialog",
+ Message = "储物箱使能失败"
+ });
+ logger.Info($"储物箱使能失败");
+ _portUtil.ResetData();
+ stop();
+ }
+
+ }
+ if (channelStocks[0].DrawerState == 1)
+ {
+ // 查询锁状态
+ byte[] resultBackDoor = await _portUtil.BackDoorState();
+
+ int[] rBackDoor = resultBackDoor.Select(it => Convert.ToInt32(it)).ToArray();
+ int[] lockStates = _portUtil.CheckStorageStatus(rBackDoor.Skip(3).Take(2).ToArray());
+ int[] lightStates = _portUtil.CheckStorageStatus(rBackDoor.Skip(5).Take(2).ToArray());
+ // 锁处于关闭状态
+ if (lockStates[Convert.ToInt32(ConfigurationManager.AppSettings["StorageBoxAddr"]) - 1] == 0)
+ {
+ // 锁还未打开过
+ if (_portUtil.statue == 0)
+ {
+ // 指示灯不闪烁了,说明规定时间内没有开锁,需要给前台提示
+ if (lightStates[Convert.ToInt32(ConfigurationManager.AppSettings["StorageBoxAddr"]) - 1] == 0)
+ {
+ // 打开失败
+ logger.Info($"储物箱指示灯不闪烁,使能过期");
+ enumerator.MoveNext();
+ next();
+ }
+ else
+ {
+ channelStocks[0].DrawerState = 1;
+ next();
+ }
+ }
+ else if (_portUtil.statue == 1)
+ {
+
+ // 锁是打开状态现在关闭了,说明操作完成了,告诉前台锁已关闭
+ _portUtil.statue = 2;
+
+ string _WindowName = "OrderTakeDialog";
+ // 重新初始化数据
+ //ResetData();
+ _eventAggregator.GetEvent().Publish(new util.DeviceMsg()
+ {
+ EventType = util.EventType.DRAWERCLOSE,
+ WindowName = _WindowName,
+ });
+ logger.Info($"储物箱关闭");
+ if (options._data == (enumerable.Count() - 1))
+ {
+ _portUtil.SpeakAsync($"取药完成,请,点击完成按钮进行确认");
+ this.Status = 3;
+ channelStocks[0].DrawerState = 0;
+ stop();
+ }
+ else
+ {
+ channelStocks[0].DrawerState = 0;
+ enumerator.MoveNext();
+ next();
+ }
+ }
+ }
+ }
+ }
+
+
+ if (channelStocks[0].DrawerState == 0)
+ {
+ // 判断是否为单支抽屉
+ if (channelStocks[0].BoardType == 2)
+ {
+ byte[] quantity = await _portUtil.CheckQuantityByDrawer();
+ BeforeQuantity = quantity.Select(it => Convert.ToInt32(it)).ToArray().Skip(3).Take(9).ToArray();
+ logger.Info($"单支抽屉,开抽屉前检测数量【{string.Join(",", BeforeQuantity)}】");
+
+ await _portUtil.HasLightOnByCol();
+ }
+ // 药盒
+ if (channelStocks[0].BoardType == 3)
+ {
+ // 药盒指示灯使能
+ await _portUtil.BoxLockLightOn();
+ }
+ if (channelStocks[0].BoardType == 6)
+ {
+ // 药盒指示灯使能
+ await _portUtil.BoxLockLightOn2();
+ }
+ byte[] buffer = await _portUtil.OpenDrawer();
+ int[] r = buffer.Select(it => Convert.ToInt32(it)).ToArray();
+
+ logger.Info($"OpenDrawer{string.Join(",", r)}");
+ if (_portUtil.DrawerState(r))
+ {
+ _portUtil.SpeakAsync($"{drawerNo}号抽屉已经打开,请,取药");
+ channelStocks[0].DrawerState = 1;
+ next();
+ }
+ else
+ {
+ AlertMsg alertMsg = new AlertMsg
+ {
+ Message = $"抽屉【{drawerNo}】打开失败,请检测硬件",
+ Type = MsgType.ERROR,
+ };
+ _eventAggregator.GetEvent().Publish(alertMsg);
+
+ logger.Info($"抽屉打开失败");
+ _portUtil.ResetData();
+ stop();
+ }
+ }
+ // 检测状态
+ else if (channelStocks[0].DrawerState == 1)
+ {
+ // 查询抽屉是否为关闭状态
+ byte[] buffer = await _portUtil.CheckDrawerStatus();
+
+ int[] r = buffer.Select(it => Convert.ToInt32(it)).ToArray();
+
+ logger.Info($"GetDrawerStatus{string.Join(",", r)}");
+ // 抽屉没有关闭
+ if (_portUtil.DrawerState(r))
+ {
+ if (channelStocks[0].BoardType == 2)
+ {
+ byte[] quantity = await _portUtil.CheckQuantityByDrawer();
+ int[] AfterQuantity = quantity.Select(it => Convert.ToInt32(it)).ToArray().Skip(3).Take(9).ToArray();
+ logger.Info($"AfterQuantity{string.Join(",", AfterQuantity)}");
+ logger.Info($"单支抽屉,抽屉未关检测数量【{string.Join(",", AfterQuantity)}】");
+
+ ChannelStocks.ForEach(cl =>
+ {
+ if (cl.DrawerNo == drawerNo)
+ {
+ logger.Info($"单支抽屉【{drawerNo}】,应取药品数量【{cl.Quantity}】,现实取数量【{BeforeQuantity[cl.ColNo - 1] - AfterQuantity[cl.ColNo - 1]}】");
+ }
+
+ });
+ }
+ next(); // continue iteration
+ }
+ else
+ {
+ ChannelStocks.ForEach(cl =>
+ {
+ if (cl.DrawerNo == drawerNo)
+ {
+ cl.TakeQuantity = cl.Quantity;
+
+ }
+ });
+ channelStocks[0].DrawerState = 2;
+ if (options._data == (enumerable.Count() - 1))
+ {
+ _portUtil.SpeakAsync($"取药完成,请,点击完成按钮进行确认");
+ this.Status = 3;
+ channelStocks[0].DrawerState = 0;
+ stop();
+ }
+ else
+ {
+ options._data += 1;
+ channelStocks[0].DrawerState = 0;
+ enumerator.MoveNext();
+ next();
+ }
+ }
+ }
+
+
+ }
+ }
+ catch (Exception e)
+ {
+ //_portUtil.ResetData();
+ logger.Info($"处方取药发生错误,{e.Message}"); AlertMsg alertMsg = new AlertMsg
+ {
+ Message = $"处方取药发生错误{e.Message}",
+ Type = MsgType.ERROR,
+ };
+ _eventAggregator.GetEvent().Publish(alertMsg);
+
+ stop();
+ }
+ });
+ }
private void OpenOneByOne()
{
IGrouping grouping = enumerator.Current;
@@ -382,7 +640,7 @@ namespace DM_Weight.ViewModels
};
_eventAggregator.GetEvent().Publish(alertMsg);
}
- if(!f.IsSuccess)
+ if (!f.IsSuccess)
{
AlertMsg alertMsg = new AlertMsg
{
@@ -422,8 +680,17 @@ namespace DM_Weight.ViewModels
{
get => new DelegateCommand(() =>
{
- _portUtil.ResetData();
- Status = 0;
+ //Status = 0;
+ //_portUtil.statue = 0;
+ //_portUtil.ResetData();
+
+
+ if (Status != 0)
+ {
+ _portUtil.ResetData();
+ Status = 0;
+ }
+ RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
});
}
diff --git a/DM_Weight/Views/Dialog/OrderTakeDialog.xaml b/DM_Weight/Views/Dialog/OrderTakeDialog.xaml
index 651c5e2..45527ea 100644
--- a/DM_Weight/Views/Dialog/OrderTakeDialog.xaml
+++ b/DM_Weight/Views/Dialog/OrderTakeDialog.xaml
@@ -198,7 +198,7 @@
materialDesign:ButtonProgressAssist.IsIndicatorVisible="{Binding Status, Converter={StaticResource StatusConverter}, ConverterParameter=opearBtnLoading}"
materialDesign:ButtonProgressAssist.IsIndeterminate="{Binding Status, Converter={StaticResource StatusConverter}, ConverterParameter=opearBtnLoading}"
Content="取药"
- Command="{Binding OpenDrawer}">
+ Command="{Binding OpenDrawer_New}">