Nenhuma Descrição

MainWindow.xaml.cs 101KB

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