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

#ifndef U_Main_Ex_IntegracaoH
#define U_Main_Ex_IntegracaoH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <OleServer.hpp>
#include <Mask.hpp>


//-- Includes manuais
#include <tlhelp32.h>
#include <Menus.hpp>
#include <ComCtrls.hpp> // Include necessário para utilizar eventos
#include <vcl.h>
#include <time.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

//--- Add Forms
#include "U_ObtemTotais.h"
#include "CesnaActiveX_OCX.h"




//--- Protótipos das funções de cmd do objeto activex
// Apenas para conhecimento, não precisa ser definido.
/*
FcmMaster->FcmOpen(unsigned char Master);
FcmMaster->FcmSetNumSlaves(int NumSl);
FcmMaster->FcmConfiguraSlave(unsigned char sl, unsigned char bo);
FcmMaster->FcmVersaoFcmMaster();
FcmMaster->FcmSolicitaVersaoSlave(unsigned char sl);
FcmMaster->FcmAutorizaAbastecimento(unsigned char sl,unsigned char bo,unsigned char bi);
FcmMaster->FcmSolicitaUltimoAbastecimento(unsigned char sl, unsigned char bo, unsigned char bi);
FcmMaster->FcmSolicitaTotalizador(unsigned char sl, unsigned char bo, unsigned char bi);
FcmMaster->FcmSolicitaPrecoPorLitro(unsigned char sl, unsigned char bo, unsigned char bi);
FcmMaster->FcmTrocaPreco(unsigned char sl, unsigned char bo, unsigned char bi, long preco, unsigned char cd);
FcmMaster->FcmProgramaPrePago(unsigned char sl, unsigned char bo, unsigned char bi, long preco, *unsigned char cd);
FcmMaster->FcmTravaBomba(unsigned char sl,unsigned char bo);
FcmMaster->FcmDestravaBomba(unsigned char sl,unsigned char bo);
FcmMaster->FcmConfigTot(unsigned char sl,unsigned char bo, unsigned char bi, long valor);
FcmMaster->FcmObtemStatusBo(unsigned char sl,unsigned char bo);
FcmMaster->FcmClose();
*/
//------------ Definições ---------------------------------------------------
//--- Estado das Bombas
#define ESTADO_INDEFINIDO 							(unsigned char) 1
#define BOMBA_PRONTA                                (unsigned char) 2
#define BOMBA_PEDE_AUTORIZACAO                      (unsigned char) 3
#define BOMBA_AGUARDANDO_CARTAO                     (unsigned char) 4
#define BOMBA_LIBERADA_PARA_ABASTECER               (unsigned char) 5
#define BOMBA_ABASTECENDO                           (unsigned char) 6
#define BOMBA_ABASTECEU                             (unsigned char) 7
#define BOMBA_TROCOU_PRECO_POR_LITRO                (unsigned char) 8
#define BOMBA_RESERVADA_PRE_PAGO                    (unsigned char) 9
#define BOMBA_BLOQUEADA                             (unsigned char) 10
#define BOMBA_DESBLOQUEADA                          (unsigned char) 11
#define BOMBA_INFORMOU_FALHA                        (unsigned char) 12
#define BOMBA_NAO_RESPONDE                          (unsigned char) 13

//--- Estados do Concentrador
#define CONCENTRADOR_INFORMOU_VERSAO                (unsigned char) 14
#define CONCENTRADOR_ACEITOU_CONFIGURACAO           (unsigned char) 15
#define FALHA_DE_COMUNICACAO_COM_CONCENTRADOR 		(unsigned char) 16
#define CONCENTRADOR_INFORMOU_EVENTO                (unsigned char) 17
#define RESET_CONCENTRADOR                          (unsigned char) 18

//--- Estados de Erros na DLL
#define EVENTO_MASTER_ERRO_LRC                      (unsigned char) 1
#define EVENTO_MASTER_ERRO_PROTOCOLO                (unsigned char) 2
#define EVENTO_MASTER_FINALIZOU                     (unsigned char) 3

//--- Tipo de Bombas
#define BOMBA_WAYNE_3G                          	(unsigned char)  1
#define BOMBA_WAYNE_DUPLEX							(unsigned char)  2
#define BOMBA_WAYNE_MINNOW							(unsigned char)  3
#define BOMBA_WAYNE_RIFRAN							(unsigned char)  4
#define BOMBA_GILBARCO								(unsigned char)  5
#define BOMBA_STRATEMA_LOOP							(unsigned char)  6
#define BOMBA_STRATEMA_RS485						(unsigned char)  7
#define BOMBA_STRATEMA_RS485_4800					(unsigned char)  8
#define BOMBA_ASPRO_DEVELCO							(unsigned char)  9
#define BOMBA_ASPRO_METROVAL						(unsigned char) 10
#define BOMBA_TOKHEIM								(unsigned char) 11


//-- Definições para o exemplo
#define MAX_SL                              4
#define MAX_BO                              16
#define MAX_BI                              4
//---------------------------------------------------------------------------
class TFrmMain : public TForm
{
__published:	// IDE-managed Components
	TGroupBox *GroupBoxFcmMaster;
	TButton *BtnInitCom;
	TButton *BtnVersaoDll;
	TGroupBox *GroupBoxFcmSlave;
	TLabel *Label2;
	TGroupBox *GroupBoxCmdBombas;
	TButton *BtnAutoriza;
	TButton *BtnUltVol;
	TButton *BtnObtemPpl;
	TButton *BtnTrocaPpl;
	TLabel *Label3;
	TButton *BtnTrava;
	TButton *BtnDestrava;
	TButton *BtnTot;
	TButton *BtnPrePago;
	TLabel *Label4;
	TLabel *Label1;
	TLabel *LblSlave;
	TLabel *Label6;
	TLabel *Label7;
	TCheckBox *ChBoxAutoriza;
	TMemo *MemDisp;
	TLabel *Label5;
	TLabel *Label8;
	TLabel *Label9;
	TButton *BtnConfigura;
	TMainMenu *MainMenu1;
	TMenuItem *ObtemEncerrantes1;
	TMenuItem *Operaes1;
	TMenuItem *ObtemEncerrantes2;
	TButton *BtnObtemStatusBo;
	TComboBox *CmbNumMab;
	TComboBox *CmbNumBo;
	TCheckBox *ChbTodas;
	TComboBox *CmbEndMab;
	TComboBox *CmbEndBomba;
	TComboBox *CmbEndBico;
	TMaskEdit *MEdtPpl;
	TMaskEdit *MEdtPrePago;
	TButton *BtnSair;
	TButton *BtnObtemConfig;
	TButton *BtnObtemNumAbastOff;
	TComboBox *CmbBoxModoOp;
	TLabel *Label11;
	TLabel *Label12;
	TComboBox *CmbBoxTipoBomba;
	TCesna *Cesna;
	TButton *BtnObtemAbastFlash;
	void __fastcall FormCreate(TObject *Sender);
	void __fastcall BtnInitComClick(TObject *Sender);
	void __fastcall BtnVersaoDllClick(TObject *Sender);
	void __fastcall BtnConfiguraClick(TObject *Sender);
	void __fastcall BtnAutorizaClick(TObject *Sender);
	void __fastcall BtnUltVolClick(TObject *Sender);
	void __fastcall BtnTotClick(TObject *Sender);
	void __fastcall BtnObtemPplClick(TObject *Sender);
	void __fastcall BtnTrocaPplClick(TObject *Sender);
	void __fastcall BtnPrePagoClick(TObject *Sender);
	void __fastcall BtnTravaClick(TObject *Sender);
	void __fastcall BtnDestravaClick(TObject *Sender);
	void __fastcall ObtemEncerrantes2Click(TObject *Sender);
	void __fastcall BtnObtemStatusBoClick(TObject *Sender);
	void __fastcall BtnSairClick(TObject *Sender);
	void __fastcall FormDestroy(TObject *Sender);
	void __fastcall BtnObtemConfigClick(TObject *Sender);
	void __fastcall BtnObtemNumAbastOffClick(TObject *Sender);
	void __fastcall CesnaIdCodeMudancaDeEstado(	TObject *Sender,
												unsigned_char Mab,
												unsigned_char Bomba,
												unsigned_char Bico,
												unsigned_char Estado);
	void __fastcall CesnaIdCodeNumAbastOff(		TObject *Sender,
												unsigned_char Mab,
												long NumeroAbastecimantosOffline);
	void __fastcall CesnaIdCodeVolume(			TObject *Sender,
												unsigned_char Mab,
												unsigned_char Bomba,
												unsigned_char Bico,
												long Volume,
												unsigned_char VolumeCasaDecimal);
	void __fastcall CesnaIdCodePrecoPorLitro(	TObject *Sender,
												unsigned_char Slave,
												unsigned_char Bomba,
												unsigned_char Bico,
												long PrecoPorLitro,
												unsigned_char PrecoPorLitroCasaDecimal);
	void __fastcall CesnaIdCodeTotalizador(		TObject *Sender,
												unsigned_char Mab,
												unsigned_char Bomba,
												unsigned_char Bico,
												long Totalizador,
												unsigned_char TotalizadorCasaDecimal);
	void __fastcall CesnaIdCodeAbasteceu(		TObject *Sender,
												unsigned_char Mab,
												unsigned_char Bomba,
												unsigned_char Bico,
												long Volume,
												unsigned_char VolumeCasaDecimal,
												long TotalInicial,
												unsigned_char TotalInicialCasaDecimal,
												long TotalFinal,
												unsigned_char TotalFinalCasaDecimal,
												long PrecoPorLitro,
												unsigned_char PrecoPorLitroCasaDecimal,
												long Data,
												long Hora,
												BSTR TagFrentista,
												BSTR TagCliente,
												unsigned_char FlagAbastecimento,
												long NumeroAbastecimento);
	void __fastcall CesnaIdCodeConfigMab(		TObject *Sender,
												unsigned_char Mab,
												unsigned_char NumeroBombas,
												unsigned_char ModoOperacao,
												unsigned_char TipoBomba);
	void __fastcall CesnaIdCodeErroCrcTab(		TObject *Sender,
												unsigned_char Mab,
												long Crc);
	void __fastcall CesnaIdCodeInfoEvento(		TObject *Sender,
												unsigned_char Mab,
												unsigned_char Bomba,
												unsigned_char Bico,
												unsigned_char Evento);
	void __fastcall CesnaIdCodeValorBombaAbast(	TObject *Sender,
												unsigned_char Mab,
												unsigned_char Bomba,
												unsigned_char Bico,
												long Valor,
												unsigned_char ValorCasaDecimal);
	void __fastcall BtnObtemAbastFlashClick(TObject *Sender);


private:	// User declarations
public:		// User declarations
		__fastcall TFrmMain(TComponent* Owner);
//--- Funções  Globais -----------------------------------------------------
	void __fastcall Display(					AnsiString X);					// Função para Mostrar Eventos no Display
	bool 			VersaoArquivo(				AnsiString Arquivo,
												int *Major,
												int *Minor,
												int *Release,
												int *Build);					// Obtem Versão do prog

	bool __fastcall ObtemEnderecos(				unsigned char *PtrEndMab,
												unsigned char *PtrEndBo,
												unsigned char *PtrEndBi);		// Função para obter endereços para comando
	AnsiString __fastcall DecodificaTipoBo(		unsigned char Byte);			// Decodifica tipo da Bomba para Str

//--- Variáveis Globais ----------------------------------------------------
	unsigned char	Master;								// Var Global para FcmMaster
	unsigned char	Slave; 								// Var Global para FcmSlave
	unsigned char	Bomba; 								// Var Global para Bomba
	unsigned char	Bico; 								// Var Global para Bico
	unsigned long	RespTotais[MAX_SL][MAX_BO][MAX_BI];	// Var para armazenar tot recebido.
	unsigned char   RespCdTot[MAX_SL][MAX_BO][MAX_BI];	// Var para armazenar cd tot recebido
	bool			FlagOpen[3];						// Var para controle de inicio de comunicação. Evitar enviar comando open se já enviado
	unsigned char   ModoOperacao;						// Var Global com o modo de operação escolhido
	unsigned char   FormatoMsgAbast;					// Var Global para o formato da Msg de abastecimento

	bool			FlagComAtiva;						// Indica Comunicação Ativa

};
//---------------------------------------------------------------------------
extern PACKAGE TFrmMain *FrmMain;
//---------------------------------------------------------------------------
#endif
