#include "stdafx.h"
#include "LibAllert.h"
#include "constants.h"
#include "funs.h"
#include "vars.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

class CAllertLibData
{
public:
 //--
 CAllertLibData(){SetZero();}
 ~CAllertLibData(){Clean();}
 //--
 void SetZero(){WWriteLog=NULL;ProgDir="";ProgDataDir="";ProgID=NULL;}
 void Clean(){SetZero();}
 //--
 void *ProgID;
 void (*WWriteLog)(const char *str);
 CString ProgDir,ProgDataDir;
 int (*FSetSensorsValue)(void *ProgID,int v[19]);
 int (*FSetText)(void *ProgID,const char *);
 //--
} *AllertLibData=NULL;


EXP(void *) FInitAllertLib(void *ProgID, const char *ProgDir,const char *ProgDataDir,void (*WWriteLog)(const char *str))
{
 AllertLibData=new CAllertLibData;
 AllertLibData->ProgID=ProgID;
 AllertLibData->WWriteLog=WWriteLog;
 AllertLibData->ProgDir=ProgDir;
 AllertLibData->ProgDataDir=ProgDataDir;
 //int set[19]; memset(set,-1,sizeof(set));
 //AllertLibData->FSetSensorsValue(AllertLibData->ProgID,set);
 return AllertLibData;
}

EXP(void) FFinishAllertLib(void *AllertLibID)
{
 if(AllertLibID==NULL)return; CAllertLibData* p=(CAllertLibData*)AllertLibID;
 int set[19]; memset(set,0,sizeof(set));
 p->FSetSensorsValue(p->ProgID,set);
 delete (CAllertLibData*)AllertLibData;
}


EXP(int) SetAllertLibAddData(void *AllertLibID, int id, void *data, int dataLength)
{
 if(AllertLibID==NULL)return -1; CAllertLibData* p=(CAllertLibData*)AllertLibID;
 if(id==1)
 {
  p->FSetSensorsValue=(int (*)(void *ProgID,int v[19]))data;
   int set[19]; memset(set,-1,sizeof(set));
   p->FSetSensorsValue(p->ProgID,set);
 }
 else if(id==2)
  p->FSetText=(int (*)(void *ProgID,const char *))data;
 else 
  return -2;
 return 0;
}

EXP(int) FAllertLibStartPush(void *AllertLibID)
{
return 0;
}
EXP(int) FAllertLibStopPush(void *AllertLibID)
{
return 0;
}

EXP(int) FCallAllertLib(void *AllertLibID, int vNormir[19], int vInitial[19], int stage, int irec, int tMillisec)
{int set[19]; memset(set,0,sizeof(set));
 if(AllertLibID==NULL)return -1; CAllertLibData* p=(CAllertLibData*)AllertLibID; if(p->ProgID==NULL)return -2;
 for(int i=0;i<19;i++)
 {
  if(vNormir[i]>100)
   set[i]=255;
  //set[i]=vNormir[i];
 }
 p->FSetSensorsValue(p->ProgID,set);
 return 0;
}
