Esempio n. 1
0
        private static bool Connect()
        {
            bool rc = false;

            try
            {
                var DBPath = rootFolder + "ICM802.mdb";

                conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;"
                                           + "Data Source=" + DBPath);
                conn.Open();

                client      = new XmlRpcClient();
                client.Url  = odooUrl;
                client.Path = "common";

                // LOGIN

                XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");
                requestLogin.AddParams(db, user, pass, XmlRpcParameter.EmptyStruct());

                responseLogin = client.Execute(requestLogin);

                if (responseLogin.IsFault())
                {
                    logger.Error("無法連線到 odoo 資料庫");
                }
                else
                {
                    rc = true;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
            }

            return(rc);
        }
Esempio n. 2
0
 public void applyAccessCredentials(string database, string username, string pass) => addParameterAnyType(database, username, pass, XmlRpcParameter.EmptyStruct());
Esempio n. 3
0
        public static void TestReadRecords()
        {
            XmlRpcClient client = new XmlRpcClient();

            client.Url  = Url;
            client.Path = "common";

            // LOGIN

            XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");

            requestLogin.AddParams(db, user, pass, XmlRpcParameter.EmptyStruct());

            XmlRpcResponse responseLogin = client.Execute(requestLogin);

            // Console.WriteLine("authenticate");
            // Console.WriteLine("REQUEST: ");
            // client.WriteRequest(Console.Out);

            // Console.WriteLine();
            // Console.WriteLine();
            // Console.WriteLine("RESPONSE: ");
            // client.WriteResponse(Console.Out);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("LOGIN: "******"object";

            XmlRpcRequest requestSearch = new XmlRpcRequest("execute_kw");

            requestSearch.AddParams(db, responseLogin.GetInt(), pass, "res.partner", "search",
                                    XmlRpcParameter.AsArray(
                                        XmlRpcParameter.AsArray(
                                            XmlRpcParameter.AsArray("is_company", "=", true), XmlRpcParameter.AsArray("customer", "=", true)
                                            )
                                        )
                                    );

            requestSearch.AddParamStruct(
                XmlRpcParameter.AsMember("limit", 2)
                );

            XmlRpcResponse responseSearch = client.Execute(requestSearch);

            // Console.WriteLine();
            // Console.WriteLine();
            // Console.WriteLine("search");
            // Console.WriteLine("REQUEST: ");
            // client.WriteRequest(Console.Out);

            // Console.WriteLine();
            // Console.WriteLine();
            // Console.WriteLine("RESPONSE: ");
            // client.WriteResponse(Console.Out);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("SEARCH: ");
            if (responseSearch.IsFault())
            {
                Console.WriteLine(responseSearch.GetFaultString());
            }
            else
            {
                Console.WriteLine(responseSearch.GetString());
            }

            // READ

            XmlRpcRequest requestRead = new XmlRpcRequest("execute_kw");

            requestRead.AddParams(db, responseLogin.GetInt(), pass, "res.partner", "read",
                                  XmlRpcParameter.AsArray(
                                      responseSearch.GetArray()
                                      )
                                  );

            requestRead.AddParamStruct(XmlRpcParameter.AsMember("fields",
                                                                XmlRpcParameter.AsArray("name")
                                                                )
                                       );

            XmlRpcResponse responseRead = client.Execute(requestRead);

            // Console.WriteLine();
            // Console.WriteLine();
            // Console.WriteLine("read");
            // Console.WriteLine("REQUEST: ");
            // client.WriteRequest(Console.Out);

            // Console.WriteLine();
            // Console.WriteLine();
            // Console.WriteLine("RESPONSE: ");
            // client.WriteResponse(Console.Out);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("READ: ");
            if (responseRead.IsFault())
            {
                Console.WriteLine(responseRead.GetFaultString());
            }
            else
            {
                Console.WriteLine(responseRead.GetString());
            }
        }
Esempio n. 4
0
        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人員", "錯誤");
                }
            }
        }
Esempio n. 5
0
        public static void TestCreateRecord()
        {
            XmlRpcClient client = new XmlRpcClient();

            client.Url  = Url;
            client.Path = "common";

            // LOGIN

            XmlRpcRequest requestLogin = new XmlRpcRequest("authenticate");

            requestLogin.AddParams(db, user, pass, XmlRpcParameter.EmptyStruct());

            XmlRpcResponse responseLogin = client.Execute(requestLogin);

            Console.WriteLine("authenticate");
            Console.WriteLine("REQUEST: ");
            client.WriteRequest(Console.Out);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("RESPONSE: ");
            client.WriteResponse(Console.Out);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("LOGIN: "******"object";

            XmlRpcRequest requestCreate = new XmlRpcRequest("execute_kw");

            requestCreate.AddParams(db, responseLogin.GetInt(), pass, "res.partner", "create",
                                    XmlRpcParameter.AsArray(
                                        XmlRpcParameter.AsStruct(
                                            XmlRpcParameter.AsMember("name", "Albert Einstein")
                                            , XmlRpcParameter.AsMember("image", Convert.ToBase64String(File.ReadAllBytes("img/einstein.jpg")))
                                            , XmlRpcParameter.AsMember("email", "*****@*****.**")
                                            )
                                        )
                                    );

            XmlRpcResponse responseCreate = client.Execute(requestCreate);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("create");
            Console.WriteLine("REQUEST: ");
            client.WriteRequest(Console.Out);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("RESPONSE: ");
            client.WriteResponse(Console.Out);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("READ: ");
            if (responseCreate.IsFault())
            {
                Console.WriteLine(responseCreate.GetFaultString());
            }
            else
            {
                Console.WriteLine(responseCreate.GetString());
            }
        }