MainPageView.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using ivf_tl_Entity.GlobalEnums;
  2. using ivf_tl_Operate.CustomUserControls;
  3. using ivf_tl_Operate.ViewModel;
  4. using ivf_tl_Operate.Windows;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Xml;
  23. namespace ivf_tl_Operate.View
  24. {
  25. /// <summary>
  26. /// MainPageView.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class MainPageView : UserControl
  29. {
  30. private MainPageViewModel vm { get { return (MainPageViewModel)this.DataContext; } }
  31. public MainPageView()
  32. {
  33. InitializeComponent();
  34. AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo1);
  35. AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo2);
  36. AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo3);
  37. AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo4);
  38. AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo5);
  39. AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo6);
  40. AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo7);
  41. AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo8);
  42. AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo9);
  43. AppData.Instance.CustomHouseInfoList.Add(this._HouseInfo10);
  44. var houseEnabled = ConfigurationManager.AppSettings["houseEnabled"].ToString();
  45. if (!string.IsNullOrEmpty(houseEnabled))
  46. {
  47. var houseEnabledList = houseEnabled.Split(",");
  48. int enableHouse;
  49. foreach (var item in houseEnabledList)
  50. {
  51. if (int.TryParse(item, out enableHouse))
  52. {
  53. SerEn(enableHouse);
  54. }
  55. }
  56. }
  57. #if DEBUG
  58. this.lanQie.Visibility = Visibility.Visible;
  59. List<int> items = new List<int>();
  60. for (int i = 0; i < 100; i++)
  61. {
  62. items.Add(i);
  63. }
  64. this._timingAutoFocus_ComboBox.ItemsSource = items;
  65. #endif
  66. }
  67. public void SerEn(int houseen)
  68. {
  69. switch (houseen)
  70. {
  71. case 1:
  72. this._HouseInfo1.IsEnabled = false;
  73. break;
  74. case 2:
  75. this._HouseInfo2.IsEnabled = false;
  76. break;
  77. case 3:
  78. this._HouseInfo3.IsEnabled = false;
  79. break;
  80. case 4:
  81. this._HouseInfo4.IsEnabled = false;
  82. break;
  83. case 5:
  84. this._HouseInfo5.IsEnabled = false;
  85. break;
  86. case 6:
  87. this._HouseInfo6.IsEnabled = false;
  88. break;
  89. case 7:
  90. this._HouseInfo7.IsEnabled = false;
  91. break;
  92. case 8:
  93. this._HouseInfo8.IsEnabled = false;
  94. break;
  95. case 9:
  96. this._HouseInfo9.IsEnabled = false;
  97. break;
  98. case 10:
  99. this._HouseInfo10.IsEnabled = false;
  100. break;
  101. }
  102. }
  103. private void ExLog(Exception ex, string name)
  104. {
  105. AppData.Instance.LogHelper.ExceptionLog(ex, $"MainPageView.{name}", LogEnum.RunException);
  106. }
  107. private void ErrorLog(string mess, LogEnum logEnum)
  108. {
  109. AppData.Instance.LogHelper.TLLog(mess, logEnum);
  110. }
  111. /// <summary>
  112. /// 设置按钮
  113. /// </summary>
  114. /// <param name="sender"></param>
  115. /// <param name="e"></param>
  116. private void Setting_Click(object sender, RoutedEventArgs e)
  117. {
  118. SettingPageView settingPageView = new SettingPageView();
  119. AppData.Instance.MainWindow.LoadPage(settingPageView);
  120. }
  121. /// <summary>
  122. /// 用户按钮
  123. /// </summary>
  124. /// <param name="sender"></param>
  125. /// <param name="e"></param>
  126. private void User_Click(object sender, RoutedEventArgs e)
  127. {
  128. AppData.Instance.MainWindow.Mark(true);
  129. var a = new LoginWindow(AppData.Instance.MainWindow).ShowDialog();
  130. AppData.Instance.MainWindow.Mark(false);
  131. if (a != true) return;
  132. //vm.StartMqtt();
  133. }
  134. public void SetAlarmMute(long id, int mute)
  135. {
  136. if (vm == null) return;
  137. vm.SetAlarmMute(id, mute);
  138. AppData.Instance.HttpHelper.MuteAlarmApi(id, mute);
  139. }
  140. /// <summary>
  141. /// 点击舱室
  142. /// </summary>
  143. /// <param name="obj"></param>
  144. private void CustomHouseInfo_ClickHouseEvent(CustomHouseInfo customHouseInfo)
  145. {
  146. if (!this.IsLoaded || !this.IsLoaded) return;
  147. var currentDish = customHouseInfo.ExDishSource;
  148. if (currentDish != null && currentDish.id > 0)
  149. {
  150. DetailPageViewModel detailPageViewModel = new DetailPageViewModel(customHouseInfo.ExHouseSource, currentDish);
  151. DetailPageView detailPageView = new DetailPageView()
  152. {
  153. DataContext = detailPageViewModel,
  154. };
  155. AppData.Instance.MainWindow.LoadPage(detailPageView);
  156. }
  157. else
  158. {
  159. var isBalacne = false;
  160. long balanceId = 0;
  161. var currentExbalance = vm.HouseSnToExBalance(customHouseInfo.HouseSn);
  162. if (currentExbalance != null)
  163. {
  164. isBalacne = true;
  165. balanceId = currentExbalance.id;
  166. }
  167. //Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Input, () =>
  168. //{
  169. //});
  170. AddDishWindowView addDishWindowView = new AddDishWindowView(customHouseInfo.HouseSn, isBalacne, balanceId);
  171. AppData.Instance.MainWindow.Mark(true);
  172. var a = addDishWindowView.ShowDialog();
  173. AppData.Instance.MainWindow.Mark(false);
  174. if (a == true)
  175. {
  176. vm.Init();
  177. }
  178. }
  179. }
  180. private CustomHouseInfo GetHouseInfoControl(int houseSn)
  181. {
  182. CustomHouseInfo customHouseInfo = null;
  183. switch (houseSn)
  184. {
  185. case 1:
  186. customHouseInfo = this._HouseInfo1;
  187. break;
  188. case 2:
  189. customHouseInfo = this._HouseInfo2;
  190. break;
  191. case 3:
  192. customHouseInfo = this._HouseInfo3;
  193. break;
  194. case 4:
  195. customHouseInfo = this._HouseInfo4;
  196. break;
  197. case 5:
  198. customHouseInfo = this._HouseInfo5;
  199. break;
  200. case 6:
  201. customHouseInfo = this._HouseInfo6;
  202. break;
  203. case 7:
  204. customHouseInfo = this._HouseInfo7;
  205. break;
  206. case 8:
  207. customHouseInfo = this._HouseInfo8;
  208. break;
  209. case 9:
  210. customHouseInfo = this._HouseInfo9;
  211. break;
  212. case 10:
  213. customHouseInfo = this._HouseInfo10;
  214. break;
  215. }
  216. return customHouseInfo;
  217. }
  218. /// <summary>
  219. /// 设置胚胎状态
  220. /// </summary>
  221. /// <param name="houseSn"></param>
  222. /// <param name="well"></param>
  223. /// <param name="embryoState"></param>
  224. public void SetMainEmbryoState(int houseSn, int well, int embryoState)
  225. {
  226. try
  227. {
  228. CustomHouseInfo customHouseInfo = GetHouseInfoControl(houseSn);
  229. if (customHouseInfo == null) return;
  230. customHouseInfo.SetEmbryoState(well, embryoState);
  231. }
  232. catch (Exception ex)
  233. {
  234. ExLog(ex, "SetMainEmbryoState");
  235. }
  236. }
  237. /// <summary>
  238. /// 结束培养
  239. /// </summary>
  240. /// <param name="housesn"></param>
  241. public void EndDish(int housesn)
  242. {
  243. vm.EndDish(housesn);
  244. }
  245. public void UpdateInfo()
  246. {
  247. vm.Init();
  248. }
  249. private void Button_Click(object sender, RoutedEventArgs e)
  250. {
  251. AppData.Instance.MainWindow.WindowState = WindowState.Minimized;
  252. }
  253. private void DishRecord_Click(object sender, RoutedEventArgs e)
  254. {
  255. DishRecordViewModel dishRecordViewModel = new DishRecordViewModel() { TLSn = vm.tlsn, ParentViewModel = vm };
  256. DishRecordView dishRecordView = new DishRecordView()
  257. {
  258. DataContext = dishRecordViewModel,
  259. };
  260. AppData.Instance.MainWindow.LoadPage(dishRecordView);
  261. }
  262. private void Chart_Click(object sender, RoutedEventArgs e)
  263. {
  264. ChartPageViewModel dishRecordViewModel = new ChartPageViewModel(1, vm) { tlSn = vm.tlsn };
  265. ChartPageView dishRecordView = new ChartPageView(1)
  266. {
  267. DataContext = dishRecordViewModel,
  268. };
  269. AppData.Instance.MainWindow.LoadPage(dishRecordView);
  270. }
  271. private void _status_MouseUp(object sender, MouseButtonEventArgs e)
  272. {
  273. }
  274. private void _status_MouseDown(object sender, MouseButtonEventArgs e)
  275. {
  276. AlarmPageViewModel alarmPageViewModel = new AlarmPageViewModel();
  277. AlarmPageView alarmPageView = new AlarmPageView() { DataContext = alarmPageViewModel };
  278. AppData.Instance.MainWindow.LoadPage(alarmPageView);
  279. }
  280. private void Button_Click_1(object sender, RoutedEventArgs e)
  281. {
  282. GetDateWindow getDate = new GetDateWindow(AppData.Instance.MainWindow);
  283. getDate.ShowDialog();
  284. return;
  285. ChangeLanguage("Chinese.xaml");
  286. }
  287. private void Button_Click_2(object sender, RoutedEventArgs e)
  288. {
  289. ChangeLanguage("English.xaml");
  290. }
  291. private void Button_Click_5(object sender, RoutedEventArgs e)
  292. {
  293. if (vm.ExHouse6.pressureAlarm == 0)
  294. {
  295. vm.ExHouse6.pressureAlarm = 1;
  296. }
  297. else
  298. {
  299. vm.ExHouse6.pressureAlarm = 0;
  300. }
  301. }
  302. private void Button_Click_4(object sender, RoutedEventArgs e)
  303. {
  304. if (vm.ExHouse6.temperatureAlarm == 0)
  305. {
  306. vm.ExHouse6.temperatureAlarm = 1;
  307. }
  308. else
  309. {
  310. vm.ExHouse6.temperatureAlarm = 0;
  311. }
  312. }
  313. public void ChangeLanguage(string languageName)
  314. {
  315. try
  316. {
  317. ResourceDictionary langRd = null;
  318. #if DEBUG
  319. string xamlFilePath = @"C:\PersonalSpace\work\1 VisualWorkSpace\SurfaceLan\" + languageName;
  320. #else
  321. string xamlFilePath = $"{System.AppDomain.CurrentDomain.BaseDirectory}Resources\\Language\\{languageName}";
  322. #endif
  323. if (!File.Exists(xamlFilePath))
  324. {
  325. new MessageWindow(AppData.Instance.MainWindow, $"切换语言失败,配置文件不存在:{xamlFilePath}", "消息提示").ShowDialog();
  326. return;
  327. }
  328. using (var stream = new FileStream(xamlFilePath, FileMode.Open))
  329. {
  330. langRd = System.Windows.Markup.XamlReader.Load(stream) as ResourceDictionary;
  331. }
  332. if (langRd != null)
  333. {
  334. int count = Application.Current.Resources.MergedDictionaries.Count;
  335. if (count >= 1)
  336. {
  337. Application.Current.Resources.MergedDictionaries.RemoveAt(count - 1);
  338. }
  339. Application.Current.Resources.MergedDictionaries.Add(langRd);
  340. setAppConfig(languageName);
  341. //Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  342. //config.AppSettings.Settings["Language"].Value = languageName;
  343. //config.Save(ConfigurationSaveMode.Modified);
  344. //System.Configuration.ConfigurationManager.RefreshSection("appSettings");
  345. }
  346. else
  347. {
  348. new MessageWindow(AppData.Instance.MainWindow, $"切换语言失败,文件转ResourceDictionary失败;{xamlFilePath}", "消息提示").ShowDialog();
  349. }
  350. }
  351. catch (Exception ex)
  352. {
  353. new MessageWindow(AppData.Instance.MainWindow, $"切换语言异常,{JsonConvert.SerializeObject(ex)}", "消息提示").ShowDialog();
  354. return;
  355. }
  356. }
  357. public void setAppConfig(string languageName)
  358. {
  359. try
  360. {
  361. string fileName = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ivf_tl_Operate.dll.config");
  362. if (!File.Exists(fileName)) return;
  363. var xmlDoc = new XmlDocument();
  364. xmlDoc.Load(fileName);
  365. var tags = xmlDoc.GetElementsByTagName("appSettings");
  366. XmlNode appSettingsNode = xmlDoc.SelectSingleNode("configuration/appSettings");
  367. if (appSettingsNode != null)
  368. {
  369. XmlNodeList addNodes = appSettingsNode.SelectNodes("add");
  370. foreach (XmlNode addNode in addNodes)
  371. {
  372. if (addNode.Attributes["key"].Value == "Language")
  373. {
  374. addNode.Attributes["value"].Value = languageName;
  375. }
  376. }
  377. }
  378. xmlDoc.Save(fileName);
  379. System.Configuration.ConfigurationManager.RefreshSection("appSettings");
  380. xmlDoc = null;
  381. }
  382. catch (Exception ex)
  383. {
  384. return;
  385. }
  386. }
  387. private void Button_Click_3(object sender, RoutedEventArgs e)
  388. {
  389. if (AppData.Instance.OutInterString != "1")
  390. {
  391. MessageBox.Show("在配置文件切换外网使用");
  392. return;
  393. }
  394. Task.Run(async () =>
  395. {
  396. #if DEBUG
  397. string fileName = @"C:\PersonalSpace\work\1 VisualWorkSpace\SurfaceLan\";
  398. # else
  399. string fileName = $"{System.AppDomain.CurrentDomain.BaseDirectory}Resources\\Language\\";
  400. #endif
  401. if (!Directory.Exists(fileName)) Directory.CreateDirectory(fileName);
  402. string Body = JsonConvert.SerializeObject(new { clientType = 3, region = 1 });
  403. await AppData.Instance.HttpHelper.DownLoadFileAsync("/api/tl/control/data/downloadLanguageConf", $"{fileName}Chinese.xaml", Body);
  404. Body = JsonConvert.SerializeObject(new { clientType = 3, region = 2 });
  405. #if DEBUG
  406. fileName = @"C:\PersonalSpace\work\1 VisualWorkSpace\SurfaceLan\";
  407. #else
  408. fileName = $"{System.AppDomain.CurrentDomain.BaseDirectory}Resources\\Language\\";
  409. #endif
  410. if (!Directory.Exists(fileName)) Directory.CreateDirectory(fileName);
  411. await AppData.Instance.HttpHelper.DownLoadFileAsync("/api/tl/control/data/downloadLanguageConf", $"{fileName}English.xaml", Body);
  412. Dispatcher.Invoke(() =>
  413. {
  414. MessageBox.Show("多语言模板更新完成");
  415. });
  416. });
  417. }
  418. }
  419. }