#pragma once


namespace zigbee_test_with_car {

	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::Windows::Forms::Button^  button1;
	protected: 
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::TextBox^  umiestnenie;
	private: System::Windows::Forms::TextBox^  uhol;
	private: System::Windows::Forms::TextBox^  vzdialenost;



	private: System::Windows::Forms::DataGridView^  dataGridView1;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  vykon;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  kvalita;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  prijate;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  dobre;
	private: System::Windows::Forms::DataGridViewTextBoxColumn^  zle;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Label^  label5;
	private: System::IO::Ports::SerialPort^  serialPort1;
	private: System::Windows::Forms::TextBox^  port;

	private: System::Windows::Forms::Label^  label6;
	private: System::Windows::Forms::TextBox^  subor;

	private: System::Windows::Forms::Label^  label7;
	private: System::Windows::Forms::TextBox^  hit;
	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());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->umiestnenie = (gcnew System::Windows::Forms::TextBox());
			this->uhol = (gcnew System::Windows::Forms::TextBox());
			this->vzdialenost = (gcnew System::Windows::Forms::TextBox());
			this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
			this->vykon = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->kvalita = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->prijate = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->dobre = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->zle = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
			this->port = (gcnew System::Windows::Forms::TextBox());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->subor = (gcnew System::Windows::Forms::TextBox());
			this->label7 = (gcnew System::Windows::Forms::Label());
			this->hit = (gcnew System::Windows::Forms::TextBox());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->BeginInit();
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(491, 12);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(69, 26);
			this->button1->TabIndex = 0;
			this->button1->Text = L"Start";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(491, 44);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(69, 26);
			this->button2->TabIndex = 1;
			this->button2->Text = L"Stop";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(12, 85);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(32, 13);
			this->label1->TabIndex = 2;
			this->label1->Text = L"Uhol:";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(12, 63);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(68, 13);
			this->label2->TabIndex = 3;
			this->label2->Text = L"Umiestnenie:";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(12, 108);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(65, 13);
			this->label3->TabIndex = 4;
			this->label3->Text = L"Vzdialenosť:";
			// 
			// umiestnenie
			// 
			this->umiestnenie->Location = System::Drawing::Point(93, 60);
			this->umiestnenie->Name = L"umiestnenie";
			this->umiestnenie->Size = System::Drawing::Size(193, 20);
			this->umiestnenie->TabIndex = 5;
			// 
			// uhol
			// 
			this->uhol->Location = System::Drawing::Point(93, 82);
			this->uhol->Name = L"uhol";
			this->uhol->Size = System::Drawing::Size(193, 20);
			this->uhol->TabIndex = 6;
			// 
			// vzdialenost
			// 
			this->vzdialenost->Location = System::Drawing::Point(93, 105);
			this->vzdialenost->Name = L"vzdialenost";
			this->vzdialenost->Size = System::Drawing::Size(193, 20);
			this->vzdialenost->TabIndex = 7;
			// 
			// dataGridView1
			// 
			this->dataGridView1->AllowUserToDeleteRows = false;
			this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
			this->dataGridView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^  >(5) {this->vykon, 
				this->kvalita, this->prijate, this->dobre, this->zle});
			this->dataGridView1->Location = System::Drawing::Point(12, 143);
			this->dataGridView1->Name = L"dataGridView1";
			this->dataGridView1->ReadOnly = true;
			this->dataGridView1->Size = System::Drawing::Size(545, 45);
			this->dataGridView1->TabIndex = 8;
			// 
			// vykon
			// 
			this->vykon->HeaderText = L"Výkon [dB]";
			this->vykon->Name = L"vykon";
			this->vykon->ReadOnly = true;
			// 
			// kvalita
			// 
			this->kvalita->HeaderText = L"Kvalita [dB]";
			this->kvalita->Name = L"kvalita";
			this->kvalita->ReadOnly = true;
			// 
			// prijate
			// 
			this->prijate->HeaderText = L"Prijatých";
			this->prijate->Name = L"prijate";
			this->prijate->ReadOnly = true;
			// 
			// dobre
			// 
			this->dobre->HeaderText = L"Dobrých";
			this->dobre->Name = L"dobre";
			this->dobre->ReadOnly = true;
			// 
			// zle
			// 
			this->zle->HeaderText = L"Zlých";
			this->zle->Name = L"zle";
			this->zle->ReadOnly = true;
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(292, 85);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(17, 13);
			this->label4->TabIndex = 9;
			this->label4->Text = L"[°]";
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(292, 108);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(21, 13);
			this->label5->TabIndex = 10;
			this->label5->Text = L"[m]";
			// 
			// serialPort1
			// 
			this->serialPort1->BaudRate = 38400;
			this->serialPort1->DtrEnable = true;
			this->serialPort1->RtsEnable = true;
			// 
			// port
			// 
			this->port->Location = System::Drawing::Point(93, 12);
			this->port->Name = L"port";
			this->port->Size = System::Drawing::Size(193, 20);
			this->port->TabIndex = 12;
			this->port->Text = L"COM1";
			// 
			// label6
			// 
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(12, 15);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(29, 13);
			this->label6->TabIndex = 11;
			this->label6->Text = L"Port:";
			// 
			// subor
			// 
			this->subor->Location = System::Drawing::Point(93, 34);
			this->subor->Name = L"subor";
			this->subor->Size = System::Drawing::Size(193, 20);
			this->subor->TabIndex = 14;
			this->subor->Text = L"zigbee_test_with_car.txt";
			// 
			// label7
			// 
			this->label7->AutoSize = true;
			this->label7->Location = System::Drawing::Point(12, 37);
			this->label7->Name = L"label7";
			this->label7->Size = System::Drawing::Size(38, 13);
			this->label7->TabIndex = 13;
			this->label7->Text = L"Súbor:";
			// 
			// hit
			// 
			this->hit->Location = System::Drawing::Point(346, 13);
			this->hit->Name = L"hit";
			this->hit->Size = System::Drawing::Size(22, 20);
			this->hit->TabIndex = 15;
			this->hit->Visible = false;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::Color::Chocolate;
			this->ClientSize = System::Drawing::Size(572, 190);
			this->Controls->Add(this->hit);
			this->Controls->Add(this->subor);
			this->Controls->Add(this->label7);
			this->Controls->Add(this->port);
			this->Controls->Add(this->label6);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->dataGridView1);
			this->Controls->Add(this->vzdialenost);
			this->Controls->Add(this->uhol);
			this->Controls->Add(this->umiestnenie);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Test with car";
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 serialPort1->PortName = 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_vykon = "";
				String^ string_signal = "";
				String^ string_prijate = "";
				String^ string_good = "";
				String^ string_bad = "";
				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_vykon = String::Concat(string_vykon,string_hit->Substring(i,1)); break;									
									case 1: string_signal = String::Concat(string_signal,string_hit->Substring(i,1)); break;
									case 2: string_prijate = String::Concat(string_prijate,string_hit->Substring(i,1)); break;
									case 3: string_good = String::Concat(string_good,string_hit->Substring(i,1)); break;
									case 4: string_bad = String::Concat(string_bad,string_hit->Substring(i,1)); break;
									
									default: break;
								}
							}
							i++;
						}					
						FileStream^ z_file = File::Open(subor->Text,FileMode::Append);
						AddText(z_file,umiestnenie->Text);
						AddText(z_file,";");
						AddText(z_file,uhol->Text);
						AddText(z_file,";");
						AddText(z_file,vzdialenost->Text);
						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,"\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 = string_vykon;
						dataGridView1->Rows[0]->Cells[1]->Value = string_signal;
						dataGridView1->Rows[0]->Cells[2]->Value = string_prijate;
						dataGridView1->Rows[0]->Cells[3]->Value = string_good;
						dataGridView1->Rows[0]->Cells[4]->Value = string_bad;						
					}
				}
	}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
			 serialPort1->Close();
		 }
};
}

