using ivf_tl_Entity.GlobalEnums;
using ivf_tl_Operate.CustomUserControls;
using ivf_tl_Operate.ViewModel;
using ivf_tl_Operate.Windows;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml;
namespace ivf_tl_Operate.View
{
///
/// MainPageView.xaml 的交互逻辑
///
public partial class MainPageView : UserControl
{
private MainPageViewModel vm { get { return (MainPageViewModel)this.DataContext; } }
public MainPageView()
{
InitializeComponent();
AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo1);
AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo2);
AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo3);
AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo4);
AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo5);
AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo6);
AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo7);
AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo8);
AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo9);
AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo10);
var houseEnabled = ConfigurationManager.AppSettings["houseEnabled"].ToString();
if (!string.IsNullOrEmpty(houseEnabled))
{
var houseEnabledList = houseEnabled.Split(",");
int enableHouse;
foreach (var item in houseEnabledList)
{
if (int.TryParse(item, out enableHouse))
{
SerEn(enableHouse);
}
}
}
#if DEBUG
this.lanQie.Visibility = Visibility.Visible;
List items = new List();
for (int i = 0; i < 100; i++)
{
items.Add(i);
}
this._timingAutoFocus_ComboBox.ItemsSource = items;
#endif
}
public void SerEn(int houseen)
{
switch (houseen)
{
case 1:
this._HouseInfo1.IsEnabled = false;
break;
case 2:
this._HouseInfo2.IsEnabled = false;
break;
case 3:
this._HouseInfo3.IsEnabled = false;
break;
case 4:
this._HouseInfo4.IsEnabled = false;
break;
case 5:
this._HouseInfo5.IsEnabled = false;
break;
case 6:
this._HouseInfo6.IsEnabled = false;
break;
case 7:
this._HouseInfo7.IsEnabled = false;
break;
case 8:
this._HouseInfo8.IsEnabled = false;
break;
case 9:
this._HouseInfo9.IsEnabled = false;
break;
case 10:
this._HouseInfo10.IsEnabled = false;
break;
}
}
private void ExLog(Exception ex, string name)
{
AppData.Instance.LogHelper.ExceptionLog(ex, $"MainPageView.{name}", LogEnum.RunException);
}
private void ErrorLog(string mess, LogEnum logEnum)
{
AppData.Instance.LogHelper.TLLog(mess, logEnum);
}
///
/// 设置按钮
///
///
///
private void Setting_Click(object sender, RoutedEventArgs e)
{
SettingPageView settingPageView = new SettingPageView();
AppData.Instance.MainWindow.LoadPage(settingPageView);
}
///
/// 用户按钮
///
///
///
private void User_Click(object sender, RoutedEventArgs e)
{
AppData.Instance.MainWindow.Mark(true);
var a = new LoginWindow(AppData.Instance.MainWindow).ShowDialog();
AppData.Instance.MainWindow.Mark(false);
if (a != true) return;
//vm.StartMqtt();
}
public void SetAlarmMute(long id, int mute)
{
if (vm == null) return;
vm.SetAlarmMute(id, mute);
AppData.Instance.HttpHelper.MuteAlarmApi(id, mute);
}
///
/// 点击舱室
///
///
private void CustomHouseInfo_ClickHouseEvent(CustomHouseInfo customHouseInfo)
{
if (!this.IsLoaded || !this.IsLoaded) return;
var currentDish = customHouseInfo.ExDishSource;
if (currentDish != null && currentDish.id > 0)
{
DetailPageViewModel detailPageViewModel = new DetailPageViewModel(customHouseInfo.ExHouseSource, currentDish);
DetailPageView detailPageView = new DetailPageView()
{
DataContext = detailPageViewModel,
};
AppData.Instance.MainWindow.LoadPage(detailPageView);
}
else
{
var isBalacne = false;
long balanceId = 0;
var currentExbalance = vm.HouseSnToExBalance(customHouseInfo.HouseSn);
if (currentExbalance != null)
{
isBalacne = true;
balanceId = currentExbalance.id;
}
//Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input, () =>
//{
//});
AddDishWindowView addDishWindowView = new AddDishWindowView(customHouseInfo.HouseSn, isBalacne, balanceId);
AppData.Instance.MainWindow.Mark(true);
var a = addDishWindowView.ShowDialog();
AppData.Instance.MainWindow.Mark(false);
if (a == true)
{
vm.Init();
}
}
}
private CustomHouseInfo GetHouseInfoControl(int houseSn)
{
CustomHouseInfo customHouseInfo = null;
switch (houseSn)
{
case 1:
customHouseInfo = this._HouseInfo1;
break;
case 2:
customHouseInfo = this._HouseInfo2;
break;
case 3:
customHouseInfo = this._HouseInfo3;
break;
case 4:
customHouseInfo = this._HouseInfo4;
break;
case 5:
customHouseInfo = this._HouseInfo5;
break;
case 6:
customHouseInfo = this._HouseInfo6;
break;
case 7:
customHouseInfo = this._HouseInfo7;
break;
case 8:
customHouseInfo = this._HouseInfo8;
break;
case 9:
customHouseInfo = this._HouseInfo9;
break;
case 10:
customHouseInfo = this._HouseInfo10;
break;
}
return customHouseInfo;
}
///
/// 设置胚胎状态
///
///
///
///
public void SetMainEmbryoState(int houseSn, int well, int embryoState)
{
try
{
CustomHouseInfo customHouseInfo = GetHouseInfoControl(houseSn);
if (customHouseInfo == null) return;
customHouseInfo.SetEmbryoState(well, embryoState);
}
catch (Exception ex)
{
ExLog(ex, "SetMainEmbryoState");
}
}
///
/// 结束培养
///
///
public void EndDish(int housesn)
{
vm.EndDish(housesn);
}
public void UpdateInfo()
{
vm.Init();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
AppData.Instance.MainWindow.WindowState = WindowState.Minimized;
}
private void DishRecord_Click(object sender, RoutedEventArgs e)
{
DishRecordViewModel dishRecordViewModel = new DishRecordViewModel() { TLSn = vm.tlsn, ParentViewModel = vm };
DishRecordView dishRecordView = new DishRecordView()
{
DataContext = dishRecordViewModel,
};
AppData.Instance.MainWindow.LoadPage(dishRecordView);
}
private void Chart_Click(object sender, RoutedEventArgs e)
{
ChartPageViewModel dishRecordViewModel = new ChartPageViewModel(1, vm) { tlSn = vm.tlsn };
ChartPageView dishRecordView = new ChartPageView(1)
{
DataContext = dishRecordViewModel,
};
AppData.Instance.MainWindow.LoadPage(dishRecordView);
}
private void _status_MouseUp(object sender, MouseButtonEventArgs e)
{
}
private void _status_MouseDown(object sender, MouseButtonEventArgs e)
{
AlarmPageViewModel alarmPageViewModel = new AlarmPageViewModel();
AlarmPageView alarmPageView = new AlarmPageView() { DataContext = alarmPageViewModel };
AppData.Instance.MainWindow.LoadPage(alarmPageView);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
GetDateWindow getDate = new GetDateWindow(AppData.Instance.MainWindow);
getDate.ShowDialog();
return;
ChangeLanguage("Chinese.xaml");
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
ChangeLanguage("English.xaml");
}
private void Button_Click_5(object sender, RoutedEventArgs e)
{
if (vm.ExHouse6.pressureAlarm == 0)
{
vm.ExHouse6.pressureAlarm = 1;
}
else
{
vm.ExHouse6.pressureAlarm = 0;
}
}
private void Button_Click_4(object sender, RoutedEventArgs e)
{
if (vm.ExHouse6.temperatureAlarm == 0)
{
vm.ExHouse6.temperatureAlarm = 1;
}
else
{
vm.ExHouse6.temperatureAlarm = 0;
}
}
public void ChangeLanguage(string languageName)
{
try
{
ResourceDictionary langRd = null;
#if DEBUG
string xamlFilePath = @"C:\PersonalSpace\work\1 VisualWorkSpace\SurfaceLan\" + languageName;
#else
string xamlFilePath = $"{System.AppDomain.CurrentDomain.BaseDirectory}Resources\\Language\\{languageName}";
#endif
if (!File.Exists(xamlFilePath))
{
new MessageWindow(AppData.Instance.MainWindow, $"切换语言失败,配置文件不存在:{xamlFilePath}", "消息提示").ShowDialog();
return;
}
using (var stream = new FileStream(xamlFilePath, FileMode.Open))
{
langRd = System.Windows.Markup.XamlReader.Load(stream) as ResourceDictionary;
}
if (langRd != null)
{
int count = Application.Current.Resources.MergedDictionaries.Count;
if (count >= 1)
{
Application.Current.Resources.MergedDictionaries.RemoveAt(count - 1);
}
Application.Current.Resources.MergedDictionaries.Add(langRd);
setAppConfig(languageName);
//Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//config.AppSettings.Settings["Language"].Value = languageName;
//config.Save(ConfigurationSaveMode.Modified);
//System.Configuration.ConfigurationManager.RefreshSection("appSettings");
}
else
{
new MessageWindow(AppData.Instance.MainWindow, $"切换语言失败,文件转ResourceDictionary失败;{xamlFilePath}", "消息提示").ShowDialog();
}
}
catch (Exception ex)
{
new MessageWindow(AppData.Instance.MainWindow, $"切换语言异常,{JsonConvert.SerializeObject(ex)}", "消息提示").ShowDialog();
return;
}
}
public void setAppConfig(string languageName)
{
try
{
string fileName = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ivf_tl_Operate.dll.config");
if (!File.Exists(fileName)) return;
var xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
var tags = xmlDoc.GetElementsByTagName("appSettings");
XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
if (appSettingsNode != null)
{
XmlNodeList addNodes = appSettingsNode.SelectNodes("add");
foreach (XmlNode addNode in addNodes)
{
if (addNode.Attributes["key"].Value == "Language")
{
addNode.Attributes["value"].Value = languageName;
}
}
}
xmlDoc.Save(fileName);
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
xmlDoc = null;
}
catch (Exception ex)
{
return;
}
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
if (AppData.Instance.OutInterString != "1")
{
MessageBox.Show("在配置文件切换外网使用");
return;
}
Task.Run(async () =>
{
#if DEBUG
string fileName = @"C:\PersonalSpace\work\1 VisualWorkSpace\SurfaceLan\";
# else
string fileName = $"{System.AppDomain.CurrentDomain.BaseDirectory}Resources\\Language\\";
#endif
if (!Directory.Exists(fileName)) Directory.CreateDirectory(fileName);
string Body = JsonConvert.SerializeObject(new { clientType = 3, region = 1 });
await AppData.Instance.HttpHelper.DownLoadFileAsync("/api/tl/control/data/downloadLanguageConf", $"{fileName}Chinese.xaml", Body);
Body = JsonConvert.SerializeObject(new { clientType = 3, region = 2 });
#if DEBUG
fileName = @"C:\PersonalSpace\work\1 VisualWorkSpace\SurfaceLan\";
#else
fileName = $"{System.AppDomain.CurrentDomain.BaseDirectory}Resources\\Language\\";
#endif
if (!Directory.Exists(fileName)) Directory.CreateDirectory(fileName);
await AppData.Instance.HttpHelper.DownLoadFileAsync("/api/tl/control/data/downloadLanguageConf", $"{fileName}English.xaml", Body);
Dispatcher.Invoke(() =>
{
MessageBox.Show("多语言模板更新完成");
});
});
}
}
}