private static string AddToOdoo(object[] pressValues) { string timestampStr = ""; try { string gear_qr = ((string)pressValues[0]).Trim(); string pinion_qr = ((string)pressValues[1]).Trim(); string product_model = (string)pressValues[2]; DateTime timestamp = (DateTime)pressValues[3]; timestampStr = timestamp.ToString(); float press_distance = float.Parse((string)pressValues[4]); float press_duration = float.Parse((string)pressValues[5]); float compressor_pressure = float.Parse((string)pressValues[6]); float final_pressure = float.Parse((string)pressValues[7]); float pos1_pressure = float.Parse((string)pressValues[8]); float pos2_pressure = float.Parse((string)pressValues[9]); float pos3_pressure = float.Parse((string)pressValues[10]); float pos4_pressure = float.Parse((string)pressValues[11]); float pos5_pressure = float.Parse((string)pressValues[12]); string result = (string)pressValues[13]; string ng_reason = (string)pressValues[14]; string chartFileName = GetChartFile(timestamp); if (chartFileName != "") { try { string destDir = rootFolder + @"110438"; if (!Directory.Exists(destDir)) { Directory.CreateDirectory(destDir); } File.Copy(rootFolder + @"JPG\" + chartFileName, destDir + @"\" + chartFileName); } catch (Exception e1) { logger.Error(e1.Message, e1); } } logger.Info("QR1: " + gear_qr); logger.Info("QR2: " + pinion_qr); logger.Info("Time: " + timestamp.ToString()); logger.Info("chart: " + chartFileName); if (gear_qr == "" && pinion_qr == "") { logger.Warn("No QR code"); } else { client.Path = "object"; List <object> domain = new List <object>(); if (gear_qr != "") { domain.Add(XmlRpcParameter.AsArray("gear_qr", "=", gear_qr)); } if (pinion_qr != "") { domain.Add(XmlRpcParameter.AsArray("pinion_qr", "=", pinion_qr)); } if (domain.Count == 2) { domain.Insert(0, "|"); } XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw"); requestSearch.AddParams(db, responseLogin.GetInt(), pass, "batom.tesla.qrcode", "search_read", XmlRpcParameter.AsArray( domain ), XmlRpcParameter.AsStruct( XmlRpcParameter.AsMember("fields", XmlRpcParameter.AsArray("gear_qr", "pinion_qr")) // ,XmlRpcParameter.AsMember("limit", 2) ) ); XmlRpcResponse responseSearch = client.Execute(requestSearch); if (responseSearch.IsFault()) { logger.Error(responseSearch.GetFaultString()); } else if (!responseSearch.IsArray()) { logger.Error("查詢 odoo 資料庫異常"); } else { int qr_id = -1; // used as the flag (ok: qr_id > 0) List <Object> valueList = responseSearch.GetArray(); if (valueList.Count == 0) { Dictionary <string, object> values = new Dictionary <string, object>(); if (gear_qr != "") { values["gear_qr"] = gear_qr; } if (pinion_qr != "") { values["pinion_qr"] = pinion_qr; } XmlRpcRequest requestCreate = new XmlRpcRequest("execute_kw"); requestCreate.AddParams(db, responseLogin.GetInt(), pass, "batom.tesla.qrcode", "create", XmlRpcParameter.AsArray(values) ); XmlRpcResponse responseCreate = client.Execute(requestCreate); if (responseCreate.IsFault()) { logger.Error(responseCreate.GetFaultString()); } else { qr_id = responseCreate.GetInt(); } } else { string db_gear_qr = ""; string db_pinion_qr = ""; foreach (Dictionary <string, object> valueDictionary in valueList) { foreach (KeyValuePair <string, object> kvp in valueDictionary) { if (kvp.Key == "id") { qr_id = (int)kvp.Value; } else if (kvp.Key == "gear_qr" && kvp.Value is string) { db_gear_qr = (string)kvp.Value; } else if (kvp.Key == "pinion_qr" && kvp.Value is string) { db_pinion_qr = (string)kvp.Value; } } } if ((gear_qr == "" || gear_qr == db_gear_qr) && (pinion_qr == "" || pinion_qr == db_pinion_qr)) { // existing qr record, do nothing } else if (ValueConflict(gear_qr, db_gear_qr) || ValueConflict(pinion_qr, db_pinion_qr)) { logger.Error("與資料庫中下列 QR code 組合衝突,無法儲存:\n" + "軸: " + db_pinion_qr + "\n" + "餅: " + db_gear_qr ); } else { Dictionary <string, object> values = new Dictionary <string, object>(); if (gear_qr != "") { values["gear_qr"] = gear_qr; } if (pinion_qr != "") { values["pinion_qr"] = pinion_qr; } XmlRpcRequest requestWrite = new XmlRpcRequest("execute_kw"); requestWrite.AddParams(db, responseLogin.GetInt(), pass, "batom.tesla.qrcode", "write", XmlRpcParameter.AsArray(XmlRpcParameter.AsArray(qr_id), values) ); XmlRpcResponse responseWrite = client.Execute(requestWrite); if (responseWrite.IsFault()) { logger.Error(responseWrite.GetFaultString()); qr_id = -1; } } } if (qr_id > 0) { domain = new List <object>(); domain.Add(XmlRpcParameter.AsArray("timestamp", "=", timestampStr)); requestSearch = new XmlRpcRequest("execute_kw"); requestSearch.AddParams(db, responseLogin.GetInt(), pass, "batom.tesla.press_info", "search_read", XmlRpcParameter.AsArray( domain ), XmlRpcParameter.AsStruct( XmlRpcParameter.AsMember("fields", XmlRpcParameter.AsArray("timestamp")) // ,XmlRpcParameter.AsMember("limit", 2) ) ); responseSearch = client.Execute(requestSearch); if (responseSearch.IsFault()) { logger.Error(responseSearch.GetFaultString()); } else if (!responseSearch.IsArray()) { logger.Error("查詢 odoo 資料庫異常"); } else { valueList = responseSearch.GetArray(); if (valueList.Count > 0) { logger.Warn("壓配資料已存在"); } else { Dictionary <string, object> values = new Dictionary <string, object>(); values["qr_id"] = qr_id; values["product_model"] = product_model; values["timestamp"] = timestampStr; values["press_distance"] = press_distance; values["press_duration"] = press_duration; values["compressor_pressure"] = compressor_pressure; values["final_pressure"] = final_pressure; values["pos1_pressure"] = pos1_pressure; values["pos2_pressure"] = pos2_pressure; values["pos3_pressure"] = pos3_pressure; values["pos4_pressure"] = pos4_pressure; values["pos5_pressure"] = pos5_pressure; values["result"] = result; values["ng_reason"] = ng_reason; values["chart_file_name"] = chartFileName; if (chartFileName != "") { try { values["chart"] = Convert.ToBase64String(File.ReadAllBytes(rootFolder + @"JPG\" + chartFileName)); } catch (Exception e2) { logger.Error(e2.Message, e2); } } else { values["chart"] = false; } XmlRpcRequest requestCreate = new XmlRpcRequest("execute_kw"); requestCreate.AddParams(db, responseLogin.GetInt(), pass, "batom.tesla.press_info", "create", XmlRpcParameter.AsArray(values) ); XmlRpcResponse responseCreate = client.Execute(requestCreate); if (responseCreate.IsFault()) { logger.Error(responseCreate.GetFaultString()); } } } } } } } catch (Exception ex) { logger.Error(ex.Message, ex); } return(timestampStr); }
private void Save() { string pressfit_qr = ""; string gear_qr = ""; string pinion_qr = ""; if (txtPressfit.Text != null) { pressfit_qr = txtPressfit.Text.Trim(); } if (txtGear.Text != null) { gear_qr = txtGear.Text.Trim(); } if (txtPinion.Text != null) { pinion_qr = txtPinion.Text.Trim(); } if (pressfit_qr == "" && gear_qr == "" && pinion_qr == "") { MessageBox.Show("請掃描 QR Code", "錯誤"); } else { try { Cursor.Current = Cursors.WaitCursor; XmlRpcClient client = new XmlRpcClient(); client.Url = odooUrl; client.Path = "common"; // LOGIN XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate"); requestLogin.AddParams(db, user, pass, XmlRpcParameter.EmptyStruct()); XmlRpcResponse responseLogin = client.Execute(requestLogin); if (responseLogin.IsFault()) { Cursor.Current = Cursors.Default; MessageBox.Show("無法連線到資料庫,請通知 IT 人員", "錯誤"); } else { client.Path = "object"; List <object> domain = new List <object>(); if (pressfit_qr != "") { domain.Add(XmlRpcParameter.AsArray("pressfit_qr", "=", pressfit_qr)); } if (gear_qr != "") { domain.Add(XmlRpcParameter.AsArray("gear_qr", "=", gear_qr)); } if (pinion_qr != "") { domain.Add(XmlRpcParameter.AsArray("pinion_qr", "=", pinion_qr)); } if (domain.Count >= 2) { if (domain.Count == 3) { domain.Insert(0, "|"); } domain.Insert(0, "|"); } XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw"); requestSearch.AddParams(db, responseLogin.GetInt(), pass, "batom.tesla.qrcode", "search_read", XmlRpcParameter.AsArray( domain ), XmlRpcParameter.AsStruct( XmlRpcParameter.AsMember("fields", XmlRpcParameter.AsArray("pressfit_qr", "gear_qr", "pinion_qr")) // ,XmlRpcParameter.AsMember("limit", 2) ) ); XmlRpcResponse responseSearch = client.Execute(requestSearch); if (responseSearch.IsFault()) { MessageBox.Show(responseSearch.GetFaultString(), "錯誤"); } else if (!responseSearch.IsArray()) { Cursor.Current = Cursors.Default; MessageBox.Show("查詢資料庫異常,請通知 IT 人員", "錯誤"); } else { List <Object> valueList = responseSearch.GetArray(); if (valueList.Count == 0) { Dictionary <string, object> values = new Dictionary <string, object>(); if (pressfit_qr != "") { values["pressfit_qr"] = pressfit_qr; } if (gear_qr != "") { values["gear_qr"] = gear_qr; } if (pinion_qr != "") { values["pinion_qr"] = pinion_qr; } XmlRpcRequest requestCreate = new XmlRpcRequest("execute_kw"); requestCreate.AddParams(db, responseLogin.GetInt(), pass, "batom.tesla.qrcode", "create", XmlRpcParameter.AsArray(values) ); XmlRpcResponse responseCreate = client.Execute(requestCreate); if (responseCreate.IsFault()) { MessageBox.Show(responseCreate.GetFaultString(), "錯誤"); } else { AutoClosingMessageBox.Show("已儲存", "完成", 1000); ClearFields(); } } else { string db_pressfit_qr = ""; string db_gear_qr = ""; string db_pinion_qr = ""; int id = -1; foreach (Dictionary <string, object> valueDictionary in valueList) { foreach (KeyValuePair <string, object> kvp in valueDictionary) { if (kvp.Key == "id") { id = (int)kvp.Value; } else if (kvp.Key == "pressfit_qr" && kvp.Value is string) { db_pressfit_qr = (string)kvp.Value; } else if (kvp.Key == "gear_qr" && kvp.Value is string) { db_gear_qr = (string)kvp.Value; } else if (kvp.Key == "pinion_qr" && kvp.Value is string) { db_pinion_qr = (string)kvp.Value; } } } if ((pressfit_qr == "" || pressfit_qr == db_pressfit_qr) && (gear_qr == "" || gear_qr == db_gear_qr) && (pinion_qr == "" || pinion_qr == db_pinion_qr)) { Cursor.Current = Cursors.Default; MessageBox.Show("QR code 組合已存在", "錯誤"); } else if (ValueConflict(pressfit_qr, db_pressfit_qr) || ValueConflict(gear_qr, db_gear_qr) || ValueConflict(pinion_qr, db_pinion_qr)) { Cursor.Current = Cursors.Default; MessageBox.Show("與資料庫中下列 QR code 組合衝突,無法儲存:\n\n" + "總成:" + db_pressfit_qr + "\n" + "軸: " + db_pinion_qr + "\n" + "餅: " + db_gear_qr, "錯誤" ); } else { Dictionary <string, object> values = new Dictionary <string, object>(); if (pressfit_qr != "") { values["pressfit_qr"] = pressfit_qr; } if (gear_qr != "") { values["gear_qr"] = gear_qr; } if (pinion_qr != "") { values["pinion_qr"] = pinion_qr; } XmlRpcRequest requestWrite = new XmlRpcRequest("execute_kw"); requestWrite.AddParams(db, responseLogin.GetInt(), pass, "batom.tesla.qrcode", "write", XmlRpcParameter.AsArray(XmlRpcParameter.AsArray(id), values) ); XmlRpcResponse responseWrite = client.Execute(requestWrite); if (responseWrite.IsFault()) { MessageBox.Show(responseWrite.GetFaultString(), "錯誤"); } else { AutoClosingMessageBox.Show("已儲存", "完成", 1000); ClearFields(); } } } } Cursor.Current = Cursors.Default; } } catch { MessageBox.Show("無法儲存,請通知IT人員", "錯誤"); } } }