Ejemplo n.º 1
0
        private void Process()
        {
            string pathFile = "data.txt";

            label5.Text      = "Online";
            label5.ForeColor = Color.Green;
            //sftp.DownloadFile("Register_data.txt",pathFile);
            string nameFile = "device_" + GetFile() + ".txt";

            sftp.DownloadFile(nameFile, pathFile);
            string[] x = sftp.getFile(pathFile);
            sftp.DeleteFile(nameFile);
            Register[] registerData = ConverDataRegister(x);

            if (registerData.Length == 0)
            {
                return;
            }
            //for (int i = 0; i < registerData.Length; i++)
            //{
            //    richTextBox1.Text ="Пристрій №"+ registerData[i].Device_number.ToString() + " Регістр №" + registerData[i].Register_number.ToString() + " Значення: "+registerData[i].Value_register.ToString() + " Дата: " + registerData[i].date.ToString() + "\n";
            //}
            //label_t_air.Text = registerData[registerData.Length - 1].Device_number.ToString();
            //label_t_ground.Text = registerData[registerData.Length - 1].Register_number.ToString();
            //label_humidity.Text = registerData[registerData.Length - 1].Value_register.ToString();
            //label_press.Text = registerData[registerData.Length - 1].date.ToString();
            toolStripStatusLabel2.Text = "Дані з приладу завантажено...";
            FileStream   download_stream = new FileStream("Data_result.txt", FileMode.Append);
            StreamWriter data_result     = new StreamWriter(download_stream);

            for (int i = 0; i < registerData.Length; i++)
            {
                data_result.WriteLine(registerData[i].date + " " + registerData[i].Device_number + " " + registerData[i].Register_number + " " + registerData[i].Value_register + "\n");
            }
            data_result.Close();
            for (int i = 0; i < registerData.Length; i++)
            {
                mySqlHelper.WriteData(registerData[i].date, registerData[i].Device_number, registerData[i].Register_number, registerData[i].Value_register);
            }

            toolStripStatusLabel2.Text = "Дані з приладу збережено в базу даних. Зібрано " + registerData.Length + " даних.";

            DateTime dt      = registerData[registerData.Length - 1].date;
            string   strDate = dt.ToString("yyy-MM-dd");
            ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/registerData[registerData.Length - 1].Device_number;
            string commandSQL = "SELECT * FROM " + Properties.Settings.Default.DB_table + "_" + GetFile() + " WHERE DATE(Day_and_time) = '" + strDate + "';";

            UpdateGraphic_Date(commandSQL, 1, 4);
        }
Ejemplo n.º 2
0
        private void Table_device_Load(object sender, EventArgs e)
        {
            SftpClient client;

            client = new SftpClient(Properties.Settings.Default.host, 22, Properties.Settings.Default.username, Properties.Settings.Default.password);

            StreamWriter write_request = new StreamWriter("Settings_register.txt");

            write_request.WriteLine("247");
            write_request.Close();

            FileStream upload_stream = new FileStream("Settings_register.txt", FileMode.Open);

            try
            {
                if (!client.IsConnected)
                {
                    client.Connect();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            if (client.IsConnected)
            {
                client.ChangeDirectory("/home/pi/ModBus");
                client.UploadFile(upload_stream, "/home/pi/ModBus" + "/Settings_register.txt", null);
                client.Disconnect();
                upload_stream.Close();
            }
            else
            {
                upload_stream.Close();
            }

            string download_file_name = "Detecting_data.txt";

            try
            {
                if (!client.IsConnected)
                {
                    client.Connect();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            if (client.IsConnected)
            {
                string working_directory = "/home/pi/ModBus";
                string windows_file      = "Download.txt";
                bool   exist             = false;

                try
                {
                    client.ChangeDirectory(working_directory);

                    var listDirectory = client.ListDirectory(working_directory);

                    foreach (var file in listDirectory)
                    {
                        if (file.Name == download_file_name)
                        {
                            exist = true;
                            FileStream file_stream = new FileStream(windows_file, FileMode.Create);
                            client.DownloadFile(download_file_name, file_stream);
                            client.DeleteFile(download_file_name);
                            file_stream.Close();
                            MessageBox.Show("File saved in 'Download.txt'", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }

                        else
                        {
                        }
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                if (exist == false)
                {
                    MessageBox.Show("Requested file not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                client.Disconnect();
            }

            else
            {
                MessageBox.Show("Connection failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            dataGridView1.RowCount = 1;
            string[]   x            = sftp.getFile("Download.txt");
            Register[] registerData = ConverDataRegister(x);
            for (int i = 0; i < registerData.Length; i++)
            {
                dataGridView1.RowCount    = dataGridView1.RowCount + 1;
                dataGridView1[0, i].Value = dataGridView1.RowCount - 1;
                dataGridView1[1, i].Value = Convert.ToString(registerData[i].Value_register.ToString());
                dataGridView1[2, i].Value = "9600";
                dataGridView1[3, i].Value = "Online";
            }
        }