#pragma once


namespace zigbee_long_test {

	using namespace System;
	using namespace System::IO;
	using namespace System::Text;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>

	void AddText(FileStream^ fs, String^ value){
		array<Byte>^info = (gcnew UTF8Encoding(true))->GetBytes(value);
		fs->Write(info,0,info->Length);
	}

	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			this->serialPort1->DataReceived::add(gcnew System::IO::Ports::SerialDataReceivedEventHandler(this,&Form1::serialPort1_DataReceived));
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::IO::Ports::SerialPort^  serialPort1;
	private: System::Windows::Forms::Button^  button_start;
	private: System::Windows::Forms::Button^  stop;
	private: System::Windows::Forms::DataGridView^  dataGridView1;
	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::TextBox^  pocasie;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  prostredie;
	private: System::Windows::Forms::TextBox^  subor;
	private: System::Windows::Forms::TextBox^  hit;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  data_datun;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  data_kanal;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  data_vykon;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  data_signal;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  data_prijate;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  data_good;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  data_bad;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  data_doba_prenos;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  data_prostredie;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  data_pocasie;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::TextBox^  serialPort1_port;
	private: System::Windows::Forms::Label^  label1;

	protected: 
	private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			System::Windows::Forms::DataGridViewCellStyle^  dataGridViewCellStyle2 = (gcnew System::Windows::Forms::DataGridViewCellStyle());
			this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
			this->button_start = (gcnew System::Windows::Forms::Button());
			this->stop = (gcnew System::Windows::Forms::Button());
			this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
			this->data_datun = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->data_kanal = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->data_vykon = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->data_signal = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->data_prijate = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->data_good = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->data_bad = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->data_doba_prenos = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->data_prostredie = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->data_pocasie = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->pocasie = (gcnew System::Windows::Forms::TextBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->prostredie = (gcnew System::Windows::Forms::TextBox());
			this->subor = (gcnew System::Windows::Forms::TextBox());
			this->hit = (gcnew System::Windows::Forms::TextBox());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->serialPort1_port = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->BeginInit();
			this->SuspendLayout();
			// 
			// serialPort1
			// 
			this->serialPort1->BaudRate = 38400;
			this->serialPort1->DtrEnable = true;
			this->serialPort1->RtsEnable = true;
			// 
			// button_start
			// 
			this->button_start->Location = System::Drawing::Point(902, 14);
			this->button_start->Name = L"button_start";
			this->button_start->Size = System::Drawing::Size(66, 25);
			this->button_start->TabIndex = 0;
			this->button_start->Text = L"Start";
			this->button_start->UseVisualStyleBackColor = true;
			this->button_start->Click += gcnew System::EventHandler(this, &Form1::button_start_Click);
			// 
			// stop
			// 
			this->stop->Location = System::Drawing::Point(902, 45);
			this->stop->Name = L"stop";
			this->stop->Size = System::Drawing::Size(66, 25);
			this->stop->TabIndex = 2;
			this->stop->Text = L"Stop";
			this->stop->UseVisualStyleBackColor = true;
			this->stop->Click += gcnew System::EventHandler(this, &Form1::stop_Click);
			// 
			// dataGridView1
			// 
			this->dataGridView1->AllowUserToDeleteRows = false;
			this->dataGridView1->AutoSizeColumnsMode = System::Windows::Forms::DataGridViewAutoSizeColumnsMode::ColumnHeader;
			this->dataGridView1->BackgroundColor = System::Drawing::Color::White;
			this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::DisableResizing;
			this->dataGridView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^  >(10) {this->data_datun, 
				this->data_kanal, this->data_vykon, this->data_signal, this->data_prijate, this->data_good, this->data_bad, this->data_doba_prenos, 
				this->data_prostredie, this->data_pocasie});
			dataGridViewCellStyle2->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleLeft;
			dataGridViewCellStyle2->BackColor = System::Drawing::SystemColors::Window;
			dataGridViewCellStyle2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 6.75F, System::Drawing::FontStyle::Regular, 
				System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(238)));
			dataGridViewCellStyle2->ForeColor = System::Drawing::SystemColors::ControlText;
			dataGridViewCellStyle2->SelectionBackColor = System::Drawing::SystemColors::HighlightText;
			dataGridViewCellStyle2->SelectionForeColor = System::Drawing::SystemColors::ControlText;
			dataGridViewCellStyle2->WrapMode = System::Windows::Forms::DataGridViewTriState::False;
			this->dataGridView1->DefaultCellStyle = dataGridViewCellStyle2;
			this->dataGridView1->Location = System::Drawing::Point(7, 102);
			this->dataGridView1->Name = L"dataGridView1";
			this->dataGridView1->ReadOnly = true;
			this->dataGridView1->Size = System::Drawing::Size(961, 49);
			this->dataGridView1->TabIndex = 9;
			this->dataGridView1->CellContentClick += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &Form1::dataGridView1_CellContentClick);
			// 
			// data_datun
			// 
			this->data_datun->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::None;
			this->data_datun->HeaderText = L"Dátum";
			this->data_datun->Name = L"data_datun";
			this->data_datun->ReadOnly = true;
			// 
			// data_kanal
			// 
			this->data_kanal->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::None;
			this->data_kanal->HeaderText = L"Kanál";
			this->data_kanal->Name = L"data_kanal";
			this->data_kanal->ReadOnly = true;
			this->data_kanal->Width = 45;
			// 
			// data_vykon
			// 
			this->data_vykon->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::None;
			this->data_vykon->HeaderText = L"Výkon [dB]";
			this->data_vykon->Name = L"data_vykon";
			this->data_vykon->ReadOnly = true;
			this->data_vykon->Width = 70;
			// 
			// data_signal
			// 
			this->data_signal->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::None;
			this->data_signal->HeaderText = L"Kvalita [dB]";
			this->data_signal->Name = L"data_signal";
			this->data_signal->ReadOnly = true;
			this->data_signal->Width = 70;
			// 
			// data_prijate
			// 
			this->data_prijate->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::None;
			this->data_prijate->HeaderText = L"Prijatých";
			this->data_prijate->Name = L"data_prijate";
			this->data_prijate->ReadOnly = true;
			this->data_prijate->Width = 60;
			// 
			// data_good
			// 
			this->data_good->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::None;
			this->data_good->HeaderText = L"Dobrých";
			this->data_good->Name = L"data_good";
			this->data_good->ReadOnly = true;
			this->data_good->Width = 60;
			// 
			// data_bad
			// 
			this->data_bad->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::None;
			this->data_bad->HeaderText = L"Zlých";
			this->data_bad->Name = L"data_bad";
			this->data_bad->ReadOnly = true;
			this->data_bad->Width = 45;
			// 
			// data_doba_prenos
			// 
			this->data_doba_prenos->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::None;
			this->data_doba_prenos->HeaderText = L"Doba [s]";
			this->data_doba_prenos->Name = L"data_doba_prenos";
			this->data_doba_prenos->ReadOnly = true;
			this->data_doba_prenos->Width = 65;
			// 
			// data_prostredie
			// 
			this->data_prostredie->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::None;
			this->data_prostredie->HeaderText = L"Prostredie";
			this->data_prostredie->Name = L"data_prostredie";
			this->data_prostredie->ReadOnly = true;
			this->data_prostredie->Width = 200;
			// 
			// data_pocasie
			// 
			this->data_pocasie->AutoSizeMode = System::Windows::Forms::DataGridViewAutoSizeColumnMode::None;
			this->data_pocasie->HeaderText = L"Počasie";
			this->data_pocasie->Name = L"data_pocasie";
			this->data_pocasie->ReadOnly = true;
			this->data_pocasie->Width = 200;
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(4, 12);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(48, 13);
			this->label6->TabIndex = 13;
			this->label6->Text = L"Počasie:";
			// 
			// pocasie
			// 
			this->pocasie->Location = System::Drawing::Point(69, 9);
			this->pocasie->Name = L"pocasie";
			this->pocasie->Size = System::Drawing::Size(243, 20);
			this->pocasie->TabIndex = 14;
			this->pocasie->Text = L"JASNO, SUCHO, 20, 80, 1000.22, 9.22";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(4, 34);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(57, 13);
			this->label2->TabIndex = 16;
			this->label2->Text = L"Prostredie:";
			// 
			// prostredie
			// 
			this->prostredie->Location = System::Drawing::Point(69, 31);
			this->prostredie->Name = L"prostredie";
			this->prostredie->Size = System::Drawing::Size(243, 20);
			this->prostredie->TabIndex = 17;
			this->prostredie->Text = L"ASFALT, ZASTAVANE, 50, AUTO-KANCELARIA";
			// 
			// subor
			// 
			this->subor->Location = System::Drawing::Point(401, 9);
			this->subor->Name = L"subor";
			this->subor->Size = System::Drawing::Size(227, 20);
			this->subor->TabIndex = 20;
			this->subor->Text = L"meranie_20090214.txt";
			// 
			// hit
			// 
			this->hit->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)), 
				static_cast<System::Int32>(static_cast<System::Byte>(192)));
			this->hit->BorderStyle = System::Windows::Forms::BorderStyle::None;
			this->hit->Location = System::Drawing::Point(318, 9);
			this->hit->Name = L"hit";
			this->hit->Size = System::Drawing::Size(10, 13);
			this->hit->TabIndex = 21;
			this->hit->Visible = false;
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(336, 12);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(38, 13);
			this->label5->TabIndex = 22;
			this->label5->Text = L"Súbor:";
			// 
			// serialPort1_port
			// 
			this->serialPort1_port->Location = System::Drawing::Point(401, 32);
			this->serialPort1_port->Name = L"serialPort1_port";
			this->serialPort1_port->Size = System::Drawing::Size(227, 20);
			this->serialPort1_port->TabIndex = 26;
			this->serialPort1_port->Text = L"COM1";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(336, 35);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(29, 13);
			this->label1->TabIndex = 25;
			this->label1->Text = L"Port:";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)), 
				static_cast<System::Int32>(static_cast<System::Byte>(192)));
			this->ClientSize = System::Drawing::Size(974, 157);
			this->Controls->Add(this->serialPort1_port);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->hit);
			this->Controls->Add(this->subor);
			this->Controls->Add(this->prostredie);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->pocasie);
			this->Controls->Add(this->label6);
			this->Controls->Add(this->dataGridView1);
			this->Controls->Add(this->stop);
			this->Controls->Add(this->button_start);
			this->Cursor = System::Windows::Forms::Cursors::Hand;
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::SizableToolWindow;
			this->Name = L"Form1";
			this->Text = L"Zigbee long-time test";
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button_start_Click(System::Object^  sender, System::EventArgs^  e) {
				
				 serialPort1->PortName = serialPort1_port->Text;
				 serialPort1->Open();
				 
				 
			 }
	private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e){
		
				this->Invoke(gcnew EventHandler(this,&Form1::do_receive));

	 }
	private: System::Void do_receive(System::Object^  sender, System::EventArgs^  e) {
				String^ string_sci = "";
				String^ string_hit = "";
				String^ string_kanal = "";
				String^ string_vykon = "";
				String^ string_signal = "";
				String^ string_prijate = "";
				String^ string_good = "";
				String^ string_bad = "";
				String^ string_cas = "";
				String^ now_time = "";
				int i, j;

				string_sci = serialPort1->ReadExisting();
				hit->AppendText(string_sci);
				if (string_sci->Substring((string_sci->Length)-1,1) == "@"){
					string_hit = hit->Text;
					hit->Text="";
					if (string_hit->Substring(0,1) == "*"){
						i = 1;
						j = 0;
						while (string_hit->Substring(i,1) != "@"){
							if (string_hit->Substring(i,1) == "#"){
								j++;
							}
							else{
								switch (j){
									case 0: string_kanal = String::Concat(string_kanal,string_hit->Substring(i,1)); break;									
									case 1: string_vykon = String::Concat(string_vykon,string_hit->Substring(i,1)); break;
									case 2: string_signal = String::Concat(string_signal,string_hit->Substring(i,1)); break;
									case 3: string_prijate = String::Concat(string_prijate,string_hit->Substring(i,1)); break;
									case 4: string_good = String::Concat(string_good,string_hit->Substring(i,1)); break;
									case 5: string_bad = String::Concat(string_bad,string_hit->Substring(i,1)); break;
									case 6: string_cas = String::Concat(string_cas,string_hit->Substring(i,1)); break;
									default: break;
								}
							}
							i++;
						}					
						now_time = DateTime::Now.ToString();
						FileStream^ z_file = File::Open(subor->Text,FileMode::Append);
						AddText(z_file,now_time);
						AddText(z_file,";");
						AddText(z_file,string_kanal);
						AddText(z_file,";");
						AddText(z_file,string_vykon);
						AddText(z_file,";");
						AddText(z_file,string_signal);
						AddText(z_file,";");
						AddText(z_file,string_prijate);
						AddText(z_file,";");
						AddText(z_file,string_good);
						AddText(z_file,";");
						AddText(z_file,string_bad);
						AddText(z_file,";");
						AddText(z_file,string_cas);
						AddText(z_file,";");
						AddText(z_file,prostredie->Text);
						AddText(z_file,";");
						AddText(z_file,pocasie->Text);
						AddText(z_file,";");
						AddText(z_file,"\r\n");
						delete(IDisposable^)z_file;
					
						//array<String^>^z_row = {now_time,string_kanal,string_vykon,string_signal,string_prijate,string_good,string_bad,string_cas,prostredie->Text,pocasie->Text};
						//dataGridView1->Rows->Add(z_row);
						dataGridView1->Rows[0]->Cells[0]->Value = now_time;
						dataGridView1->Rows[0]->Cells[1]->Value = string_kanal;
						dataGridView1->Rows[0]->Cells[2]->Value = string_vykon;
						dataGridView1->Rows[0]->Cells[3]->Value = string_signal;
						dataGridView1->Rows[0]->Cells[4]->Value = string_prijate;
						dataGridView1->Rows[0]->Cells[5]->Value = string_good;
						dataGridView1->Rows[0]->Cells[6]->Value = string_bad;
						dataGridView1->Rows[0]->Cells[7]->Value = string_cas;
						dataGridView1->Rows[0]->Cells[8]->Value = prostredie->Text;
						dataGridView1->Rows[0]->Cells[9]->Value = pocasie->Text;
					}
				}
	}
	private: System::Void stop_Click(System::Object^  sender, System::EventArgs^  e) {

				 serialPort1->Close();
		 }

	private: System::Void dataGridView1_CellContentClick(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
		 }

};
}

