XiChang/DM_Weight/ViewModels/BiaoDingDialogViewModel.cs

283 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DM_Weight.Common;
using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using log4net;
using log4net.Repository.Hierarchy;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DM_Weight.ViewModels
{
public class BiaoDingDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime
{
private readonly ILog logger = LogManager.GetLogger(typeof(BiaoDingWindowViewModel));
public string Title => "标定";
public bool KeepAlive => false;
private string titleStr;
public string TitleStr
{
get => titleStr;
set { SetProperty(ref titleStr, value); }
}
private ChannelStock channelStock;
private string WindowName = "BiaoDingDialog";
private string strMessage;
public string StrMessage
{
get => strMessage;
set => SetProperty(ref strMessage, value);
}
private int _status = 1;
public int Status { get => _status; set => SetProperty(ref _status, value); }
//标定数量
private int _bdQuantity = 0;
public int BDQuantity
{
get => _bdQuantity;
set => SetProperty(ref _bdQuantity, value);
}
public event Action<IDialogResult> RequestClose;
public bool CanCloseDialog()
{
return true;
}
public void OnDialogClosed()
{
// 取消消息订阅
//_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
}
public void OnDialogOpened(IDialogParameters parameters)
{
//_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
channelStock = parameters.GetValue<ChannelStock>("channelStock");
if (channelStock != null)
{
TitleStr = $"正在标定{channelStock.ColNo}号库位";
if (channelStock.Quantity > 0)
{
StrMessage = "库位库存不为零,请取出药品后点击【清空】按钮,清空完成后输入标定数量并放入对应数量药品并根据下一步提示操作";
}
else
{
StrMessage = "输入标定数量并放入对应数量药品后点击【标定】按钮";
//无库存发送计数清零指令
_portUtil.ClearCount(channelStock.DrawerNo, channelStock.ColNo);
Status = 3;
}
}
}
private PortUtil _portUtil;
IEventAggregator _eventAggregator;
public BiaoDingDialogViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
{
_portUtil = portUtil;
_eventAggregator = eventAggregator;
}
//void DoMyPrismEvent(DeviceMsg msg)
//{
// if (msg.WindowName.Equals(WindowName))
// {
// IGrouping<int, ChannelStock> grouping = enumerator.Current;
// int DrawerNo = grouping.Key;
// List<ChannelStock> channelStocks = grouping.ToList();
// switch (msg.EventType)
// {
// // 抽屉打开
// case EventType.DRAWEROPEN:
// if (Status == 1)
// {
// if (channelStocks[0].process == 1)
// {
// channelStocks.ForEach(it => it.process = 2);
// }
// }
// //是冰箱抽屉则开冰箱抽屉时发送延迟报警指令
// CheckIsFridgeOpen();
// break;
// // 抽屉关闭
// case EventType.DRAWERCLOSE:
// if (Status == 1)
// {
// if (channelStocks[0].process == 2)
// {
// channelStocks.ForEach(it => it.process = 3);
// }
// IGrouping<int, ChannelStock> groupingBefore = enumerator.Current;
// int DrawerNoBefore = groupingBefore.Key;
// if (enumerator.MoveNext())
// {
// IGrouping<int, ChannelStock> groupingAfter = enumerator.Current;
// int DrawerNoAfter = groupingAfter.Key;
// if (DrawerNoBefore < 9 && DrawerNoAfter > 8)
// {
// Thread.Sleep(50);
// }
// OpenOneByOne();
// }
// // 已经全部取出
// else
// {
// Status = 3;
// }
// }
// //是冰箱抽屉则开冰箱抽屉时发送延迟报警指令
// CheckIsFridgeClose();
// break;
// // 数量变化
// case EventType.UPDATEQUANTITY:
// if (Status == 1)
// {
// logger.Info($"抽屉【{DrawerNo}】库位药品数量【{msg.Quantitys}】");
// }
// break;
// // 打开失败
// case EventType.OPENERROR:
// AlertMsg alertMsg = new AlertMsg
// {
// Message = msg.Message,
// Type = MsgType.ERROR
// };
// _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
// Status = 0;
// break;
// }
// }
//}
//标定
public DelegateCommand BiaoDingCommand
{
get => new DelegateCommand(async () =>
{
if (BDQuantity <= 0)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "请输入标定数量",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
return;
}
try
{
if (Status == 3)
{
//提示输入标定数量发26指令
_portUtil.SetNumCount(channelStock.DrawerNo, channelStock.ColNo, BDQuantity);
Status = 4;
await Task.Delay(200);
//发27指令查询数量是否写标定时写入的数量一致一致则标定成功不一致则标定失败
int stock = await _portUtil.CheckQuantityForBiaoDing(channelStock.ColNo);
if (!(stock == BDQuantity))
{
int i = 1;
while (i<=50&&i>0)
{
await Task.Delay(200);
stock = await _portUtil.CheckQuantityForBiaoDing(channelStock.ColNo);
if (stock == BDQuantity)
{
logger.Info($"标定完成{stock}{BDQuantity},{i}"); //标定成功
AlertMsg alertMsg = new AlertMsg
{
Message = "标定成功",
Type = MsgType.SUCCESS,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
i = 0;
}
else
{
i++;
logger.Info($"标定数量不一致{stock}{BDQuantity}");
}
}
RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
if (i == 51)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "标定失败",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
else
{
//标定成功
AlertMsg alertMsg = new AlertMsg
{
Message = "标定成功",
Type = MsgType.SUCCESS,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
logger.Info($"标定完成{stock}{BDQuantity}");
}
}
}
catch (Exception e)
{
logger.Error($"标定异常{e.Message}");
}
});
}
//清零
public DelegateCommand ClearCommand
{
get => new DelegateCommand(async () =>
{
//发计数请零指令
_portUtil.ClearCount(channelStock.DrawerNo, channelStock.ColNo);
Status = 3;
});
}
//取消
public DelegateCommand CancelCommand
{
get => new DelegateCommand(() =>
{
RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
});
}
//关闭
public DelegateCommand BtnCloseCommand
{
get => new DelegateCommand(() =>
{
RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
});
}
}
}