/*****************************************************************************
COMMENT: Opens a SimConnect for a specific activ thread or process
GROUP: ESP
ENTRY: void
EXIT: HRESULT should be S_OK if successful
*/
HRESULT ESPinterface::OpenESPconnection()
{
HRESULT rval;
EvHdl = CreateEvent(NULL,FALSE,FALSE,NULL);
rval = SimConnect_Open(&SChdl,"ARIES",NULL,0,EvHdl,0);
if(rval == S_OK)
rep.Protocoll(BIT3,"SimConnect_Open successful");
else
rep.Protocoll(BIT3,"SimConnect_Open failed with %d",rval);
ESPcon = SChdl;
return(rval);
}
/*****************************************************************************
COMMENT: This thread keeps in contact with ESP an requests all necessary data
GROUP: ESP
ENTRY: ESPinterface* a pointer to the existing class
EXIT: Always 0
*/
unsigned int WINAPI ESPthread(void* ESPclassPtr)
{
HRESULT hr;
double cycleTime,delta;
DWORD nh,lpCnt = 0;
ESP = (ESPinterface*)ESPclassPtr;
ESP->ESPstatus |= ESP_THREAD_ACTIVE;
hr = ESP->OpenESPconnection();
ESPwx.SimCnct = ESP->ESPcon; // share this handle with the WX class
rep.Protocoll(BIT3,"In ESP thread");
if(S_OK == hr)
{
rep.Protocoll(BIT3,"Connected to ESP");
ESP->PreparerequestSets(); // all type of requests like SimConnect_AddToDataDefinition and SimConnect_SubscribeToSystemEvent
while(ESP->ESPstatus & ESP_THREAD_ACTIVE)
{
WaitForSingleObject(ESP->EvHdl,INFINITE);
if(ESP->ESPstatus & ESP_THREAD_ACTIVE)
{
hr = SimConnect_RequestDataOnSimObjectType(ESP->ESPcon, REQUEST_1, RUN_DATA_DEF,0, SIMCONNECT_SIMOBJECT_TYPE_USER); // runtime data
if(!lpCnt)
rep.Protocoll(BIT3,"Received First ESP data");
lpCnt++;
if(!(lpCnt % 10)) // every 200 ms( 5 times per second)
{
hr = SimConnect_RequestDataOnSimObjectType(ESP->ESPcon, REQUEST_2, SLOW_DATA_DEF, 0, SIMCONNECT_SIMOBJECT_TYPE_USER); // slow time data
}
if(!(lpCnt % 100) && !( rxMsgs & METAR_RECEIVED)) // every 2000 ms
{ // wx data
SimConnect_WeatherRequestObservationAtStation(ESP->ESPcon,REQUEST_3,"GLOB");
}
SimConnect_CallDispatch(ESP->ESPcon,(DispatchProc)&ESP->ESPdispatcher, NULL);
}
Sleep(ESP->ThreadCycleTimeMS); // normally 20 ms or whatever is convenient
}
}
ESP->CloseESPconnection();
ESP->EvHdl = NULL;
ESP->ESPcon = NULL;
return 0;
}