//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "DataModule.h"
#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CPort"
#pragma resource "*.dfm"

TDModule *DModule;
//---------------------------------------------------------------------------
__fastcall TDModule::TDModule(TComponent* Owner)
        : TDataModule(Owner)
{
  F1_DataCount=0;
  F1_ChartPageCount=0;


  Tss[0] = 0;
  Tmm[0] = 0;
  Thh[0] = 0;


}
//---------------------------------------------------------------------------

void __fastcall TDModule::F1_TimerTimer(TObject *Sender)
{
   int i;

   if(F1_Timer->Tag == 0){
      F1_OldDT = Now();
      F1_Timer->Tag = 1;
   }

    F1_NowDT = Now() - F1_OldDT;

    i=900-(F1_ChartPageCount*50);

    Form1->F1_ListView->Items->Add();
    Form1->F1_ListView->Items->Item[F1_DataCount]->Caption = F1_DataCount;
    Form1->F1_ListView->Items->Item[F1_DataCount]->SubItems->Add(Now());
    Form1->F1_ListView->Items->Item[F1_DataCount]->SubItems->Add(Form1->F1_Edit_LV->Text);
    Form1->F1_ListView->Items->Item[F1_DataCount]->SubItems->Add("10");
    Form1->F1_ListView->Items->Item[F1_DataCount]->SubItems->Add("754");
    Form1->F1_ListView->Items->Item[F1_DataCount]->SubItems->Add("900");
    Form1->F1_ListView->Items->Item[F1_DataCount]->SubItems->Add("850");
    Form1->F1_ListView->Items->Item[F1_DataCount]->SubItems->Add("600");
    Form1->F1_ListView->Items->Item[F1_DataCount]->SubItems->Add("300");


    Form1->F1_Chart->Series[0]->AddXY(F1_NowDT,StrToFloat(Form1->F1_Edit_LV->Text),"",(TColor)clTeeColor);
    Form1->F1_Chart->Series[1]->AddXY(F1_NowDT,StrToFloat(Form1->F1_Edit_HV->Text),"",(TColor)clTeeColor);
    Form1->F1_Chart->Series[2]->AddXY(F1_NowDT,i-350,"",(TColor)clTeeColor);
    Form1->F1_Chart->Series[3]->AddXY(F1_NowDT,i-500,"",(TColor)clTeeColor);
    Form1->F1_Chart->Series[4]->AddXY(F1_NowDT,i-600,"",(TColor)clTeeColor);
    Form1->F1_Chart->Series[5]->AddXY(F1_NowDT,i-700,"",(TColor)clTeeColor);
    Form1->F1_Chart->Series[6]->AddXY(F1_NowDT,i-800,"",(TColor)clTeeColor);
    Form1->F1_Chart->Series[7]->AddXY(F1_NowDT,i-830,"",(TColor)clTeeColor);
    if(Form1->F1_Chart->Series[8]->Count() >= 1) Form1->F1_Chart->Series[8]->Delete(Form1->F1_Chart->Series[8]->Count() - 1);
    Form1->F1_Chart->Series[8]->AddXY(F1_NowDT,1000,"",(TColor)clTeeColor);

    F1_DataCount++;
   
    if(F1_DataCount > StrToInt(Form1->F1_Autosavenum_E->Text) && Form1->F1_Autosave_CB->Checked){
       Form1->F1_Autosave_CB->Tag = 1;
       F1_Timer->Enabled = false;
       Form1->F1_Save2Excel->Click();
    }

    unsigned short hh;
    unsigned short mm;
    unsigned short ss;
    unsigned short ms;
    F1_NowDT.DecodeTime(&hh,&mm,&ss,&ms);
    if(mm >= 2)  {
       F1_Timer->Tag = 0;
       F1_ChartPageCount++;
       Form1->F1_Chart->Series[0]->Clear();
       Form1->F1_Chart->Series[1]->Clear();
       Form1->F1_Chart->Series[2]->Clear();
       Form1->F1_Chart->Series[3]->Clear();
       Form1->F1_Chart->Series[4]->Clear();
       Form1->F1_Chart->Series[5]->Clear();
       Form1->F1_Chart->Series[6]->Clear();
       Form1->F1_Chart->Series[7]->Clear();
    }

}
//---------------------------------------------------------------------------


void __fastcall TDModule::Furnace11Click(TObject *Sender)
{
   TMenuItem * MyMI = (TMenuItem*)Sender;

   switch(MyMI->Tag){
     case 1:
            if(MyMI->Checked) Form1->F1_Autosave_CB->Checked = true;
              else Form1->F1_Autosave_CB->Checked = false;
            break;
     case 2:
            if(MyMI->Checked) Form1->F2_Autosave_CB->Checked = true;
              else Form1->F2_Autosave_CB->Checked = false;
            break;
     case 3:
            if(MyMI->Checked) Form1->F3_Autosave_CB->Checked = true;
              else Form1->F3_Autosave_CB->Checked = false;
            break;
     case 4:
            if(MyMI->Checked) Form1->F4_Autosave_CB->Checked = true;
              else Form1->F4_Autosave_CB->Checked = false;
            break;
   }

}
//---------------------------------------------------------------------------

void __fastcall TDModule::Timer1Timer(TObject *Sender)
{
  TSpeedButton * MySB[4] = {Form1->F1_Timer_SB,Form1->F2_Timer_SB,Form1->F3_Timer_SB,Form1->F4_Timer_SB};
  TStaticText  * MyST[4][3] = {Form1->F1_SS_ST,Form1->F1_MM_ST,Form1->F1_HH_ST,
                               Form1->F2_SS_ST,Form1->F2_MM_ST,Form1->F2_HH_ST,
                               Form1->F3_SS_ST,Form1->F3_MM_ST,Form1->F3_HH_ST,
                               Form1->F4_SS_ST,Form1->F4_MM_ST,Form1->F4_HH_ST};
  AnsiString Temp;
 for(int i = 0; i < 4; i++)
  if(MySB[i]->Tag){
     Tss[i]++;
     if(Tss[i] >= 60) {Tmm[i]++; Tss[i] = 0;}
     if(Tmm[i] >= 60) {Thh[i]++; Tmm[i] = 0;}
     MyST[i][0]->Caption = Temp.sprintf("%02d",Tss[i]);
     MyST[i][1]->Caption = Temp.sprintf("%02d",Tmm[i]);
     MyST[i][2]->Caption = Temp.sprintf("%02d",Thh[i]);
  }
}
//---------------------------------------------------------------------------


void __fastcall TDModule::F2_TimerTimer(TObject *Sender)
{
   int i;

   if(F2_Timer->Tag == 0){
      F2_OldDT = Now();
      F2_Timer->Tag = 1;
   }

    F2_NowDT = Now() - F2_OldDT;

    i=900-(F2_ChartPageCount*50);

    Form1->F2_ListView->Items->Add();
    Form1->F2_ListView->Items->Item[F2_DataCount]->Caption = F2_DataCount;
    Form1->F2_ListView->Items->Item[F2_DataCount]->SubItems->Add(Now());
    Form1->F2_ListView->Items->Item[F2_DataCount]->SubItems->Add(Form1->F2_Edit_LV->Text);
    Form1->F2_ListView->Items->Item[F2_DataCount]->SubItems->Add("10");
    Form1->F2_ListView->Items->Item[F2_DataCount]->SubItems->Add("754");
    Form1->F2_ListView->Items->Item[F2_DataCount]->SubItems->Add("900");
    Form1->F2_ListView->Items->Item[F2_DataCount]->SubItems->Add("850");
    Form1->F2_ListView->Items->Item[F2_DataCount]->SubItems->Add("600");
    Form1->F2_ListView->Items->Item[F2_DataCount]->SubItems->Add("300");


    Form1->F2_Chart->Series[0]->AddXY(F2_NowDT,StrToFloat(Form1->F2_Edit_LV->Text),"",(TColor)clTeeColor);
    Form1->F2_Chart->Series[1]->AddXY(F2_NowDT,StrToFloat(Form1->F2_Edit_HV->Text),"",(TColor)clTeeColor);
    Form1->F2_Chart->Series[2]->AddXY(F2_NowDT,i-350,"",(TColor)clTeeColor);
    Form1->F2_Chart->Series[3]->AddXY(F2_NowDT,i-500,"",(TColor)clTeeColor);
    Form1->F2_Chart->Series[4]->AddXY(F2_NowDT,i-600,"",(TColor)clTeeColor);
    Form1->F2_Chart->Series[5]->AddXY(F2_NowDT,i-700,"",(TColor)clTeeColor);
    Form1->F2_Chart->Series[6]->AddXY(F2_NowDT,i-800,"",(TColor)clTeeColor);
    Form1->F2_Chart->Series[7]->AddXY(F2_NowDT,i-830,"",(TColor)clTeeColor);
    if(Form1->F2_Chart->Series[8]->Count() >= 1) Form1->F2_Chart->Series[8]->Delete(Form1->F2_Chart->Series[8]->Count() - 1);
    Form1->F2_Chart->Series[8]->AddXY(F2_NowDT,1000,"",(TColor)clTeeColor);

    F2_DataCount++;

    if(F2_DataCount > StrToInt(Form1->F2_Autosavenum_E->Text) && Form1->F2_Autosave_CB->Checked){
       Form1->F2_Autosave_CB->Tag = 1;
       F2_Timer->Enabled = false;
       Form1->F2_Save2Excel->Click();
    }

    unsigned short hh;
    unsigned short mm;
    unsigned short ss;
    unsigned short ms;
    F2_NowDT.DecodeTime(&hh,&mm,&ss,&ms);
    if(mm >= 2)  {
       F2_Timer->Tag = 0;
       F2_ChartPageCount++;
       Form1->F2_Chart->Series[0]->Clear();
       Form1->F2_Chart->Series[1]->Clear();
       Form1->F2_Chart->Series[2]->Clear();
       Form1->F2_Chart->Series[3]->Clear();
       Form1->F2_Chart->Series[4]->Clear();
       Form1->F2_Chart->Series[5]->Clear();
       Form1->F2_Chart->Series[6]->Clear();
       Form1->F2_Chart->Series[7]->Clear();
    }
}
//---------------------------------------------------------------------------

void __fastcall TDModule::F3_TimerTimer(TObject *Sender)
{
   int i;

   if(F3_Timer->Tag == 0){
      F3_OldDT = Now();
      F3_Timer->Tag = 1;
   }

    F3_NowDT = Now() - F3_OldDT;

    i=900-(F3_ChartPageCount*50);

    Form1->F3_ListView->Items->Add();
    Form1->F3_ListView->Items->Item[F3_DataCount]->Caption = F3_DataCount;
    Form1->F3_ListView->Items->Item[F3_DataCount]->SubItems->Add(Now());
    Form1->F3_ListView->Items->Item[F3_DataCount]->SubItems->Add(Form1->F3_Edit_LV->Text);
    Form1->F3_ListView->Items->Item[F3_DataCount]->SubItems->Add("10");
    Form1->F3_ListView->Items->Item[F3_DataCount]->SubItems->Add("754");
    Form1->F3_ListView->Items->Item[F3_DataCount]->SubItems->Add("900");
    Form1->F3_ListView->Items->Item[F3_DataCount]->SubItems->Add("850");
    Form1->F3_ListView->Items->Item[F3_DataCount]->SubItems->Add("600");
    Form1->F3_ListView->Items->Item[F3_DataCount]->SubItems->Add("300");


    Form1->F3_Chart->Series[0]->AddXY(F3_NowDT,StrToFloat(Form1->F3_Edit_LV->Text),"",(TColor)clTeeColor);
    Form1->F3_Chart->Series[1]->AddXY(F3_NowDT,StrToFloat(Form1->F3_Edit_HV->Text),"",(TColor)clTeeColor);
    Form1->F3_Chart->Series[2]->AddXY(F3_NowDT,i-350,"",(TColor)clTeeColor);
    Form1->F3_Chart->Series[3]->AddXY(F3_NowDT,i-500,"",(TColor)clTeeColor);
    Form1->F3_Chart->Series[4]->AddXY(F3_NowDT,i-600,"",(TColor)clTeeColor);
    Form1->F3_Chart->Series[5]->AddXY(F3_NowDT,i-700,"",(TColor)clTeeColor);
    Form1->F3_Chart->Series[6]->AddXY(F3_NowDT,i-800,"",(TColor)clTeeColor);
    Form1->F3_Chart->Series[7]->AddXY(F3_NowDT,i-830,"",(TColor)clTeeColor);
    if(Form1->F3_Chart->Series[8]->Count() >= 1) Form1->F3_Chart->Series[8]->Delete(Form1->F3_Chart->Series[8]->Count() - 1);
    Form1->F3_Chart->Series[8]->AddXY(F3_NowDT,1000,"",(TColor)clTeeColor);

    F3_DataCount++;

    if(F3_DataCount > StrToInt(Form1->F3_Autosavenum_E->Text) && Form1->F3_Autosave_CB->Checked){
       Form1->F3_Autosave_CB->Tag = 1;
       F3_Timer->Enabled = false;
       Form1->F3_Save2Excel->Click();
    }

    unsigned short hh;
    unsigned short mm;
    unsigned short ss;
    unsigned short ms;
    F3_NowDT.DecodeTime(&hh,&mm,&ss,&ms);
    if(mm >= 2)  {
       F3_Timer->Tag = 0;
       F3_ChartPageCount++;
       Form1->F3_Chart->Series[0]->Clear();
       Form1->F3_Chart->Series[1]->Clear();
       Form1->F3_Chart->Series[2]->Clear();
       Form1->F3_Chart->Series[3]->Clear();
       Form1->F3_Chart->Series[4]->Clear();
       Form1->F3_Chart->Series[5]->Clear();
       Form1->F3_Chart->Series[6]->Clear();
       Form1->F3_Chart->Series[7]->Clear();
    }
}
//---------------------------------------------------------------------------

void __fastcall TDModule::F4_TimerTimer(TObject *Sender)
{
   int i;

   if(F4_Timer->Tag == 0){
      F4_OldDT = Now();
      F4_Timer->Tag = 1;
   }

    F4_NowDT = Now() - F4_OldDT;

    i=900-(F4_ChartPageCount*50);

    Form1->F4_ListView->Items->Add();
    Form1->F4_ListView->Items->Item[F4_DataCount]->Caption = F3_DataCount;
    Form1->F4_ListView->Items->Item[F4_DataCount]->SubItems->Add(Now());
    Form1->F4_ListView->Items->Item[F4_DataCount]->SubItems->Add(Form1->F3_Edit_LV->Text);
    Form1->F4_ListView->Items->Item[F4_DataCount]->SubItems->Add("10");
    Form1->F4_ListView->Items->Item[F4_DataCount]->SubItems->Add("754");
    Form1->F4_ListView->Items->Item[F4_DataCount]->SubItems->Add("900");
    Form1->F4_ListView->Items->Item[F4_DataCount]->SubItems->Add("850");
    Form1->F4_ListView->Items->Item[F4_DataCount]->SubItems->Add("600");
    Form1->F4_ListView->Items->Item[F4_DataCount]->SubItems->Add("300");


    Form1->F4_Chart->Series[0]->AddXY(F4_NowDT,StrToFloat(Form1->F4_Edit_LV->Text),"",(TColor)clTeeColor);
    Form1->F4_Chart->Series[1]->AddXY(F4_NowDT,StrToFloat(Form1->F4_Edit_HV->Text),"",(TColor)clTeeColor);
    Form1->F4_Chart->Series[2]->AddXY(F4_NowDT,i-350,"",(TColor)clTeeColor);
    Form1->F4_Chart->Series[3]->AddXY(F4_NowDT,i-500,"",(TColor)clTeeColor);
    Form1->F4_Chart->Series[4]->AddXY(F4_NowDT,i-600,"",(TColor)clTeeColor);
    Form1->F4_Chart->Series[5]->AddXY(F4_NowDT,i-700,"",(TColor)clTeeColor);
    Form1->F4_Chart->Series[6]->AddXY(F4_NowDT,i-800,"",(TColor)clTeeColor);
    Form1->F4_Chart->Series[7]->AddXY(F4_NowDT,i-830,"",(TColor)clTeeColor);
    if(Form1->F4_Chart->Series[8]->Count() >= 1) Form1->F4_Chart->Series[8]->Delete(Form1->F4_Chart->Series[8]->Count() - 1);
    Form1->F4_Chart->Series[8]->AddXY(F4_NowDT,1000,"",(TColor)clTeeColor);

    F4_DataCount++;

    if(F4_DataCount > StrToInt(Form1->F4_Autosavenum_E->Text) && Form1->F4_Autosave_CB->Checked){
       Form1->F4_Autosave_CB->Tag = 1;
       F4_Timer->Enabled = false;
       Form1->F4_Save2Excel->Click();
    }

    unsigned short hh;
    unsigned short mm;
    unsigned short ss;
    unsigned short ms;
    F4_NowDT.DecodeTime(&hh,&mm,&ss,&ms);
    if(mm >= 2)  {
       F4_Timer->Tag = 0;
       F4_ChartPageCount++;
       Form1->F4_Chart->Series[0]->Clear();
       Form1->F4_Chart->Series[1]->Clear();
       Form1->F4_Chart->Series[2]->Clear();
       Form1->F4_Chart->Series[3]->Clear();
       Form1->F4_Chart->Series[4]->Clear();
       Form1->F4_Chart->Series[5]->Clear();
       Form1->F4_Chart->Series[6]->Clear();
       Form1->F4_Chart->Series[7]->Clear();
    }
}
//---------------------------------------------------------------------------

