Esempio n. 1
0
 private void SDPlayFileProcess(DeviceListener.Device d, string info, bool completed, int process)
 {
     //progressBar1.Value = process;
     if (completed)
     {
         if (process >= 0)
         {
             panel2.BackColor = Color.LimeGreen;
             panel3.BackColor = Color.LimeGreen;
             PlaySuccessNotify();
         }
         else
         {
             panel2.BackColor = Color.Red;
             panel3.BackColor = Color.Red;
             PlayFaultNotify();
         }
         progressBar1.Value = 0; // process;
         progressBar2.Value = 0; // process;
     }
     else
     {
         if (process >= 0)
         {
             progressBar1.Value = process;
             progressBar2.Value = process;
         }
     }
 }
Esempio n. 2
0
        private void button5_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                DeviceItem            item = (DeviceItem)comboBox1.SelectedItem;
                DeviceListener.Device d    = deviceListener.Find(item.id);

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string filepathname = openFileDialog1.FileName;

                    subForm = new FormProcess();
                    //subForm.Parent = this;
                    subForm.TopMost = true;
                    subForm.SetInfo(filepathname);
                    subForm.SetProcess(0);
                    subForm.Show();

                    ThreadParam ap = new ThreadParam();
                    ap.id       = d.id;
                    ap.filename = filepathname;

                    dataThread = new Thread(SDFileUploadThread);
                    dataThread.IsBackground = true;
                    dataThread.Start(ap);
                }
            }
        }
Esempio n. 3
0
        private void EventLogin(DeviceListener.Device d)
        {
            if ((DeviceID != null) && (DeviceID.Equals(d.id) == false))
            {
                return;
            }

            DeviceItem item = new DeviceItem();

            item.id         = d.id;
            item.port       = d.peerport;
            item.ip         = d.peerip;
            item.version    = d.version;
            item.devicetype = d.devicetype;

            DeviceID     = d.id;
            label4.Text  = item.ToString();
            label10.Text = item.devicetype;
            label11.Text = item.version;
            //label11.te

            PlaySuccessNotify();

            panel1.BackColor = Color.LimeGreen;
        }
Esempio n. 4
0
        private void button4_Click(object sender, EventArgs e)
        {
            int ret;

            if (comboBox1.SelectedItem != null)
            {
                listView1.Items.Clear();

                DeviceItem            item = (DeviceItem)comboBox1.SelectedItem;
                DeviceListener.Device d    = deviceListener.Find(item.id);
                if (d != null)
                {
                    string filename;
                    int    filesize;
                    ret = deviceListener.SDCardGetFirstFile(d, out filename, out filesize);
                    while (ret == 0)
                    {
                        ListViewItem ls = new ListViewItem("");
                        ls.Text = (listView1.Items.Count + 1).ToString();
                        ls.SubItems.Add(filename);
                        ls.SubItems.Add(filesize.ToString());
                        listView1.Items.Add(ls);

                        ret = deviceListener.SDCardGetNextFile(d, out filename, out filesize);
                    }
                }
            }
        }
Esempio n. 5
0
 private void SDPlayFileProcess(DeviceListener.Device d, string info, bool completed, int process)
 {
     progressBar1.Value = process;
     if (completed)
     {
         panel3.BackColor = Color.LimeGreen;
         PlaySuccessNotify();
     }
 }
Esempio n. 6
0
        //private delegate void LoginHandler();

        private void EventLogin(DeviceListener.Device d)
        {
            DeviceItem item = new DeviceItem();

            item.id   = d.id;
            item.port = d.peerport;
            item.ip   = d.peerip;
            listBox1.Items.Add(item);
        }
Esempio n. 7
0
 private void FilePlayProcess(DeviceListener.Device d, string info, bool completed, int param)
 {
     progressBar2.Value = param;
     if (completed)
     {
         progressBar2.Value = 0;
         panel5.BackColor   = Color.LimeGreen;
         PlaySuccessNotify();
     }
 }
Esempio n. 8
0
        private void button8_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                DeviceItem            item = (DeviceItem)comboBox1.SelectedItem;
                DeviceListener.Device d    = deviceListener.Find(item.id);

                deviceListener.SDCardPlayFileStop(d);
            }
        }
Esempio n. 9
0
        private void button7_Click(object sender, EventArgs e)
        {
            if (comboBox4.SelectedItem != null)
            {
                DeviceItem            item = (DeviceItem)comboBox4.SelectedItem;
                DeviceListener.Device d    = deviceListener.Find(item.id);

                int vol = (int)numericUpDown2.Value;
                deviceListener.AudioSetVolume(d, vol);
            }
        }
Esempio n. 10
0
 private void FileEmergencyPlayProcess(DeviceListener.Device d, string info, bool completed, int param)
 {
     progressBar1.Value = param;
     if (completed)
     {
         progressBar1.Value = 0;
         button9.Enabled    = true;
         button8.Enabled    = false;
     }
     //Application.DoEvents();
 }
Esempio n. 11
0
 private void EventLogout(DeviceListener.Device d)
 {
     foreach (DeviceItem item in listBox1.Items)
     {
         if (item.id == d.id)
         {
             listBox1.Items.Remove(item);
             break;
         }
     }
 }
Esempio n. 12
0
 private void MicInputThreadCallBack(DeviceListener.Device d, string info, bool completed, int param)
 {
     if (completed)
     {
         button4.Text      = "开始";
         button4.BackColor = SystemColors.Control;
         if (param < 0)
         {
             MessageBox.Show(info);
         }
     }
 }
Esempio n. 13
0
        private void SDFilePlayThread(object obj)
        {
            IPAddress   localaddr = IPAddress.Parse("0.0.0.0");
            TcpListener tcpserver = new TcpListener(localaddr, 0);

            tcpserver.Start();
            IPEndPoint localep = (IPEndPoint)tcpserver.LocalEndpoint;

            WorkProcessHandler h = new WorkProcessHandler(UploadFileProcess);

            ThreadParam ap = (ThreadParam)obj;

            DeviceListener.Device d = deviceListener.Find(ap.id);
            if (d == null)
            {
                return;
            }
            string fname = System.IO.Path.GetFileName(ap.filename);
            int    ret   = deviceListener.SDCardPlayFile(d, fname, ap.volume);

            if (ret != 0)
            {
                return;
            }
            TcpClient tc = null;

            try
            {
                while (true)
                {
                    int ret2 = deviceListener.SDCardGetPlayFileStatus(d, out string filename, out int runtime, out int process);
                    if (ret2 == 0)
                    {
                        this.BeginInvoke(h, d, filename, false, process);
                    }
                    else
                    {
                        break;
                    }
                    Thread.Sleep(1000);
                }
                this.BeginInvoke(h, d, null, true, 0);

                dataThread = null;
            }
            catch (ThreadAbortException abortException)
            {
                dataThread = null;
            }
        }
Esempio n. 14
0
        private void button8_Click(object sender, EventArgs e)
        {
            if (comboBox4.SelectedItem != null)
            {
                DeviceItem            item = (DeviceItem)comboBox4.SelectedItem;
                DeviceListener.Device d    = deviceListener.Find(item.id);

                dataThread.Abort();
                deviceListener.FileEmergencyPlayStop(d);

                button9.Enabled = true;
                button8.Enabled = false;
            }
        }
Esempio n. 15
0
        private void button4_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                DeviceItem            item = (DeviceItem)comboBox1.SelectedItem;
                DeviceListener.Device d    = deviceListener.Find(item.id);

                dataThread.Abort();
                //Thread.Sleep(100);
                //deviceListener.FilePlayStop(d);

                button3.Enabled = true;
                button4.Enabled = false;
            }
        }
Esempio n. 16
0
 private void button3_Click(object sender, EventArgs e)
 {
     if (comboBox1.SelectedItem != null)
     {
         DeviceItem            item = (DeviceItem)comboBox1.SelectedItem;
         DeviceListener.Device d    = deviceListener.Find(item.id);
         if (d != null)
         {
             int ret = deviceListener.SDCardGetInfo(d, out string disk_size, out string free_size);
             if (ret == 0)
             {
                 label4.Text = disk_size;
                 label5.Text = free_size;
             }
         }
     }
 }
Esempio n. 17
0
        private void SDFilePlayThread(object obj)
        {
            WorkProcessHandler h = new WorkProcessHandler(SDPlayFileProcess);

            ThreadParam ap = (ThreadParam)obj;

            DeviceListener.Device d = deviceListener.Find(ap.id);
            if (d == null)
            {
                this.BeginInvoke(h, d, null, true, -1);
                return;
            }
            string fname = System.IO.Path.GetFileName(ap.filename);
            int    ret   = deviceListener.SDCardPlayFile(d, fname, ap.volume);

            if (ret != 0)
            {
                this.BeginInvoke(h, d, null, true, -2);
                return;
            }
            //TcpClient tc = null;
            try
            {
                while (true)
                {
                    int ret2 = deviceListener.SDCardGetPlayFileStatus(d, out string filename, out int runtime, out int process);
                    if (ret2 == 0)
                    {
                        this.BeginInvoke(h, d, filename, false, process);
                    }
                    else
                    {
                        break;
                    }
                    Thread.Sleep(1000);
                }
                this.BeginInvoke(h, d, null, true, 0);
            }
            catch (ThreadAbortException abortException)
            {
            }
            finally
            {
            }
        }
Esempio n. 18
0
 private void UploadFileProcess(DeviceListener.Device d, string info, bool completed, int param)
 {
     if (!completed)
     {
         if (subForm != null)
         {
             subForm.SetInfo(info);
             subForm.SetProcess(param);
         }
     }
     else
     {
         if (subForm != null)
         {
             subForm.Close();
             subForm = null;
         }
     }
     //Application.DoEvents();
 }
Esempio n. 19
0
        private void button11_Click(object sender, EventArgs e)
        {
            if (DeviceID == null)
            {
                return;
            }
            DeviceListener.Device d = deviceListener.Find(DeviceID);
            panel5.BackColor = SystemColors.Control;

            ThreadParam ap = new ThreadParam();

            ap.id         = DeviceID;
            ap.filename   = "../test2.mp3";
            ap.streamtype = "mp3";
            ap.volume     = 85;

            dataThread = new Thread(FilePlayThread);
            dataThread.IsBackground = true;
            dataThread.Start(ap);
        }
Esempio n. 20
0
        private void button6_Click(object sender, EventArgs e)
        {
            int ret;

            if (comboBox1.SelectedItem != null)
            {
                DeviceItem            item = (DeviceItem)comboBox1.SelectedItem;
                DeviceListener.Device d    = deviceListener.Find(item.id);

                for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
                {
                    ListViewItem ls       = (ListViewItem)listView1.SelectedItems[i];
                    string       filename = ls.SubItems[1].Text;
                    ret = deviceListener.SDCardDeleteFile(d, filename);
                    if (ret == 0)
                    {
                        listView1.Items.Remove(ls);
                    }
                }
            }
        }
Esempio n. 21
0
        private void button8_Click(object sender, EventArgs e)
        {
            if (DeviceID == null)
            {
                return;
            }
            DeviceListener.Device d = deviceListener.Find(DeviceID);

            panel3.BackColor = SystemColors.Control;

            ThreadParam ap = new ThreadParam();

            ap.filename = "product_test.mp3";
            ap.id       = DeviceID;
            ap.volume   = 85;

            label6.Text = ap.filename;

            dataThread = new Thread(SDFilePlayThread);
            dataThread.IsBackground = true;
            dataThread.Start(ap);
        }
Esempio n. 22
0
 private void uploadFileProcess(DeviceListener.Device d, string info, bool completed, int param)
 {
     if (!completed)
     {
         if (subForm != null)
         {
             subForm.SetInfo(info);
             subForm.SetProcess(param);
         }
     }
     else
     {
         subForm.Close();
         deviceListener.EventWorkProcess -= uploadFileProcess;
         subForm = null;
         if (param != 0)
         {
             MessageBox.Show("upload file is fault, errcode is " + param.ToString());
         }
     }
     Application.DoEvents();
 }
Esempio n. 23
0
        private void button7_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                DeviceItem            item = (DeviceItem)comboBox1.SelectedItem;
                DeviceListener.Device d    = deviceListener.Find(item.id);

                subForm = new FormProcess();
                //subForm.Parent = this;
                //subForm.TopMost = true;
                subForm.SetInfo("");
                subForm.SetProcess(0);
                subForm.Show();

                ThreadParam ap = new ThreadParam();
                ap.filename = label7.Text;
                ap.id       = d.id;
                ap.volume   = (int)numericUpDown1.Value;

                dataThread = new Thread(SDFilePlayThread);
                dataThread.IsBackground = true;
                dataThread.Start(ap);
            }
        }
Esempio n. 24
0
        private void EventLogin(DeviceListener.Device d)
        {
            if ((DeviceID != null) && (DeviceID.Equals(d.id) == false))
            {
                return;
            }

            DeviceItem item = new DeviceItem();

            item.id         = d.id;
            item.port       = d.peerport;
            item.ip         = d.peerip;
            item.version    = d.version;
            item.devicetype = d.devicetype;

            DeviceID    = d.id;
            label5.Text = item.ToString();
            label7.Text = item.version;

            label13.Text = item.ToString();
            label11.Text = item.version;
            //
            //label10.Text = item.devicetype;
            //label11.Text = item.version;
            //label11.te

            PlaySuccessNotify();

            panel1.BackColor = Color.LimeGreen;
            panel4.BackColor = Color.LimeGreen;
            //panel2.BackColor = SystemColors.Control;

            button2.Enabled = false;
            button4.Enabled = false;
            test_step       = 1;
        }
Esempio n. 25
0
        private void startSDPlay()
        {
            if (DeviceID == null)
            {
                return;
            }
            DeviceListener.Device d = deviceListener.Find(DeviceID);

            panel2.BackColor = SystemColors.Control;
            panel3.BackColor = SystemColors.Control;

            ThreadParam ap = new ThreadParam();

            ap.filename = "product_test.mp3";
            ap.id       = DeviceID;
            ap.volume   = 85;

            label14.Text = ap.filename;
            label9.Text  = ap.filename;

            SDPlayThreadHandle = new Thread(SDFilePlayThread);
            SDPlayThreadHandle.IsBackground = true;
            SDPlayThreadHandle.Start(ap);
        }
Esempio n. 26
0
        private void FileEmergencyPlayThread(object obj)
        {
            UdpClient  udpserver = new UdpClient(9999);
            IPEndPoint ep        = new IPEndPoint(IPAddress.Any, 0);

            WorkProcessHandler h = new WorkProcessHandler(FileEmergencyPlayProcess);

            ThreadParam ap = (ThreadParam)obj;

            DeviceListener.Device d = deviceListener.Find(ap.id);
            if (d == null)
            {
                return;
            }
            int ret = deviceListener.FileEmergencyPlayStart(d, "0.0.0.0", 9999, ap.filename, ap.streamtype, ap.volume, d.id.ToString());

            if (ret != 0)
            {
                return;
            }

            FileStream fs = File.OpenRead(ap.filename);

            if (fs == null)
            {
                return;
            }

            try
            {
                long   starttime = DateTime.Now.Ticks;
                string d_ip      = null;
                int    d_port    = 0;
                while ((DateTime.Now.Ticks - starttime) < 3000 * 10000)//3000ms
                {
                    if (udpserver.Available > 0)
                    {
                        byte[] bs = udpserver.Receive(ref ep);
                        string s  = System.Text.Encoding.Default.GetString(bs);
                        if (s.Contains(d.id.ToString()))
                        {
                            d_ip   = ep.Address.ToString();
                            d_port = ep.Port;
                            break;
                        }
                    }
                }

                if (d_port != 0)
                {
                    int     playfiletime   = 0;
                    int     time           = System.Environment.TickCount;
                    int     starttime1     = time;
                    bool    eof            = false;
                    byte[]  fbs            = new byte[320];
                    short[] pcm            = new short[160];
                    int     sequenceNumber = 0;
                    long    timestamp      = 0;
                    fs.Read(fbs, 0, 44);
                    long writebytes = 44;
                    while (!eof)
                    {
                        int runtime = System.Environment.TickCount - starttime1;
                        if ((runtime + 500) >= playfiletime)
                        {
                            int readbytes = fs.Read(fbs, 0, fbs.Length);

                            IntPtr p = Marshal.UnsafeAddrOfPinnedArrayElement(fbs, 0);
                            Marshal.Copy(p, pcm, 0, 160);
                            //GroovyCodecs.G711.uLaw.ULawEncoder g711encode = new GroovyCodecs.G711.uLaw.ULawEncoder();
                            //byte[] ba = g711encode.Process(pcm);
                            byte[] ba = g711.g711Encode_ulwa(pcm);

                            RtpPacket packet = new RtpPacket(RtpPayloadType.G711_uLaw, sequenceNumber, timestamp, ba, ba.Length);
                            sequenceNumber++;
                            timestamp    += 20;// ba.Length;
                            playfiletime += 20;
                            udpserver.Send(packet.ToArray(), packet.Length, d_ip, d_port);

                            writebytes += readbytes;
                            if (readbytes < fbs.Length)
                            {
                                eof = true;
                            }
                            int process = (int)(100 * writebytes / fs.Length);
                            this.BeginInvoke(h, d, ap.filename, false, process);
                        }
                    }
                }
                udpserver.Close();
                Thread.Sleep(500);
                deviceListener.FileEmergencyPlayStop(d);
                this.BeginInvoke(h, d, ap.filename, true, 0);
            }
            catch (ThreadAbortException abortException)
            {
                udpserver.Close();
                dataThread = null;
            }
        }
Esempio n. 27
0
        private void FilePlayThread(object obj)
        {
            IPAddress   localaddr = IPAddress.Parse("0.0.0.0");
            TcpListener tcpserver = new TcpListener(localaddr, 0);

            tcpserver.Start();
            IPEndPoint localep = (IPEndPoint)tcpserver.LocalEndpoint;

            WorkProcessHandler h = new WorkProcessHandler(FilePlayProcess);

            ThreadParam ap = (ThreadParam)obj;

            DeviceListener.Device d = deviceListener.Find(ap.id);
            if (d == null)
            {
                return;
            }
            int ret = deviceListener.FilePlayStart(d, "0.0.0.0", localep.Port, ap.streamtype, ap.volume, null);

            if (ret != 0)
            {
                return;
            }
            TcpClient tc = null;

            try
            {
                long starttime = DateTime.Now.Ticks;

                while ((DateTime.Now.Ticks - starttime) < 3000 * 10000)//3000ms
                {
                    if (tcpserver.Pending())
                    {
                        tc = tcpserver.AcceptTcpClient();
                        break;
                    }
                }
                if (tc != null)
                {
                    FileStream fs = File.OpenRead(ap.filename);
                    if (fs != null)
                    {
                        bool          eof        = false;
                        long          writebytes = 0;
                        byte[]        fbs        = new byte[1024];
                        NetworkStream tcs        = tc.GetStream();
                        while (!eof)
                        {
                            if (tcs.CanWrite)
                            {
                                int readbytes = fs.Read(fbs, 0, fbs.Length);

                                tcs.Write(fbs, 0, readbytes);
                                writebytes += readbytes;

                                if (readbytes < fbs.Length)
                                {
                                    eof = true;
                                }

                                int process = (int)(100 * writebytes / fs.Length);

                                this.BeginInvoke(h, d, ap.filename, false, process);
                            }
                            else
                            {
                                Thread.Sleep(10);
                            }
                        }
                    }
                    tc.Close();
                }

                Thread.Sleep(500);
                deviceListener.FilePlayStop(d);
                this.BeginInvoke(h, d, ap.filename, true, 0);
                dataThread = null;
            }
            catch (ThreadAbortException abortException)
            {
                //this.BeginInvoke(h, ap.filename, true, 0);
                if (tc != null)
                {
                    tc.Close();
                }

                dataThread = null;
            }
        }
Esempio n. 28
0
        private void IntercomThread(object obj)
        {
            int  sequenceNumber = 0;
            long timestamp      = 0;

            short[] inputbuffer = new short[2048];

            DeviceListener.Device d = deviceListener.Find(DeviceID);

            UdpClient udpserver;

            try
            {
                udpserver = new UdpClient(9999);
            }
            catch (Exception ex)
            {
                MessageBox.Show("port is in use");
                return;
            }

            int port           = 9999;
            DelegateShowWave h = new DelegateShowWave(ShowWave);

            int ret;

            ret = deviceListener.IntercomStart(d, "0.0.0.0", port, "g711-u", "rtp", 100, 20, "mic", "disable");
            if (ret != 0)
            {
                return;
            }

            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);

            udpserver.Client.ReceiveTimeout = 3000;
            byte[] bs = udpserver.Receive(ref ipep);

            if (bs == null)
            {
                return;
            }

            string dev_ip   = null;
            int    dev_port = 0;
            string str      = System.Text.Encoding.Default.GetString(bs);

            if (str.Contains(DeviceID.ToString()) == false)
            {
                return;
            }

            dev_ip   = ipep.Address.ToString();
            dev_port = ipep.Port;

            try
            {
                short[] micpack        = new short[2048];
                int     micpack_length = 0;

                short[] speakerpack        = new short[2048];
                int     speakerpack_length = 0;

                while (true)
                {
                    micpack_length     = 0;
                    speakerpack_length = 0;

                    while (udpserver.Available > 0)
                    {
                        bs = udpserver.Receive(ref ipep);

                        if (dev_ip.Equals(ipep.Address.ToString()))
                        {
                            RtpPacket packet = new RtpPacket(bs, bs.Length);
                            if (packet.PayloadType == RtpPayloadType.G711_uLaw)
                            {
                                short[] pcmbuf1 = g711.g711Decode_ulaw(packet.Payload);
                                this.Invoke(h, pcmbuf1);
                            }
                        }
                    }
                }
            }
            catch (ThreadAbortException abortException)
            {
                deviceListener.IntercomStop(d);

                udpserver.Close();
            }
        }
Esempio n. 29
0
        private void button15_Click(object sender, EventArgs e)
        {
            label8.Text = "---";
            label9.Text = "---";
            if (DeviceID == null)
            {
                MessageBox.Show("device is not login.");
                return;
            }
            DeviceListener.Device d = deviceListener.Find(DeviceID);

            string pakfilename = checkBox2.Text;

            d = deviceListener.Find(DeviceID);
            if ((checkBox2.Checked) && (File.Exists(pakfilename)))
            {
                IPAddress   localaddr = IPAddress.Parse("0.0.0.0");
                TcpListener tcpserver = new TcpListener(localaddr, 0);
                tcpserver.Start();
                IPEndPoint localep = (IPEndPoint)tcpserver.LocalEndpoint;

                TcpClient tc        = null;
                int       starttime = System.Environment.TickCount;
                try
                {
                    int ret = deviceListener.FirmwareUpdate(d, "0.0.0.0", localep.Port, pakfilename, "data");
                    if (ret == 0)
                    {
                        while ((System.Environment.TickCount - starttime) < 3000 * 10000)//3000ms
                        {
                            if (tcpserver.Pending())
                            {
                                tc = tcpserver.AcceptTcpClient();
                                break;
                            }
                            Application.DoEvents();
                        }

                        if (tc != null)
                        {
                            NetworkStream tcs = tc.GetStream();
                            FileStream    fs  = File.OpenRead(pakfilename);
                            if (fs != null)
                            {
                                bool   eof        = false;
                                long   writebytes = 0;
                                byte[] fbs        = new byte[1024];

                                while (!eof)
                                {
                                    if (tcs.CanWrite)
                                    {
                                        int readbytes = fs.Read(fbs, 0, fbs.Length);
                                        tcs.Write(fbs, 0, readbytes);
                                        writebytes += readbytes;
                                        if (writebytes >= fs.Length)
                                        {
                                            eof = true;
                                        }
                                        int process = (int)(100 * writebytes / fs.Length);

                                        label9.Text = process.ToString() + "%";

                                        Application.DoEvents();
                                    }
                                }
                            }
                            int timer = System.Environment.TickCount;
                            tc.Client.Shutdown(System.Net.Sockets.SocketShutdown.Send);

                            byte[] rb   = new byte[256];
                            int    rlen = tcs.Read(rb, 0, rb.Length);
                            while (rlen > 0)
                            {
                                rlen = tcs.Read(rb, 0, rb.Length);
                            }
                            tc.Close();
                            int t = System.Environment.TickCount - timer;
                        }
                    }
                }
                finally
                {
                    tcpserver.Stop();
                    tcpserver = null;
                }
            }

            Thread.Sleep(1000);
            string binfilename = checkBox1.Text;

            if ((checkBox1.Checked) && (File.Exists(binfilename)))
            {
                IPAddress   localaddr = IPAddress.Parse("0.0.0.0");
                TcpListener tcpserver = new TcpListener(localaddr, 0);
                tcpserver.Start();
                IPEndPoint localep = (IPEndPoint)tcpserver.LocalEndpoint;

                TcpClient tc        = null;
                int       starttime = System.Environment.TickCount;
                try
                {
                    int ret = deviceListener.FirmwareUpdate(d, "0.0.0.0", localep.Port, binfilename, "bin");
                    if (ret == 0)
                    {
                        while ((System.Environment.TickCount - starttime) < 3000 * 10000)//3000ms
                        {
                            if (tcpserver.Pending())
                            {
                                tc = tcpserver.AcceptTcpClient();
                                break;
                            }
                            Application.DoEvents();
                        }

                        if (tc != null)
                        {
                            NetworkStream tcs = tc.GetStream();
                            FileStream    fs  = File.OpenRead(binfilename);
                            if (fs != null)
                            {
                                bool   eof        = false;
                                long   writebytes = 0;
                                byte[] fbs        = new byte[1024];

                                while (!eof)
                                {
                                    if (tcs.CanWrite)
                                    {
                                        int readbytes = fs.Read(fbs, 0, fbs.Length);
                                        tcs.Write(fbs, 0, readbytes);
                                        writebytes += readbytes;
                                        if (writebytes >= fs.Length)
                                        {
                                            eof = true;
                                        }
                                        int process = (int)(100 * writebytes / fs.Length);

                                        label8.Text = process.ToString() + "%";

                                        Application.DoEvents();
                                    }
                                    Thread.Sleep(10);
                                }
                            }

                            int timer = System.Environment.TickCount;
                            tc.Client.Shutdown(System.Net.Sockets.SocketShutdown.Send);

                            byte[] rb   = new byte[256];
                            int    rlen = tcs.Read(rb, 0, rb.Length);
                            while (rlen > 0)
                            {
                                rlen = tcs.Read(rb, 0, rb.Length);
                            }
                            tc.Close();
                            int t = System.Environment.TickCount - timer;
                            //label8.Text = t.ToString();
                        }
                    }
                }
                finally
                {
                    tcpserver.Stop();
                    tcpserver = null;
                }
            }

            d = deviceListener.Find(DeviceID);
            deviceListener.Apply(d);
            PlaySuccessNotify();

            button2_Click_1(null, null);
        }
Esempio n. 30
0
        private void button9_Click(object sender, EventArgs e)
        {
            DeviceListener.Device d = deviceListener.Find(DeviceID);
            if (d == null)
            {
                panel2.BackColor = SystemColors.ControlDark;
                label5.Text      = string.Format("DEVICE ID({0:D8} IS NOT EXITS)", d.id);

                return;
            }

            panel2.BackColor = SystemColors.Control;
            int       okpack    = 0;
            int       faultpack = 0;
            UdpClient uc        = new UdpClient();

            byte[] cmd = new byte[] { 0xff, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
                                      0xcc, 0xcc, 0x01, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
                                      0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
                                      0xcc, 0xcc, 0x18, 0x15 };
            //IPAddress ipaddr = IPAddress.Parse(d.peerip);
            IPEndPoint ep  = new IPEndPoint(IPAddress.Parse(d.peerip), 65276);
            IPEndPoint rep = new IPEndPoint(IPAddress.Any, 0);

            uc.Client.ReceiveTimeout = 1000;
            try
            {
                for (int i = 0; i < 1000; i++)
                {
                    uc.Send(cmd, cmd.Length, ep);
                    byte[] rb = uc.Receive(ref rep);
                    if (rb != null)
                    {
                        if (rep.Address.ToString().Equals(d.peerip))
                        {
                            okpack++;
                        }
                        else
                        {
                            faultpack++;
                        }
                    }
                    else
                    {
                        faultpack++;
                    }

                    label5.Text = string.Format("PING SEND={0:D}, RECV={1:D}, DISCARD:{2:D}", i + 1, okpack, faultpack);
                    if (faultpack > 4)
                    {
                        break;
                    }
                    Application.DoEvents();
                }
            }
            catch (Exception ex)
            {
            }

            if (okpack == 1000)
            {
                panel2.BackColor = Color.LimeGreen;
                PlaySuccessNotify();
            }
            else
            {
                panel2.BackColor = Color.Red;
                PlayFaultNotify();
            }
        }