private void backSOAP_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;


            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
            }
            else
            {
                if (soapcount <= 20)
                {
                    soapcount += 1;
                }
                else
                {
                    soapcount = 0;
                }

                // Perform a time consuming operation and report progress.
                //System.Threading.Thread.Sleep(500);
                //worker.ReportProgress(i * 10);
                try
                {
                    //Cursor.Current = Cursors.WaitCursor;
                    //SmartServer.SOAP rs = new SmartServer.SOAP();   //get device tree list
                    SOAP20_DLL.SOAP rs = new SOAP20_DLL.SOAP();   //read or write NV

                    //string getLon = rs.request(Lonip.Text);
                    string getLon = rs.request(Lonip.Text);

                    backSOAPrun.Items.Clear();
                    backSOAPrunPath.Items.Clear();
                    //MessageBox.Show(getLon);
                    string[] spDevice = getLon.Split('※');
                    //searchBar.Maximum = spDevice.Length - 1;
                    for (int i = 1; i <= spDevice.Length - 1; i++)
                    {
                        string[] dd = spDevice[i].Split('$');
                        backSOAPrun.Items.Add(dd[0]);
                        backSOAPrunPath.Items.Add(spDevice[i]);
                        //LonDeviceListPath.Items.Add(spDevice[i]);
                        //searchBar.Value = i;
                        worker.ReportProgress(i * 10);
                    }

                    Cursor.Current = Cursors.Default;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    //Cursor.Current = Cursors.Default;
                }
                finally
                {
                }
            }
        }
        private void CMDApply_Click(object sender, EventArgs e)
        {
            if (inputVal.Text != "")
            {
                if (target != "")
                {
                    string[] path = target.Split('@');
                    //SmartServer.SOAP ss = new SmartServer.SOAP();
                    SOAP20_DLL.SOAP ws = new SOAP20_DLL.SOAP();
                    //ss.writeNV(ip, path[2], inputVal.Text);
                    string requestVal          = ws.writeNV(ip, path[2], inputVal.Text);
                    OutputPopObjectstruct edit = (OutputPopObjectstruct)memoryData.OutputPopData[listIndex];

                    string formatForMySqlDate = DateTime.Now.ToString("yyyy-MM-dd");
                    string formatForMySqlTime = DateTime.Now.ToString("HH:mm:ss");
                    //save to database
                    if (memoryData.database.Count != 0)
                    {
                        database SQLstr = (database)memoryData.database[0];

                        //save to database
                        //open database   ///////////////////////////////////////////////////////////////////////
                        string          connStr = "server=" + SQLstr.ip + ";port=" + SQLstr.port + ";uid=" + SQLstr.user + ";pwd=" + SQLstr.password + ";database=" + SQLstr.DBname;
                        MySqlConnection conn    = new MySqlConnection(connStr);
                        MySqlCommand    addList = conn.CreateCommand();
                        conn.Open();

                        string sql = @"SELECT * FROM output_list WHERE site='" + edit.site + "' and fab='" + edit.fab + "' and area='" + edit.area
                                     + "' and device='" + edit.device + "' and function='" + edit.function + "' and NVname='" + edit.NV + "' ";

                        MySqlCommand    cmdCHK  = new MySqlCommand(sql, conn);
                        MySqlDataReader dataCHK = cmdCHK.ExecuteReader();
                        int             listid  = 0;

                        if (dataCHK.HasRows == true)
                        {
                            dataCHK.Read();
                            //有查到資料
                            listid = int.Parse(dataCHK.GetString(0));
                            dataCHK.Close();

                            addList.CommandText = @"update nico_db.output_list set description = '" + edit.description + "' where id = " + listid;
                            addList.ExecuteNonQuery();

                            MySqlCommand addValue = conn.CreateCommand();

                            addValue.CommandText = @"Insert into output_value(list_id,NVvalue_txt,date,time) values('" + listid + "','"
                                                   + inputVal.Text + "','" + formatForMySqlDate + "','" + formatForMySqlTime + "')";

                            addValue.ExecuteNonQuery();
                        }
                        else
                        {
                            dataCHK.Close();
                            // MySqlCommand addList = conn.CreateCommand();

                            //沒有查到資料
                            addList.CommandText = @"Insert into output_list(site,fab,area,device,function,NVname,NVtype,description,ip) values('"
                                                  + edit.site + "','" + edit.fab + "','" + edit.area + "','" + edit.device + "','" + edit.function + "','"
                                                  + edit.NV + "','" + edit.NVtype + "','" + edit.description + "','" + edit.ip + "');";

                            addList.ExecuteNonQuery();
                            //get list_id
                            dataCHK = cmdCHK.ExecuteReader();
                            dataCHK.Read();
                            if (dataCHK.HasRows == true)
                            {
                                listid = int.Parse(dataCHK.GetString(0));
                                dataCHK.Close();
                            }

                            //write to output_value
                            MySqlCommand addValue = conn.CreateCommand();

                            addValue.CommandText = @"Insert into output_value(list_id,NVvalue_txt,date,time) values('" + listid + "','"
                                                   + inputVal.Text + "','" + formatForMySqlDate + "','" + formatForMySqlTime + "')";

                            addValue.ExecuteNonQuery();
                        }

                        conn.Close();
                        //close database    ////////////////////////////////////////////////////////////////////////
                    }


                    //send email
                    if (edit.mail == true)
                    {
                        for (int m = 0; m < memoryData.GroupContact2.Count; m++)
                        {
                            ContactGroup2 mg = (ContactGroup2)memoryData.GroupContact2[m];
                            if (edit.contactGroup == mg.groupname)
                            {
                                using (FileStream input = new FileStream(Application.StartupPath + @"\Resources\SMTP.dat", FileMode.Open))
                                {   // 讀取整數值
                                    BinaryReader reader = new BinaryReader(input);
                                    string       data   = reader.ReadString();

                                    int    numOfBytes = data.Length / 8;
                                    byte[] bytes      = new byte[numOfBytes];
                                    for (int ri = 0; ri < numOfBytes; ++ri)
                                    {
                                        bytes[ri] = Convert.ToByte(data.Substring(8 * ri, 8), 2);
                                    }
                                    //File.WriteAllBytes(fileName, bytes);
                                    string   val          = GetString(bytes);
                                    string[] SpStr        = val.Split('$');
                                    string   SMTPhost     = SpStr[1];
                                    string   SMTPport     = SpStr[2];
                                    Boolean  SSL          = Boolean.Parse(SpStr[3]);
                                    string   userName     = SpStr[4];
                                    string   userPassword = SpStr[5];

                                    string targetpath = edit.site + "/" + edit.fab + "/" + edit.area + "/" + edit.device + "/" + edit.function + "/" + edit.NV;


                                    for (int sg = 0; sg < mg.groupmember.Count; sg++)
                                    {
                                        Contactstruct sgm = (Contactstruct)mg.groupmember[sg];
                                        if (sgm.mail != null && sgm.mail != "")
                                        {
                                            email.From    = new MailAddress(userName);                                                     //寄件人
                                            email.Subject = "IO trigger!  target : " + targetpath;                                         //標題
                                            email.Body    = "trigger time = " + formatForMySqlDate + " " + formatForMySqlTime + Environment.NewLine
                                                            + "trigger value = " + inputVal.Text + Environment.NewLine + edit.description; //內容
                                            email.To.Add(sgm.mail);                                                                        //收件人
                                            SMTP.EnableSsl   = true;
                                            SMTP.Port        = int.Parse(SMTPport);                                                        //smtp port
                                            SMTP.Host        = SMTPhost;                                                                   //smtp host(主機ip)
                                            SMTP.Credentials = new System.Net.NetworkCredential(userName, userPassword);                   //寄件人,密碼
                                            SMTP.Send(email);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("not target!");
                }
            }
        }