teste de publish
Diff: devices.cpp
- Revision:
- 19:d52176ff683d
- Parent:
- 18:66fa6fcba212
- Child:
- 20:e0656406d5b8
diff -r 66fa6fcba212 -r d52176ff683d devices.cpp --- a/devices.cpp Mon May 07 22:45:07 2018 +0000 +++ b/devices.cpp Fri May 25 23:45:54 2018 +0000 @@ -9,76 +9,16 @@ estadoConfiguracao_scheduleExceptionSet }; -/* -Antigo -uint8_t resizeArray(uint32_t atual, uint32_t novo, char **array,uint16_t sizeUnit) -{ - uint32_t i; - char *p = (char *) malloc(sizeUnit*novo); - char *aux; - - if(p==NULL) { - return 0; - } - - aux = *array; - - if(atual) { - //So sigo em frente se tinha algo dentro deste array - for(i=0; i<(novo*sizeUnit); i++) { - p[i]=aux[i]; - } - free(aux); - } - - *array = p; - return 1; -} -*/ - -/*uint8_t resizeArray(uint32_t atual, uint32_t novo, char **array,uint16_t sizeUnit) -{ - uint32_t i; - char *p = (char *) malloc(sizeUnit*novo); +uint8_t numAlarms; +uint8_t numSchedules; +uint8_t numScheduleExceptions; +uint16_t numReadings; +alarm alarms[maxAlarms] __attribute__ ((section("AHBSRAM0"))); +reading readings[maxReadings] __attribute__ ((section("AHBSRAM0"))); +schedule schedules[maxSchedules] __attribute__ ((section("AHBSRAM0"))); +scheduleException scheduleExceptions[maxSchedulesExceptions] __attribute__ ((section("AHBSRAM0"))); - if(p==NULL) { - return 0; - } - - if(atual) { - //So sigo em frente se tinha algo dentro deste array - for(i=0; i<(novo*sizeUnit); i++) { - p[i]=*array[i]; - } - free(*array); - } - - *array = p; - return 1; -}*/ - -void writeReadingsToSD(){ - int i; - for(i=0;i<devices;i++){ - dispositivos[i]->writeReadingsToSD(); - } -} - -void verifyAlarms(){ - int i; - for(i=0;i<devices;i++){ - dispositivos[i]->verifyAlarms(); - } -} - -void verifySchedules(){ - int i; - for(i=0;i<devices;i++){ - dispositivos[i]->verifySchedules(); - } -} - -void device::floatToBin(uint8_t dir, float *v,uint8_t *p) +void floatToBin(uint8_t dir, float *v,uint8_t *p) { //Variáveis locais union { @@ -100,7 +40,7 @@ } } -void device::uint32_t_ToBin(uint8_t dir, uint32_t *v,uint8_t *p) +void uint32_t_ToBin(uint8_t dir, uint32_t *v,uint8_t *p) { //Variáveis locais union { @@ -122,7 +62,7 @@ } } -void device::uint16_t_ToBin(uint8_t dir, uint16_t *v,uint8_t *p) +void uint16_t_ToBin(uint8_t dir, uint16_t *v,uint8_t *p) { //Variáveis locais union { @@ -144,8 +84,70 @@ } } +void mostraAlarmeEmExecucao(uint32_t indice){ + printf("Alarme %u:\n",indice); + printf("\tId <%lu>.\n",alarms[indice].id); + printf("\tSeconds <%lu>.\n",alarms[indice].seconds); + printf("\tAddr %u, func %u, reg %lu.\n",alarms[indice].addrModbusRead,alarms[indice].funcModbusRead,alarms[indice].regModbusRead); + if(alarms[indice].type==modBusType_float) { + float value; + floatToBin(0,&value,&alarms[indice].value[0]); + if(alarms[indice].max){ + printf("\tFloat max <%f>.\n",value); + } + else{printf("\tFloat min <%f>.\n",value);} + } + + if(alarms[indice].type==modBusType_uint32_t) { + uint32_t value; + uint32_t_ToBin(0,&value,&alarms[indice].value[0]); + if(alarms[indice].max){ + printf("\tuint32_t max <%lu>.\n",value); + } + else{printf("\tuint32_t min <%lu>.\n",value);} + } + + if(alarms[indice].type==modBusType_uint16_t) { + uint16_t value; + uint16_t_ToBin(0,&value,&alarms[indice].value[0]); + if(alarms[indice].max){ + printf("\tuint16_t max <%lu>.\n",value); + } + else{printf("\tuint16_t min <%lu>.\n",value);} + } + + if(alarms[indice].type==modBusType_int32_t) { + uint32_t value; + uint32_t_ToBin(0,&value,&alarms[indice].value[0]); + if(alarms[indice].max){ + printf("\tint32_t max <%ld>.\n",(int32_t)value); + } + else{printf("\tint32_t min <%ld>.\n",(int32_t)value);} + } + + if(alarms[indice].type==modBusType_int16_t) { + uint16_t value; + uint16_t_ToBin(0,&value,&alarms[indice].value[0]); + if(alarms[indice].max){ + printf("\tint16_t max <%ld>.\n",(int16_t)value); + } + else{printf("\tint16_t min <%ld>.\n",(int16_t)value);} + } + + if(alarms[indice].type==modBusType_bit) { + uint8_t value; + value=alarms[indice].value[0]; + if(alarms[indice].max){ + printf("\tBIT max <%lu>.\n",value); + } + else{printf("\tBIT min <%lu>.\n",value);} + } + + printf("\tact:%lu\n",alarms[indice].idAct); +} + //Funcao restaurada -void device::verifyAlarms(){ +void verifyAlarms(){ //Inicio da verificação uint16_t alarmIndex; float leitura_float,value_float; @@ -155,79 +157,82 @@ bool alarmFound; char aux[30]; - for(alarmIndex = 0;alarmIndex < this->numAlarms;alarmIndex++){ + //for(alarmIndex = 0;alarmIndex < alarmes;alarmIndex++){ + for(alarmIndex = 0;alarmIndex < alarmes;alarmIndex++){ - /*pc.printf("\r\nTeste Alarm, id<%lu>.\r\n",this->alarms[alarmIndex].id); - pc.printf("ON<%lu>.\r\n",this->alarms[alarmIndex].on); - pc.printf("secAlarm<%lu>.\r\n",this->alarms[alarmIndex].secAlarm); - pc.printf("alarmFound<%lu>.\r\n",this->alarms[alarmIndex].alarmFound);*/ - if(this->alarms[alarmIndex].on){ - alarmFound=this->alarms[alarmIndex].alarmFound; - switch(this->alarms[alarmIndex].type){ + /*pc.printf("\r\nTeste Alarm, id<%lu>.\r\n",alarms[alarmIndex].id); + pc.printf("ON<%lu>.\r\n",alarms[alarmIndex].on); + pc.printf("secAlarm<%lu>.\r\n",alarms[alarmIndex].secAlarm); + pc.printf("alarmFound<%lu>.\r\n",alarms[alarmIndex].alarmFound);*/ + //if((alarms[alarmIndex].on)&&(alarms[alarmIndex].id<100000000)){ + if(alarms[alarmIndex].on){ + if(debug){mostraAlarmeEmExecucao(alarmIndex);} + alarmFound=alarms[alarmIndex].alarmFound; + switch(alarms[alarmIndex].type){ case modBusType_float: //Montar float! - this->floatToBin(0,&value_float,&this->alarms[alarmIndex].value[0]); + floatToBin(0,&value_float,&alarms[alarmIndex].value[0]); - if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; + if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; modBusMaster1::readFloat( - this->alarms[alarmIndex].addrModbusRead, - this->alarms[alarmIndex].funcModbusRead, - this->alarms[alarmIndex].regModbusRead, + alarms[alarmIndex].addrModbusRead, + alarms[alarmIndex].funcModbusRead, + alarms[alarmIndex].regModbusRead, 1, &leitura_float ); }else{ - //leitura_float = temperaturas[this->alarms[alarmIndex].regModbusRead]; + //leitura_float = temperaturas[alarms[alarmIndex].regModbusRead]; //Busca Interna - if(this->alarms[alarmIndex].regModbusRead<100){//Registradores das entradas de temperatura + if(alarms[alarmIndex].regModbusRead<100){//Registradores das entradas de temperatura //Populando floatBuffer com a temperaturas - if(this->alarms[alarmIndex].regModbusRead < num_temperatureSensors){ - leitura_float = temperaturas[this->alarms[alarmIndex].regModbusRead]; + if(alarms[alarmIndex].regModbusRead < num_temperatureSensors){ + leitura_float = temperaturas[alarms[alarmIndex].regModbusRead]; } } - if((this->alarms[alarmIndex].regModbusRead>99)&&(this->alarms[alarmIndex].regModbusRead<104)){//Registradores das entradas analógicas - leitura_float = aiFiltrada[this->alarms[alarmIndex].regModbusRead - 100]; + if((alarms[alarmIndex].regModbusRead>99)&&(alarms[alarmIndex].regModbusRead<104)){//Registradores das entradas analógicas + leitura_float = aiFiltrada[alarms[alarmIndex].regModbusRead - 100]; } } - if(this->alarms[alarmIndex].max!=0){ + if(alarms[alarmIndex].max!=0){ if(leitura_float > value_float){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } } }else{ if(leitura_float < value_float){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } @@ -236,61 +241,61 @@ break; case modBusType_uint32_t: //Montar uint32_t! - this->uint32_t_ToBin(0,&value_uint32_t,&this->alarms[alarmIndex].value[0]); + uint32_t_ToBin(0,&value_uint32_t,&alarms[alarmIndex].value[0]); - if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; + if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; //Bloco de leitura da variável modBus; modBusMaster1::readRegister32BIT( - this->alarms[alarmIndex].addrModbusRead, - this->alarms[alarmIndex].funcModbusRead, - this->alarms[alarmIndex].regModbusRead, + alarms[alarmIndex].addrModbusRead, + alarms[alarmIndex].funcModbusRead, + alarms[alarmIndex].regModbusRead, 1, &leitura_uint32_t ); }else{ - leitura_uint32_t = pulsosEDs[this->alarms[alarmIndex].regModbusRead]; + leitura_uint32_t = pulsosEDs[alarms[alarmIndex].regModbusRead]; } - if(this->alarms[alarmIndex].max!=0){ + if(alarms[alarmIndex].max!=0){ if(leitura_uint32_t > value_uint32_t){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; - pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0; + pulsosEDs[alarms[alarmIndex].regModbusRead]=0; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } } }else{ if(leitura_uint32_t < value_uint32_t){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; - pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0; + pulsosEDs[alarms[alarmIndex].regModbusRead]=0; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } @@ -300,61 +305,61 @@ case modBusType_uint16_t: //Montar uint16_t! - this->uint16_t_ToBin(0,&value_uint16_t,&this->alarms[alarmIndex].value[0]); + uint16_t_ToBin(0,&value_uint16_t,&alarms[alarmIndex].value[0]); - if(this->alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){ + if(alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){ //Bloco de leitura da variável modBus; modBusMaster1::readRegister16BIT( - this->alarms[alarmIndex].addrModbusRead, - this->alarms[alarmIndex].funcModbusRead, - this->alarms[alarmIndex].regModbusRead, + alarms[alarmIndex].addrModbusRead, + alarms[alarmIndex].funcModbusRead, + alarms[alarmIndex].regModbusRead, 1, &leitura_uint16_t ); }else{ - if(this->alarms[alarmIndex].regModbusRead<4){ - leitura_uint16_t = aiFiltrada[this->alarms[alarmIndex].regModbusRead]; + if(alarms[alarmIndex].regModbusRead<4){ + leitura_uint16_t = aiFiltrada[alarms[alarmIndex].regModbusRead]; } } - if(this->alarms[alarmIndex].max!=0){ + if(alarms[alarmIndex].max!=0){ if(leitura_uint16_t > value_uint16_t){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } } }else{ if(leitura_uint16_t < value_uint16_t){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } @@ -364,61 +369,61 @@ case modBusType_int32_t: //Montar uint32_t! - this->uint32_t_ToBin(0,&value_uint32_t,&this->alarms[alarmIndex].value[0]); + uint32_t_ToBin(0,&value_uint32_t,&alarms[alarmIndex].value[0]); - if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; + if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; //Bloco de leitura da variável modBus; modBusMaster1::readRegister32BIT( - this->alarms[alarmIndex].addrModbusRead, - this->alarms[alarmIndex].funcModbusRead, - this->alarms[alarmIndex].regModbusRead, + alarms[alarmIndex].addrModbusRead, + alarms[alarmIndex].funcModbusRead, + alarms[alarmIndex].regModbusRead, 1, &leitura_uint32_t ); }else{ - leitura_uint32_t = pulsosEDs[this->alarms[alarmIndex].regModbusRead]; + leitura_uint32_t = pulsosEDs[alarms[alarmIndex].regModbusRead]; } - if(this->alarms[alarmIndex].max!=0){ + if(alarms[alarmIndex].max!=0){ if(((int32_t)leitura_uint32_t) > ((int32_t)value_uint32_t)){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; - pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0; + pulsosEDs[alarms[alarmIndex].regModbusRead]=0; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } } }else{ if(((int32_t)leitura_uint32_t) < ((int32_t)value_uint32_t)){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; - pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0; + pulsosEDs[alarms[alarmIndex].regModbusRead]=0; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } @@ -428,61 +433,61 @@ case modBusType_int16_t: //Montar uint16_t! - this->uint16_t_ToBin(0,&value_uint16_t,&this->alarms[alarmIndex].value[0]); + uint16_t_ToBin(0,&value_uint16_t,&alarms[alarmIndex].value[0]); - if(this->alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){ + if(alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){ //Bloco de leitura da variável modBus; modBusMaster1::readRegister16BIT( - this->alarms[alarmIndex].addrModbusRead, - this->alarms[alarmIndex].funcModbusRead, - this->alarms[alarmIndex].regModbusRead, + alarms[alarmIndex].addrModbusRead, + alarms[alarmIndex].funcModbusRead, + alarms[alarmIndex].regModbusRead, 1, &leitura_uint16_t ); }else{ - if(this->alarms[alarmIndex].regModbusRead<4){ - leitura_uint16_t = aiFiltrada[this->alarms[alarmIndex].regModbusRead]; + if(alarms[alarmIndex].regModbusRead<4){ + leitura_uint16_t = aiFiltrada[alarms[alarmIndex].regModbusRead]; } } - if(this->alarms[alarmIndex].max!=0){ + if(alarms[alarmIndex].max!=0){ if(((int16_t)leitura_uint16_t) > ((int16_t)value_uint16_t)){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } } }else{ if(((int16_t)leitura_uint16_t) < ((int16_t)value_uint16_t)){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } @@ -494,35 +499,35 @@ case modBusType_bit: //Montar bit! //O valor de alarm para bit é o value[0]!!! - value_bit = (this->alarms[alarmIndex].value[0] > 0); //Qualquer valor maior que zero da saída 1 + value_bit = (alarms[alarmIndex].value[0] > 0); //Qualquer valor maior que zero da saída 1 - if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; + if(alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; //Bloco de leitura da variável modBus; modBusMaster1::readCoils( - this->alarms[alarmIndex].addrModbusRead, - this->alarms[alarmIndex].regModbusRead, + alarms[alarmIndex].addrModbusRead, + alarms[alarmIndex].regModbusRead, 1, &leitura_bit ); }else{ - leitura_bit = entradasDigitais[this->alarms[alarmIndex].regModbusRead]; + leitura_bit = entradasDigitais[alarms[alarmIndex].regModbusRead]; } if(leitura_bit == value_bit){ - this->alarms[alarmIndex].secAlarm++; - if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ - this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; + alarms[alarmIndex].secAlarm++; + if(alarms[alarmIndex].secAlarm >= alarms[alarmIndex].seconds){ + alarms[alarmIndex].secAlarm = alarms[alarmIndex].seconds; alarmFound = true; } }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; + if(alarms[alarmIndex].secAlarm==1){ + alarms[alarmIndex].secAlarm=0; alarmFound = false; }else{ - if(this->alarms[alarmIndex].secAlarm){ + if(alarms[alarmIndex].secAlarm){ if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; + alarms[alarmIndex].secAlarm--; }else{ - this->alarms[alarmIndex].secAlarm=0; + alarms[alarmIndex].secAlarm=0; } } } @@ -530,14 +535,14 @@ break; } - + //Verifico se houve alarme não tratado - if(this->alarms[alarmIndex].alarmFound!=alarmFound){ - this->alarms[alarmIndex].alarmFound=alarmFound; + if(alarms[alarmIndex].alarmFound!=alarmFound){ + alarms[alarmIndex].alarmFound=alarmFound; if(alarmFound){ - printf("Alarm FOUND! Id <%lu>.\n",this->alarms[alarmIndex].id); + printf("Alarm FOUND! Id <%lu>.\n",alarms[alarmIndex].id); sdCardBuf.fill("log{alarm:event:found;id:",25); - sprintf(aux,"%lu",this->alarms[alarmIndex].id); + sprintf(aux,"%lu",alarms[alarmIndex].id); sdCardBuf.fill(aux,strlen(aux)); sdCardBuf.fill(";timestamp:",11); @@ -545,17 +550,17 @@ sdCardBuf.fill(aux,strlen(aux)); sdCardBuf.fill(";reading:",9); - if(this->alarms[alarmIndex].type == modBusType_float){ + if(alarms[alarmIndex].type == modBusType_float){ sprintf(aux,"%f",leitura_float); - }else if(this->alarms[alarmIndex].type == modBusType_uint32_t){ + }else if(alarms[alarmIndex].type == modBusType_uint32_t){ sprintf(aux,"%lu",leitura_uint32_t); - }else if(this->alarms[alarmIndex].type == modBusType_uint16_t){ + }else if(alarms[alarmIndex].type == modBusType_uint16_t){ sprintf(aux,"%lu",leitura_uint16_t); - }else if(this->alarms[alarmIndex].type == modBusType_int32_t){ + }else if(alarms[alarmIndex].type == modBusType_int32_t){ sprintf(aux,"%ld",(int32_t)leitura_uint32_t); - }else if(this->alarms[alarmIndex].type == modBusType_int16_t){ + }else if(alarms[alarmIndex].type == modBusType_int16_t){ sprintf(aux,"%ld",(int16_t)leitura_uint16_t); - }else if(this->alarms[alarmIndex].type == modBusType_bit){ + }else if(alarms[alarmIndex].type == modBusType_bit){ sprintf(aux,"%lu",leitura_bit); } sdCardBuf.fill(aux,strlen(aux)); @@ -563,11 +568,11 @@ sdCard::insereDadosBank(sdCardBuf.get(),leitura_uint16_t); boolExecAct=true; - act = this->alarms[alarmIndex].idAct; + act = alarms[alarmIndex].idAct; }else{ - printf("Alarm RELEASE! id <%lu>.\n",this->alarms[alarmIndex].id); + printf("Alarm RELEASE! id <%lu>.\n",alarms[alarmIndex].id); sdCardBuf.fill("log{alarm:event:release;id:",27); - sprintf(aux,"%lu",this->alarms[alarmIndex].id); + sprintf(aux,"%lu",alarms[alarmIndex].id); sdCardBuf.fill(aux,strlen(aux)); sdCardBuf.fill(";timestamp:",11); @@ -585,7 +590,7 @@ } } -void device::verifySchedules(){ +void verifySchedules(){ char stringTime[30]; char *weekdayString; time_t seconds = time(NULL)+5; @@ -623,19 +628,19 @@ //pc.printf("Passo 1 .\r\n"); for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){ //pc.printf("Passo 2.\r\n"); - if(this->schedules[indexSchedule].weekday&(0x1<<weekday)){ + if(schedules[indexSchedule].weekday&(0x1<<weekday)){ //pc.printf("Passo 3.\r\n"); - if(this->schedules[indexSchedule].minute == minuto){ + if(schedules[indexSchedule].minute == minuto){ //pc.printf("Passo 4.\r\n"); //Verifico se já executei e então executo a ação. - if(!this->schedules[indexSchedule].actSent){ + if(!schedules[indexSchedule].actSent){ //pc.printf("Passo 5 .\r\n"); //Executa a ação; - for(indexScheduleException = 0;indexScheduleException<this->numScheduleExceptions;indexScheduleException++){ + for(indexScheduleException = 0;indexScheduleException<numScheduleExceptions;indexScheduleException++){ //pc.printf("Passo 6.\r\n"); - if(this->schedules[indexSchedule].id == this->scheduleExceptions[indexScheduleException].id_schedule){ + if(schedules[indexSchedule].id == scheduleExceptions[indexScheduleException].id_schedule){ //pc.printf("Passo 7.\r\n"); - seconds = this->scheduleExceptions[indexScheduleException].timestamp; + seconds = scheduleExceptions[indexScheduleException].timestamp; strftime(stringTime, 30, "%y,%d,%m", localtime(&seconds)); anoScheduleException=atoi(strtok(stringTime,",")); diaScheduleException=atoi(strtok(NULL,",")); @@ -666,19 +671,19 @@ } if(!exceptionDetected){ diversos::wdt.kick(90.0); - this->execAct(this->schedules[indexSchedule].idAct); - this->schedules[indexSchedule].actSent = true; + execAct(schedules[indexSchedule].idAct); + schedules[indexSchedule].actSent = true; } } }else{ - this->schedules[indexSchedule].actSent = false; + schedules[indexSchedule].actSent = false; } } } } /* -void device::verifySchedules(){ +void verifySchedules(){ char stringTime[15]; char *weekdayString; time_t seconds = time(NULL); @@ -703,64 +708,64 @@ //printf("\r\n\r\nMinuto %lu.\r\n\r\n",minuto); for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){ //printf("Executando item %u do for de schedules.\r\n\r\n",indexSchedule); - if((this->schedules[indexSchedule].weekday == weekday)||(this->schedules[indexSchedule].weekday == 0)){ + if((schedules[indexSchedule].weekday == weekday)||(schedules[indexSchedule].weekday == 0)){ //Se for um alarme para o dia de hoje ou o alarme independer de dia (Se o dia for zero) //printf("Hoje eh dia de executar a acão do schedule indice %u.\r\n\r\n",indexSchedule); - if(this->schedules[indexSchedule].minute == minuto){ + if(schedules[indexSchedule].minute == minuto){ //Verifico se já executei e então executo a ação. - if(!this->schedules[indexSchedule].actSent){ + if(!schedules[indexSchedule].actSent){ //Executa a ação; //printf("Executando a acao!\r\n\r\n"); - this->execAct(this->schedules[indexSchedule].idAct); - this->schedules[indexSchedule].actSent = true; + execAct(schedules[indexSchedule].idAct); + schedules[indexSchedule].actSent = true; } }else{ - this->schedules[indexSchedule].actSent = false; + schedules[indexSchedule].actSent = false; } } } } */ /* -uint8_t device::execActSchedule(uint32_t index){ - switch(this->schedules[index].actType){ +uint8_t execActSchedule(uint32_t index){ + switch(schedules[index].actType){ case modBusType_float: float floatValueToWrite; //writeFloat(uint8_t,uint16_t,uint8_t,float*); - this->floatToBin(0,&floatValueToWrite,&this->schedules[index].actValue[0]); + floatToBin(0,&floatValueToWrite,&schedules[index].actValue[0]); modBusMaster1::writeFloat( - this->schedules[index].param1, - this->schedules[index].param2, - this->schedules[index].param3, + schedules[index].param1, + schedules[index].param2, + schedules[index].param3, &floatValueToWrite ); break; case modBusType_uint32_t: uint32_t uint32ValueToWrite; - this->uint32_t_ToBin(0,&uint32ValueToWrite,&this->schedules[index].actValue[0]); + uint32_t_ToBin(0,&uint32ValueToWrite,&schedules[index].actValue[0]); modBusMaster1::writeRegister32BIT( - this->schedules[index].param1, - this->schedules[index].param2, - this->schedules[index].param3, + schedules[index].param1, + schedules[index].param2, + schedules[index].param3, &uint32ValueToWrite ); break; case modBusType_uint16_t: uint16_t uint16ValueToWrite; - this->uint16_t_ToBin(0,&uint16ValueToWrite,&this->schedules[index].actValue[0]); + uint16_t_ToBin(0,&uint16ValueToWrite,&schedules[index].actValue[0]); modBusMaster1::writeRegister16BIT( - this->schedules[index].param1, - this->schedules[index].param2, - this->schedules[index].param3, + schedules[index].param1, + schedules[index].param2, + schedules[index].param3, &uint16ValueToWrite ); break; case modBusType_bit: bool boolValueToWrite; - boolValueToWrite = (this->schedules[index].actValue[0] != 0); + boolValueToWrite = (schedules[index].actValue[0] != 0); modBusMaster1::writeSingleCoil( - this->schedules[index].param1, - this->schedules[index].param3, + schedules[index].param1, + schedules[index].param3, boolValueToWrite ); break; @@ -773,7 +778,7 @@ //printf("\r\n===================DEBUG(Schedule)===================\r\n"); //"Ir:0,6E01FFFFS" - sprintf(indexBusca,"Ir:%u,",this->schedules[index].param3); + sprintf(indexBusca,"Ir:%u,",schedules[index].param3); //printf("indexBusca = <%s>.\r\n",indexBusca); if(sdCard::abreArquivo(&sdCard::devices,"r")){ do{ @@ -790,7 +795,7 @@ //Neste ponto já tenho o meu comando pronto para ser executado dentro de comandoIr; //printf("\r\n\r\n\r\nEstou enviando o comando %s de dentro do schedule.\r\n\r\n\r\n",strtok(NULL,"S")); deserializaPacoteIR(strtok(NULL,"S")); - enviaComandoIR(this->schedules[index].param1,this->schedules[index].param2); + enviaComandoIR(schedules[index].param1,schedules[index].param2); } }else{ return false; @@ -802,45 +807,45 @@ } */ /* -uint8_t device::execActAlarm(uint32_t index){ - switch(this->alarms[index].actType){ +uint8_t execActAlarm(uint32_t index){ + switch(alarms[index].actType){ case modBusType_float: float floatValueToWrite; //writeFloat(uint8_t,uint16_t,uint8_t,float*); - this->floatToBin(0,&floatValueToWrite,&this->alarms[index].actValue[0]); + floatToBin(0,&floatValueToWrite,&alarms[index].actValue[0]); modBusMaster1::writeFloat( - this->alarms[index].addrModbusWrite, - this->alarms[index].funcModbusWrite, - this->alarms[index].regModbusWrite, + alarms[index].addrModbusWrite, + alarms[index].funcModbusWrite, + alarms[index].regModbusWrite, &floatValueToWrite ); break; case modBusType_uint32_t: uint32_t uint32ValueToWrite; - this->uint32_t_ToBin(0,&uint32ValueToWrite,&this->alarms[index].actValue[0]); + uint32_t_ToBin(0,&uint32ValueToWrite,&alarms[index].actValue[0]); modBusMaster1::writeRegister32BIT( - this->alarms[index].addrModbusWrite, - this->alarms[index].funcModbusWrite, - this->alarms[index].regModbusWrite, + alarms[index].addrModbusWrite, + alarms[index].funcModbusWrite, + alarms[index].regModbusWrite, &uint32ValueToWrite ); break; case modBusType_uint16_t: uint16_t uint16ValueToWrite; - this->uint16_t_ToBin(0,&uint16ValueToWrite,&this->alarms[index].actValue[0]); + uint16_t_ToBin(0,&uint16ValueToWrite,&alarms[index].actValue[0]); modBusMaster1::writeRegister16BIT( - this->alarms[index].addrModbusWrite, - this->alarms[index].funcModbusWrite, - this->alarms[index].regModbusWrite, + alarms[index].addrModbusWrite, + alarms[index].funcModbusWrite, + alarms[index].regModbusWrite, &uint16ValueToWrite ); break; case modBusType_bit: bool boolValueToWrite; - boolValueToWrite = (this->alarms[index].actValue[0] != 0); + boolValueToWrite = (alarms[index].actValue[0] != 0); modBusMaster1::writeSingleCoil( - this->alarms[index].addrModbusWrite, - this->alarms[index].regModbusWrite, + alarms[index].addrModbusWrite, + alarms[index].regModbusWrite, boolValueToWrite ); break; @@ -849,7 +854,7 @@ } */ -void device::setReading(char *linha) +void setReading(char *linha) { uint8_t addr=0; uint8_t func=0; @@ -863,40 +868,40 @@ return; } - this->numReadings++; + numReadings++; if(strstr(linha,"float,")) { - this->readings[this->numReadings-1].type=modBusType_float; + readings[numReadings-1].type=modBusType_float; inputData = true; } if(strstr(linha,"int32_t,")) { - this->readings[this->numReadings-1].type=modBusType_int32_t; + readings[numReadings-1].type=modBusType_int32_t; inputData = true; } if(strstr(linha,"int16_t,")) { - this->readings[this->numReadings-1].type=modBusType_int16_t; + readings[numReadings-1].type=modBusType_int16_t; inputData = true; } if(strstr(linha,"uint32_t,")) { - this->readings[this->numReadings-1].type=modBusType_uint32_t; + readings[numReadings-1].type=modBusType_uint32_t; inputData = true; } if(strstr(linha,"uint16_t,")) { - this->readings[this->numReadings-1].type=modBusType_uint16_t; + readings[numReadings-1].type=modBusType_uint16_t; inputData = true; } if(strstr(linha,"uint8_t,")) { - this->readings[this->numReadings-1].type=modBusType_uint8_t; + readings[numReadings-1].type=modBusType_uint8_t; inputData = true; } if(strstr(linha,"bit,")) { - this->readings[this->numReadings-1].type=modBusType_bit; + readings[numReadings-1].type=modBusType_bit; inputData = true; } @@ -915,15 +920,15 @@ pChar = strtok(NULL,","); numRegs = atoi(pChar); - this->readings[this->numReadings-1].addr=addr; - this->readings[this->numReadings-1].func=func; - this->readings[this->numReadings-1].reg=reg; - this->readings[this->numReadings-1].numRegs=numRegs; + readings[numReadings-1].addr=addr; + readings[numReadings-1].func=func; + readings[numReadings-1].reg=reg; + readings[numReadings-1].numRegs=numRegs; } } -void device::setAlarm(char *linha) +void setAlarm(char *linha) { float floatVar; uint32_t aux32_t; @@ -933,217 +938,217 @@ if(strstr(linha,"alarmSet")) { //Inicialização de contadores de alarme //É necessário inicializar estes dados para quando a máquina de alarmes rodar ter um inicio correto. - this->alarms[this->numAlarms].secAlarm=0; - this->alarms[this->numAlarms].idAct=0; - this->alarms[this->numAlarms].on=0; - this->alarms[this->numAlarms].alarmFound = false; + alarms[alarmes].secAlarm=0; + alarms[alarmes].idAct=0; + alarms[alarmes].on=0; + alarms[alarmes].alarmFound = false; //Inicialização de contadores de alarme - this->numAlarms++; + alarmes++; return; } if(strstr(linha,"idAlarm:")){ strtok(linha,":"); - this->alarms[this->numAlarms-1].id = atoi(strtok(NULL,"\r\n")); + alarms[alarmes-1].id = atoi(strtok(NULL,"\r\n")); return; } if(strstr(linha,"seconds:")) { strtok(linha,":"); pChar = strtok(NULL,"\r\n"); - this->alarms[this->numAlarms-1].seconds = atoi(pChar); + alarms[alarmes-1].seconds = atoi(pChar); return; } if(strstr(linha,"on:")) { strtok(linha,":"); pChar = strtok(NULL,"\r\n"); - this->alarms[this->numAlarms-1].on = atoi(pChar); + alarms[alarmes-1].on = atoi(pChar); return; } if(strstr(linha,"idAct:")) { strtok(linha,":"); - this->alarms[this->numAlarms-1].idAct = atoi(strtok(NULL,"")); + alarms[alarmes-1].idAct = atoi(strtok(NULL,"")); return; } if(strstr(linha,"bit,")){ - this->alarms[this->numAlarms-1].type=modBusType_bit; + alarms[alarmes-1].type=modBusType_bit; pChar = strtok(linha,","); //Parametro1 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); + alarms[alarmes-1].addrModbusRead = atoi(pChar); //Parametro2 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); + alarms[alarmes-1].funcModbusRead = atoi(pChar); //Parametro3 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); + alarms[alarmes-1].regModbusRead = atoi(pChar); //Parametro4 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um + alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um //Parametro5 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].value[0]= (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um + alarms[alarmes-1].value[0]= (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um return; } if(strstr(linha,"float,")) { - this->alarms[this->numAlarms-1].type=modBusType_float; + alarms[alarmes-1].type=modBusType_float; strtok(linha,","); //Parametro 1; pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); + alarms[alarmes-1].addrModbusRead = atoi(pChar); //Parametro 2; pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); + alarms[alarmes-1].funcModbusRead = atoi(pChar); //Parametro 3; pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); + alarms[alarmes-1].regModbusRead = atoi(pChar); //Parametro 4; pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um + alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um //Parametro 5; pChar = strtok(NULL,"\r\n"); //floatVar = diversos::stringToFloat(pChar,100); floatVar = atof(pChar); - this->floatToBin(1,&floatVar,&this->alarms[this->numAlarms-1].value[0]); + floatToBin(1,&floatVar,&alarms[alarmes-1].value[0]); return; } if(strstr(linha,"uint32_t,")) { - this->alarms[this->numAlarms-1].type=modBusType_uint32_t; + alarms[alarmes-1].type=modBusType_uint32_t; //sscanf(linha,"max:uint32_t,%lu,%lu,%lu,%lu",&aux1,&aux2,&aux3,&aux4); strtok(linha,","); //parametro1 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); + alarms[alarmes-1].addrModbusRead = atoi(pChar); //parametro2 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); + alarms[alarmes-1].funcModbusRead = atoi(pChar); //parametro3 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); + alarms[alarmes-1].regModbusRead = atoi(pChar); //Parametro 4; pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um + alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um //parametro5 pChar = strtok(NULL,"\r\n"); aux32_t = atoi(pChar); - this->uint32_t_ToBin(1,&aux32_t,&this->alarms[this->numAlarms-1].value[0]); + uint32_t_ToBin(1,&aux32_t,&alarms[alarmes-1].value[0]); return; } if(strstr(linha,"uint16_t,")) { - this->alarms[this->numAlarms-1].type=modBusType_uint16_t; + alarms[alarmes-1].type=modBusType_uint16_t; strtok(linha,","); //parametro1 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); + alarms[alarmes-1].addrModbusRead = atoi(pChar); //parametro2 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); + alarms[alarmes-1].funcModbusRead = atoi(pChar); //parametro3 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); + alarms[alarmes-1].regModbusRead = atoi(pChar); //Parametro 4; pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um + alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um //parametro5 pChar = strtok(NULL,"\r\n"); aux16_t = atoi(pChar); - this->uint16_t_ToBin(1,&aux16_t,&this->alarms[this->numAlarms-1].value[0]); + uint16_t_ToBin(1,&aux16_t,&alarms[alarmes-1].value[0]); return; } if(strstr(linha,"int32_t,")) { - this->alarms[this->numAlarms-1].type=modBusType_int32_t; + alarms[alarmes-1].type=modBusType_int32_t; //sscanf(linha,"max:uint32_t,%lu,%lu,%lu,%lu",&aux1,&aux2,&aux3,&aux4); strtok(linha,","); //parametro1 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); + alarms[alarmes-1].addrModbusRead = atoi(pChar); //parametro2 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); + alarms[alarmes-1].funcModbusRead = atoi(pChar); //parametro3 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].regModbusRead = atol(pChar); + alarms[alarmes-1].regModbusRead = atol(pChar); //Parametro 4; pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um + alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um //parametro5 pChar = strtok(NULL,"\r\n"); aux32_t = atol(pChar); - this->uint32_t_ToBin(1,&aux32_t,&this->alarms[this->numAlarms-1].value[0]); + uint32_t_ToBin(1,&aux32_t,&alarms[alarmes-1].value[0]); return; } if(strstr(linha,"int16_t,")) { - this->alarms[this->numAlarms-1].type=modBusType_int16_t; + alarms[alarmes-1].type=modBusType_int16_t; strtok(linha,","); //parametro1 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); + alarms[alarmes-1].addrModbusRead = atoi(pChar); //parametro2 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); + alarms[alarmes-1].funcModbusRead = atoi(pChar); //parametro3 pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); + alarms[alarmes-1].regModbusRead = atoi(pChar); //Parametro 4; pChar = strtok(NULL,","); - this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um + alarms[alarmes-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um //parametro5 pChar = strtok(NULL,"\r\n"); aux16_t = atoi(pChar); - this->uint16_t_ToBin(1,&aux16_t,&this->alarms[this->numAlarms-1].value[0]); + uint16_t_ToBin(1,&aux16_t,&alarms[alarmes-1].value[0]); return; } } -void device::setSchedule(char *linha) +void setSchedule(char *linha) { //float floatVar; //uint32_t aux32_t; @@ -1153,40 +1158,40 @@ if(strstr(linha,"scheduleSet")) { //Inicialização de contadores de schedules //É necessário inicializar estes dados para quando a máquina de schedules rodar ter um inicio correto. - this->schedules[this->numSchedules].actSent = false; + schedules[numSchedules].actSent = false; //Inicialização de contadores de schedules - this->numSchedules++; + numSchedules++; } if(strstr(linha,"idSchedule:")) { strtok(linha,":"); pChar = strtok(NULL,""); - this->schedules[this->numSchedules-1].id = atoi(pChar); + schedules[numSchedules-1].id = atoi(pChar); } if(strstr(linha,"dayTime:")) { strtok(linha,":"); pChar = strtok(NULL,","); - this->schedules[this->numSchedules-1].weekday = atoi(pChar); + schedules[numSchedules-1].weekday = atoi(pChar); pChar = strtok(NULL,""); - this->schedules[this->numSchedules-1].minute = atoi(pChar); + schedules[numSchedules-1].minute = atoi(pChar); } if(strstr(linha,"idAct:")) { strtok(linha,":"); pChar = strtok(NULL,""); - this->schedules[this->numSchedules-1].idAct = atoi(pChar); + schedules[numSchedules-1].idAct = atoi(pChar); } /*pc.printf("Debugando erro em scheduleSet.\r\n"); pc.printf("Linha <%s>\r\n",linha); - pc.printf("actSent = %lu",this->schedules[this->numSchedules-1].actSent); - pc.printf("idSchedule = %lu",this->schedules[this->numSchedules-1].id); - pc.printf("dayTime = %lu",this->schedules[this->numSchedules-1].weekday); - pc.printf("idAct = %lu",this->schedules[this->numSchedules-1].idAct); + pc.printf("actSent = %lu",schedules[numSchedules-1].actSent); + pc.printf("idSchedule = %lu",schedules[numSchedules-1].id); + pc.printf("dayTime = %lu",schedules[numSchedules-1].weekday); + pc.printf("idAct = %lu",schedules[numSchedules-1].idAct); pc.printf("Debugando erro em scheduleSet.\r\n");*/ } -void device::setScheduleException(char *linha) +void setScheduleException(char *linha) { //float floatVar; //uint32_t aux32_t; @@ -1194,22 +1199,22 @@ char *pChar; if(strstr(linha,"scheduleExceptionSet")) { //Inicialização de contadores de schedulesExceptions - this->numScheduleExceptions++; + numScheduleExceptions++; } if(strstr(linha,"idSchedule:")) { strtok(linha,":"); - this->scheduleExceptions[this->numScheduleExceptions-1].id_schedule = atoi(strtok(NULL,"\r\n")); + scheduleExceptions[numScheduleExceptions-1].id_schedule = atoi(strtok(NULL,"\r\n")); } if(strstr(linha,"timestamp:")) { strtok(linha,":"); pChar = strtok(NULL,","); - this->scheduleExceptions[this->numScheduleExceptions-1].timestamp = atoi(pChar); + scheduleExceptions[numScheduleExceptions-1].timestamp = atoi(pChar); } } -/*void device::writeReadingsToSD(){ +/*void writeReadingsToSD(){ //Esta função faz a inserção de todos os dados no arquivo armazenamento. uint16_t readingIndex=0; @@ -1230,59 +1235,59 @@ //O timestamp é posto nos 4 primeiros bytes (segundos) sdCard::insereDadosBank(&sdCard::armazenamento,(char *)&seconds_uint32_t,4); - for(readingIndex = 0;readingIndex < this->numReadings;readingIndex++){ - switch(this->readings[readingIndex].type){ + for(readingIndex = 0;readingIndex < numReadings;readingIndex++){ + switch(readings[readingIndex].type){ case modBusType_float: - floatBuffer = (float*) malloc(this->readings[readingIndex].numRegs*sizeof(float)); + floatBuffer = (float*) malloc(readings[readingIndex].numRegs*sizeof(float)); modBusMaster1::readFloat( - this->readings[readingIndex].addr, - this->readings[readingIndex].func, - this->readings[readingIndex].reg, - this->readings[readingIndex].numRegs, + readings[readingIndex].addr, + readings[readingIndex].func, + readings[readingIndex].reg, + readings[readingIndex].numRegs, floatBuffer ); //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); - sdCard::insereDadosBank((char *)floatBuffer,sizeof(float)*this->readings[readingIndex].numRegs); + sdCard::insereDadosBank((char *)floatBuffer,sizeof(float)*readings[readingIndex].numRegs); free(floatBuffer); break; case modBusType_uint32_t: - uint32_tBuffer = (uint32_t*) malloc(this->readings[readingIndex].numRegs*sizeof(uint32_t)); + uint32_tBuffer = (uint32_t*) malloc(readings[readingIndex].numRegs*sizeof(uint32_t)); modBusMaster1::readRegister32BIT( - this->readings[readingIndex].addr, - this->readings[readingIndex].func, - this->readings[readingIndex].reg, - this->readings[readingIndex].numRegs, + readings[readingIndex].addr, + readings[readingIndex].func, + readings[readingIndex].reg, + readings[readingIndex].numRegs, uint32_tBuffer ); //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); - sdCard::insereDadosBank((char *)uint32_tBuffer,sizeof(uint32_t)*this->readings[readingIndex].numRegs); + sdCard::insereDadosBank((char *)uint32_tBuffer,sizeof(uint32_t)*readings[readingIndex].numRegs); free(uint32_tBuffer); break; case modBusType_uint16_t: - uint16_tBuffer = (uint16_t*) malloc(this->readings[readingIndex].numRegs*sizeof(uint16_t)); + uint16_tBuffer = (uint16_t*) malloc(readings[readingIndex].numRegs*sizeof(uint16_t)); modBusMaster1::readRegister16BIT( - this->readings[readingIndex].addr, - this->readings[readingIndex].func, - this->readings[readingIndex].reg, - this->readings[readingIndex].numRegs, + readings[readingIndex].addr, + readings[readingIndex].func, + readings[readingIndex].reg, + readings[readingIndex].numRegs, uint16_tBuffer ); //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); - sdCard::insereDadosBank((char *)uint16_tBuffer,sizeof(uint16_t)*this->readings[readingIndex].numRegs); + sdCard::insereDadosBank((char *)uint16_tBuffer,sizeof(uint16_t)*readings[readingIndex].numRegs); free(uint16_tBuffer); break; case modBusType_bit: - boolBuffer = (bool*) malloc(this->readings[readingIndex].numRegs*sizeof(bool)); + boolBuffer = (bool*) malloc(readings[readingIndex].numRegs*sizeof(bool)); modBusMaster1::readCoils( - this->readings[readingIndex].addr, - this->readings[readingIndex].reg, - this->readings[readingIndex].numRegs, + readings[readingIndex].addr, + readings[readingIndex].reg, + readings[readingIndex].numRegs, boolBuffer ); //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); - sdCard::insereDadosBank((char *)boolBuffer,sizeof(bool)*this->readings[readingIndex].numRegs); + sdCard::insereDadosBank((char *)boolBuffer,sizeof(bool)*readings[readingIndex].numRegs); free(boolBuffer); break; @@ -1291,7 +1296,7 @@ sdCard::insereDadosBank("}log",4); }*/ -uint8_t device::execAct(uint32_t index){ +uint8_t execAct(uint32_t index){ //Primeiro preciso decidir que tipo de act estou lidando para tanto preciso pegar a ACT inteira. //Busca no arquivo //Abre arquivo @@ -1520,7 +1525,7 @@ while(pChar!=NULL){ pc.printf("Lido Alarm Set %lu - <%lu>.\r\n",indexAlarms,atoi(pChar)); if(atoi(pChar)){ - dispositivos[0]->alarmOnOff(atoi(pChar),1); + alarmOnOff(atoi(pChar),1); } pChar = strtok(NULL,","); } @@ -1529,7 +1534,7 @@ while(pChar!=NULL){ pc.printf("Lido Alarm Reset %lu - <%lu>.\r\n",indexAlarms,atoi(pChar)); if(atoi(pChar)){ - dispositivos[0]->alarmOnOff(atoi(pChar),0); + alarmOnOff(atoi(pChar),0); } pChar = strtok(NULL,","); } @@ -1538,7 +1543,7 @@ return commandExecOk; } -void device::alarmOnOff(uint32_t idAlarm,bool state){ +void alarmOnOff(uint32_t idAlarm,bool state){ uint16_t alarmIndex; char indexBusca[30]; char alarmString[40]; @@ -1547,8 +1552,8 @@ char charState; for(alarmIndex = 0; alarmIndex < alarmes; alarmIndex++){ - if(this->alarms[alarmIndex].id == idAlarm){ - this->alarms[alarmIndex].on = state; + if(alarms[alarmIndex].id == idAlarm){ + alarms[alarmIndex].on = state; } } @@ -1581,7 +1586,7 @@ sdCard::fechaArquivo(&sdCard::devices); } -void device::writeReadingsToSD(){ +void writeReadingsToSD(){ //Buffering before insertData //Esta função faz a inserção de todos os dados no arquivo armazenamento. uint16_t readingIndex=0; @@ -1613,76 +1618,76 @@ //O timestamp é posto nos 4 primeiros bytes (segundos) sdCardBuf.fill(seconds_char,5); - for(readingIndex = 0;readingIndex < this->numReadings;readingIndex++){ - switch(this->readings[readingIndex].type){ + for(readingIndex = 0;readingIndex < numReadings;readingIndex++){ + switch(readings[readingIndex].type){ case modBusType_float: - if(this->readings[readingIndex].addr != enderecoControladoraVirtual){ - //Busca externa via Modbus - floatBuffer = (float*) malloc(((this->readings[readingIndex].numRegs*sizeof(float))+1)); //Alocando mais um byte para status - ((char *)floatBuffer)[(this->readings[readingIndex].numRegs*sizeof(float))] = modBusMaster1::readFloat( - this->readings[readingIndex].addr, - this->readings[readingIndex].func, - this->readings[readingIndex].reg, - this->readings[readingIndex].numRegs, + if(readings[readingIndex].addr != enderecoControladoraVirtual){ + //Busca fora do drome via Modbus + floatBuffer = (float*) malloc(((readings[readingIndex].numRegs*sizeof(float))+1)); //Alocando mais um byte para status + ((char *)floatBuffer)[(readings[readingIndex].numRegs*sizeof(float))] = modBusMaster1::readFloat( + readings[readingIndex].addr, + readings[readingIndex].func, + readings[readingIndex].reg, + readings[readingIndex].numRegs, floatBuffer ); }else{ //Busca Interna floatBuffer = (float*) malloc(sizeof(float)); - if((this->readings[readingIndex].reg<100)&&(this->readings[readingIndex].reg < num_temperatureSensors)){//Registradores das entradas de temperatura + if((readings[readingIndex].reg<100)&&(readings[readingIndex].reg < num_temperatureSensors)){//Registradores das entradas de temperatura //Populando floatBuffer com a temperaturas - floatBuffer[0] = temperaturas[this->readings[readingIndex].reg]; + floatBuffer[0] = temperaturas[readings[readingIndex].reg]; } - if((this->readings[readingIndex].reg>=100)&&(this->readings[readingIndex].reg<104)){ + if((readings[readingIndex].reg>=100)&&(readings[readingIndex].reg<104)){ //Registradores das entradas analógicas - floatBuffer[0] = aiFiltrada[this->readings[readingIndex].reg - 100]; + floatBuffer[0] = aiFiltrada[readings[readingIndex].reg - 100]; } - this->readings[readingIndex].numRegs = 1; + readings[readingIndex].numRegs = 1; } //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); - sdCardBuf.fill((char *)floatBuffer,((sizeof(float)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status + sdCardBuf.fill((char *)floatBuffer,((sizeof(float)*readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status free(floatBuffer); break; case modBusType_uint32_t: case modBusType_int32_t: - if(this->readings[readingIndex].addr != enderecoControladoraVirtual){ - uint32_tBuffer = (uint32_t*) malloc((this->readings[readingIndex].numRegs*sizeof(uint32_t))+1); //Alocando mais um byte para status - ((char *)uint32_tBuffer)[(this->readings[readingIndex].numRegs*sizeof(uint32_t))] = modBusMaster1::readRegister32BIT(//Ponho o status no ultimo byte - this->readings[readingIndex].addr, - this->readings[readingIndex].func, - this->readings[readingIndex].reg, - this->readings[readingIndex].numRegs, + if(readings[readingIndex].addr != enderecoControladoraVirtual){ + uint32_tBuffer = (uint32_t*) malloc((readings[readingIndex].numRegs*sizeof(uint32_t))+1); //Alocando mais um byte para status + ((char *)uint32_tBuffer)[(readings[readingIndex].numRegs*sizeof(uint32_t))] = modBusMaster1::readRegister32BIT(//Ponho o status no ultimo byte + readings[readingIndex].addr, + readings[readingIndex].func, + readings[readingIndex].reg, + readings[readingIndex].numRegs, uint32_tBuffer ); //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); }else{ uint32_tBuffer = (uint32_t*) malloc(sizeof(uint32_t)+1); //Alocando mais um byte para status - uint32_tBuffer[0] = pulsosEDs[this->readings[readingIndex].reg]; - this->readings[readingIndex].numRegs = 1; - pulsosEDs[this->readings[readingIndex].reg] = 0; + uint32_tBuffer[0] = pulsosEDs[readings[readingIndex].reg]; + readings[readingIndex].numRegs = 1; + pulsosEDs[readings[readingIndex].reg] = 0; } - sdCardBuf.fill((char *)uint32_tBuffer,((sizeof(uint32_t)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status + sdCardBuf.fill((char *)uint32_tBuffer,((sizeof(uint32_t)*readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status free(uint32_tBuffer); break; case modBusType_uint16_t: case modBusType_int16_t: - if(this->readings[readingIndex].addr!=enderecoControladoraVirtual){ - uint16_tBuffer = (uint16_t*) malloc(((this->readings[readingIndex].numRegs*sizeof(uint16_t))+1));//Alocando mais um byte para status - ((char *)uint16_tBuffer)[(this->readings[readingIndex].numRegs*sizeof(uint16_t))] = modBusMaster1::readRegister16BIT(// - this->readings[readingIndex].addr, - this->readings[readingIndex].func, - this->readings[readingIndex].reg, - this->readings[readingIndex].numRegs, + if(readings[readingIndex].addr!=enderecoControladoraVirtual){ + uint16_tBuffer = (uint16_t*) malloc(((readings[readingIndex].numRegs*sizeof(uint16_t))+1));//Alocando mais um byte para status + ((char *)uint16_tBuffer)[(readings[readingIndex].numRegs*sizeof(uint16_t))] = modBusMaster1::readRegister16BIT(// + readings[readingIndex].addr, + readings[readingIndex].func, + readings[readingIndex].reg, + readings[readingIndex].numRegs, uint16_tBuffer ); //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); - sdCardBuf.fill((char *)uint16_tBuffer,((sizeof(uint16_t)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status + sdCardBuf.fill((char *)uint16_tBuffer,((sizeof(uint16_t)*readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status free(uint16_tBuffer); }else{ - if((this->readings[readingIndex].reg>0)&&((this->readings[readingIndex].reg + this->readings[readingIndex].numRegs)<=4)){ - sdCardBuf.fill((char *)&aiFiltrada[this->readings[readingIndex].reg-1],sizeof(uint16_t)*this->readings[readingIndex].numRegs); + if((readings[readingIndex].reg>0)&&((readings[readingIndex].reg + readings[readingIndex].numRegs)<=4)){ + sdCardBuf.fill((char *)&aiFiltrada[readings[readingIndex].reg-1],sizeof(uint16_t)*readings[readingIndex].numRegs); seconds_char[4]=0; sdCardBuf.fill((char *)&seconds_char[4],1); } @@ -1691,19 +1696,19 @@ case modBusType_bit: boolBuffer = (bool*) malloc(sizeof(bool)+1);//Alocando mais um byte para status - if(this->readings[readingIndex].addr != enderecoControladoraVirtual){ - ((char *)boolBuffer)[(this->readings[readingIndex].numRegs*sizeof(bool))] = modBusMaster1::readCoils( - this->readings[readingIndex].addr, - this->readings[readingIndex].reg, - this->readings[readingIndex].numRegs, + if(readings[readingIndex].addr != enderecoControladoraVirtual){ + ((char *)boolBuffer)[(readings[readingIndex].numRegs*sizeof(bool))] = modBusMaster1::readCoils( + readings[readingIndex].addr, + readings[readingIndex].reg, + readings[readingIndex].numRegs, boolBuffer ); }else{ - boolBuffer[0] = entradasDigitais[this->readings[readingIndex].reg]; + boolBuffer[0] = entradasDigitais[readings[readingIndex].reg]; ((char *)boolBuffer)[1]=0; } //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); - sdCardBuf.fill((char *)boolBuffer,((sizeof(bool)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status + sdCardBuf.fill((char *)boolBuffer,((sizeof(bool)*readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status free(boolBuffer); break; @@ -1722,11 +1727,7 @@ -device::device() -{ - this->numAlarms=0; - this->numReadings=0; -} + int criaDevices(FILE *devicesCfg) { @@ -1742,17 +1743,12 @@ linhas++; } - printf("Contei %u linha(s), %u device(s).\n",linhas,devices); - - //Criando vetor de objetos dispositivos; - - dispositivos = (device **) malloc(sizeof(device)*devices); - - for(i=0; i<devices; i++) { - dispositivos[i] = new device(); - //dispositivos[i]->id=i; - } - + printf("Contei %u linha(s), %u device(s).\n",linhas,devices); + + alarmes=0; + numSchedules=0; + numScheduleExceptions=0; + numReadings=0; rewind(devicesCfg); return 1; } @@ -1760,71 +1756,22 @@ int configuraDevices(FILE *devicesCfg) { - uint16_t linhas=0; + void *pointer; //uint16_t leituras = 0; char linha[maxCaractereLeLinha]; uint8_t estadoConfiguracao=estadoConfiguracao_idle; - int8_t dispositivoEmConfiguracao = -1; - - while(fgets(linha,maxCaractereLeLinha,devicesCfg)){ - if(strstr(linha,"alarmSet")) { - //printf("Criando alarme no dispositivo de indice %u.\n",dispositivoEmConfiguracao); - alarmes++; - estadoConfiguracao = estadoConfiguracao_alarmSet; - } - - if(strstr(linha,"readingsEnd")) { - //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); - estadoConfiguracao = estadoConfiguracao_idle; - leituras++; - } - - if(estadoConfiguracao == estadoConfiguracao_readingsSet){ - leituras++; - } - - if(strstr(linha,"readingsSet")) { - //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); - estadoConfiguracao = estadoConfiguracao_readingsSet; - } + alarmes = 0; + bool condicaoDeSaida = false; - if(strstr(linha,"scheduleSet")) { - //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); - qtdSchedules++; - estadoConfiguracao = estadoConfiguracao_scheduleSet; - } - - if(strstr(linha,"scheduleExceptionsSet")) { - //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); - qtdScheduleExceptions++; - estadoConfiguracao = estadoConfiguracao_scheduleSet; - } - - linhas++; - } - printf("Contei %u linha(s), configurei %u dispositivo(s), instalei %u alarme(s), %u leitura(s) e %u schedule(s) nos dispositivo(s).\n",linhas,devices,alarmes,leituras,qtdSchedules); - //Reservar memoria aqui. - - //if(resizeArray(this->numReadings, this->numReadings+1, (char **)&this->readings,sizeof(reading))) { - //(char *) malloc(sizeUnit*novo); - dispositivos[0]->readings = (reading*) malloc(sizeof(reading)*leituras); - dispositivos[0]->alarms = (alarm*) malloc(sizeof(alarm)*alarmes); - dispositivos[0]->schedules = (schedule*) malloc(sizeof(schedule)*qtdSchedules); - dispositivos[0]->scheduleExceptions = (scheduleException*) malloc(sizeof(scheduleException)*qtdScheduleExceptions); - - //Retornando ao inicio o arquivo - rewind(devicesCfg); - - while(fgets(linha,maxCaractereLeLinha,devicesCfg)) { - if(strstr(linha,"deviceCreate\\")) { - dispositivoEmConfiguracao++; + while(fgets(linha,maxCaractereLeLinha,devicesCfg)&&(!condicaoDeSaida)){ + if(strstr(linha,"deviceCreate\\")) { //printf("Configurando dispositivo de indice %u.\n",dispositivoEmConfiguracao); estadoConfiguracao = estadoConfiguracao_creatingDevice; } if(strstr(linha,"alarmSet")) { //printf("Criando alarme no dispositivo de indice %u.\n",dispositivoEmConfiguracao); - alarmes++; + //alarmes++; estadoConfiguracao = estadoConfiguracao_alarmSet; } @@ -1857,34 +1804,34 @@ if(strstr(linha,">")) { //printf("Fechando dispositivo de indice %u.\n",dispositivoEmConfiguracao); + condicaoDeSaida = true; estadoConfiguracao = estadoConfiguracao_idle; } switch(estadoConfiguracao) { case estadoConfiguracao_creatingDevice: /*if(strstr(linha,"id:")) { - sscanf(linha,"id:%u",&dispositivos[dispositivoEmConfiguracao]->id); + sscanf(linha,"id:%u",&id); }*/ break; case estadoConfiguracao_alarmSet: //Inclue linha nos alarmes do dispositivo em configuração - dispositivos[dispositivoEmConfiguracao]->setAlarm(linha); + setAlarm(linha); break; case estadoConfiguracao_readingsSet: //Inclue linha nas leituras do dispositivo em configuração - dispositivos[dispositivoEmConfiguracao]->setReading(linha); + setReading(linha); break; case estadoConfiguracao_scheduleSet: //Inclue linha nos agendamentos do dispositivo em configuração - dispositivos[dispositivoEmConfiguracao]->setSchedule(linha); + setSchedule(linha); break; case estadoConfiguracao_scheduleExceptionSet: //Inclue linha nas excessões dos agendamentos do dispositivo em configuração - dispositivos[dispositivoEmConfiguracao]->setScheduleException(linha); + setScheduleException(linha); break; } - } - + } return 1; } @@ -1892,117 +1839,115 @@ void testaTudoDevices() { - int i; int j; - - for(i=0; i<devices; i++) { - printf("Dispositivo %u tem %u alarmes sendo eles:\n",i,dispositivos[i]->numAlarms); + + printf("O dispositivo tem %u alarmes sendo eles:\n",alarmes); - for(j=0; j<dispositivos[i]->numAlarms; j++) { - printf("Alarme %u:\n",j); - printf("\tId <%lu>.\n",dispositivos[i]->alarms[j].id); - printf("\tSeconds <%lu>.\n",dispositivos[i]->alarms[j].seconds); - printf("\tAddr %u, func %u, reg %lu.\n",dispositivos[i]->alarms[j].addrModbusRead,dispositivos[i]->alarms[j].funcModbusRead,dispositivos[i]->alarms[j].regModbusRead); - if(dispositivos[i]->alarms[j].type==modBusType_float) { - float value; - dispositivos[i]->floatToBin(0,&value,&dispositivos[i]->alarms[j].value[0]); - if(dispositivos[i]->alarms[j].max){ - printf("\tFloat max <%f>.\n",value); - } - else{printf("\tFloat min <%f>.\n",value);} - } - - if(dispositivos[i]->alarms[j].type==modBusType_uint32_t) { - uint32_t value; - dispositivos[i]->uint32_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]); - if(dispositivos[i]->alarms[j].max){ - printf("\tuint32_t max <%lu>.\n",value); - } - else{printf("\tuint32_t min <%lu>.\n",value);} + for(j=0; j<alarmes; j++) { + printf("Alarme %u:\n",j); + printf("\tId <%lu>.\n",alarms[j].id); + printf("\tSeconds <%lu>.\n",alarms[j].seconds); + printf("\tAddr %u, func %u, reg %lu.\n",alarms[j].addrModbusRead,alarms[j].funcModbusRead,alarms[j].regModbusRead); + if(alarms[j].type==modBusType_float) { + float value; + floatToBin(0,&value,&alarms[j].value[0]); + if(alarms[j].max){ + printf("\tFloat max <%f>.\n",value); } - - if(dispositivos[i]->alarms[j].type==modBusType_uint16_t) { - uint16_t value; - dispositivos[i]->uint16_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]); - if(dispositivos[i]->alarms[j].max){ - printf("\tuint16_t max <%lu>.\n",value); - } - else{printf("\tuint16_t min <%lu>.\n",value);} - } - - if(dispositivos[i]->alarms[j].type==modBusType_int32_t) { - uint32_t value; - dispositivos[i]->uint32_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]); - if(dispositivos[i]->alarms[j].max){ - printf("\tint32_t max <%ld>.\n",(int32_t)value); - } - else{printf("\tint32_t min <%ld>.\n",(int32_t)value);} + else{printf("\tFloat min <%f>.\n",value);} + } + + if(alarms[j].type==modBusType_uint32_t) { + uint32_t value; + uint32_t_ToBin(0,&value,&alarms[j].value[0]); + if(alarms[j].max){ + printf("\tuint32_t max <%lu>.\n",value); } - - if(dispositivos[i]->alarms[j].type==modBusType_int16_t) { - uint16_t value; - dispositivos[i]->uint16_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]); - if(dispositivos[i]->alarms[j].max){ - printf("\tint16_t max <%ld>.\n",(int16_t)value); - } - else{printf("\tint16_t min <%ld>.\n",(int16_t)value);} - } - - if(dispositivos[i]->alarms[j].type==modBusType_bit) { - uint8_t value; - value=dispositivos[i]->alarms[j].value[0]; - if(dispositivos[i]->alarms[j].max){ - printf("\tBIT max <%lu>.\n",value); - } - else{printf("\tBIT min <%lu>.\n",value);} - } - - printf("\tact:%lu\n",dispositivos[i]->alarms[j].idAct); + else{printf("\tuint32_t min <%lu>.\n",value);} + } + + if(alarms[j].type==modBusType_uint16_t) { + uint16_t value; + uint16_t_ToBin(0,&value,&alarms[j].value[0]); + if(alarms[j].max){ + printf("\tuint16_t max <%lu>.\n",value); + } + else{printf("\tuint16_t min <%lu>.\n",value);} + } + + if(alarms[j].type==modBusType_int32_t) { + uint32_t value; + uint32_t_ToBin(0,&value,&alarms[j].value[0]); + if(alarms[j].max){ + printf("\tint32_t max <%ld>.\n",(int32_t)value); + } + else{printf("\tint32_t min <%ld>.\n",(int32_t)value);} } - printf("Dispositivo %u tem %u schedules sendo eles:\n",i,dispositivos[i]->numSchedules); - for(j=0; j<dispositivos[i]->numSchedules; j++) { - printf("Schedule %u:\n",j); - printf("\tId <%lu>.\n",dispositivos[i]->schedules[j].id); - printf("\tweekday <%lu>.\n",dispositivos[i]->schedules[j].weekday); - printf("\tminute <%lu>.\n",dispositivos[i]->schedules[j].minute); - printf("\tidAct <%lu>.\n",dispositivos[i]->schedules[j].idAct); - } + if(alarms[j].type==modBusType_int16_t) { + uint16_t value; + uint16_t_ToBin(0,&value,&alarms[j].value[0]); + if(alarms[j].max){ + printf("\tint16_t max <%ld>.\n",(int16_t)value); + } + else{printf("\tint16_t min <%ld>.\n",(int16_t)value);} + } + + if(alarms[j].type==modBusType_bit) { + uint8_t value; + value=alarms[j].value[0]; + if(alarms[j].max){ + printf("\tBIT max <%lu>.\n",value); + } + else{printf("\tBIT min <%lu>.\n",value);} + } - printf("Dispositivo %u tem %u scheduleExceptions sendo eles:\n",i,dispositivos[i]->numScheduleExceptions); - for(j=0; j<dispositivos[i]->numScheduleExceptions; j++) { - printf("scheduleException %u:\n",j); - printf("\tid_schedule <%lu>.\n",dispositivos[i]->scheduleExceptions[j].id_schedule); - printf("\ttimestamp <%lu>.\n",dispositivos[i]->scheduleExceptions[j].timestamp); - } + printf("\tact:%lu\n",alarms[j].idAct); + } + + printf("Dispositivo %u tem %u schedules sendo eles:\n",numSchedules); + for(j=0; j<numSchedules; j++) { + printf("Schedule %u:\n",j); + printf("\tId <%lu>.\n",schedules[j].id); + printf("\tweekday <%lu>.\n",schedules[j].weekday); + printf("\tminute <%lu>.\n",schedules[j].minute); + printf("\tidAct <%lu>.\n",schedules[j].idAct); + } + + printf("O dispositivo tem %u scheduleExceptions sendo eles:\n",numScheduleExceptions); + for(j=0; j<numScheduleExceptions; j++) { + printf("scheduleException %u:\n",j); + printf("\tid_schedule <%lu>.\n",scheduleExceptions[j].id_schedule); + printf("\ttimestamp <%lu>.\n",scheduleExceptions[j].timestamp); + } - printf("Dispositivo %u tem %u leituras sendo elas:\n",i,dispositivos[i]->numReadings); - for(j=0; j<dispositivos[i]->numReadings; j++) { - printf("Leitura %u:",j); - switch(dispositivos[i]->readings[j].type) { - case modBusType_float: - printf("Tipo float "); - break; - case modBusType_uint32_t: - printf("Tipo uint32_t "); - break; - case modBusType_uint16_t: - printf("Tipo uint16_t "); - break; - case modBusType_int32_t: - printf("Tipo int32_t "); - break; - case modBusType_int16_t: - printf("Tipo int16_t "); - break; - case modBusType_uint8_t: - printf("Tipo uint8_t "); - break; - case modBusType_bit: - printf("Tipo bool "); - break; - } - printf("addr %u, func %u, reg %u, numRegs %u.\n",dispositivos[i]->readings[j].addr,dispositivos[i]->readings[j].func,dispositivos[i]->readings[j].reg,dispositivos[i]->readings[j].numRegs); + printf("O dispositivo tem %u leituras sendo elas:\n",numReadings); + for(j=0; j<numReadings; j++) { + printf("Leitura %u:",j); + switch(readings[j].type) { + case modBusType_float: + printf("Tipo float "); + break; + case modBusType_uint32_t: + printf("Tipo uint32_t "); + break; + case modBusType_uint16_t: + printf("Tipo uint16_t "); + break; + case modBusType_int32_t: + printf("Tipo int32_t "); + break; + case modBusType_int16_t: + printf("Tipo int16_t "); + break; + case modBusType_uint8_t: + printf("Tipo uint8_t "); + break; + case modBusType_bit: + printf("Tipo bool "); + break; } + printf("addr %u, func %u, reg %u, numRegs %u.\n",readings[j].addr,readings[j].func,readings[j].reg,readings[j].numRegs); } } +