Management.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. 
  2. using DefaultEdit.Communication;
  3. using DefaultEdit.DataBase;
  4. using DefaultEdit.Log4xml;
  5. using DefaultEdit.Model;
  6. using Newtonsoft.Json.Linq;
  7. using NextTreatMesDemo.Models;
  8. using NextTreatMesDemo.Utils;
  9. using ObservableCollections;
  10. using Prism.Modularity;
  11. using Prism.Mvvm;
  12. using ScottPlot;
  13. using SqlSugar;
  14. using System;
  15. using System.Collections.Concurrent;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.Diagnostics;
  19. using System.IO;
  20. using System.Linq;
  21. using System.Text;
  22. using System.Threading;
  23. using System.Threading.Tasks;
  24. using System.Windows;
  25. using System.Xml.Linq;
  26. namespace DefaultEdit.Core
  27. {
  28. public class Management : BindableBase
  29. {
  30. private CurentApplicationSettings _CurentApplicationSettings=new CurentApplicationSettings();
  31. private IServer _httpService = new MesServer();
  32. private S7PlcCommunicate s7PlcCommunicate;
  33. public event Action<string,bool> RuningToWorkEvent;
  34. public event Action ProduceDataToChangeEvent;
  35. private ObservableCollection<RfidInfo> _RfIDList=new ObservableCollection<RfidInfo>();
  36. public ObservableCollection<RfidInfo> RfIDList { get => _RfIDList; set { SetProperty(ref _RfIDList, value); } }
  37. // private ObservableCollection<RfidInfo> _RuningData=new ObservableCollection<RfidInfo>();
  38. // public ObservableCollection<RfidInfo> RuningData { get => _RuningData; set => _RuningData = value; }
  39. private CurrConfig _currConfig = new CurrConfig();
  40. public CurrConfig CurrConfig { get => _currConfig; set { SetProperty(ref _currConfig, value); } }
  41. private tb_formulasdata _formulasdata = new tb_formulasdata();
  42. public tb_formulasdata Formulasdata { get => _formulasdata; set { SetProperty(ref _formulasdata, value); } }
  43. private DateTime _DateTimeNow;
  44. /// <summary>
  45. /// 当前时间
  46. /// </summary>
  47. public DateTime DateTimeNow
  48. {
  49. get { return _DateTimeNow; }
  50. set { SetProperty(ref _DateTimeNow, value); }
  51. }
  52. private ObservableCollection<string> _CurrStationRfid=new ObservableCollection<string>();
  53. public ObservableCollection<string> CurrStationRfid { get => _CurrStationRfid; set => _CurrStationRfid = value; }
  54. /// <summary>
  55. /// 用户Id(从MES接口内获取)
  56. /// </summary>
  57. private string _UserId = "";
  58. /// <summary>
  59. /// 用户名(从MES接口内获取)
  60. /// </summary>
  61. private string _UserName = "未登录";
  62. /// <summary>
  63. /// 用于记录后处理数据来打印小票
  64. /// </summary>
  65. public ObservableCollection<ProcessDataModel> processDataModels = new ObservableCollection<ProcessDataModel>();
  66. public double[] ProduceData = new double[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 };
  67. public IServer HttpService { get => _httpService; set { SetProperty(ref _httpService, value); } }
  68. public string UserId { get => _UserId; set { SetProperty(ref _UserId, value); } }
  69. public string UserName { get => _UserName; set { SetProperty(ref _UserName, value); } }
  70. public CurentApplicationSettings CurentApplicationSettings { get => _CurentApplicationSettings; set { SetProperty(ref _CurentApplicationSettings, value); } }
  71. public bool Ismeswork { get => ismeswork; set { SetProperty(ref ismeswork, value); } }
  72. private bool ismeswork = true;
  73. //运行线程
  74. private Thread WorkThread;
  75. private Stopwatch[] stopwatchs = new Stopwatch[3];
  76. public Management()
  77. {
  78. stopwatchs[ 0 ] = new Stopwatch();
  79. stopwatchs[ 1 ] = new Stopwatch();
  80. stopwatchs[ 2 ] = new Stopwatch();
  81. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  82. {
  83. for ( int i = 0; i < 6; i++ )
  84. {
  85. CurrStationRfid.Add("");
  86. }
  87. });
  88. RuningToWorkEvent += Management_RuningToWorkEvent; ;
  89. WorkThread = new Thread(DataBridging);
  90. WorkThread.Name = "Plc读取";
  91. WorkThread.IsBackground = true;
  92. WorkThread.Start();
  93. }
  94. public void ConnectPlc()
  95. {
  96. s7PlcCommunicate = new S7PlcCommunicate(CurentApplicationSettings.PlcIP);
  97. s7PlcCommunicate.OpenPlc();
  98. }
  99. private async void Management_RuningToWorkEvent(string key, bool value)
  100. {
  101. try
  102. {
  103. switch ( key )
  104. {
  105. case "1工位离心开始":
  106. if ( value )
  107. {
  108. stopwatchs[ 0 ].Restart();
  109. if ( RfIDList.Count != 0 )
  110. {
  111. int index= RfIDList.ToList().FindIndex(x => x.StationId == 1);
  112. if ( index != -1 )
  113. {
  114. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  115. {
  116. RfIDList[ index ].StationId = 2;
  117. CurrStationRfid[ 1 ] = RfIDList[ index ].Rfid;
  118. RfIDList[ index ].State = "离心中";
  119. });
  120. LogHelper.Info(RfIDList[ index ].Rfid + " 1工位离心开始");
  121. }
  122. else
  123. {
  124. LogHelper.Info(" 1工位离心开始");
  125. }
  126. index = RfIDList.ToList().FindIndex(x => x.StationId == 1);
  127. if ( index != -1 )
  128. {
  129. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  130. {
  131. RfIDList[ index ].StationId = 2;
  132. CurrStationRfid[ 2 ] = RfIDList[ index ].Rfid;
  133. RfIDList[ index ].State = "离心中";
  134. });
  135. LogHelper.Info(RfIDList[ index ].Rfid + " 1工位离心开始");
  136. }
  137. else
  138. {
  139. LogHelper.Info(" 1工位离心开始");
  140. }
  141. }
  142. else
  143. {
  144. LogHelper.Info(" 1工位离心开始");
  145. }
  146. LogHelper.Debuginfo("1工位离心开始已置位");
  147. }
  148. else
  149. {
  150. LogHelper.Debuginfo("1工位离心开始已复位");
  151. }
  152. break;
  153. case "1工位离心完成":
  154. if ( value )
  155. {
  156. stopwatchs[ 0 ].Stop();
  157. if ( RfIDList.Count != 0 )
  158. {
  159. int index= RfIDList.ToList().FindIndex(x => x.StationId == 2);
  160. if ( index != -1 )
  161. {
  162. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  163. {
  164. RfIDList[ index ].StationId = 3;
  165. RfIDList[ index ].State = "离心完成";
  166. });
  167. LogHelper.Info(CurrStationRfid[ 1 ] + " 1工位离心完成");
  168. }
  169. else
  170. {
  171. LogHelper.Info(" 1工位离心完成");
  172. }
  173. index = RfIDList.ToList().FindIndex(x => x.StationId == 2);
  174. if ( index != -1 )
  175. {
  176. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  177. {
  178. RfIDList[ index ].StationId = 3;
  179. RfIDList[ index ].State = "离心完成";
  180. });
  181. LogHelper.Info(CurrStationRfid[ 2 ] + " 1工位离心完成");
  182. }
  183. else
  184. {
  185. LogHelper.Info(" 1工位离心完成");
  186. }
  187. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  188. {
  189. CurrStationRfid[ 1 ] = "";
  190. CurrStationRfid[ 2 ] = "";
  191. });
  192. }
  193. else
  194. {
  195. LogHelper.Info(" 1工位离心完成");
  196. }
  197. LogHelper.Debuginfo("1工位离心完成已置位");
  198. }
  199. else
  200. {
  201. LogHelper.Debuginfo("1工位离心完成已复位");
  202. }
  203. break;
  204. case "2工位离心开始":
  205. if ( value )
  206. {
  207. stopwatchs[ 1 ].Restart();
  208. if ( RfIDList.Count != 0 )
  209. {
  210. int index = RfIDList.ToList().FindIndex(x => x.StationId == 3);
  211. if ( index != -1 )
  212. {
  213. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  214. {
  215. RfIDList[ index ].StationId = 4;
  216. CurrStationRfid[ 3 ] = RfIDList[ index ].Rfid;
  217. RfIDList[ index ].State = "离心中";
  218. });
  219. LogHelper.Info(RfIDList[ index ].Rfid + " 2工位离心开始");
  220. }
  221. else
  222. {
  223. LogHelper.Info(" 2工位离心开始");
  224. }
  225. index = RfIDList.ToList().FindIndex(x => x.StationId == 1);
  226. if ( index != -1 )
  227. {
  228. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  229. {
  230. RfIDList[ index ].StationId = 4;
  231. CurrStationRfid[ 4 ] = RfIDList[ index ].Rfid;
  232. RfIDList[ index ].State = "离心中";
  233. });
  234. LogHelper.Info(RfIDList[ index ].Rfid + " 2工位离心开始");
  235. }
  236. else
  237. {
  238. LogHelper.Info(" 2工位离心开始");
  239. }
  240. }
  241. else
  242. {
  243. LogHelper.Info(" 2工位离心开始");
  244. }
  245. LogHelper.Debuginfo("2工位离心开始已置位");
  246. }
  247. else
  248. {
  249. LogHelper.Debuginfo("2工位离心开始已复位");
  250. }
  251. break;
  252. case "2工位离心完成":
  253. if ( value )
  254. {
  255. stopwatchs[ 1 ].Stop();
  256. if ( RfIDList.Count != 0 )
  257. {
  258. int index= RfIDList.ToList().FindIndex(x => x.StationId == 4);
  259. if ( index != -1 )
  260. {
  261. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  262. {
  263. RfIDList[ index ].StationId = 5;
  264. RfIDList[ index ].State = "离心完成";
  265. });
  266. LogHelper.Info(CurrStationRfid[ 3 ] + " 2工位离心完成");
  267. }
  268. else
  269. {
  270. LogHelper.Info(" 2工位离心完成");
  271. }
  272. index = RfIDList.ToList().FindIndex(x => x.StationId == 4);
  273. if ( index != -1 )
  274. {
  275. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  276. {
  277. RfIDList[ index ].StationId = 5;
  278. RfIDList[ index ].State = "离心完成";
  279. });
  280. LogHelper.Info(CurrStationRfid[ 4 ] + " 1工位离心完成");
  281. }
  282. else
  283. {
  284. LogHelper.Info(" 1工位离心完成");
  285. }
  286. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  287. {
  288. CurrStationRfid[ 3 ] = "";
  289. CurrStationRfid[ 4 ] = "";
  290. });
  291. }
  292. else
  293. {
  294. LogHelper.Info(" 2工位离心完成");
  295. }
  296. LogHelper.Debuginfo("2工位离心完成已置位");
  297. }
  298. else
  299. {
  300. LogHelper.Debuginfo("2工位离心完成已复位");
  301. }
  302. break;
  303. case "脱模开始":
  304. if ( value )
  305. {
  306. LogHelper.Info(CurrConfig.Rfid + " 脱模开始");
  307. PlateInPlace(CurrConfig.Rfid);
  308. LogHelper.Debuginfo("脱模开始已置位");
  309. }
  310. else
  311. {
  312. LogHelper.Debuginfo("脱模开始已复位");
  313. }
  314. break;
  315. case "脱模完成":
  316. if ( value )
  317. {
  318. LogHelper.Info(CurrConfig.Rfid + " 脱模完成");
  319. if ( processDataModels.Count != 0 )
  320. {
  321. ProcessDataModel data=processDataModels.ToList().Find(it=>it.BoardId== CurrConfig.Rfid);
  322. if ( data != null )
  323. {
  324. PrintInfo(data, "手动");
  325. }
  326. else
  327. {
  328. LogHelper.Info(" 网板信息没找到");
  329. }
  330. }
  331. LogHelper.Debuginfo("脱模完成已置位");
  332. }
  333. else
  334. {
  335. LogHelper.Debuginfo("脱模完成已复位");
  336. }
  337. break;
  338. case "固化开始":
  339. if ( value )
  340. {
  341. stopwatchs[ 2 ].Restart();
  342. if ( RfIDList.Count != 0 )
  343. {
  344. int index= RfIDList.ToList().FindIndex(x => x.StationId ==5 );
  345. if ( index != -1 )
  346. {
  347. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  348. {
  349. RfIDList[ index ].StationId = 6;
  350. CurrStationRfid[ 5 ] = RfIDList[ index ].Rfid;
  351. RfIDList[ index ].State = "固化中";
  352. });
  353. LogHelper.Info(RfIDList[ index ].Rfid + " 固化开始");
  354. }
  355. else
  356. {
  357. LogHelper.Info(" 固化开始");
  358. }
  359. index = RfIDList.ToList().FindIndex(x => x.StationId == 5);
  360. if ( index != -1 )
  361. {
  362. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  363. {
  364. RfIDList[ index ].StationId = 6;
  365. CurrStationRfid[ 6 ] = RfIDList[ index ].Rfid;
  366. RfIDList[ index ].State = "固化中";
  367. });
  368. LogHelper.Info(RfIDList[ index ].Rfid + " 固化开始");
  369. }
  370. else
  371. {
  372. LogHelper.Info(" 固化开始");
  373. }
  374. }
  375. else
  376. {
  377. LogHelper.Info(" 固化开始");
  378. }
  379. LogHelper.Debuginfo("固化开始已置位");
  380. }
  381. else
  382. {
  383. LogHelper.Debuginfo("固化开始已复位");
  384. }
  385. break;
  386. case "固化完成":
  387. if ( value )
  388. {
  389. stopwatchs[ 2 ].Stop();
  390. if ( RfIDList.Count != 0 )
  391. {
  392. int index= RfIDList.ToList().FindIndex(x => x.StationId ==6 );
  393. if ( index != -1 )
  394. {
  395. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  396. {
  397. RfIDList.RemoveAt(index);
  398. });
  399. LogHelper.Info(RfIDList[ index ].Rfid + " 固化完成");
  400. }
  401. else
  402. {
  403. LogHelper.Info(" 固化完成");
  404. }
  405. index = RfIDList.ToList().FindIndex(x => x.StationId == 6);
  406. if ( index != -1 )
  407. {
  408. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  409. {
  410. RfIDList[ index ].StationId = 100;
  411. });
  412. LogHelper.Info(RfIDList[ index ].Rfid + " 固化完成");
  413. }
  414. else
  415. {
  416. LogHelper.Info(" 固化完成");
  417. }
  418. //CurrStationRfid[5] = RfIDList[index].Rfid;
  419. //CurrStationRfid[6] = RfIDList[index].Rfid;
  420. int hour= DateTime.Now.Hour;
  421. ProduceData[ hour ] = ProduceData[ hour ] + 2;
  422. for ( int i = hour + 1; i < ProduceData.Length; i++ )
  423. {
  424. ProduceData[ i ] = 0;
  425. }
  426. ProduceDataToChangeEvent?.Invoke();
  427. }
  428. else
  429. {
  430. LogHelper.Info(" 固化完成");
  431. }
  432. LogHelper.Debuginfo("固化完成已置位");
  433. }
  434. else
  435. {
  436. LogHelper.Debuginfo("固化完成已复位");
  437. }
  438. break;
  439. default:
  440. break;
  441. }
  442. } catch ( Exception e )
  443. {
  444. LogHelper.Error(e.Message);
  445. }
  446. }
  447. public void DataBridging()
  448. {
  449. ObservableDictionary<string,bool> BoolPairs=new ObservableDictionary<string, bool>();
  450. BoolPairs.Add("1工位离心开始", false);
  451. BoolPairs.Add("1工位离心完成", false);
  452. BoolPairs.Add("2工位离心开始", false);
  453. BoolPairs.Add("2工位离心完成", false);
  454. BoolPairs.Add("脱模开始", false);
  455. BoolPairs.Add("脱模完成", false);
  456. BoolPairs.Add("固化开始", false);
  457. BoolPairs.Add("固化完成", false);
  458. BoolPairs.CollectionChanged += BoolPairs_CollectionChanged;
  459. //BoolPairs["脱模开始"] =true ;
  460. //BoolPairs["脱模完成"] = true ;
  461. while ( true )
  462. {
  463. try
  464. {
  465. if ( s7PlcCommunicate != null && s7PlcCommunicate.IsConnected )
  466. {
  467. byte []buff= s7PlcCommunicate.ReadByte(25, 780, 84);
  468. int actuallength=buff[73];
  469. byte[] buffsT = new byte[ actuallength ];
  470. Array.Copy(buff, 74, buffsT, 0, actuallength);
  471. CurrConfig.Rfid = Encoding.ASCII.GetString(buffsT);
  472. BoolPairs[ "1工位离心开始" ] = s7PlcCommunicate.GetBitAt(buff, 0, 0);
  473. BoolPairs[ "1工位离心完成" ] = s7PlcCommunicate.GetBitAt(buff, 0, 1);
  474. BoolPairs[ "2工位离心开始" ] = s7PlcCommunicate.GetBitAt(buff, 1, 0);
  475. BoolPairs[ "2工位离心完成" ] = s7PlcCommunicate.GetBitAt(buff, 1, 1);
  476. BoolPairs[ "脱模开始" ] = s7PlcCommunicate.GetBitAt(buff, 2, 0);
  477. BoolPairs[ "脱模完成" ] = s7PlcCommunicate.GetBitAt(buff, 2, 1);
  478. BoolPairs[ "固化开始" ] = s7PlcCommunicate.GetBitAt(buff, 3, 0);
  479. BoolPairs[ "固化完成" ] = s7PlcCommunicate.GetBitAt(buff, 3, 1);
  480. //固化压力 792.0
  481. CurrConfig.Pressure1 = s7PlcCommunicate.GetRealAt(buff, 12);
  482. CurrConfig.PTime1 = s7PlcCommunicate.GetTimeSpanAt(buff, 16);
  483. CurrConfig.Speed = s7PlcCommunicate.GetShortAt(buff, 32);
  484. CurrConfig.Speed2 = s7PlcCommunicate.GetShortAt(buff, 34);
  485. CurrConfig.RealSpeed = s7PlcCommunicate.GetRealAt(buff, 36);
  486. CurrConfig.RealSpeed2 = s7PlcCommunicate.GetRealAt(buff, 40);
  487. CurrConfig.Time = s7PlcCommunicate.GetTimeSpanAt(buff, 44);
  488. CurrConfig.Time2 = s7PlcCommunicate.GetTimeSpanAt(buff, 48);
  489. //CurrConfig.Power1_1 = s7PlcCommunicate.GetShortAt(buff, 20);
  490. //CurrConfig.Power1_2 = s7PlcCommunicate.GetShortAt(buff, 22);
  491. //CurrConfig.Power1_3 = s7PlcCommunicate.GetShortAt(buff, 24);
  492. //CurrConfig.Power1_4 = s7PlcCommunicate.GetShortAt(buff, 26);
  493. CurrConfig.Realtime = ( float ) ( stopwatchs[ 0 ].ElapsedMilliseconds / 1000.0 );
  494. CurrConfig.Realtime2 = ( float ) ( stopwatchs[ 1 ].ElapsedMilliseconds / 1000.0 );
  495. CurrConfig.Realtime3 = ( float ) ( stopwatchs[ 2 ].ElapsedMilliseconds / 1000.0 );
  496. for ( int i = 0; i < stopwatchs.Length; i++ )
  497. {
  498. if ( stopwatchs[ i ].ElapsedMilliseconds / 1000.0 > 1000 )
  499. {
  500. stopwatchs[ i ].Stop();
  501. }
  502. }
  503. }
  504. List<tb_formulasdata> tb_Formulasdatas = SqlSugarHelper.Queryabletb_formulasdata();
  505. if ( tb_Formulasdatas.Count != 0 )
  506. Formulasdata = tb_Formulasdatas[ 0 ];
  507. }
  508. catch
  509. {
  510. }
  511. Thread.Sleep(50);
  512. }
  513. }
  514. private void BoolPairs_CollectionChanged(in NotifyCollectionChangedEventArgs<KeyValuePair<string, bool>> e)
  515. {
  516. if ( e.NewItem.Value != e.OldItem.Value )
  517. {
  518. RuningToWorkEvent?.Invoke(e.OldItem.Key, e.NewItem.Value);
  519. }
  520. }
  521. private void Serial_ConnectionChanged(SerialCommunication arg1, bool arg2)
  522. {
  523. if ( arg2 )
  524. LogHelper.Info(arg1.PortName + ":已连接");
  525. if ( !arg2 )
  526. LogHelper.Info(arg1.PortName + ":断开连接");
  527. }
  528. private void Serial_DataReceived(SerialCommunication arg1, string arg2)
  529. {
  530. LogHelper.Info("收到" + arg1.PortName + $"消息:{arg2}");
  531. }
  532. /// <summary>
  533. /// 网板读取获取数据并上传记录
  534. /// </summary>
  535. public async Task PlateInPlace(string value)
  536. {
  537. var IsOK = true;
  538. try
  539. {
  540. if ( !string.IsNullOrEmpty(value) )
  541. {
  542. try
  543. {
  544. if ( value.Length != 8 )
  545. {
  546. return;
  547. }
  548. else
  549. {
  550. var boardId = value;
  551. string boardguid = Guid.NewGuid().ToString();
  552. var modelsinfo = "";
  553. var materialtype = "";
  554. var printdeviceId = "";
  555. var brand = "";
  556. var printTagProduct = "";
  557. var iskid = false;
  558. var ishaiwai = false;
  559. var isLarge = "无";
  560. var count = 0;
  561. var tip = "";
  562. var isoutline = false;
  563. XnRestfulResult<List<Re_Items>> groupdata=new XnRestfulResult<List<Re_Items>>();
  564. if ( Ismeswork )
  565. {
  566. groupdata = _httpService.GetGroupItems(boardId, CurentApplicationSettings.DeviceId);
  567. if ( !groupdata.Success )
  568. {
  569. if ( groupdata.RawText != null && groupdata.RawText.Contains("未点检") )
  570. {
  571. var s = MessageBox.Show("设备未点检,请点检后确认!", "提示", MessageBoxButton.OK);
  572. }
  573. LogHelper.MesInfo($"网板ID:{boardId}获取信息失败,Code:{( int ) groupdata.Code},原因:{groupdata.RawText}");
  574. isoutline = true;
  575. }
  576. else
  577. {
  578. if ( groupdata.Data.Count <= 0 )
  579. {
  580. LogHelper.MesInfo($"网板ID:{boardId}获取信息失败,Code:{( int ) groupdata.Code},原因:{groupdata.RawText}");
  581. isoutline = true;
  582. }
  583. }
  584. }
  585. else
  586. {
  587. isoutline = true;
  588. }
  589. if ( !isoutline )
  590. {
  591. ///上传后处理记录
  592. var result = UpdateRecord(boardId);
  593. if ( !string.IsNullOrEmpty(result) )
  594. {
  595. LogHelper.MesInfo($"上传记录失败,{result}!");
  596. var boxresult = MessageBox.Show($"{result}(是:放回;否:重试)", "提示", MessageBoxButton.YesNo);
  597. if ( boxresult == MessageBoxResult.Yes )
  598. {
  599. IsOK = false;
  600. return;
  601. }
  602. }
  603. ///读取网板内的牙模的信息(方便打印在小票上),如果读取失败就读取下一个牙模,读取成功就跳出循环
  604. for ( int i = 0; i < 10; i++ )
  605. {
  606. try
  607. {
  608. modelsinfo = "";
  609. count = groupdata.Data.Count;
  610. var Id = groupdata.Data[groupdata.Data.Count - i].itemId;
  611. var infos = _httpService.GetModels(Id, CurentApplicationSettings.DeviceId);
  612. if ( infos.Success )
  613. {
  614. modelsinfo = infos.Data.uvcuringReleaseBatch + " " + infos.Data.alignerSpec.material + "-" + infos.Data.alignerSpec.thickness;
  615. if ( infos.Data.order.is_expedited == "1" || infos.Data.remade || infos.Data.produceType == "R" )
  616. {
  617. modelsinfo = modelsinfo + "\r\n";
  618. if ( infos.Data.order.is_expedited == "1" )
  619. {
  620. modelsinfo = modelsinfo + " 加急";
  621. }
  622. if ( infos.Data.remade )
  623. {
  624. modelsinfo = modelsinfo + " 返工";
  625. }
  626. if ( infos.Data.produceType == "R" )
  627. {
  628. modelsinfo = modelsinfo + " 保持器";
  629. }
  630. }
  631. iskid = infos.Data.isKid;
  632. ishaiwai = infos.Data.order.country.Contains("中国") ? false : true;
  633. if ( infos.Data.processParameters != null )
  634. {
  635. var carrierf = infos.Data.processParameters.FirstOrDefault(x => x.key == "carrier");
  636. if ( carrierf != null )
  637. {
  638. isLarge = carrierf.value == "small" ? "小载具" : "大载具";
  639. }
  640. }
  641. var record = _httpService.GetProductionRecord(Id);
  642. if ( record.Success )
  643. {
  644. printdeviceId = record.Data.First().deviceId;
  645. if ( record.Data.First().description.FirstOrDefault(x => x.key == "printTaskId") != null )
  646. {
  647. var taskId = record.Data.First().description.FirstOrDefault(x => x.key == "printTaskId").value;
  648. var taskinfo = _httpService.GetPrintTask(taskId);
  649. if ( taskinfo.Success )
  650. {
  651. materialtype = taskinfo.Data.description.FirstOrDefault(x => x.key == "filmType") != null ? taskinfo.Data.description.FirstOrDefault(x => x.key == "filmType").value : "";
  652. printTagProduct = taskinfo.Data.description.FirstOrDefault(x => x.key == "printTagProduct") != null ? taskinfo.Data.description.FirstOrDefault(x => x.key == "printTagProduct").value : "";
  653. brand = taskinfo.Data.description.FirstOrDefault(x => x.key == "productOfProduce") != null ? taskinfo.Data.description.FirstOrDefault(x => x.key == "productOfProduce").value : "";
  654. break;
  655. }
  656. }
  657. }
  658. }
  659. }
  660. catch ( Exception e )
  661. {
  662. LogHelper.Error("读取网板内牙模信息异常:" + e.ToString());
  663. }
  664. }
  665. }
  666. else
  667. {
  668. LogHelper.MesInfo($"网板{value}获取数据异常,不上传记录");
  669. modelsinfo = "异常";
  670. materialtype = "异常";
  671. printTagProduct = "异常";
  672. printdeviceId = "异常";
  673. brand = "异常";
  674. }
  675. ///将生产信息记录下,用于在打印完成时打印小票
  676. App.Current.Dispatcher.Invoke(( System.Action ) delegate
  677. {
  678. ProcessDataModel data= processDataModels.ToList().Find(it => it.BoardId == boardId);
  679. if ( data != null )
  680. {
  681. processDataModels.Remove(data);
  682. }
  683. //添加记录
  684. processDataModels.Add(new ProcessDataModel()
  685. {
  686. BoardId = boardId,
  687. ModelsInfo = modelsinfo,
  688. StartTime = DateTime.Now.ToString("HH:mm:ss"),
  689. State = ProcessState.入料中,
  690. BoardGuid = boardguid,
  691. stopwatch = new System.Diagnostics.Stopwatch(),
  692. MaterialType = materialtype,
  693. PrintTagProduct = printTagProduct,
  694. PrintDeviceId = printdeviceId,
  695. IsKid = iskid,
  696. Tip = tip,
  697. IsOverSea = ishaiwai,
  698. IsLarge = isLarge,
  699. Brand = brand,
  700. Count = count,
  701. });
  702. RfidInfo rfidInfo= new RfidInfo() { Rfid = value, StationId = 1, ModelsInfo=modelsinfo, State="入料中", StartTime=DateTime.Now };
  703. if ( RfIDList.ToList().Find(x => x.Rfid == value) == null )
  704. {
  705. RfIDList.Add(rfidInfo);
  706. // RuningData.Add(rfidInfo);
  707. }
  708. });
  709. }
  710. }
  711. catch ( Exception e )
  712. {
  713. LogHelper.Error("网板读取获取数据异常,原因:" + e.ToString());
  714. }
  715. }
  716. else
  717. {
  718. LogHelper.Info("未读取到网板编号");
  719. //WriteInfo($"未读取到网板编号");
  720. }
  721. }
  722. catch
  723. {
  724. }
  725. finally
  726. {
  727. }
  728. }
  729. /// <summary>
  730. /// 上传生产记录
  731. /// </summary>
  732. /// <param name="RfidId">从扫码枪获取的网板Id</param>
  733. /// <returns></returns>
  734. public string UpdateRecord(string RfidId)
  735. {
  736. try
  737. {
  738. Rt_ProductionRecordDto rt_ProductionRecord = new Rt_ProductionRecordDto()
  739. {
  740. userId = UserId,///用户Id
  741. creationTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz"),///生产时间
  742. deviceId = CurentApplicationSettings.DeviceId,///设备编号
  743. productionType = "UVcuringNextTreat",///生产工序,固定不变
  744. description = new List<DescriptionItem>()
  745. {
  746. ///从扫码枪获取的网板Id
  747. new DescriptionItem(){ key="containerId",value=RfidId},
  748. //后处理配方(根据设备不同参数也不同,具体需要上传什么参数要具体咨询下现场的工艺人员(程朋))
  749. new DescriptionItem(){ key="CentrifugalTime1",value=Formulasdata.第一次离心时间},
  750. new DescriptionItem(){ key="CentrifugalTime2",value=Formulasdata.第二次离心时间},
  751. new DescriptionItem(){ key="LightSolidifiedTime",value=Formulasdata.光固化时间},
  752. }
  753. };
  754. var recordData = _httpService.PostRecord(rt_ProductionRecord);
  755. if ( recordData.Success )
  756. {
  757. LogHelper.MesInfo($"网板ID:{RfidId}上传记录成功");
  758. return "";
  759. }
  760. else
  761. {
  762. LogHelper.MesInfo($"网板ID:{RfidId}上传记录失败,Code:{( int ) recordData.Code},原因:{recordData.RawText}");
  763. /////记录至数据库
  764. //Rt_AngelNetRecordDto recordDto = new Rt_AngelNetRecordDto()
  765. //{
  766. // DevcieId = DataPathcs.GetConnectionStringsConfig("DeviceID"),
  767. // EquipmentType = 4,
  768. // UserId = UserName,
  769. // descriptions = new List<Description>() {
  770. // new Description(){ Key="BoardId",KeyValue=RfidId },
  771. // new Description(){ Key="Code",KeyValue=recordData.Code.ToString() },
  772. // new Description(){ Key="RawText",KeyValue=recordData.RawText }
  773. // }
  774. //};
  775. //var recorddata = alignServer.InsertProductionRecord(recordDto);
  776. return $"{recordData.RawText}";
  777. }
  778. }
  779. catch ( Exception ex )
  780. {
  781. LogHelper.Error($"网板ID:{RfidId}上传记录异常,原因{ex.Message}");
  782. return ex.ToString();
  783. }
  784. }
  785. /// <summary>
  786. /// 小票打印
  787. /// </summary>
  788. /// <param name="printinfo"></param>
  789. /// <param name="Position"></param>
  790. public async Task PrintInfo(ProcessDataModel printinfo, string Position)
  791. {
  792. try
  793. {
  794. var pmTitle = new PrintModel
  795. {
  796. FontFamily = "宋体",
  797. FontSize = 15,
  798. IsBold = true,
  799. Text = new StringReader("\n" + "<<<<后处理收料信息>>>>") //首行需要空行,某些打印机首行不为空时会出现“首行乱码问题”
  800. };
  801. var count = 0;
  802. if ( printinfo.SolidifyStation != 0 )
  803. {
  804. count = printinfo.SolidifyStation % 2 == 1 ? 1 : 2;
  805. }
  806. var printContent = new StringBuilder();
  807. printContent.Append("<<<----------------------------->>>" + "\n");
  808. printContent.Append($"出料窗口:{CurentApplicationSettings.DeviceId}" + $",{Position}传送带,第{count}板" + "\n");
  809. printContent.Append($"网板编号:{printinfo.BoardId}" + "\n");
  810. printContent.Append("出票时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n");
  811. printContent.Append("<<<----------------------------->>>" + "\n");
  812. printContent.Append(" " + "\n");
  813. var batchs = new PrintModel
  814. {
  815. FontFamily = "宋体",
  816. FontSize = 12,
  817. IsBold = true,
  818. Text = new StringReader(printContent.ToString())
  819. };
  820. var sb = new StringBuilder();
  821. if ( !string.IsNullOrEmpty(printinfo.Tip) )
  822. {
  823. sb.Append($"记录上传异常:{printinfo.Tip}" + "\n");
  824. }
  825. sb.Append($"3D打印设备:{printinfo.PrintDeviceId}" + "\n");
  826. sb.Append($"批次:{printinfo.ModelsInfo}" + "\n");
  827. sb.Append($"膜片规格:{printinfo.MaterialType}" + "\n");
  828. sb.Append($"类型:{printinfo.PrintTagProduct}" + "\n");
  829. sb.Append($"品牌:{printinfo.Brand}" + "\n");
  830. sb.Append($"是否Kid:{( printinfo.IsKid == true ? "是" : "否" )}" + "\n");
  831. sb.Append($"是否海外:{( printinfo.IsOverSea == true ? "是" : "否" )}" + "\n");
  832. sb.Append($"大小载具:{printinfo.IsLarge}" + "\n");
  833. sb.Append($"数量:{printinfo.Count.ToString()}" + "\n");
  834. var pmContent2 = new PrintModel
  835. {
  836. FontFamily = "宋体",
  837. FontSize = 10,
  838. IsBold = false,
  839. Text = new StringReader(sb.ToString().Trim())
  840. };
  841. PrintModel[] pms = { pmTitle, batchs, pmContent2 };
  842. LogHelper.Info($"打印内容{printContent}" + "\n" + sb + "\n" + "<<<------------------------------->>>");
  843. TicketPrinterHelper.Print(pms, CurentApplicationSettings.PrinterName);
  844. }
  845. catch ( Exception ex )
  846. {
  847. LogHelper.Error("打印异常,原因:" + ex.ToString());
  848. }
  849. }
  850. }
  851. }