using MaterialTraceability.Business;
using MaterialTraceability.Business.Impl;
using MaterialTraceability.Entity.DAO;
using MaterialTraceability.Entity.DTO;
using MaterialTraceability.Entity.Enum;
using MaterialTraceability.SqlSugar;
using MaterialTraceability.SqlSugar.ServiceImpl;
using MaterialTraceabilityUI.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MaterialTraceabilityUI
{
///
/// firstPage.xaml 的交互逻辑
///
public partial class firstPage : UserControl
{
private EquipBusiness equipBusiness = new EquipBusiness();
private IBaseServices logInfoServices = new BaseServices();
private IBaseServices shftInfoService = new BaseServices();
private UpLoadBusiness loadBusiness = UpLoadBusiness.Instance;
private AppConfigDto appConfig = AppConfigDto.Instance;
public firstPage()
{
InitializeComponent();
try
{
if (appConfig.processId.Contains("LY"))
{
MainBusiness.LogRefreshEvent += LogRefresh;
LYSignalReadBusiness.LogRefreshEvent += LogRefresh;
LYSignalReadBusiness.ViewModelRefreshEvent += ProductionParam;
UserControlInit();
refreshThread();
}
}
catch(Exception ex)
{
LogHelperBusiness.LogError("界面异常",ex);
}
}
public void ProductionParam(ViewModelDto viewModelDto)
{
if (viewModelDto.rfidInfo != null)
{
Action action;
switch (viewModelDto.rfidInfo.position)
{
case 0 :
action = () =>
{
this.upMaterial_SFC.Text = "SFC:"+viewModelDto.rfidInfo.sfc;
};
upMaterial_SFC.Dispatcher.BeginInvoke(action);
action = () =>
{
this.upMaterial_RFID.Text = "RFID:"+viewModelDto.rfidInfo.rfid;
};
upMaterial_RFID.Dispatcher.BeginInvoke(action);
action = () =>
{
this.upMaterial_EA.Text = "EA:"+viewModelDto.rfidInfo.ea;
};
upMaterial_EA.Dispatcher.BeginInvoke(action);
break;
case 1:
action = () =>
{
this.Down_SFC_1.Text = "SFC:" + viewModelDto.rfidInfo.sfc;
};
Down_SFC_1.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_RFID_1.Text = "RFID:"+viewModelDto.rfidInfo.rfid;
};
Down_RFID_1.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_EA_1.Text = "EA:"+viewModelDto.rfidInfo.ea;
};
Down_EA_1.Dispatcher.BeginInvoke(action);
break;
case 2:
action = () =>
{
this.Down_SFC_2.Text = "SFC:" + viewModelDto.rfidInfo.sfc;
};
Down_SFC_2.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_RFID_2.Text = "RFID:"+viewModelDto.rfidInfo.rfid;
};
Down_RFID_2.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_EA_2.Text = "EA:"+viewModelDto.rfidInfo.ea;
};
Down_EA_2.Dispatcher.BeginInvoke(action);
break;
case 5:
action = () =>
{
this.Down_SFC_3.Text = "SFC:"+viewModelDto.rfidInfo.sfc;
};
Down_SFC_3.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_RFID_3.Text = "RFID:"+viewModelDto.rfidInfo.rfid;
};
Down_RFID_3.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_EA_3.Text = "EA:"+viewModelDto.rfidInfo.ea;
};
Down_EA_3.Dispatcher.BeginInvoke(action);
break;
case 6:
action = () =>
{
this.Down_SFC_4.Text = "SFC:"+viewModelDto.rfidInfo.sfc;
};
Down_SFC_4.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_RFID_4.Text = "RFID:"+viewModelDto.rfidInfo.rfid;
};
Down_RFID_4.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_EA_4.Text = "EA:"+viewModelDto.rfidInfo.ea;
};
Down_EA_4.Dispatcher.BeginInvoke(action);
break;
case 4:
action = () =>
{
this.Down_SFC_5.Text = "SFC:"+viewModelDto.rfidInfo.sfc;
};
Down_SFC_5.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_RFID_5.Text = "RFID:"+viewModelDto.rfidInfo.rfid;
};
Down_RFID_5.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_EA_5.Text = "EA:"+viewModelDto.rfidInfo.ea;
};
Down_EA_5.Dispatcher.BeginInvoke(action);
break;
case 3:
action = () =>
{
this.Down_SFC_6.Text = "SFC:"+viewModelDto.rfidInfo.sfc;
};
Down_SFC_6.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_RFID_6.Text = "RFID:"+viewModelDto.rfidInfo.rfid;
};
Down_RFID_6.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_EA_6.Text = "EA:"+viewModelDto.rfidInfo.ea;
};
Down_EA_6.Dispatcher.BeginInvoke(action);
break;
case 8:
action = () =>
{
this.Down_SFC_7.Text = "SFC:"+viewModelDto.rfidInfo.sfc;
};
Down_SFC_7.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_RFID_7.Text = "RFID:"+viewModelDto.rfidInfo.rfid;
};
Down_RFID_7.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_EA_7.Text = "EA:"+viewModelDto.rfidInfo.ea;
};
Down_EA_7.Dispatcher.BeginInvoke(action);
break;
case 7:
action = () =>
{
this.Down_SFC_8.Text = "SFC:"+viewModelDto.rfidInfo.sfc;
};
Down_SFC_8.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_RFID_8.Text = "RFID:"+viewModelDto.rfidInfo.rfid;
};
Down_RFID_8.Dispatcher.BeginInvoke(action);
action = () =>
{
this.Down_EA_8.Text = "EA:"+viewModelDto.rfidInfo.ea;
};
Down_EA_8.Dispatcher.BeginInvoke(action);
break;
default:
break;
}
}
}
private void refreshThread()
{
System.Timers.Timer timer = new System.Timers.Timer(1000 * 30);
timer.Elapsed += new System.Timers.ElapsedEventHandler(refreshEquipStatus);
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
}
private void refreshEquipStatus(object source, System.Timers.ElapsedEventArgs e)
{
try
{
equipBusiness.CheckEquipState();
UpdateDeviceStatus();
}catch(Exception ex)
{
LogHelperBusiness.LogError("界面refreshEquipStatus异常",ex);
}
}
private void UpdateDeviceStatus()
{
Action action;
try
{
var equipInfo = equipBusiness.Equiplist.Where(x => x.positionId == 0).FirstOrDefault();
if (equipInfo != null)
{
if (equipInfo.IsConnect)
{
action = () =>
{
this.TestTB.Foreground = Brushes.Green;
};
TestTB.Dispatcher.BeginInvoke(action);
action = () =>
{
string imageUrl = "pack://application:,,,/MaterialTraceabilityUI;component/Assets/Images/正常.png";
BitmapImage bitmapImage = new BitmapImage(new Uri(imageUrl));
this.TestBImage.ImageSource = bitmapImage;
};
TestBImage.Dispatcher.BeginInvoke(action);
}
else
{
action = () =>
{
this.TestTB.Foreground = Brushes.Red;
};
TestTB.Dispatcher.BeginInvoke(action);
action = () =>
{
string imageUrl = "pack://application:,,,/MaterialTraceabilityUI;component/Assets/Images/失败-01.png";
BitmapImage bitmapImage = new BitmapImage(new Uri(imageUrl));
this.TestBImage.ImageSource = bitmapImage;
};
TestBImage.Dispatcher.BeginInvoke(action);
}
}
var equipInfo2 = equipBusiness.Equiplist.Where(x => x.positionId == 1).FirstOrDefault();
if (equipInfo2 != null)
{
if (equipInfo2.IsConnect)
{
action = () =>
{
this.TestTC.Foreground = Brushes.Green;
};
TestTC.Dispatcher.BeginInvoke(action);
action = () =>
{
string imageUrl = "pack://application:,,,/MaterialTraceabilityUI;component/Assets/Images/正常.png";
BitmapImage bitmapImage = new BitmapImage(new Uri(imageUrl));
this.TestCImage.ImageSource = bitmapImage;
};
TestCImage.Dispatcher.BeginInvoke(action);
}
else
{
action = () =>
{
this.TestTC.Foreground = Brushes.Red;
};
TestTC.Dispatcher.BeginInvoke(action);
action = () =>
{
string imageUrl = "pack://application:,,,/MaterialTraceabilityUI;component/Assets/Images/失败-01.png";
BitmapImage bitmapImage = new BitmapImage(new Uri(imageUrl));
this.TestCImage.ImageSource = bitmapImage;
};
TestCImage.Dispatcher.BeginInvoke(action);
}
}
}
catch (Exception ex)
{
LogHelperBusiness.LogError("设备状态刷新异常", ex);
}
}
public void UserControlInit()
{
//List downNumberItem = new List() { 3, 6, 9 };
//this.downNumber.ItemsSource = downNumberItem;
//this.downNumber.SelectedItem = Convert.ToInt32(ConfigHelper.GetConfig("downNumber"));
//查询前五个小时的数据
//DateTime beginTime = DateTime.Now.AddHours(-5);
//&& Convert.ToDateTime(x.bindTime) > beginTime
try
{
LogRefresh(LogType.RfidLog, "冷压RFID物料追溯系统初始化");
Expression> exp = s1 => true;
exp = exp.And(x => x.processId == appConfig.processId);
List shaftInfos = shftInfoService.Query(exp).Result;
if (shaftInfos == null || shaftInfos.Count == 0)
{
LogRefresh(LogType.RfidLog, "程序初始化数据为空");
return;
}
shaftInfos.ForEach(x =>
{
ProductionParam(new ViewModelDto()
{
rfidInfo = new RfidInfoDto()
{
rfid = x.bindRfid,
sfc = x.bindSfc,
ea = x.bindEaValue,
position = Convert.ToInt32(x.positionId)
},
plcStatus = true,
});
});
}catch(Exception ex)
{
LogHelperBusiness.LogError("冷压界面异常,UserControlInit",ex);
}
}
public void LogRefresh(LogType logType, string massage)
{
try
{
string info = massage;
string logTypeStr = "";
int isAlarm = 0;
if (logType == LogType.MesLog)
{
logTypeStr = "MES";
ListBoxItemAdd(this.MesLog, massage);
}
if (logType == LogType.PlcLog)
{
logTypeStr = "PLC";
ListBoxItemAdd(this.PlcLog, massage);
}
if (logType == LogType.RfidLog)
{
logTypeStr = "RFID";
ListBoxItemAdd(this.RfidLog, massage);
}
if (logType == LogType.AlarmLog)
{
logTypeStr = "Alarm";
isAlarm = 1;
ListBoxItemAdd(this.AlarmLog, massage);
loadBusiness.SaveLogRecord(0, massage);
}
logInfoServices.Add(new RecordLogInfo()
{
id = System.Guid.NewGuid().ToString("N"),
processId = "LY_A",
machineId = ConfigurationManager.AppSettings["machineId"].ToString(),
alarmType = logTypeStr,
alarmInfo = info,
isAlarm = isAlarm,
recordTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
});
}catch(Exception ex)
{
LogHelperBusiness.LogError("界面LogRefresh异常",ex);
}
}
private List logInfos = new List();
///
/// ListBox刷新
///
///
///
private void ListBoxItemAdd(ListBox listBox, string massage)
{
try
{
LogHelperBusiness.ViewLog(massage);
logInfos.Add(new LogInfoItem()
{
listBox = listBox,
recordTime = DateTime.Now,
message = String.Format("{0} {1}", DateTime.Now.ToString("MM-dd HH:mm:ss"), massage)
});
while(logInfos.Count > 120)
{
logInfos.RemoveAt(0);
}
Action action;
action = () =>
{
listBox.Items.Clear();
var infoList = logInfos.ToArray();
var strArray = infoList.Where(x => x.listBox == listBox).OrderByDescending(x => x.recordTime).ToList();
if (strArray != null)
{
foreach (var str in strArray)
{
SplitByLen(str.message, 50).ForEach(x =>
{
listBox.Items.Add(x);
});
}
}
};
listBox.Dispatcher.BeginInvoke(action);
}catch(Exception ex)
{
LogHelperBusiness.LogError("界面ListBoxItemAdd异常", ex);
}
}
public static List SplitByLen(string str, int separatorCharNum)
{
try
{
string tempStr = str;
List strList = new List();
if (string.IsNullOrEmpty(str) || str.Length <= separatorCharNum)
{
strList.Add(str);
return strList;
}
int iMax = Convert.ToInt32(Math.Ceiling(str.Length / (separatorCharNum * 1.0)));
for (int i = 0; i < iMax; i++)
{
string currMsg = tempStr.Substring(0, tempStr.Length > separatorCharNum ? separatorCharNum : tempStr.Length);
strList.Add(currMsg);
if (tempStr.Length > separatorCharNum)
{
tempStr = tempStr.Substring(separatorCharNum, tempStr.Length - separatorCharNum);
}
}
return strList;
}catch(Exception ex)
{
LogHelperBusiness.LogError("界面SplitByLen异常", ex);
return null;
}
}
private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
UpdateDeviceStatus();
}
///
/// 放卷位
///
///
///
private void upMaterial_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
try
{
WriteInfo write = new WriteInfo(appConfig.processId, 0);
write.ShowDialog();
refreshMaterialInfo(appConfig.processId, "0");
}catch(Exception ex)
{
LogHelperBusiness.LogError("界面upMaterial_MouseDown异常", ex);
}
}
///
/// 收卷1轴内侧
///
///
///
private void down_1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//WriteInfo write = new WriteInfo(appConfig.processId, 1);
//write.ShowDialog();
}
///
/// 收卷1轴外侧
///
///
///
private void down_2_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//WriteInfo write = new WriteInfo(appConfig.processId, 2);
//write.ShowDialog();
}
///
/// 收卷1B轴外侧
///
///
///
private void down_6_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//WriteInfo write = new WriteInfo(appConfig.processId, 3);
//write.ShowDialog();
}
///
/// 收卷1B轴内侧
///
///
///
private void down_5_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//WriteInfo write = new WriteInfo(appConfig.processId, 4);
//write.ShowDialog();
}
///
/// 收卷2A轴内侧
///
///
///
private void down_3_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//WriteInfo write = new WriteInfo(appConfig.processId, 5);
//write.ShowDialog();
}
///
/// 收卷2A轴外侧
///
///
///
private void down_4_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//WriteInfo write = new WriteInfo(appConfig.processId, 6);
//write.ShowDialog();
}
///
/// 收卷2B轴外侧
///
///
///
private void down_8_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//WriteInfo write = new WriteInfo(appConfig.processId, 7);
//write.ShowDialog();
}
///
/// 收卷2B轴内侧
///
///
///
private void down_7_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//WriteInfo write = new WriteInfo(appConfig.processId, 8);
//write.ShowDialog();
}
public void refreshMaterialInfo(string processId, string position)
{
//DateTime beginTime = DateTime.Now.AddHours(-5);
try
{
Expression> exp = s1 => true;
exp = exp.And(x => x.processId == appConfig.processId);
Expression> order = s1 => s1.bindTime;
ProShaftInfo shaftInfo = shftInfoService.QueryFirst(exp, order, false).Result;
if (shaftInfo == null)
{
//ListBoxItemAdd(this.RfidLog, "获取卷轴数据为空");
return;
}
Action action;
switch (Convert.ToInt32(shaftInfo.positionId))
{
case 0:
action = () =>
{
this.upMaterial_RFID.Text = "RFID:" + shaftInfo.bindRfid;
};
upMaterial_RFID.Dispatcher.BeginInvoke(action);
action = () =>
{
this.upMaterial_SFC.Text = "SFC:" + shaftInfo.bindSfc;
};
upMaterial_SFC.Dispatcher.BeginInvoke(action);
action = () =>
{
this.upMaterial_EA.Text = "EA:" + shaftInfo.bindEaValue;
};
upMaterial_EA.Dispatcher.BeginInvoke(action);
break;
default:
break;
}
}catch(Exception ex)
{
LogHelperBusiness.LogError("界面refreshMaterialInfo异常", ex);
}
}
///
/// 设置拆分数量
///
///
///
private void addDownNumber_Click(object sender, System.Windows.RoutedEventArgs e)
{
try
{
//string downNumberItem = this.downNumber.SelectedItem.ToString();
//ConfigHelper.UpdateAppConfig("downNumber", downNumberItem);
//string info = ConfigHelper.GetConfig("downNumber");
//if(downNumberItem == info)
//{
// LogHelperBusiness.LogInfo("设置拆分数量成功,设置为:" + downNumberItem + "卷");
// ListBoxItemAdd(this.RfidLog, "设置拆分数量成功,设置为:" + downNumberItem + "卷");
//}
}
catch(Exception ex)
{
LogHelperBusiness.LogError("设置拆分数量异常", ex);
}
}
}
}