Brak opisu

MainWindow.xaml.cs 96KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Globalization;
  15. using WpfLocalization;
  16. using System.Threading;
  17. using System.Data.SqlClient;
  18. using System.Data;
  19. using System.Diagnostics;
  20. using System.IO;
  21. using System.Collections;
  22. using System.IO.Ports;
  23. using System.Collections.ObjectModel;
  24. using System.Reflection;
  25. using System.Configuration;
  26. using System.Collections.Specialized;
  27. using System.Data.SqlServerCe;
  28. using System.ComponentModel;
  29. using System.Windows.Threading;
  30. using System.Data.OleDb;
  31. namespace WpfApplication19
  32. {
  33. /// <summary>
  34. /// Interaction logic for MainWindow.xaml
  35. /// </summary>
  36. ///
  37. public partial class MainWindow : Window
  38. {
  39. private bool isFullScreen = false;
  40. private Debug dw;
  41. public SqlConnection configConn, masterConn, transConn, proConn;
  42. private SqlCeConnection _opConn;
  43. //private SqlDataReader reader;
  44. string[] obTables = new string[] {"BEL-MAS-1", "BEL-MAS-3", "E-MAS-1", "E-MAS-3",
  45. "MG-MAS-1", "MG-MAS-3", "V-MAS-1", "V-MAS-3"};
  46. public SerialPort comport = new SerialPort();
  47. ObservableCollection<MeasurePoint> mps;
  48. int nextCol = 3;
  49. bool isMiddle = false, _haveMiddle = false;
  50. bool _isIn = false;
  51. public string inStarGS = "";
  52. public string midGS = "";
  53. string sMi16, sMi20, kind;
  54. enum RecordTypes { TopUnder, InOut }
  55. string[] masViews = new string[] { "bel_master_view", "e_master_view", "mg_master_view", "v_master_view" };
  56. List<string> list;
  57. string header;
  58. Stack<string> _lastAction = new Stack<string>();
  59. bool _isTkx = false;
  60. DateTime _lastTimeDataPort;
  61. bool _isCheck = false;
  62. DailyCheck _dc;
  63. Encoding encAscii = Encoding.ASCII;
  64. string obCondition;
  65. Dictionary<string, string> rolesDescription = new Dictionary<string, string>
  66. {
  67. {"qa_staff", "QA Staff"},{"user","User"}, {"admin", "Administrator"}, {"qa_man", "QA Ass't Manager"}
  68. };
  69. Dictionary<string, string> _sideMap = new Dictionary<string, string>
  70. {
  71. {"out", "in"},{"in","under"}, {"rim", "base"}, {"top", "under"}, {"outer", "inner"}
  72. };
  73. private double out0, out1;
  74. private double in0, in1;
  75. MeasurePoint currentMP;
  76. string currentColumn;
  77. Debug _debugWindow;
  78. Thread _aThread, _bThread;
  79. bool _clearThread = false, _canSent = true;
  80. int _pattern = 1;
  81. string _updated_at;
  82. private int _obLimit, _obCount, _ocLimit, _ocCount, _rhLimit, _rhCount;
  83. bool _isOver = false;
  84. bool _isFirstMeasure = true;
  85. DateTime _startUsing = new DateTime();
  86. int _usingTime;
  87. bool _isExisting = false;
  88. private List<string> _agreementList = new List<string>();
  89. string _currentValue, _prevValue = null;
  90. double _lower1, _upper1, _lower2, _upper2;
  91. string strAccessConnEMASTER = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.168.0.253\\mgt\\master\\E-Master.mdb;Mode=Share Deny None";
  92. string strAccessConnBELMASTER = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.168.0.253\\mgt\\master\\BEL-Master.mdb;Mode=Share Deny None";
  93. string strAccessConnMGMASTER = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.168.0.253\\mgt\\master\\MG-Master.mdb;Mode=Share Deny None";
  94. string strAccessConnVMASTER = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.168.0.253\\mgt\\master\\V-Master.mdb;Mode=Share Deny None";
  95. OleDbConnection accessConnEMASTER = null;
  96. OleDbConnection accessConnBELMASTER = null;
  97. OleDbConnection accessConnMGMASTER = null;
  98. OleDbConnection accessConnVMASTER = null;
  99. OleDbConnection tempConn = null;
  100. public MainWindow()
  101. {
  102. InitializeComponent();
  103. //dw = new Debug();
  104. //dw.Show();
  105. Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
  106. //Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
  107. //Thread.CurrentThread.CurrentCulture = ci;
  108. //OBInterface obInterface = new OBInterface(comport);
  109. /*
  110. _debugWindow = new Debug();
  111. _debugWindow.Topmost = true;
  112. _debugWindow.Show();
  113. */
  114. LoginWindow loginWindow = new LoginWindow();
  115. var r = loginWindow.ShowDialog();
  116. if (r == false)
  117. return;
  118. //this.Hide();
  119. //loginWindow.Topmost = true;
  120. initSys();
  121. //forTesting();
  122. //for old ob box
  123. //_aThread = new Thread(new ThreadStart(Sender));
  124. _aThread = new Thread(new ThreadStart(Sender2));
  125. _aThread.Start();
  126. DispatcherTimer _timer = new DispatcherTimer();
  127. lotNo.Focus();
  128. }
  129. public void ResetHeaderUsage()
  130. {
  131. _isOver = false;
  132. if (header == "OB")
  133. {
  134. _obCount = 0;
  135. }
  136. else if (header == "OC")
  137. {
  138. _ocCount = 0;
  139. }
  140. else if (header == "RH")
  141. {
  142. _rhCount = 0;
  143. }
  144. }
  145. public void Sender2()
  146. {
  147. while (comport.IsOpen && _clearThread == false)
  148. {
  149. //Console.WriteLine("Hello");
  150. //string readCommand = String.Format("{0}{1}{2}{3}",0x002, 0x052, 0x031, 0x00D);
  151. string readCommand = encAscii.GetString(new byte[] { 0x02, 0x52, 0x31, 0x0D });
  152. if (_canSent == true)
  153. {
  154. try
  155. {
  156. comport.Write(readCommand);
  157. Thread.Sleep(500);
  158. }
  159. catch
  160. {
  161. Thread.Sleep(1000);
  162. }
  163. }
  164. /*
  165. if (!_debugWindow.Dispatcher.CheckAccess())
  166. {
  167. _debugWindow.Dispatcher.Invoke(
  168. System.Windows.Threading.DispatcherPriority.Normal,
  169. new Action(
  170. delegate()
  171. {
  172. _debugWindow.debugTextBox.AppendText("Sending\n");
  173. }
  174. ));
  175. }
  176. else
  177. {
  178. _debugWindow.debugTextBox.AppendText("Sending\n");
  179. }*/
  180. }
  181. }
  182. public void Sender()
  183. {
  184. while (comport.IsOpen && _clearThread == false)
  185. {
  186. //Console.WriteLine("Hello");
  187. comport.Write("S");
  188. Thread.Sleep(500);
  189. /*
  190. if (!_debugWindow.Dispatcher.CheckAccess())
  191. {
  192. _debugWindow.Dispatcher.Invoke(
  193. System.Windows.Threading.DispatcherPriority.Normal,
  194. new Action(
  195. delegate()
  196. {
  197. _debugWindow.debugTextBox.AppendText("Sending\n");
  198. }
  199. ));
  200. }
  201. else
  202. {
  203. _debugWindow.debugTextBox.AppendText("Sending\n");
  204. }*/
  205. }
  206. }
  207. public void AutoSaveData()
  208. {
  209. //Thread.Sleep(1500);
  210. if (_prevValue == null)
  211. {
  212. _prevValue = _currentValue;
  213. }
  214. TimeSpan ts = DateTime.Now - _lastTimeDataPort;
  215. //if (ts.Seconds >= 1 && _lastTimeDataPort != DateTime.MinValue)
  216. if (_lastTimeDataPort != DateTime.MinValue)
  217. {
  218. _lastTimeDataPort = DateTime.MinValue;
  219. if (_prevValue == _currentValue)
  220. {
  221. saveCellBtn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent, saveCellBtn));
  222. }
  223. else
  224. {
  225. _prevValue = null;
  226. }
  227. }
  228. }
  229. private void forTesting()
  230. {
  231. var r = GetRecordType("114-78469F");
  232. Console.WriteLine(r);
  233. }
  234. private void initSys()
  235. {
  236. initDatabaseConnection();
  237. initCOMPort();
  238. //initMachineList();
  239. SqlDataReader reader = Utils.Query(proConn, "select * from sys_configs");
  240. string title;
  241. resetMenu();
  242. while (reader.Read())
  243. {
  244. title = reader["title"].ToString();
  245. //value = reader["value"];
  246. switch (title)
  247. {
  248. case "ob_limit":
  249. _obLimit = Convert.ToInt32(reader["value"]);
  250. break;
  251. case "oc_limit":
  252. _ocLimit = Convert.ToInt32(reader["value"]);
  253. break;
  254. case "rh_limit":
  255. _rhLimit = Convert.ToInt32(reader["value"]);
  256. break;
  257. default:
  258. //MessageBox.Show("Bad config variable :"+title);
  259. break;
  260. }
  261. }
  262. reader.Close();
  263. }
  264. private void resetMenu()
  265. {
  266. if (GlobalVars.user.empid == "root" ||
  267. GlobalVars.user.roles.Find(
  268. delegate(string s)
  269. {
  270. return s == "admin";
  271. }) != null)
  272. {
  273. configMenu.Visibility = Visibility.Visible;
  274. }
  275. else
  276. {
  277. configMenu.Visibility = Visibility.Collapsed;
  278. }
  279. }
  280. public void setFilterLogData(string q)
  281. {
  282. _isExisting = true;
  283. this._updated_at = q;
  284. }
  285. private List<string> GetRecordType(string pro2)
  286. {
  287. foreach (var view in masViews)
  288. {
  289. SqlCommand cmd = proConn.CreateCommand();
  290. string sql = @"
  291. select * from [" + view + "] v where v.PRO2='" + pro2 + "'";
  292. cmd.CommandText = sql;
  293. //Console.WriteLine(sql);
  294. SqlDataReader reader = cmd.ExecuteReader();
  295. if (!reader.HasRows)
  296. {
  297. reader.Close();
  298. continue;
  299. }
  300. bool hasIn, hasOut;
  301. while (reader.Read())
  302. {
  303. var mi17 = reader["MI17"].ToString();
  304. if (string.IsNullOrEmpty(mi17) || mi17 == "NULL")
  305. {
  306. hasOut = false;
  307. }
  308. else
  309. {
  310. hasOut = true;
  311. }
  312. string mi21 = reader["MI21"].ToString();
  313. if (string.IsNullOrEmpty(mi21) || mi21 == "NULL")
  314. {
  315. hasIn = false;
  316. }
  317. else
  318. {
  319. hasIn = true;
  320. }
  321. List<string> rList = new List<string>();
  322. if (_isTkx == true)
  323. {
  324. rList.Add("FA1");
  325. rList.Add("FA2");
  326. //rList.Add("MID");
  327. //rList.Add("IN*");
  328. reader.Close();
  329. point1Label.Content = "FA";
  330. point2Label.Content = "None";
  331. return rList;
  332. }
  333. if (view == "bel_master_view" || view == "e_master_view" || view == "mg_master_view")
  334. {
  335. if (reader["MP45"].ToString() != "NULL" && !string.IsNullOrEmpty(reader["MP45"].ToString().Trim()))
  336. {
  337. rList.Add(reader["MP45"].ToString().ToUpper());
  338. }
  339. point1Label.Content = reader["MP45"].ToString().ToUpper();
  340. //rList.Add("MID");
  341. //rList.Add("IN*");
  342. if (reader["MP49"].ToString() != "NULL" && !string.IsNullOrEmpty(reader["MP49"].ToString().Trim()))
  343. {
  344. rList.Add(reader["MP49"].ToString().ToUpper());
  345. point2Label.Content = reader["MP49"].ToString().ToUpper();
  346. }
  347. else
  348. {
  349. if (hasIn == true)
  350. {
  351. var side = reader["MP45"].ToString().ToLower();
  352. rList.Add(_sideMap[side].ToUpper());
  353. point2Label.Content = _sideMap[side].ToUpper();
  354. }
  355. }
  356. reader.Close();
  357. return rList;
  358. }
  359. else
  360. {
  361. if (reader["MP34"].ToString() != "NULL" && !string.IsNullOrEmpty(reader["MP34"].ToString().Trim()))
  362. {
  363. rList.Add(reader["MP34"].ToString().ToUpper());
  364. }
  365. point1Label.Content = reader["MP34"].ToString().ToUpper();
  366. //rList.Add("MID");
  367. //rList.Add("IN*");
  368. if (reader["MP39"].ToString() != "NULL" && !string.IsNullOrEmpty(reader["MP39"].ToString().Trim()))
  369. {
  370. rList.Add(reader["MP39"].ToString().ToUpper());
  371. point2Label.Content = reader["MP39"].ToString().ToUpper();
  372. }
  373. else
  374. {
  375. if (hasIn == true)
  376. {
  377. var side = reader["MP34"].ToString().ToLower();
  378. rList.Add(_sideMap[side].ToUpper());
  379. point2Label.Content = _sideMap[side].ToUpper();
  380. }
  381. }
  382. reader.Close();
  383. return rList;
  384. }
  385. }
  386. }
  387. return null;
  388. }
  389. //for old ob box
  390. void DataReceivedHandler(
  391. object sender,
  392. SerialDataReceivedEventArgs e)
  393. {
  394. SerialPort sp = (SerialPort)sender;
  395. byte[] data = new byte[2];
  396. data[0] = (byte)sp.ReadByte();
  397. data[1] = (byte)sp.ReadByte();
  398. //byte[] data = new byte[2];
  399. //int nByteRead = sp.Read(data, 0, 2);
  400. _lastTimeDataPort = DateTime.Now;
  401. byte[] bcddata = new byte[4];
  402. byte anddata = 240;
  403. byte tempdata;
  404. if (_pattern == 1)
  405. {
  406. anddata = (byte)240;
  407. bcddata[1] = (byte)(data[0] & anddata);
  408. bcddata[1] = (byte)(bcddata[1] >> 4);
  409. bcddata[3] = (byte)(data[1] & anddata);
  410. bcddata[3] = (byte)(bcddata[3] >> 4);
  411. anddata = (byte)1;
  412. bcddata[0] = (byte)(data[0] & (byte)anddata);
  413. anddata = (byte)15;
  414. bcddata[2] = (byte)(data[1] & (byte)anddata);
  415. tempdata = bcddata[3];
  416. bcddata[3] = (byte)(tempdata & (byte)1);
  417. bcddata[3] = (byte)(bcddata[3] << 1);
  418. tempdata = (byte)(tempdata >> 1);
  419. bcddata[3] = (byte)(bcddata[3] | (tempdata & (byte)1));
  420. bcddata[3] = (byte)(bcddata[3] << 1);
  421. tempdata = (byte)(tempdata >> 1);
  422. bcddata[3] = (byte)(bcddata[3] | (tempdata & (byte)1));
  423. bcddata[3] = (byte)(bcddata[3] << 1);
  424. tempdata = (byte)(tempdata >> 1);
  425. bcddata[3] = (byte)(bcddata[3] | (tempdata & (byte)1));
  426. tempdata = bcddata[2];
  427. bcddata[2] = (byte)(tempdata & (byte)1);
  428. bcddata[2] = (byte)(bcddata[2] << 1);
  429. tempdata = (byte)(tempdata >> 1);
  430. bcddata[2] = (byte)(bcddata[2] | (tempdata & (byte)1));
  431. bcddata[2] = (byte)(bcddata[2] << 1);
  432. tempdata = (byte)(tempdata >> 1);
  433. bcddata[2] = (byte)(bcddata[2] | (tempdata & (byte)1));
  434. bcddata[2] = (byte)(bcddata[2] << 1);
  435. tempdata = (byte)(tempdata >> 1);
  436. bcddata[2] = (byte)(bcddata[2] | (tempdata & (byte)1));
  437. }
  438. else
  439. {
  440. anddata = (byte)240;
  441. bcddata[1] = (byte)(data[0] & anddata);
  442. bcddata[1] = (byte)(bcddata[1] >> 4);
  443. bcddata[3] = (byte)(data[1] & anddata);
  444. bcddata[3] = (byte)(bcddata[3] >> 4);
  445. anddata = (byte)1;
  446. bcddata[0] = (byte)(data[0] & anddata);
  447. anddata = (byte)15;
  448. bcddata[2] = (byte)(data[1] & anddata);
  449. }
  450. Console.WriteLine("Data Received: {0}", data.Length);
  451. foreach (byte element in data)
  452. {
  453. Console.WriteLine("{0} = {1}", element, (char)element);
  454. }
  455. foreach (byte element in bcddata)
  456. {
  457. Console.WriteLine("oo {0} = {1}", element, element);
  458. }
  459. //Console.WriteLine("{0}.{1}{2}", Convert.ToString(bcddata[1]), Convert.ToString(bcddata[2]), Convert.ToString(bcddata[3]));
  460. string outputString = string.Format("{0}.{1}{2}", bcddata[1], bcddata[2], bcddata[3]);
  461. //_currentValue = outputString;
  462. //_currentValue = outputString;
  463. if (!readingBox.Dispatcher.CheckAccess())
  464. {
  465. readingBox.Dispatcher.Invoke(
  466. System.Windows.Threading.DispatcherPriority.Normal,
  467. new Action(
  468. delegate()
  469. {
  470. readingBox.Text = outputString;
  471. if (_dc != null)
  472. {
  473. _dc.measureBlock.Text = outputString;
  474. }
  475. //_dc.measureBlock.Text = outputString;
  476. }
  477. ));
  478. }
  479. //readingBox.Text = outputString;
  480. }
  481. //for new ob box
  482. void DataReceivedHandler2(
  483. object sender,
  484. SerialDataReceivedEventArgs e)
  485. {
  486. _canSent = false;
  487. SerialPort sp = (SerialPort)sender;
  488. byte[] data = new byte[11];
  489. bool readError = false;
  490. for (int i = 0; i < 11; i++)
  491. {
  492. try
  493. {
  494. data[i] = (byte)sp.ReadByte();
  495. }
  496. catch
  497. {
  498. readError = true;
  499. break;
  500. }
  501. }
  502. //data[0] = (byte)sp.ReadByte();
  503. //data[1] = (byte)sp.ReadByte();
  504. if (readError == true)
  505. return;
  506. //byte[] data = new byte[2];
  507. //int nByteRead = sp.Read(data, 0, 2);
  508. _lastTimeDataPort = DateTime.Now;
  509. //Console.WriteLine("{0}.{1}{2}", Convert.ToString(bcddata[1]), Convert.ToString(bcddata[2]), Convert.ToString(bcddata[3]));
  510. //string outputString = string.Format("{0}{1}{2}{3}{4}{5}{6}", data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
  511. string outputString = encAscii.GetString(new byte[] { data[3], data[4], data[5], data[6], data[7] });
  512. //_currentValue = outputString;
  513. //_currentValue = outputString;
  514. if (!readingBox.Dispatcher.CheckAccess())
  515. {
  516. readingBox.Dispatcher.Invoke(
  517. System.Windows.Threading.DispatcherPriority.Normal,
  518. new Action(
  519. delegate()
  520. {
  521. if (data[9] == '1')
  522. {
  523. readingBox.Text = "Error";
  524. }
  525. else
  526. {
  527. readingBox.Text = outputString;
  528. saveCellBtn_Click(null, null);
  529. }
  530. if (_dc != null)
  531. {
  532. _dc.measureBlock.Text = outputString;
  533. }
  534. //_dc.measureBlock.Text = outputString;
  535. }
  536. ));
  537. }
  538. _canSent = true;
  539. //readingBox.Text = outputString;
  540. }
  541. private void initCOMPort()
  542. {
  543. if (comport.IsOpen)
  544. comport.Close();
  545. else
  546. {
  547. comport.PortName = Utils.getSetting("machinePort");
  548. /*
  549. comport.BaudRate = int.Parse(Utils.getSetting("baudRate"));
  550. comport.DataBits = int.Parse(Utils.getSetting("dataBits"));
  551. comport.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Utils.getSetting("stopBits"));
  552. comport.Parity = (Parity)Enum.Parse(typeof(Parity), Utils.getSetting("parity"));
  553. */
  554. // for old ob box version
  555. //comport.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
  556. // for new ob
  557. comport.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler2);
  558. bool error = false;
  559. try
  560. {
  561. comport.RtsEnable = true;
  562. comport.Open();
  563. Thread.Sleep(1000);
  564. //comport.Close();
  565. //MessageBox.Show("Open Port Success");
  566. }
  567. catch (UnauthorizedAccessException) { error = true; }
  568. catch (IOException) { error = true; }
  569. catch (ArgumentException) { error = true; }
  570. if (error)
  571. {
  572. MessageBox.Show("Open Port Fail");
  573. }
  574. }
  575. }
  576. private void initDatabaseConnection()
  577. {
  578. try
  579. {
  580. _opConn = Utils.createSqlCeConnection("operatingDbCS");
  581. // Sample usage
  582. /*
  583. SqlCeDataReader reader;
  584. reader = Utils.Query(_opConn, "select * from UsingTime");
  585. while(reader.Read()){
  586. Console.WriteLine("hello");
  587. }
  588. reader.Close();
  589. */
  590. }
  591. catch (SqlCeException se)
  592. {
  593. MessageBox.Show("operatingDbCS : ", se.Message);
  594. }
  595. /*
  596. try
  597. {
  598. configConn = Utils.createSqlConnection("configDbCS");
  599. }
  600. catch (SqlException se)
  601. {
  602. MessageBox.Show("configDbCS : ", se.Message);
  603. }
  604. try
  605. {
  606. masterConn = Utils.createSqlConnection("masterDbCS");
  607. }
  608. catch (SqlException se)
  609. {
  610. MessageBox.Show("masterDbCS : ", se.Message);
  611. }
  612. try
  613. {
  614. transConn = Utils.createSqlConnection("tranDbCS");
  615. }
  616. catch (SqlException se)
  617. {
  618. MessageBox.Show("tranDbCS : ", se.Message);
  619. }
  620. */
  621. try
  622. {
  623. proConn = Utils.createSqlConnection("productionDbCS");
  624. transConn = Utils.createSqlConnection("productionDbCS");
  625. masterConn = Utils.createSqlConnection("productionDbCS");
  626. configConn = Utils.createSqlConnection("productionDbCS");
  627. }
  628. catch (SqlException se)
  629. {
  630. MessageBox.Show("productionDbCS : ", se.Message);
  631. }
  632. //mod: change config by move every table alias
  633. //transConn = masterConn = configConn = proConn;
  634. /*
  635. try
  636. {
  637. accessConnEMASTER = new OleDbConnection(strAccessConnEMASTER);
  638. accessConnEMASTER.Open();
  639. }
  640. catch (Exception ex)
  641. {
  642. MessageBox.Show("Error: Failed to create e-master connection. \n{0}", ex.Message);
  643. }
  644. try
  645. {
  646. accessConnBELMASTER = new OleDbConnection(strAccessConnBELMASTER);
  647. accessConnBELMASTER.Open();
  648. }
  649. catch (Exception ex)
  650. {
  651. MessageBox.Show("Error: Failed to create e-master connection. \n{0}", ex.Message);
  652. }
  653. try
  654. {
  655. accessConnMGMASTER = new OleDbConnection(strAccessConnMGMASTER);
  656. accessConnMGMASTER.Open();
  657. }
  658. catch (Exception ex)
  659. {
  660. MessageBox.Show("Error: Failed to create e-master connection. \n{0}", ex.Message);
  661. }
  662. try
  663. {
  664. accessConnVMASTER = new OleDbConnection(strAccessConnVMASTER);
  665. accessConnVMASTER.Open();
  666. }
  667. catch (Exception ex)
  668. {
  669. MessageBox.Show("Error: Failed to create e-master connection. \n{0}", ex.Message);
  670. } */
  671. }
  672. private void testDB()
  673. {
  674. /*
  675. string server = Utils.getSetting("dbServer");
  676. string user = Utils.getSetting("dbUser");
  677. string passwd = Utils.getSetting("dbPassword");
  678. string dbName = Utils.getSetting("dbName");
  679. var connectionString = "Data Source=" + server + ";User ID=" + user + ";Password=" + passwd + "; Initial Catalog=" + dbName;
  680. Console.WriteLine("cs = {0}", connectionString);
  681. SqlConnection sqlConnection = new SqlConnection(connectionString);
  682. SqlCommand catCMD = sqlConnection.CreateCommand();
  683. catCMD.CommandText = "SELECT count(*) as c FROM [V-MAS-3]";
  684. sqlConnection.Open();
  685. reader = catCMD.ExecuteReader();
  686. while (reader.Read())
  687. {
  688. Console.WriteLine("{0}", reader["c"]);
  689. }
  690. reader.Close();
  691. */
  692. }
  693. private void MenuItem_Click(object sender, RoutedEventArgs e)
  694. {
  695. Settings w = new Settings();
  696. w.Show();
  697. }
  698. private void closeWindows(object sender, EventArgs e)
  699. {
  700. //sqlConnection.Close();
  701. //_aThread.Join();
  702. // comport.Close();
  703. //_clearThread = true;
  704. //_debugWindow.Close();
  705. //_bThread.Abort();
  706. //dw.Close();
  707. }
  708. private void button1_Click(object sender, RoutedEventArgs e)
  709. {
  710. testDB();
  711. }
  712. private bool isAlreadyCheck(string ln)
  713. {
  714. SqlCommand catCMD = proConn.CreateCommand();
  715. string sql = @"SELECT * FROM lot_summary where lot_no = '" + ln + "'";
  716. catCMD.CommandText = sql;
  717. SqlDataReader reader = catCMD.ExecuteReader();
  718. bool r = reader.HasRows;
  719. reader.Close();
  720. return r;
  721. }
  722. private void find_Click(object sender, RoutedEventArgs e)
  723. {
  724. string ln = lotNo.Text;
  725. _startUsing = DateTime.Now;
  726. bool isFound = false;
  727. isMiddle = _isIn = false;
  728. _isExisting = false;
  729. _isCheck = false;
  730. _isTkx = false;
  731. midGS = "";
  732. inStarGS = "";
  733. measureGrid.ItemsSource = null;
  734. measureGrid.Items.Clear();
  735. nextCol = 3;
  736. middleText.Text = "Add Middle";
  737. inText.Text = "Add IN*";
  738. foreach (var view in masViews)
  739. {
  740. isFound = fillProData(ln, view);
  741. if (isFound == true)
  742. break;
  743. }
  744. if (isFound == false)
  745. {
  746. MessageBox.Show("Lot No.:" + ln + " Not Founded");
  747. return;
  748. }
  749. mps = new ObservableCollection<MeasurePoint>();
  750. list = GetRecordType(ln);
  751. if (list.Count == 0)
  752. {
  753. list.Add("OUT");
  754. list.Add("IN");
  755. point1Label.Content = "OUT";
  756. point2Label.Content = "IN";
  757. }
  758. if (isAlreadyCheck(ln))
  759. {
  760. //ChooseRecord cr = new ChooseRecord(ln);
  761. //cr.ShowDialog();
  762. _isCheck = true;
  763. ChooseRecord2 cr = new ChooseRecord2(ln);
  764. cr.ShowDialog();
  765. SqlDataReader reader = Utils.Query(proConn, "select * from data where lot_no = '" + ln + "' and updated_at = '" + this._updated_at + "' order by id");
  766. isMiddle = false;
  767. _haveMiddle = false;
  768. while (reader.Read())
  769. {
  770. var mp = new MeasurePoint();
  771. nextCol = Convert.ToInt32(reader["ndata"]);
  772. mp.Count = Convert.ToInt16(reader["row_no"]);
  773. mp.Type = reader["r_type"].ToString();
  774. if (mp.Type == "MID")
  775. {
  776. isMiddle = true;
  777. _haveMiddle = true;
  778. middleText.Text = "Remove Middle";
  779. }
  780. mp.SubOrder = Convert.ToInt16(reader["sub_order"]);
  781. mp.P1 = Math.Round(Convert.ToDouble(reader["p1"]), 2);
  782. mp.P2 = Math.Round(Convert.ToDouble(reader["p2"]), 2);
  783. mp.P3 = Math.Round(Convert.ToDouble(reader["p3"]), 2);
  784. mp.P4 = Math.Round(Convert.ToDouble(reader["p4"]), 2);
  785. mp.P5 = Math.Round(Convert.ToDouble(reader["p5"]), 2);
  786. mp.P6 = Math.Round(Convert.ToDouble(reader["p6"]), 2);
  787. mp.P7 = Math.Round(Convert.ToDouble(reader["p7"]), 2);
  788. mp.P8 = Math.Round(Convert.ToDouble(reader["p8"]), 2);
  789. mp.P9 = Math.Round(Convert.ToDouble(reader["p9"]), 2);
  790. mp.P10 = Math.Round(Convert.ToDouble(reader["p10"]), 2);
  791. mp.Avg = Math.Round(Convert.ToDouble(reader["avg"]), 2);
  792. mp.Grade = reader["grade"].ToString();
  793. mp.ResultGrade = reader["rgrade"].ToString();
  794. mp.Type = reader["r_type"].ToString();
  795. mps.Add(mp);
  796. // if row['sub_order'] == 1 and not have middle
  797. //add dummy mid row
  798. //if (Convert.ToInt32(reader["sub_order"]) == 1 && _haveMiddle == false)
  799. // mps.Add(new MeasurePoint() { Count = mp.Count, Type = "MID", SubOrder = 2 });
  800. }
  801. reader.Close();
  802. }
  803. else
  804. {
  805. for (int i = 0; i < 3; i++)
  806. {
  807. int count = 1;
  808. foreach (var l in list)
  809. {
  810. mps.Add(new MeasurePoint() { Count = i + 1, Type = l, SubOrder = count });
  811. count++;
  812. }
  813. }
  814. if (_isTkx == true)
  815. {
  816. measureGrid.Columns.ElementAt(nextCol--).Visibility = Visibility.Hidden;
  817. }
  818. }
  819. measureGrid.ItemsSource = mps;
  820. for (int i = 1; i < 10; i++)
  821. {
  822. measureGrid.Columns.ElementAt(i).Visibility = Visibility.Hidden;
  823. }
  824. for (int i = 1; i <= nextCol; i++)
  825. {
  826. measureGrid.Columns.ElementAt(i).Visibility = Visibility.Visible;
  827. }
  828. measureGrid.Focus();
  829. try
  830. {
  831. measureGrid.SelectedItem = measureGrid.Items[0];
  832. measureGrid.CurrentCell = new DataGridCellInfo(
  833. measureGrid.Items[0], measureGrid.Columns[1]);
  834. measureGrid.BeginEdit();
  835. }
  836. catch { }
  837. }
  838. private string getConditionRef(string id)
  839. {
  840. if (string.IsNullOrEmpty(id))
  841. {
  842. return "None";
  843. }
  844. SqlCommand cmd = configConn.CreateCommand();
  845. string sql = @"
  846. select * from [TABLE_TI-T4] t4 where t4.TIT4 =" + id;
  847. cmd.CommandText = sql;
  848. Console.WriteLine(sql);
  849. SqlDataReader reader = cmd.ExecuteReader();
  850. string condition = "";
  851. while (reader.Read())
  852. {
  853. condition = reader["TIT4NE"].ToString();
  854. }
  855. reader.Close();
  856. return condition;
  857. }
  858. private bool fillProData(string ln, string viewName)
  859. {
  860. /*
  861. join [V-MAS-3] v
  862. on p.PRO1 = v.TC
  863. left outer join [TABLE_TI-T4] t4
  864. on v.MI5 = t4.TIT4
  865. */
  866. string sql = @"
  867. SELECT * FROM " + viewName + " p " +
  868. "where p.PRO2 = '" + ln + "'";
  869. SqlDataReader reader = Utils.Query(proConn, sql);
  870. string pro1 = "";
  871. if (!reader.HasRows)
  872. {
  873. reader.Close();
  874. return false;
  875. }
  876. while (reader.Read())
  877. {
  878. string[] t = reader["MC20"].ToString().Split('/');
  879. string mc24 = reader["MC24"].ToString();
  880. string kind = reader["PRO17"].ToString();
  881. int gainSize;
  882. try
  883. {
  884. gainSize = Convert.ToInt32(t[0]);
  885. }
  886. catch (FormatException fe)
  887. {
  888. gainSize = 0;
  889. }
  890. if (gainSize >= 10 && gainSize <= 100 && kind == "V")
  891. header = "OB";
  892. if (gainSize >= 120 && gainSize <= 220 && kind == "V")
  893. header = "OC";
  894. if (gainSize >= 10 && gainSize <= 100 && mc24 == "MG")
  895. header = "OB";
  896. if (gainSize >= 10 && gainSize <= 100 && kind == "B")
  897. header = "OB";
  898. if (gainSize > 220 && kind == "V")
  899. header = "RH";
  900. if (gainSize > 120 && kind == "B")
  901. header = "RH";
  902. if (headerLabel.Content != header)
  903. {
  904. MessageBox.Show("Please Change Using Header to '" + header + "'");
  905. }
  906. headerLabel.Content = header;
  907. UsingTimeTB.Text = getUsingTime(header).ToString();
  908. Console.WriteLine("{0}", reader["PRO2"]);
  909. string codeString = reader["PRO1"].ToString();
  910. string temp2 = codeString[0] + "-" + codeString.Substring(1, 4) + "-" + codeString.Substring(5, 4) + "-" + codeString.Substring(9, 1);
  911. code.Text = temp2;
  912. pro1 = reader["PRO1"].ToString();
  913. customer.Text = reader["PRO1C"].ToString();
  914. if (customer.Text.Trim().ToLower().StartsWith("tkx"))
  915. {
  916. _isTkx = true;
  917. }
  918. noProduct.Text = reader["PRO5"].ToString();
  919. shape.Text = reader["PRO8"] + " " + reader["PRO9"];
  920. size.Text = reader["PRO10"] + "x" + reader["PRO11"] + "x" + reader["PRO12"];
  921. spec.Text = reader["PRO13"] + " " + reader["PRO14"] + " " + reader["PRO15"] + " " + reader["PRO16"]
  922. + " " + reader["PRO17"] + " " + reader["PRO18"];
  923. kind = reader["PRO17"] as string;
  924. int mi13 = Convert.ToInt32(reader["MI13"]);
  925. string samplingText = "";
  926. if (mi13 == 1)
  927. {
  928. samplingText = "All";
  929. }
  930. else if (mi13 == 2)
  931. {
  932. samplingText = "2 Pcs";
  933. }
  934. else if (mi13 == 3)
  935. {
  936. samplingText = "3 Pcs";
  937. }
  938. else if (mi13 == 4 || mi13 == 5)
  939. {
  940. samplingText = "5 Pcs";
  941. }
  942. sampling.Text = samplingText;
  943. double mi18 = Convert.ToDouble(reader["MI18"]);
  944. double mi19 = Convert.ToDouble(reader["MI19"]);
  945. //Console.WriteLine(mi18);
  946. double o1, o2;
  947. if (mi18 < mi19)
  948. {
  949. o1 = mi18;
  950. o2 = mi19;
  951. }
  952. else
  953. {
  954. o1 = mi19;
  955. o2 = mi18;
  956. }
  957. this.out0 = Math.Round(o1, 2);
  958. this.out1 = Math.Round(o2, 2);
  959. var mi16 = reader["MI16"].ToString();
  960. sMi16 = reader["MI16"].ToString();
  961. sMi16 = sMi16.Replace("\\", "");
  962. Console.WriteLine("mi16 = " + sMi16);
  963. //sMi20 = sMi20.Replace("\\", "");
  964. var mi17 = reader["MI17"].ToString();
  965. if (string.IsNullOrEmpty(mi17) || mi17 == "NULL")
  966. {
  967. mi17 = "None";
  968. }
  969. string temp = String.Format("{0:f} - {1:f} ({2}) {3}", o1, o2, mi16, mi17);
  970. outTB.Text = temp;
  971. _lower1 = o1;
  972. _upper1 = o2;
  973. double mi22 = Convert.ToDouble(reader["MI22"]);
  974. double mi23 = Convert.ToDouble(reader["MI23"]);
  975. var mi20 = reader["MI20"].ToString();
  976. sMi20 = reader["MI20"].ToString();
  977. sMi20 = sMi20.Replace("\\", "");
  978. string mi21 = reader["MI21"].ToString();
  979. if (string.IsNullOrEmpty(mi21) || mi21 == "NULL")
  980. {
  981. mi21 = "None";
  982. }
  983. //Console.WriteLine(mi18);
  984. if (mi22 < mi23)
  985. {
  986. o1 = mi22;
  987. o2 = mi23;
  988. }
  989. else
  990. {
  991. o1 = mi23;
  992. o2 = mi22;
  993. }
  994. this.in0 = Math.Round(o1, 2);
  995. this.in1 = Math.Round(o2, 2);
  996. _lower2 = o1;
  997. _upper2 = o2;
  998. temp = String.Format("{0:f} - {1:f} ({2}) {3} ", o1, o2, mi20, mi21);
  999. inTB.Text = temp;
  1000. point.Text = getConditionRef(reader["MI14"].ToString());
  1001. condition.Text = getConditionRef(reader["MI15"].ToString());
  1002. this.obCondition = reader["ob_condition"].ToString();
  1003. }
  1004. reader.Close();
  1005. reader = initAgreementList(ln, viewName, reader);
  1006. return true;
  1007. //return pro1;
  1008. }
  1009. private SqlDataReader initAgreementList(string ln, string viewName, SqlDataReader reader)
  1010. {
  1011. var sql2 = string.Format(@"select * from (
  1012. select MI53 as ag, PRO2 from {0}
  1013. union
  1014. select MI55 as ag, PRO2 from {0}
  1015. union
  1016. select MI31 as ag, PRO2 from {0}
  1017. union
  1018. select MI33 as ag, PRO2 from {0}
  1019. union
  1020. select INSAGM as ag, PRO2 from {0}
  1021. union
  1022. select MARAGM as ag, PRO2 from {0}
  1023. )x WHERE PRO2 = '{1}'", viewName, ln);
  1024. reader = Utils.Query(proConn, sql2);
  1025. //bool isHaveAgreement = false;
  1026. agreementCB.Items.Clear();
  1027. bool isHaveAgreement = false;
  1028. while (reader.Read())
  1029. {
  1030. //_agreementList.Add(reader["ag"].ToString());
  1031. var temp = reader["ag"].ToString();
  1032. //agreementCB.Text = "hello";
  1033. if (String.IsNullOrEmpty(temp) || temp == "NULL")
  1034. continue;
  1035. isHaveAgreement = true;
  1036. var list = temp.Split(new char[] { '/', ',', '\\' });
  1037. if (list.Length > 1)
  1038. {
  1039. foreach (var l in list)
  1040. {
  1041. if (l.Trim() != "")
  1042. {
  1043. agreementCB.Items.Add(new ComboBoxItem() { Content = l });
  1044. }
  1045. }
  1046. }
  1047. else
  1048. agreementCB.Items.Add(new ComboBoxItem() { Content = temp });
  1049. }
  1050. if (isHaveAgreement == true)
  1051. {
  1052. agreementBtn.IsEnabled = true;
  1053. agreementCB.IsEnabled = true;
  1054. agreementCB.SelectedIndex = 0;
  1055. }
  1056. else
  1057. {
  1058. agreementBtn.IsEnabled = false;
  1059. agreementCB.IsEnabled = false;
  1060. }
  1061. reader.Close();
  1062. return reader;
  1063. }
  1064. private int getUsingTime(string header)
  1065. {
  1066. var reader = Utils.Query(_opConn, "select * from UsingTime");
  1067. reader.Read();
  1068. int v = 0;
  1069. switch (header)
  1070. {
  1071. case "OB":
  1072. v = Convert.ToInt32(reader["ob"]);
  1073. _obCount = v;
  1074. _obLimit = Convert.ToInt32(reader["ob_limit"]);
  1075. break;
  1076. case "OC":
  1077. v = Convert.ToInt32(reader["oc"]);
  1078. _ocCount = v;
  1079. _ocLimit = Convert.ToInt32(reader["oc_limit"]);
  1080. break;
  1081. case "RH":
  1082. v = Convert.ToInt32(reader["rh"]);
  1083. _rhCount = v;
  1084. _rhLimit = Convert.ToInt32(reader["rh_limit"]);
  1085. break;
  1086. default:
  1087. MessageBox.Show("Bad Header Name");
  1088. break;
  1089. }
  1090. reader.Close();
  1091. return v;
  1092. }
  1093. /*
  1094. private void lotNo_TextChanged(object sender, TextChangedEventArgs e)
  1095. {
  1096. //var s = lotNo.Text;
  1097. //if(lotNo.Text.IndexOf('\n') != -1)
  1098. // find_Click(null, null);
  1099. }*/
  1100. private void openScreenKB(object sender, RoutedEventArgs e)
  1101. {
  1102. Console.WriteLine("Open KB");
  1103. }
  1104. private void lotNo_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  1105. {
  1106. }
  1107. private void openScreenKB(object sender, MouseButtonEventArgs e)
  1108. {
  1109. string windir = Environment.GetEnvironmentVariable("WINDIR");
  1110. string osk = null;
  1111. if (osk == null)
  1112. {
  1113. osk = System.IO.Path.Combine(System.IO.Path.Combine(windir, "sysnative"), "osk.exe");
  1114. if (!File.Exists(osk))
  1115. {
  1116. osk = null;
  1117. }
  1118. }
  1119. if (osk == null)
  1120. {
  1121. osk = System.IO.Path.Combine(System.IO.Path.Combine(windir, "system32"), "osk.exe");
  1122. if (!File.Exists(osk))
  1123. {
  1124. osk = null;
  1125. }
  1126. }
  1127. if (osk == null)
  1128. {
  1129. osk = "osk.exe";
  1130. }
  1131. Process.Start(osk);
  1132. }
  1133. private void addRow_Click(object sender, RoutedEventArgs e)
  1134. {
  1135. if (measureGrid.ItemsSource == null)
  1136. return;
  1137. _lastAction.Push("row");
  1138. MeasurePoint m = mps.Last();
  1139. int count = 1;
  1140. foreach (var l in list)
  1141. {
  1142. mps.Add(new MeasurePoint() { Count = m.Count + 1, Type = l, SubOrder = count });
  1143. count++;
  1144. }
  1145. }
  1146. private void addMeasureColumn_Click(object sender, RoutedEventArgs e)
  1147. {
  1148. _lastAction.Push("column");
  1149. if (nextCol <= 10)
  1150. {
  1151. measureGrid.Columns.ElementAt(++nextCol).Visibility = Visibility.Visible;
  1152. foreach (var item in measureGrid.Items)
  1153. {
  1154. //Console.WriteLine(item);
  1155. MeasurePoint c = item as MeasurePoint;
  1156. c.Avg = (c.P1 + c.P2 + c.P3 + c.P4 + c.P6 + c.P7 + c.P8 + c.P9 + c.P10) / nextCol;
  1157. c.Avg = Math.Round(c.Avg, 2);
  1158. if (c.SubOrder == 1)
  1159. {
  1160. c.Grade = computeGrade(sMi16, c.Avg, kind, "-", c.Type);
  1161. }
  1162. else if (c.SubOrder != 1 && c.Type != "MID" && c.Type != "IN*")
  1163. {
  1164. if (sMi20 != "0")
  1165. c.Grade = computeGrade(sMi20, c.Avg, kind, "-", c.Type);
  1166. else
  1167. c.Grade = computeGrade(sMi16, c.Avg, kind, "-", c.Type);
  1168. }
  1169. }
  1170. measureGrid.Items.Refresh();
  1171. }
  1172. }
  1173. private string computeGrade(string size, double lower, string kind, string defValue, string type)
  1174. {
  1175. SqlCommand cmd = configConn.CreateCommand();
  1176. if (type == "MID")
  1177. {
  1178. size = midGS;
  1179. }
  1180. else if (type == "IN*")
  1181. {
  1182. size = inStarGS;
  1183. }
  1184. int lotKind = 0;
  1185. if (code.Text[0] == '1' || code.Text[0] == '2' || code.Text[0] == '9')
  1186. {
  1187. lotKind = 1;
  1188. }
  1189. else if (code.Text[0] == '3' || code.Text[0] == '4' || code.Text[0] == '5')
  1190. {
  1191. lotKind = 2;
  1192. }
  1193. if (double.IsNaN(lower))
  1194. {
  1195. lower = 0;
  1196. }
  1197. string sql = "";
  1198. SqlDataReader reader;
  1199. string value;
  1200. if (lotKind == 1)
  1201. {
  1202. if (_isTkx == true)
  1203. {
  1204. sql = string.Format(@"select top 1 *
  1205. FROM
  1206. (
  1207. select ABS('{1}' - round(TIT2D, 2)) AS diff, * from [TABLE_TI-T2-TKX] where TIT2R = '{0}'
  1208. union all
  1209. select ABS('{1}' - round(TIT2M,2)) AS diff, * from [TABLE_TI-T2-TKX] where TIT2R = '{0}'
  1210. union all
  1211. select ABS('{1}' - round(TIT2U,2)) AS diff, * from [TABLE_TI-T2-TKX] where TIT2R = '{0}'
  1212. )
  1213. x
  1214. order by diff, TIT2G", size, lower);
  1215. }
  1216. else
  1217. {
  1218. sql = string.Format(@"SELECT * FROM [TABLE_TI-T2] WHERE (TIT2R = '{0}') AND (round(TIT2D,2) <= '{1}') AND (round(TIT2U,2) >= '{2}')", size, lower, lower);
  1219. }
  1220. cmd.CommandText = sql;
  1221. reader = cmd.ExecuteReader();
  1222. value = defValue;
  1223. Console.WriteLine(sql);
  1224. while (reader.Read())
  1225. {
  1226. value = reader["TIT2G"].ToString();
  1227. break;
  1228. }
  1229. reader.Close();
  1230. return value;
  1231. }
  1232. else if (lotKind == 2)
  1233. {
  1234. if (_isTkx == true)
  1235. {
  1236. sql = string.Format(@"select top 1 *
  1237. FROM
  1238. (
  1239. select ABS('{1}' - round(TIT2D, 2)) AS diff, * from [TABLE-E_TI-T2-TKX] where TIT2R = '{0}'
  1240. union all
  1241. select ABS('{1}' - round(TIT2M,2)) AS diff, * from [TABLE-E_TI-T2-TKX] where TIT2R = '{0}'
  1242. union all
  1243. select ABS('{1}' - round(TIT2U,2)) AS diff, * from [TABLE-E_TI-T2-TKX] where TIT2R = '{0}'
  1244. )
  1245. x
  1246. order by diff, TIT2G", size, lower);
  1247. }
  1248. else
  1249. {
  1250. sql = string.Format(@"SELECT * FROM [TABLE-E_TI-T2] WHERE (TIT2R = '{0}') AND (round(TIT2D,2) <= '{1}') AND (round(TIT2U,2) >= '{2}')", size, lower, lower);
  1251. }
  1252. cmd.CommandText = sql;
  1253. Console.WriteLine(sql);
  1254. //reader.Close();
  1255. reader = cmd.ExecuteReader();
  1256. value = defValue;
  1257. while (reader.Read())
  1258. {
  1259. value = reader["TIT2G"].ToString();
  1260. break;
  1261. }
  1262. reader.Close();
  1263. return value;
  1264. }
  1265. else
  1266. {
  1267. return "-";
  1268. }
  1269. //return value;
  1270. }
  1271. //int row;
  1272. private void measureGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
  1273. {
  1274. //MessageBox.Show("refresh");
  1275. int row = e.Row.GetIndex();
  1276. (sender as DataGrid).RowEditEnding -= measureGrid_RowEditEnding;
  1277. (sender as DataGrid).CommitEdit();
  1278. var c = mps[row];
  1279. computeValues(c, true);
  1280. measureGrid.Items.Refresh();
  1281. (sender as DataGrid).RowEditEnding += measureGrid_RowEditEnding;
  1282. }
  1283. private void computeValues(MeasurePoint c, bool isCompGrade = false)
  1284. {
  1285. c.Avg = (c.P1 + c.P2 + c.P3 + c.P4 + c.P5 + c.P6 + c.P7 + c.P8 + c.P9 + c.P10) / nextCol;
  1286. c.Avg = Math.Round(c.Avg, 2);
  1287. if (isCompGrade == true)
  1288. {
  1289. if (c.SubOrder == 1)
  1290. {
  1291. c.Grade = computeGrade(sMi16, c.Avg, kind, "-", c.Type);
  1292. }
  1293. else if (c.SubOrder != 1 && c.Type != "MID" && c.Type != "IN*")
  1294. {
  1295. if (sMi20 != "0")
  1296. c.Grade = computeGrade(sMi20, c.Avg, kind, "-", c.Type);
  1297. else
  1298. c.Grade = computeGrade(sMi16, c.Avg, kind, "-", c.Type);
  1299. }
  1300. else if (c.Type == "MID" || c.Type == "IN*")
  1301. {
  1302. c.Grade = computeGrade("0", c.Avg, kind, "-", c.Type);
  1303. }
  1304. }
  1305. if (c.SubOrder == 1 || c.Type == "IN*" || c.Type == "MID" || c.Type == "FA1" || c.Type == "FA2")
  1306. {
  1307. if (this.header == "OB" || this.header == "OC")
  1308. {
  1309. if (c.Avg >= this.out0 && c.Avg <= this.out1)
  1310. {
  1311. c.ResultGrade = "OK";
  1312. }
  1313. else if (c.Avg < this.out0)
  1314. {
  1315. c.ResultGrade = "Hard";
  1316. }
  1317. else if (c.Avg > this.out1)
  1318. {
  1319. c.ResultGrade = "Soft";
  1320. }
  1321. }
  1322. else
  1323. {
  1324. if (c.Avg >= this.out0 && c.Avg <= this.out1)
  1325. {
  1326. c.ResultGrade = "OK";
  1327. }
  1328. else if (c.Avg < this.out0)
  1329. {
  1330. c.ResultGrade = "Soft";
  1331. }
  1332. else if (c.Avg > this.out1)
  1333. {
  1334. c.ResultGrade = "Hard";
  1335. }
  1336. }
  1337. }
  1338. else if (c.SubOrder != 1)
  1339. {
  1340. if (this.header == "OB" || this.header == "OC")
  1341. {
  1342. if (c.Avg >= this.in0 && c.Avg <= this.in1)
  1343. {
  1344. c.ResultGrade = "OK";
  1345. }
  1346. else if (c.Avg < this.in0)
  1347. {
  1348. c.ResultGrade = "Hard";
  1349. }
  1350. else if (c.Avg > this.in1)
  1351. {
  1352. c.ResultGrade = "Soft";
  1353. }
  1354. }
  1355. else
  1356. {
  1357. if (c.Avg >= this.in0 && c.Avg <= this.in1)
  1358. {
  1359. c.ResultGrade = "OK";
  1360. }
  1361. else if (c.Avg < this.in0)
  1362. {
  1363. c.ResultGrade = "Soft";
  1364. }
  1365. else if (c.Avg > this.in1)
  1366. {
  1367. c.ResultGrade = "Hard";
  1368. }
  1369. }
  1370. }
  1371. }
  1372. private void measureGrid_KeyUp(object sender, KeyEventArgs e)
  1373. {
  1374. Console.WriteLine("hello");
  1375. //MessageBox.Show(e.Key.ToString());
  1376. var info = measureGrid.CurrentColumn;
  1377. var item = measureGrid.CurrentItem as MeasurePoint;
  1378. int col = info.DisplayIndex;
  1379. var mp = (MeasurePoint)measureGrid.CurrentItem;
  1380. int row = mps.IndexOf(mp);
  1381. if (e.Key == Key.Tab)
  1382. {
  1383. //measureGrid.RaiseEvent(new RoutedEventArgs(DataGrid.rowediten));
  1384. //MessageBox.Show("row " + item.Count.ToString());
  1385. //MessageBox.Show("row " + mps.IndexOf(mp).ToString());
  1386. //MessageBox.Show("col " + info.DisplayIndex.ToString());
  1387. if (col < nextCol)
  1388. {
  1389. measureGrid.CurrentCell = new DataGridCellInfo(
  1390. measureGrid.Items[row], measureGrid.Columns[col + 1]);
  1391. measureGrid.BeginEdit();
  1392. }
  1393. else
  1394. {
  1395. try
  1396. {
  1397. measureGrid.CurrentCell = new DataGridCellInfo(
  1398. measureGrid.Items[row + 1], measureGrid.Columns[1]);
  1399. measureGrid.BeginEdit();
  1400. }
  1401. catch
  1402. {
  1403. measureGrid.CurrentCell = new DataGridCellInfo(
  1404. measureGrid.Items[0], measureGrid.Columns[1]);
  1405. measureGrid.BeginEdit();
  1406. }
  1407. }
  1408. }
  1409. if (e.Key == Key.Return)
  1410. {
  1411. if (isMiddle == false)
  1412. row += 2;
  1413. if (row > measureGrid.Items.Count)
  1414. {
  1415. measureGrid.CurrentCell = new DataGridCellInfo(
  1416. measureGrid.Items[0], measureGrid.Columns[1]);
  1417. measureGrid.BeginEdit();
  1418. }
  1419. }
  1420. }
  1421. private void measureGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
  1422. {
  1423. /*
  1424. row = e.Row.GetIndex();
  1425. (sender as DataGrid).CellEditEnding -= measureGrid_CellEditEnding;
  1426. (sender as DataGrid).CommitEdit();
  1427. var c = mps[row];
  1428. c.Avg = (c.P1 + c.P2 + c.P3 + c.P4 + c.P6 + c.P7 + c.P8 + c.P9 + c.P10) / (nextCol - 1);
  1429. c.Grade = computeGrade(sMi16, c.Avg, kind, "-");
  1430. (sender as DataGrid).CellEditEnding += measureGrid_CellEditEnding;
  1431. */
  1432. }
  1433. private void measureGrid_LostFocus(object sender, RoutedEventArgs e)
  1434. {
  1435. /*
  1436. measureGrid.LostFocus -= measureGrid_LostFocus;
  1437. measureGrid.Items.Refresh();
  1438. measureGrid.LostFocus += measureGrid_LostFocus;
  1439. */
  1440. }
  1441. private void measureGrid_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  1442. {
  1443. //measureGrid.Items.Refresh();
  1444. }
  1445. private void commitBtn_Click(object sender, RoutedEventArgs e)
  1446. {
  1447. if (measureGrid.ItemsSource == null)
  1448. return;
  1449. var now = DateTime.Now;
  1450. var id = Utils.getSetting("currentMachine");
  1451. double sum = 0;
  1452. int count = 0;
  1453. List<List<float>> s = new List<List<float>>();
  1454. _isExisting = true;
  1455. Dictionary<string, List<double>> sumType = new Dictionary<string, List<double>>();
  1456. foreach (var item in measureGrid.Items)
  1457. {
  1458. //Console.WriteLine(item);
  1459. MeasurePoint mp = item as MeasurePoint;
  1460. if (mp.Avg == null)
  1461. {
  1462. mp.Avg = 0;
  1463. }
  1464. if (mp.Avg == 0)
  1465. continue;
  1466. if (mp.Grade == null)
  1467. {
  1468. mp.Grade = "-";
  1469. }
  1470. if (mp.ResultGrade == null)
  1471. {
  1472. mp.ResultGrade = "-";
  1473. }
  1474. /*
  1475. if (mp.Type == "MID" && this.isMiddle == false)
  1476. continue;
  1477. if (mp.Type == "IN*" && _isIn == false)
  1478. continue;
  1479. */
  1480. if (sumType.ContainsKey(mp.Type.Trim()) == false)
  1481. {
  1482. sumType[mp.Type.Trim()] = new List<double>();
  1483. }
  1484. (sumType[mp.Type.Trim()] as List<double>).Add(mp.Avg);
  1485. string sql = string.Format(" insert into data(lot_no, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, row_no, avg, grade, rgrade, r_type, created_at, updated_at, ndata, sub_order, machine_id, code, header) " +
  1486. " values ('{0}', {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, '{13}', '{14}', '{15}', '{16}', '{17}', {18}, {19}, {20}, '{21}', '{22}' )"
  1487. , lotNo.Text.Trim(), mp.P1, mp.P2, mp.P3, mp.P4, mp.P5, mp.P6, mp.P7, mp.P8, mp.P9, mp.P10, mp.Count, mp.Avg, mp.Grade.Trim(), mp.ResultGrade.Trim(), mp.Type.Trim(), now, now, nextCol, mp.SubOrder, id, code.Text.Trim(), this.header);
  1488. sum += mp.Avg;
  1489. //var s0 = lotNo.Text.Trim();
  1490. /*
  1491. var tempList = new List<float>();
  1492. for(int i = 1; i < nextCol; i++){
  1493. tempList.Add(mp.GetValue("P"+i));
  1494. }
  1495. s.Add(tempList);
  1496. */
  1497. count++;
  1498. using (SqlCommand command = new SqlCommand(sql, proConn))
  1499. {
  1500. command.ExecuteNonQuery();
  1501. }
  1502. }
  1503. //Math.Round(
  1504. int i = 0;
  1505. string[] keys = new string[sumType.Keys.Count];
  1506. sumType.Keys.CopyTo(keys, 0);
  1507. for (int k = 0; k < keys.Length; k++)
  1508. {
  1509. keys[k] = "'" + keys[k] + "'";
  1510. }
  1511. //List<string> avgValues = new List<string>();
  1512. string[] values = new string[sumType.Keys.Count];
  1513. int j = 0;
  1514. string[] columns = new string[sumType.Keys.Count];
  1515. string[] columnValues = new string[sumType.Keys.Count];
  1516. foreach (var name in sumType)
  1517. {
  1518. var temp = name.Value as List<double>;
  1519. double st = 0.0;
  1520. foreach (double n in temp)
  1521. {
  1522. st += n;
  1523. }
  1524. values[j] = Math.Round((double)(st / temp.Count), 2).ToString();
  1525. columns[j] = "avg" + j + "_name";
  1526. columnValues[j] = "avg" + j;
  1527. j++;
  1528. }
  1529. double avg = Math.Round(sum / count, 2);
  1530. string grade = "-";
  1531. if (Convert.ToInt32(sMi16) <= Convert.ToInt32(sMi20))
  1532. {
  1533. grade = computeGrade(sMi16, avg, kind, "-", "OUT");
  1534. }
  1535. else
  1536. {
  1537. if (Convert.ToInt32(sMi20) == 0)
  1538. grade = computeGrade(sMi16, avg, kind, "-", "OUT");
  1539. else
  1540. grade = computeGrade(sMi20, avg, kind, "-", "OUT");
  1541. }
  1542. try
  1543. {
  1544. string sql2 = string.Format("insert into lot_summary(lot_no, created_at, avg, code, start_time, end_time, grade, machine_id, " + String.Join(",", columns) + "," + String.Join(",", columnValues) + ") values('{0}', '{1}', {2}, '{3}', '{4}', '{5}', '{6}', {7}, {8}, {9})",
  1545. lotNo.Text.Trim(), now, avg, code.Text.Trim(), _startUsing, now, grade.Trim(), id, String.Join(",", keys), String.Join(",", values
  1546. ));
  1547. using (SqlCommand command = new SqlCommand(sql2, proConn))
  1548. {
  1549. command.ExecuteNonQuery();
  1550. }
  1551. _updated_at = now.ToString();
  1552. PrintLabel(lotNo.Text, now);
  1553. MessageBox.Show("Commit Complete");
  1554. lotNo.Focus();
  1555. lotNo.SelectAll();
  1556. }
  1557. catch (Exception ex)
  1558. {
  1559. MessageBox.Show("Data Input Error "+ ex.Message);
  1560. }
  1561. }
  1562. private void PrintLabel(string lotNo, DateTime dt)
  1563. {
  1564. string sql = string.Format("select * from data where lot_no = '{0}' and created_at = '{1}' order by id", lotNo, dt);
  1565. SqlDataReader reader = Utils.Query(proConn, sql);
  1566. string s = "";
  1567. int n = 0;
  1568. List<double> outs = new List<double>();
  1569. List<double> ins = new List<double>();
  1570. bool outValid = false;
  1571. bool inValid = false;
  1572. string inAvg = "";
  1573. string outAvg = "";
  1574. int row = 0;
  1575. int nrow = 0;
  1576. while (reader.Read())
  1577. {
  1578. nrow++;
  1579. n = Convert.ToInt32(reader["ndata"]);
  1580. string r_type = reader["r_type"].ToString();
  1581. try
  1582. {
  1583. r_type = r_type.Substring(0, 3);
  1584. }
  1585. catch
  1586. {
  1587. ;
  1588. }
  1589. s += string.Format("{0,3}", r_type.ToUpper()) + " ";
  1590. for (int i = 1; i <= n; i++)
  1591. {
  1592. if (n > 3)
  1593. {
  1594. s += string.Format("{0,-3:0.00}", Convert.ToDouble(reader["p" + i])) + " ";
  1595. }
  1596. else
  1597. {
  1598. s += string.Format("{0,-3:0.00}", Convert.ToDouble(reader["p" + i])) + " ";
  1599. }
  1600. row++;
  1601. }
  1602. //s = s.Trim();
  1603. var grade = reader["grade"].ToString().Trim();
  1604. var rgrade = reader["rgrade"].ToString().Trim();
  1605. try
  1606. {
  1607. if (n >= 5)
  1608. rgrade = rgrade.Substring(0, 2);
  1609. }
  1610. catch
  1611. {
  1612. ;
  1613. }
  1614. //if (reader["rgrade"].ToString().ToLower() != "ok")
  1615. //{
  1616. // s += string.Format(" {0,-6:*0.00*}", Convert.ToDouble(reader["avg"])) + " " + string.Format("{0,-2}",grade) + " " + rgrade;
  1617. //}
  1618. //else
  1619. //{
  1620. if (n >= 5)
  1621. {
  1622. s += string.Format("{0,5:0.00}", Convert.ToDouble(reader["avg"])) + " " + string.Format("{0,-2}", grade) + " " + rgrade;
  1623. }
  1624. else
  1625. {
  1626. s += string.Format("{0,7:0.00}", Convert.ToDouble(reader["avg"])) + " " + string.Format("{0,-2}", grade) + " " + rgrade;
  1627. }
  1628. //}
  1629. //s = s.Trim();
  1630. s += "\n";
  1631. /*
  1632. UPP,UPPE = Out
  1633. LOW,LOWE = In
  1634. Rim = Out
  1635. Base =In
  1636. Soko = In
  1637. IN* = In
  1638. */
  1639. if (r_type.ToUpper() == "OUT" || r_type.ToUpper() == "TOP" || r_type.ToUpper() == "FA1" || r_type.ToUpper() == "UPP" || r_type.ToUpper() == "UPPE" || r_type.ToUpper() == "RIM")
  1640. {
  1641. outs.Add(Convert.ToDouble(reader["avg"]));
  1642. }
  1643. if (r_type.ToUpper() == "IN" || r_type.ToUpper() == "UNDER" || r_type.ToUpper() == "UND" || r_type.ToUpper() == "FA2" || r_type.ToUpper() == "LOW" || r_type.ToUpper() == "LOWE" || r_type.ToUpper() == "BASE" || r_type.ToUpper() == "SOKO" || r_type.ToUpper() == "IN*")
  1644. {
  1645. ins.Add(Convert.ToDouble(reader["avg"]));
  1646. }
  1647. }
  1648. //string avg = string.Format("{0.00}", Convert.ToDouble(reader["avg"]));
  1649. double[] inArray = new double[] {
  1650. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1651. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1652. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1653. double[] outArray = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1654. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1655. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  1656. double sumIns = 0;
  1657. double sumOuts = 0;
  1658. for (int i = 0; i < ins.Count; i++)
  1659. {
  1660. inArray[i] = ins[i];
  1661. sumIns += ins[i];
  1662. }
  1663. for (int i = 0; i < outs.Count; i++)
  1664. {
  1665. outArray[i] = outs[i];
  1666. sumOuts += outs[i];
  1667. }
  1668. double avgIns = 0;
  1669. if (ins.Count == 0)
  1670. avgIns = 0;
  1671. else
  1672. avgIns = sumIns / ins.Count;
  1673. double avgOut = 0;
  1674. if(outs.Count == 0 )
  1675. avgOut = 0;
  1676. else
  1677. avgOut = sumOuts / outs.Count;
  1678. reader.Close();
  1679. sql = string.Format("select m.name , ls.avg, ls.grade, ls.start_time, ls.end_time, ls.code from lot_summary ls, machines m where ls.lot_no = '{0}' and ls.created_at = '{1}' and ls.machine_id = m.id", lotNo, dt);
  1680. reader = Utils.Query(proConn, sql);
  1681. var temp = list;
  1682. var types = string.Join("/", temp.ToArray());
  1683. string code = "";
  1684. string et = "";
  1685. while (reader.Read())
  1686. {
  1687. code = reader["code"].ToString();
  1688. s += string.Format("{0} Use {1}Times {2} AVG {3:0.00} {4}\n", reader["name"].ToString(), UsingTimeTB.Text.Trim(), types, Convert.ToDouble(reader["avg"]), reader["grade"].ToString());
  1689. var startTime = Convert.ToDateTime(reader["start_time"]);
  1690. var endTime = Convert.ToDateTime(reader["end_time"]);
  1691. et = string.Format("{0:dd/MM/yyyy HH:mm:ss}", endTime);
  1692. s += string.Format("Date:{0:dd/MM/yyyy} Time:{1:HH:mm}-{2:HH:mm}\n", startTime, startTime, endTime);
  1693. }
  1694. code = code.Replace("-", "");
  1695. string tableName = "";
  1696. try
  1697. {
  1698. accessConnEMASTER = new OleDbConnection(strAccessConnEMASTER);
  1699. accessConnEMASTER.Open();
  1700. }
  1701. catch (Exception ex)
  1702. {
  1703. MessageBox.Show("Error: Failed to create e-master connection. \n{0}", ex.Message);
  1704. }
  1705. try
  1706. {
  1707. accessConnBELMASTER = new OleDbConnection(strAccessConnBELMASTER);
  1708. accessConnBELMASTER.Open();
  1709. }
  1710. catch (Exception ex)
  1711. {
  1712. MessageBox.Show("Error: Failed to create e-master connection. \n{0}", ex.Message);
  1713. }
  1714. try
  1715. {
  1716. accessConnMGMASTER = new OleDbConnection(strAccessConnMGMASTER);
  1717. accessConnMGMASTER.Open();
  1718. }
  1719. catch (Exception ex)
  1720. {
  1721. MessageBox.Show("Error: Failed to create e-master connection. \n{0}", ex.Message);
  1722. }
  1723. try
  1724. {
  1725. accessConnVMASTER = new OleDbConnection(strAccessConnVMASTER);
  1726. accessConnVMASTER.Open();
  1727. }
  1728. catch (Exception ex)
  1729. {
  1730. MessageBox.Show("Error: Failed to create e-master connection. \n{0}", ex.Message);
  1731. }
  1732. if (code[0] == '1' || code[0] == '9')
  1733. {
  1734. tempConn = accessConnVMASTER;
  1735. tableName = "V-REREKI";
  1736. }
  1737. if (code[0] == '2')
  1738. {
  1739. tempConn = accessConnMGMASTER;
  1740. tableName = "MG-REREKI";
  1741. }
  1742. if (code[0] == '3' || code[0] == '4')
  1743. {
  1744. tempConn = accessConnEMASTER;
  1745. tableName = "E-REREKI";
  1746. }
  1747. if (code[0] == '5')
  1748. {
  1749. tempConn = accessConnBELMASTER;
  1750. tableName = "BEL-REREKI";
  1751. }
  1752. OleDbCommand deleteCommand = new OleDbCommand("delete from ["+tableName+"] where RE01 = '"+code+"' and RE02 = '"+lotNo+"'", tempConn);
  1753. deleteCommand.ExecuteNonQuery();
  1754. //int lotCount = (int)selectCountCommand.ExecuteScalar();
  1755. //if (lotCount == 0)
  1756. //{
  1757. string rereki = string.Format("insert into [" + tableName + "](RE01, RE02, RE03,RE04, [OB-O1], [OB-O2], [OB-O3], [OB-O4], [OB-O5], [OB-O6], [OB-O7], [OB-O8],[OB-O9], [OB-O10], [OB-O11], [OB-O12], [OB-O13], [OB-O14], [OB-O15],[OB-O16], [OB-O17], [OB-O18], [OB-O19], [OB-O20], [OB-O21], [OB-O22], [OB-O23], [OB-O24], [OB-O25],[OB-O26], [OB-O27],[OB-O28],[OB-O29],[OB-O30], [OB-I1], [OB-I2], [OB-I3], [OB-I4], [OB-I5], [OB-I6], [OB-I7], [OB-I8], [OB-I9], [OB-I10], [OB-I11], [OB-I12], [OB-I13], [OB-I14], [OB-I15], [OB-I16], [OB-I17], [OB-I18], [OB-I19], [OB-I20], [OB-I21], [OB-I22], [OB-I23], [OB-I24], [OB-I25], [OB-I26], [OB-I27], [OB-I28], [OB-I29], [OB-I30], [OB-O], [OB-I])"
  1758. + "values('{0}', '{1}', '{2}', {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12},{13}, {14}, {15},{16}, {17}, {18},{19},{20},{21},{22}, {23},{24}, {25}, {26}, {27}, {28},{29},{30},{31},{32},{33},{34},{35},{36}, {37},{38}, {39},{40}, {41},{42},{43},{44},{45},{46},{47},{48}, {49},{50}, {51}, {52}, {53}, {54}, {55}, {56}, {57}, {58}, {59},{60},{61},{62},{63},{64}, {65})", code, lotNo, et, nrow, outArray[0], outArray[1], outArray[2], outArray[3], outArray[4], outArray[5], outArray[6], outArray[7], outArray[8], outArray[9], outArray[10], outArray[11], outArray[12], outArray[13], outArray[14], outArray[15], outArray[16], outArray[17], outArray[18], outArray[19], outArray[20], outArray[21], outArray[22], outArray[23], outArray[24], outArray[25], outArray[26], outArray[27], outArray[28], outArray[29], inArray[0], inArray[1], inArray[2], inArray[3], inArray[4], inArray[5], inArray[6], inArray[7], inArray[8], inArray[9], inArray[10], inArray[11], inArray[12], inArray[13], inArray[14], inArray[15], inArray[16], inArray[17], inArray[18], inArray[19], inArray[20], inArray[21], inArray[22], inArray[23], inArray[24], inArray[25], inArray[26], inArray[27], inArray[28], inArray[29], avgOut, avgIns);
  1759. OleDbCommand insertCmd = new OleDbCommand(rereki, tempConn);
  1760. //MessageBox.Show("re = " + rereki);
  1761. insertCmd.ExecuteNonQuery();
  1762. //close all db access
  1763. accessConnBELMASTER.Close();
  1764. accessConnEMASTER.Close();
  1765. accessConnMGMASTER.Close();
  1766. accessConnVMASTER.Close();
  1767. //}
  1768. reader.Close();
  1769. //Utils.Query(proConn, "select P1, P2, P3, P4, P5, P6, P7, P8, P9, P10 from lot_summary where lot_no =
  1770. //Printer.Print(s, n); // 14/06/19
  1771. printMultipage(s, n); // latest
  1772. //Printer.Print(s);
  1773. //var p = new Printer();
  1774. }
  1775. void printMultipage(string s, int n)
  1776. {
  1777. var lines = s.Split('\n');
  1778. const int NL = 8;
  1779. var el = lines.Count() / NL;
  1780. string postFix = "";
  1781. for (var i = 0; i < el; i++)
  1782. {
  1783. var pages = String.Format("Lot No. {0} {1}\n", lotNo.Text, this.obCondition);
  1784. //pages += String.Format("Std.Wb={0:0.##}g\nStd.I-5-5={1:0.##}g\n", stdWbUI.Text, i55UI.Text);
  1785. for (var j = 0; j < NL; j++)
  1786. {
  1787. pages += lines[j + (i * NL)] + "\n";
  1788. }
  1789. Printer.Print(pages);
  1790. }
  1791. /*
  1792. Printer p = new Printer();
  1793. Console.WriteLine("printer print");
  1794. p.PrintDoc(s);*/
  1795. var remain = lines.Count() % NL;
  1796. if (remain == 0)
  1797. return;
  1798. var pager = String.Format("Lot No. {0} {1}\n", lotNo.Text, this.obCondition);
  1799. //pager += String.Format("Std.Wb={0:0.##}g\nStd.I-5-5={1:0.##}g\n", stdWbUI.Text, i55UI.Text);
  1800. for (var j = el * NL; j < lines.Count(); j++)
  1801. {
  1802. if (lines[j] == "")
  1803. continue;
  1804. pager += lines[j] + "\n";
  1805. }
  1806. Printer.Print(pager);
  1807. }
  1808. private void measureGrid_LoadingRow(object sender, DataGridRowEventArgs e)
  1809. {
  1810. /*
  1811. DataGridRow c = e.Row;
  1812. MeasurePoint mp = e.Row.Item as MeasurePoint;
  1813. if (mp.Type == "MID" && this.isMiddle == false)
  1814. {
  1815. c.Visibility = Visibility.Collapsed;
  1816. }
  1817. if (mp.Type == "IN*" && _isIn == false)
  1818. {
  1819. c.Visibility = Visibility.Collapsed;
  1820. }
  1821. */
  1822. }
  1823. private void addMiddleBtn_Click(object sender, RoutedEventArgs e)
  1824. {
  1825. if (measureGrid.ItemsSource == null)
  1826. return;
  1827. _lastAction.Push("middle");
  1828. this.isMiddle = !this.isMiddle;
  1829. if (this.isMiddle == false)
  1830. {
  1831. middleText.Text = "Add Middle";
  1832. RemoveMid();
  1833. }
  1834. else
  1835. {
  1836. middleText.Text = "Remove Middle";
  1837. EditMIDWindow w = new EditMIDWindow();
  1838. w.ShowDialog();
  1839. AddMid();
  1840. }
  1841. //this.measureGrid.CommitEdit();
  1842. measureGrid.Items.Refresh();
  1843. }
  1844. private void RemoveMid()
  1845. {
  1846. list.Remove("MID");
  1847. ObservableCollection<MeasurePoint> temp = new ObservableCollection<MeasurePoint>();
  1848. foreach (var m in mps)
  1849. {
  1850. if (m.Type != "MID")
  1851. {
  1852. temp.Add(m);
  1853. }
  1854. }
  1855. mps = temp;
  1856. measureGrid.ItemsSource = null;
  1857. measureGrid.ItemsSource = mps;
  1858. }
  1859. private void AddMid()
  1860. {
  1861. list.Insert(1, "MID");
  1862. ObservableCollection<MeasurePoint> temp = new ObservableCollection<MeasurePoint>();
  1863. foreach (var m in mps)
  1864. {
  1865. temp.Add(m);
  1866. if (m.SubOrder == 1)
  1867. {
  1868. temp.Add(new MeasurePoint() { Type = "MID", SubOrder = m.SubOrder + 1, Count = m.Count });
  1869. }
  1870. }
  1871. mps = temp;
  1872. measureGrid.ItemsSource = null;
  1873. measureGrid.ItemsSource = mps;
  1874. }
  1875. private void deleteData_Click(object sender, RoutedEventArgs e)
  1876. {
  1877. this.currentMP.setValue(currentColumn, 0);
  1878. this.computeValues(currentMP, true);
  1879. measureGrid.Items.Refresh();
  1880. }
  1881. private void measureGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
  1882. {
  1883. this.currentColumn = e.Column.Header.ToString();
  1884. this.currentMP = (MeasurePoint)e.Row.Item;
  1885. cellIndicattor.Text = "(" + currentMP.Type + "-" + currentMP.Count + ", " + currentColumn + ")";
  1886. }
  1887. private void measureGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
  1888. {
  1889. //this.currentMP = (MeasurePoint)measureGrid.CurrentItem;
  1890. //this.currentColumn = measureGrid.CurrentColumn.Header.ToString();
  1891. //cellIndicattor.Text = "("+currentMP.Type + "-" +currentMP.Count+", "+currentColumn+")";
  1892. }
  1893. private void measureGrid_CurrentCellChanged(object sender, EventArgs e)
  1894. {
  1895. try
  1896. {
  1897. this.currentColumn = measureGrid.CurrentColumn.Header.ToString();
  1898. this.currentMP = (MeasurePoint)measureGrid.CurrentItem;
  1899. cellIndicattor.Text = "(" + currentMP.Type + "-" + currentMP.Count + ", " + currentColumn + ")";
  1900. //this.computeValues(currentMP);
  1901. }
  1902. catch (NullReferenceException ne) { }
  1903. }
  1904. private void saveCellBtn_Click(object sender, RoutedEventArgs e)
  1905. {
  1906. measureGrid.Focus();
  1907. if (currentColumn == null)
  1908. return;
  1909. if (_isOver == true)
  1910. return;
  1911. /*
  1912. if(_isFirstMeasure == true){
  1913. _startUsing = DateTime.Now;
  1914. _isFirstMeasure = false;
  1915. }*/
  1916. if (currentColumn != "Average" && currentColumn != "Grade" && currentColumn != "Result Grade")
  1917. {
  1918. this.currentMP.setValue(currentColumn, Convert.ToDouble(readingBox.Text));
  1919. //var c = measureGrid.SelectedValue;
  1920. this.computeValues(currentMP, true);
  1921. }
  1922. try
  1923. {
  1924. var info = measureGrid.CurrentColumn;
  1925. var item = measureGrid.CurrentItem as MeasurePoint;
  1926. int col = info.DisplayIndex;
  1927. var mp = (MeasurePoint)measureGrid.CurrentItem;
  1928. int row = mps.IndexOf(mp);
  1929. //measureGrid.RaiseEvent(new RoutedEventArgs(DataGrid.rowediten));
  1930. //MessageBox.Show("row " + item.Count.ToString());
  1931. //MessageBox.Show("row " + mps.IndexOf(mp).ToString());
  1932. //MessageBox.Show("col " + info.DisplayIndex.ToString());
  1933. measureGrid.Items.Refresh();
  1934. measureGrid.Focus();
  1935. if (col < nextCol)
  1936. {
  1937. measureGrid.CurrentCell = new DataGridCellInfo(
  1938. measureGrid.Items[row], measureGrid.Columns[col + 1]);
  1939. measureGrid.BeginEdit();
  1940. }
  1941. else
  1942. {
  1943. try
  1944. {
  1945. //this.computeValues(currentMP, true);
  1946. measureGrid.CurrentCell = new DataGridCellInfo(
  1947. measureGrid.Items[row + 1], measureGrid.Columns[1]);
  1948. measureGrid.SelectedItem = measureGrid.Items[row + 1];
  1949. measureGrid.BeginEdit();
  1950. }
  1951. catch
  1952. {
  1953. measureGrid.CurrentCell = new DataGridCellInfo(
  1954. measureGrid.Items[0], measureGrid.Columns[1]);
  1955. measureGrid.SelectedItem = measureGrid.Items[0];
  1956. measureGrid.BeginEdit();
  1957. }
  1958. }
  1959. }
  1960. catch
  1961. {
  1962. ;
  1963. }
  1964. updateUsingTime(this.header);
  1965. }
  1966. private void updateUsingTime(string p, int n = -1)
  1967. {
  1968. switch (p)
  1969. {
  1970. case "OB":
  1971. _obCount++;
  1972. _usingTime = _obCount;
  1973. if (_obCount > _obLimit)
  1974. {
  1975. _obCount = _obLimit;
  1976. Alert alert = new Alert();
  1977. alert.SetAlertMsg("OB Header Exceeds Usage Limit");
  1978. alert.ShowDialog();
  1979. _isOver = true;
  1980. }
  1981. _isOver = false;
  1982. if (n == -1)
  1983. {
  1984. Utils.Query(_opConn, "update UsingTime set ob = " + _obCount);
  1985. UsingTimeTB.Text = _obCount.ToString();
  1986. }
  1987. else
  1988. {
  1989. Utils.Query(_opConn, "update UsingTime set ob = " + n);
  1990. UsingTimeTB.Text = n.ToString();
  1991. _obCount = n;
  1992. }
  1993. break;
  1994. case "OC":
  1995. _ocCount++;
  1996. _usingTime = _ocCount;
  1997. if (_ocCount > _ocLimit)
  1998. {
  1999. _ocCount = _ocLimit;
  2000. Alert alert = new Alert();
  2001. alert.SetAlertMsg("OC Header Exceeds Usage Limit");
  2002. alert.ShowDialog();
  2003. _isOver = true;
  2004. }
  2005. _isOver = false;
  2006. if (n == -1)
  2007. {
  2008. Utils.Query(_opConn, "update UsingTime set oc = " + _ocCount);
  2009. UsingTimeTB.Text = _ocCount.ToString();
  2010. }
  2011. else
  2012. {
  2013. Utils.Query(_opConn, "update UsingTime set oc = " + n);
  2014. UsingTimeTB.Text = n.ToString();
  2015. _ocCount = n;
  2016. }
  2017. break;
  2018. case "RH":
  2019. _rhCount++;
  2020. _usingTime = _rhCount;
  2021. if (_rhCount > _rhLimit)
  2022. {
  2023. _rhCount = _rhLimit;
  2024. Alert alert = new Alert();
  2025. alert.SetAlertMsg("RH Header Exceeds Usage Limit");
  2026. alert.ShowDialog();
  2027. _isOver = true;
  2028. }
  2029. _isOver = false;
  2030. if (n == -1)
  2031. {
  2032. Utils.Query(_opConn, "update UsingTime set rh = " + _rhCount);
  2033. UsingTimeTB.Text = _rhCount.ToString();
  2034. }
  2035. else
  2036. {
  2037. Utils.Query(_opConn, "update UsingTime set rh = " + n);
  2038. UsingTimeTB.Text = n.ToString();
  2039. _rhCount = n;
  2040. }
  2041. break;
  2042. default:
  2043. //MessageBox.Show("Please Find a Lot NO First");
  2044. break;
  2045. }
  2046. }
  2047. public void refreshUsingTime()
  2048. {
  2049. string p = this.header;
  2050. string sql = string.Format("select {0} as n from UsingTime", this.header.ToLower());
  2051. var reader = Utils.Query(_opConn, sql);
  2052. reader.Read();
  2053. UsingTimeTB.Text = reader["n"].ToString();
  2054. if (p == "OB")
  2055. {
  2056. _obCount = Convert.ToInt32(reader["n"]);
  2057. }
  2058. else if (p == "OC")
  2059. {
  2060. _ocCount = Convert.ToInt32(reader["n"]);
  2061. }
  2062. }
  2063. private void IntranetBtn_Click(object sender, RoutedEventArgs e)
  2064. {
  2065. SqlDataReader reader = Utils.Query(proConn, "select value from sys_configs where title = 'internal_url'");
  2066. reader.Read();
  2067. string target = reader["value"].ToString();
  2068. //string target = reader["value"].Read().ToString();
  2069. try
  2070. {
  2071. System.Diagnostics.Process.Start(target);
  2072. }
  2073. catch (System.ComponentModel.Win32Exception noBrowser)
  2074. {
  2075. if (noBrowser.ErrorCode == -2147467259)
  2076. MessageBox.Show(noBrowser.Message);
  2077. }
  2078. catch (System.Exception other)
  2079. {
  2080. MessageBox.Show(other.Message);
  2081. }
  2082. reader.Close();
  2083. }
  2084. private void undoBtn_Click(object sender, RoutedEventArgs e)
  2085. {
  2086. if (_lastAction.Count == 0)
  2087. return;
  2088. string action = _lastAction.Pop();
  2089. if (action == "middle")
  2090. {
  2091. this.isMiddle = !this.isMiddle;
  2092. if (this.isMiddle == false)
  2093. {
  2094. middleText.Text = "Add Middle";
  2095. RemoveMid();
  2096. }
  2097. else
  2098. {
  2099. middleText.Text = "Remove Middle";
  2100. AddMid();
  2101. }
  2102. }
  2103. if (action == "in")
  2104. {
  2105. _isIn = !_isIn;
  2106. if (_isIn == false)
  2107. {
  2108. inText.Text = "Add IN*";
  2109. RemoveIn();
  2110. }
  2111. else
  2112. {
  2113. inText.Text = "Remove IN*";
  2114. AddIn();
  2115. }
  2116. }
  2117. else if (action == "row")
  2118. {
  2119. for (int i = 0; i < list.Count; i++)
  2120. {
  2121. mps.RemoveAt(mps.Count - 1);
  2122. }
  2123. }
  2124. else if (action == "column")
  2125. {
  2126. measureGrid.Columns.ElementAt(nextCol--).Visibility = Visibility.Hidden;
  2127. foreach (var item in measureGrid.Items)
  2128. {
  2129. //Console.WriteLine(item);
  2130. MeasurePoint c = item as MeasurePoint;
  2131. c.setValue((nextCol + 1).ToString(), (float)0.0);
  2132. c.Avg = (c.P1 + c.P2 + c.P3 + c.P4 + c.P6 + c.P7 + c.P8 + c.P9 + c.P10) / (nextCol);
  2133. c.Avg = Math.Round(c.Avg, 2);
  2134. if (c.SubOrder == 1)
  2135. {
  2136. c.Grade = computeGrade(sMi16, c.Avg, kind, "-", c.Type);
  2137. }
  2138. else if (c.SubOrder != 1 && c.Type != "MID" && c.Type != "IN*")
  2139. {
  2140. if (sMi20 != "0")
  2141. c.Grade = computeGrade(sMi20, c.Avg, kind, "-", c.Type);
  2142. else
  2143. c.Grade = computeGrade(sMi16, c.Avg, kind, "-", c.Type);
  2144. }
  2145. }
  2146. }
  2147. else if (action == "remove_column")
  2148. {
  2149. measureGrid.Columns.ElementAt(++nextCol).Visibility = Visibility.Visible;
  2150. foreach (var item in measureGrid.Items)
  2151. {
  2152. //Console.WriteLine(item);
  2153. MeasurePoint c = item as MeasurePoint;
  2154. c.setValue((nextCol).ToString(), (float)0.0);
  2155. computeValues(c, true);
  2156. }
  2157. }
  2158. measureGrid.Items.Refresh();
  2159. }
  2160. private void printBtn_Click(object sender, RoutedEventArgs e)
  2161. {
  2162. if (_isExisting == true)
  2163. {
  2164. PrintLabel(lotNo.Text, Convert.ToDateTime(_updated_at));
  2165. }
  2166. else
  2167. {
  2168. MessageBox.Show("You can print when you have commited data");
  2169. }
  2170. }
  2171. private void agreementBtn_Click(object sender, RoutedEventArgs e)
  2172. {
  2173. var fileName = agreementCB.Text;
  2174. try
  2175. {
  2176. //System.Diagnostics.Process.Start(@"agreements\" + fileName);
  2177. var server = Utils.getSetting("webServerURL");
  2178. string data = string.Format(@"title={0}&empid={1}&password={2}", fileName, GlobalVars.user.empid, GlobalVars.user.password);
  2179. WebAppConnector wp = new WebAppConnector()
  2180. {
  2181. Uri = Utils.getSetting("webServerURL") + "/agreements/title.json",
  2182. Method = "POST",
  2183. PostData = data
  2184. };
  2185. Response r = null;
  2186. try
  2187. {
  2188. r = wp.sendRequest();
  2189. }
  2190. catch
  2191. {
  2192. MessageBox.Show("Cannot connect to webserver :" + server);
  2193. return;
  2194. }
  2195. string ans = (string)r.jsonBody["msg"];
  2196. //Console.WriteLine((JArray)r.jsonBody["roles"]);
  2197. if (r.status == "OK" && ans == "y")
  2198. {
  2199. System.Diagnostics.Process.Start(server + (string)r.jsonBody["url"]);
  2200. }
  2201. else
  2202. {
  2203. MessageBox.Show("Cannot find agreement file");
  2204. }
  2205. }
  2206. catch (Win32Exception ew)
  2207. {
  2208. MessageBox.Show(ew.Message);
  2209. }
  2210. }
  2211. private void thaiMenu_Checked(object sender, RoutedEventArgs e)
  2212. {
  2213. engMenu.IsChecked = false;
  2214. //var selectedItem = comboBoxLanguage.SelectedItem as ComboBoxItem;
  2215. var culture = CultureInfo.GetCultureInfo("th-TH");
  2216. Dispatcher.Thread.CurrentCulture = culture;
  2217. Dispatcher.Thread.CurrentUICulture = culture;
  2218. LocalizationManager.UpdateValues();
  2219. }
  2220. private void engMenu_Checked(object sender, RoutedEventArgs e)
  2221. {
  2222. thaiMenu.IsChecked = false;
  2223. var culture = CultureInfo.GetCultureInfo("en-US");
  2224. Dispatcher.Thread.CurrentCulture = culture;
  2225. Dispatcher.Thread.CurrentUICulture = culture;
  2226. LocalizationManager.UpdateValues();
  2227. }
  2228. private void windowItem_Checked(object sender, RoutedEventArgs e)
  2229. {
  2230. this.WindowStyle = WindowStyle.SingleBorderWindow;
  2231. this.WindowState = WindowState.Normal;
  2232. this.isFullScreen = false;
  2233. fullScreenItem.IsChecked = false;
  2234. }
  2235. private void fullScreenItem_Checked(object sender, RoutedEventArgs e)
  2236. {
  2237. this.WindowStyle = WindowStyle.None;
  2238. this.WindowState = WindowState.Maximized;
  2239. this.isFullScreen = true;
  2240. windowItem.IsChecked = false;
  2241. }
  2242. private void measureGrid_KeyDown(object sender, KeyEventArgs e)
  2243. {
  2244. }
  2245. private void measureGrid_Loaded(object sender, RoutedEventArgs e)
  2246. {
  2247. //MessageBox.Show("Refresh Finish");
  2248. }
  2249. private void measureGrid_SourceUpdated(object sender, DataTransferEventArgs e)
  2250. {
  2251. MessageBox.Show("refresh finish");
  2252. }
  2253. private void measureGrid_ColumnDisplayIndexChanged(object sender, DataGridColumnEventArgs e)
  2254. {
  2255. }
  2256. private void measureGrid_PreviewKeyDown(object sender, KeyEventArgs e)
  2257. {
  2258. }
  2259. private void lotNo_MouseDown(object sender, MouseButtonEventArgs e)
  2260. {
  2261. //lotNo.SelectAll();
  2262. }
  2263. private void lotNo_GotFocus(object sender, RoutedEventArgs e)
  2264. {
  2265. //lotNo.SelectAll();
  2266. }
  2267. private void lotNo_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  2268. {
  2269. lotNo.SelectAll();
  2270. }
  2271. private void lotNo_GotMouseCapture(object sender, MouseEventArgs e)
  2272. {
  2273. lotNo.SelectAll();
  2274. }
  2275. private void resetBtn_Click(object sender, RoutedEventArgs e)
  2276. {
  2277. updateUsingTime(this.header, 0);
  2278. measureGrid.Focus();
  2279. try
  2280. {
  2281. measureGrid.CurrentCell = new DataGridCellInfo(
  2282. measureGrid.Items[0], measureGrid.Columns[1]);
  2283. measureGrid.SelectedItem = measureGrid.Items[0];
  2284. measureGrid.BeginEdit();
  2285. }
  2286. catch
  2287. {
  2288. }
  2289. }
  2290. private void editUsingTime_Click(object sender, RoutedEventArgs e)
  2291. {
  2292. EditUsingTime editUsingTime = new EditUsingTime();
  2293. editUsingTime.ShowDialog();
  2294. }
  2295. private void addInBtn_Click(object sender, RoutedEventArgs e)
  2296. {
  2297. //measureGrid.ItemsSource = null;
  2298. if (measureGrid.ItemsSource == null)
  2299. return;
  2300. _isIn = !_isIn;
  2301. if (_isIn == false)
  2302. {
  2303. inText.Text = "Add IN*";
  2304. RemoveIn();
  2305. }
  2306. else
  2307. {
  2308. inText.Text = "Remove IN*";
  2309. EditINWindow w = new EditINWindow();
  2310. w.ShowDialog();
  2311. AddIn();
  2312. }
  2313. //ObservableCollection<MeasurePoint> temp = new ObservableCollection<MeasurePoint>();
  2314. _lastAction.Push("in");
  2315. measureGrid.Items.Refresh();
  2316. /*
  2317. for(int i = 0; i < mps.Count; i++){
  2318. var mp = mps[i];
  2319. if(mp.SubOrder == 1){
  2320. mps.Insert(mps.IndexOf(mp) + 1, new MeasurePoint() { Count = mp.Count, Type = "IN*", SubOrder = 3});
  2321. }
  2322. }
  2323. measureGrid.ItemsSource = mps;
  2324. */
  2325. //measureGrid.ItemsSource = null;
  2326. //measureGrid.Items.Add(0, new MeasurePoint() { Count = 1, Type = "In"});
  2327. }
  2328. private void RemoveIn()
  2329. {
  2330. list.Remove("IN*");
  2331. ObservableCollection<MeasurePoint> temp = new ObservableCollection<MeasurePoint>();
  2332. foreach (var m in mps)
  2333. {
  2334. if (m.Type != "IN*")
  2335. {
  2336. temp.Add(m);
  2337. }
  2338. }
  2339. mps = temp;
  2340. measureGrid.ItemsSource = null;
  2341. measureGrid.ItemsSource = mps;
  2342. }
  2343. private void AddIn()
  2344. {
  2345. list.Insert(1, "IN*");
  2346. ObservableCollection<MeasurePoint> temp = new ObservableCollection<MeasurePoint>();
  2347. foreach (var m in mps)
  2348. {
  2349. temp.Add(m);
  2350. if (m.SubOrder == 1)
  2351. {
  2352. temp.Add(new MeasurePoint() { Type = "IN*", SubOrder = m.SubOrder + 1, Count = m.Count });
  2353. }
  2354. }
  2355. mps = temp;
  2356. measureGrid.ItemsSource = null;
  2357. measureGrid.ItemsSource = mps;
  2358. }
  2359. private void dailyCheckBtn_Click(object sender, RoutedEventArgs e)
  2360. {
  2361. _dc = new DailyCheck();
  2362. //_dc.Show();
  2363. _dc.ShowDialog();
  2364. //_dc.ShowDialog();
  2365. }
  2366. private void removeMeasureColumn_Click(object sender, RoutedEventArgs e)
  2367. {
  2368. _lastAction.Push("remove_column");
  2369. measureGrid.Columns.ElementAt(nextCol).Visibility = Visibility.Hidden;
  2370. if (nextCol <= 10)
  2371. {
  2372. measureGrid.Columns.ElementAt(--nextCol).Visibility = Visibility.Visible;
  2373. foreach (var item in measureGrid.Items)
  2374. {
  2375. //Console.WriteLine(item);
  2376. MeasurePoint c = item as MeasurePoint;
  2377. c.setValue((nextCol + 1).ToString(), 0);
  2378. computeValues(c, true);
  2379. }
  2380. measureGrid.Items.Refresh();
  2381. }
  2382. }
  2383. private void lotNo_KeyDown(object sender, KeyEventArgs e)
  2384. {
  2385. if (e.Key == Key.Return)
  2386. {
  2387. //MessageBox.Show("Hello");
  2388. find_Click(null, null);
  2389. }
  2390. }
  2391. private void checkLogin_Click(object sender, RoutedEventArgs e)
  2392. {
  2393. var server = Utils.getSetting("webServerURL");
  2394. //string data = string.Format(@"title={0}", fileName);
  2395. WebAppConnector wp = new WebAppConnector()
  2396. {
  2397. Uri = Utils.getSetting("webServerURL") + "/sites/get_current_user.json",
  2398. Method = "POST",
  2399. PostData = ""
  2400. };
  2401. Response r = null;
  2402. try
  2403. {
  2404. r = wp.sendRequest();
  2405. }
  2406. catch
  2407. {
  2408. MessageBox.Show("Cannot connect to webserver :" + server);
  2409. }
  2410. string ans = (string)r.jsonBody["u"];
  2411. //Console.WriteLine((JArray)r.jsonBody["roles"]);
  2412. }
  2413. private void searchAgreementBtn_Click(object sender, RoutedEventArgs e)
  2414. {
  2415. SearchAgreement sa = new SearchAgreement();
  2416. sa.ShowDialog();
  2417. }
  2418. private void editINStar_Click(object sender, RoutedEventArgs e)
  2419. {
  2420. EditINWindow w = new EditINWindow();
  2421. w.ShowDialog();
  2422. foreach (var item in measureGrid.Items)
  2423. {
  2424. MeasurePoint c = item as MeasurePoint;
  2425. computeValues(c, true);
  2426. }
  2427. measureGrid.Items.Refresh();
  2428. }
  2429. private void editMID_Click(object sender, RoutedEventArgs e)
  2430. {
  2431. EditMIDWindow w = new EditMIDWindow();
  2432. w.ShowDialog();
  2433. foreach (var item in measureGrid.Items)
  2434. {
  2435. MeasurePoint c = item as MeasurePoint;
  2436. computeValues(c, true);
  2437. }
  2438. measureGrid.Items.Refresh();
  2439. }
  2440. private void logoutMenu_Click(object sender, RoutedEventArgs e)
  2441. {
  2442. this.Hide();
  2443. LoginWindow lw = new LoginWindow();
  2444. var r = lw.ShowDialog();
  2445. resetMenu();
  2446. }
  2447. private void about_Click(object sender, RoutedEventArgs e)
  2448. {
  2449. Window1 w = new Window1();
  2450. w.ShowDialog();
  2451. }
  2452. private void Window_Closing(object sender, CancelEventArgs e)
  2453. {
  2454. if (comport.IsOpen)
  2455. {
  2456. comport.Close();
  2457. _clearThread = true;
  2458. }
  2459. }
  2460. }
  2461. }