teste de publish
Diff: devices.cpp
- Revision:
- 5:8efd609dea7d
- Parent:
- 4:3f92979d1ffb
- Child:
- 6:f20c0e094294
--- a/devices.cpp Wed Jun 07 19:38:14 2017 +0000 +++ b/devices.cpp Tue Jul 11 11:55:44 2017 +0000 @@ -144,11 +144,6 @@ } } - - - - - //Funcao restaurada void device::verifyAlarms(){ //Inicio da verificação @@ -159,38 +154,228 @@ bool leitura_bit,value_bit; bool alarmFound; char aux[30]; - - for(alarmIndex = 0;alarmIndex < this->numAlarms;alarmIndex++){ - alarmFound=this->alarms[alarmIndex].alarmFound; - switch(this->alarms[alarmIndex].type){ - case modBusType_float: - //Montar float! - this->floatToBin(0,&value_float,&this->alarms[alarmIndex].value[0]); + + for(alarmIndex = 0;alarmIndex < this->numAlarms;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){ + case modBusType_float: + //Montar float! + this->floatToBin(0,&value_float,&this->alarms[alarmIndex].value[0]); + + if(this->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, + 1, + &leitura_float + ); + }else{ + //leitura_float = temperaturas[this->alarms[alarmIndex].regModbusRead]; + //Busca Interna + if(this->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((this->alarms[alarmIndex].regModbusRead>100)&&(this->alarms[alarmIndex].regModbusRead<104)){//Registradores das entradas analógicas + leitura_float = aiFiltrada[this->alarms[alarmIndex].regModbusRead - 100]; + } + } + + if(this->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; + alarmFound = true; + } + }else{ + if(this->alarms[alarmIndex].secAlarm==1){ + this->alarms[alarmIndex].secAlarm=0; + alarmFound = false; + }else{ + if(this->alarms[alarmIndex].secAlarm){ + if(alarmFound){ + this->alarms[alarmIndex].secAlarm--; + }else{ + this->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; + alarmFound = true; + } + }else{ + if(this->alarms[alarmIndex].secAlarm==1){ + this->alarms[alarmIndex].secAlarm=0; + alarmFound = false; + }else{ + if(this->alarms[alarmIndex].secAlarm){ + if(alarmFound){ + this->alarms[alarmIndex].secAlarm--; + }else{ + this->alarms[alarmIndex].secAlarm=0; + } + } + } + } + } + break; + case modBusType_uint32_t: + //Montar uint32_t! + this->uint32_t_ToBin(0,&value_uint32_t,&this->alarms[alarmIndex].value[0]); + + if(this->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, + 1, + &leitura_uint32_t + ); + }else{ + leitura_uint32_t = pulsosEDs[this->alarms[alarmIndex].regModbusRead]; + } + + if(this->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; + alarmFound = true; + pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0; + } + }else{ + if(this->alarms[alarmIndex].secAlarm==1){ + this->alarms[alarmIndex].secAlarm=0; + alarmFound = false; + }else{ + if(this->alarms[alarmIndex].secAlarm){ + if(alarmFound){ + this->alarms[alarmIndex].secAlarm--; + }else{ + this->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; + alarmFound = true; + pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0; + } + }else{ + if(this->alarms[alarmIndex].secAlarm==1){ + this->alarms[alarmIndex].secAlarm=0; + alarmFound = false; + }else{ + if(this->alarms[alarmIndex].secAlarm){ + if(alarmFound){ + this->alarms[alarmIndex].secAlarm--; + }else{ + this->alarms[alarmIndex].secAlarm=0; + } + } + } + } + } + break; - if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; - modBusMaster1::readFloat( - this->alarms[alarmIndex].addrModbusRead, - this->alarms[alarmIndex].funcModbusRead, + case modBusType_uint16_t: + //Montar uint16_t! + this->uint16_t_ToBin(0,&value_uint16_t,&this->alarms[alarmIndex].value[0]); + + if(this->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, + 1, + &leitura_uint16_t + ); + }else{ + if((this->alarms[alarmIndex].regModbusRead > 0) &&(this->alarms[alarmIndex].regModbusRead<4)){ + leitura_uint16_t = aiFiltrada[this->alarms[alarmIndex].regModbusRead-1]; + } + } + + if(this->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; + alarmFound = true; + } + }else{ + if(this->alarms[alarmIndex].secAlarm==1){ + this->alarms[alarmIndex].secAlarm=0; + alarmFound = false; + }else{ + if(this->alarms[alarmIndex].secAlarm){ + if(alarmFound){ + this->alarms[alarmIndex].secAlarm--; + }else{ + this->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; + alarmFound = true; + } + }else{ + if(this->alarms[alarmIndex].secAlarm==1){ + this->alarms[alarmIndex].secAlarm=0; + alarmFound = false; + }else{ + if(this->alarms[alarmIndex].secAlarm){ + if(alarmFound){ + this->alarms[alarmIndex].secAlarm--; + }else{ + this->alarms[alarmIndex].secAlarm=0; + } + } + } + } + } + break; + + 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 + + //Bloco de leitura da variável modBus; + modBusMaster1::readCoils( + this->alarms[alarmIndex].addrModbusRead, this->alarms[alarmIndex].regModbusRead, 1, - &leitura_float - ); - }else{ - //leitura_float = temperaturas[this->alarms[alarmIndex].regModbusRead]; - //Busca Interna - if(this->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((this->alarms[alarmIndex].regModbusRead>100)&&(this->alarms[alarmIndex].regModbusRead<104)){//Registradores das entradas analógicas - leitura_float = aiFiltrada[this->alarms[alarmIndex].regModbusRead - 100]; - } - } - - if(this->alarms[alarmIndex].max!=0){ - if(leitura_float > value_float){ + &leitura_bit + ); + + 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; @@ -209,282 +394,149 @@ } } } - } - }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; - alarmFound = true; - } - }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; - alarmFound = false; - }else{ - if(this->alarms[alarmIndex].secAlarm){ - if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; - }else{ - this->alarms[alarmIndex].secAlarm=0; - } - } - } - } - } - break; - case modBusType_uint32_t: - //Montar uint32_t! - this->uint32_t_ToBin(0,&value_uint32_t,&this->alarms[alarmIndex].value[0]); - - if(this->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, - 1, - &leitura_uint32_t - ); - }else{ - leitura_uint32_t = pulsosEDs[this->alarms[alarmIndex].regModbusRead]; - } - - if(this->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; - alarmFound = true; - pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0; - } - }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; - alarmFound = false; - }else{ - if(this->alarms[alarmIndex].secAlarm){ - if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; - }else{ - this->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; - alarmFound = true; - pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0; - } - }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; - alarmFound = false; - }else{ - if(this->alarms[alarmIndex].secAlarm){ - if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; - }else{ - this->alarms[alarmIndex].secAlarm=0; - } - } - } - } - } - break; + } + break; - case modBusType_uint16_t: - //Montar uint16_t! - this->uint16_t_ToBin(0,&value_uint16_t,&this->alarms[alarmIndex].value[0]); - - if(this->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, - 1, - &leitura_uint16_t - ); - }else{ - if((this->alarms[alarmIndex].regModbusRead > 0) &&(this->alarms[alarmIndex].regModbusRead<4)){ - leitura_uint16_t = aiFiltrada[this->alarms[alarmIndex].regModbusRead-1]; - } - } + } - if(this->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; - alarmFound = true; - } - }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; - alarmFound = false; - }else{ - if(this->alarms[alarmIndex].secAlarm){ - if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; - }else{ - this->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; - alarmFound = true; - } - }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; - alarmFound = false; - }else{ - if(this->alarms[alarmIndex].secAlarm){ - if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; - }else{ - this->alarms[alarmIndex].secAlarm=0; - } - } - } - } - } - break; - - 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 - - //Bloco de leitura da variável modBus; - modBusMaster1::readCoils( - this->alarms[alarmIndex].addrModbusRead, - this->alarms[alarmIndex].regModbusRead, - 1, - &leitura_bit - ); - - 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; - alarmFound = true; - } - }else{ - if(this->alarms[alarmIndex].secAlarm==1){ - this->alarms[alarmIndex].secAlarm=0; - alarmFound = false; - }else{ - if(this->alarms[alarmIndex].secAlarm){ - if(alarmFound){ - this->alarms[alarmIndex].secAlarm--; - }else{ - this->alarms[alarmIndex].secAlarm=0; - } - } - } - } - break; - + //Verifico se houve alarme não tratado + if(this->alarms[alarmIndex].alarmFound!=alarmFound){ + this->alarms[alarmIndex].alarmFound=alarmFound; + if(alarmFound){ + printf("Alarm FOUND! Id <%lu>.\n",this->alarms[alarmIndex].id); + sdCardBuf.fill("log{alarm:event:found;id:",25); + sprintf(aux,"%lu",this->alarms[alarmIndex].id); + sdCardBuf.fill(aux,strlen(aux)); + + sdCardBuf.fill(";timestamp:",11); + sprintf(aux,"%lu",time(NULL)); + sdCardBuf.fill(aux,strlen(aux)); + + sdCardBuf.fill(";reading:",9); + if(this->alarms[alarmIndex].type == modBusType_float){ + sprintf(aux,"%f",leitura_float); + }else if(this->alarms[alarmIndex].type == modBusType_uint32_t){ + sprintf(aux,"%lu",leitura_uint32_t); + }else if(this->alarms[alarmIndex].type == modBusType_uint16_t){ + sprintf(aux,"%lu",leitura_uint16_t); + }else if(this->alarms[alarmIndex].type == modBusType_bit){ + sprintf(aux,"%lu",leitura_bit); + } + sdCardBuf.fill(aux,strlen(aux)); + leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit + sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),leitura_uint16_t); + + boolExecAct=true; + act = this->alarms[alarmIndex].idAct; + }else{ + printf("Alarm RELEASE! id <%lu>.\n",this->alarms[alarmIndex].id); + sdCardBuf.fill("log{alarm:event:release;id:",27); + sprintf(aux,"%lu",this->alarms[alarmIndex].id); + sdCardBuf.fill(aux,strlen(aux)); + + sdCardBuf.fill(";timestamp:",11); + sprintf(aux,"%lu",time(NULL)); + sdCardBuf.fill(aux,strlen(aux)); + + leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit + sdCard::arquivoAberto(40000); + sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),leitura_uint16_t); + } + //atrasa o envio em 3 segundos para que possa ser gravado o registro do alarme. + enviaDadosPorAlarme=10; + } } - - //Verifico se houve alarme não tratado - if(this->alarms[alarmIndex].alarmFound!=alarmFound){ - this->alarms[alarmIndex].alarmFound=alarmFound; - if(alarmFound){ - printf("Alarm FOUND! Id <%lu>.\n",this->alarms[alarmIndex].id); - sdCardBuf.fill("log{alarm:event:found;id:",25); - sprintf(aux,"%lu",this->alarms[alarmIndex].id); - sdCardBuf.fill(aux,strlen(aux)); - - sdCardBuf.fill(";timestamp:",11); - sprintf(aux,"%lu",time(NULL)); - sdCardBuf.fill(aux,strlen(aux)); - - sdCardBuf.fill(";reading:",9); - if(this->alarms[alarmIndex].type == modBusType_float){ - sprintf(aux,"%f",leitura_float); - }else if(this->alarms[alarmIndex].type == modBusType_uint32_t){ - sprintf(aux,"%lu",leitura_uint32_t); - }else if(this->alarms[alarmIndex].type == modBusType_uint16_t){ - sprintf(aux,"%lu",leitura_uint16_t); - }else if(this->alarms[alarmIndex].type == modBusType_bit){ - sprintf(aux,"%lu",leitura_bit); - } - sdCardBuf.fill(aux,strlen(aux)); - leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit - if((!modemCom::status.emComunicacao)&&(!sdCard::arquivoAberto())){ - sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),leitura_uint16_t); - } - - this->execAct(this->alarms[alarmIndex].idAct); - }else{ - printf("Alarm RELEASE! id <%lu>.\n",this->alarms[alarmIndex].id); - sdCardBuf.fill("log{alarm:event:release;id:",27); - sprintf(aux,"%lu",this->alarms[alarmIndex].id); - sdCardBuf.fill(aux,strlen(aux)); - - sdCardBuf.fill(";timestamp:",11); - sprintf(aux,"%lu",time(NULL)); - sdCardBuf.fill(aux,strlen(aux)); - - leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit - if((!modemCom::status.emComunicacao)&&(!sdCard::arquivoAberto())){ - sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),leitura_uint16_t); - } - } - //atrasa o envio em 3 segundos para que possa ser gravado o registro do alarme. - enviaDadosPorAlarme=10; - } } } - void device::verifySchedules(){ - char stringTime[15]; + char stringTime[30]; char *weekdayString; - time_t seconds = time(NULL); + time_t seconds = time(NULL)+5; uint8_t hora; uint16_t minuto; uint8_t weekday=0; + uint16_t anoAtual=0; + uint16_t diaAtual=0; + uint16_t mesAtual=0; + uint16_t anoScheduleException=0; + uint16_t diaScheduleException=0; + uint16_t mesScheduleException=0; const char* weekdayTable[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; uint8_t indexSchedule; + uint8_t indexScheduleException; + bool exceptionDetected=false; - //Tue,19:09 - strftime(stringTime, 15, "%a,%R", localtime(&seconds)); + pc.printf("Verifing schedule.\r\n"); + strftime(stringTime, 30, "%a,%R,%y,%d,%m", localtime(&seconds)); + //pc.printf("VerifySchedule <%s>.\r\n",stringTime); + //Thu,20:48,17,2,7 weekdayString = strtok(stringTime,","); weekday = 0; while((weekday<10)&&(strstr(weekdayTable[weekday],weekdayString)==NULL)){ weekday++; } hora = atoi(strtok(NULL,":")); - minuto = atoi(strtok(NULL,"")); - minuto = (hora*60)+minuto; - for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){ - //this->schedules[indexSchedule].weekday (Sun bit0, Mon bit1, Tue bit2, etc...) - if(this->schedules[indexSchedule].weekday&(0x1<<weekday)){ + minuto = atoi(strtok(NULL,",")); + minuto = (hora*60)+minuto; + anoAtual = atoi(strtok(NULL,",")); + diaAtual = atoi(strtok(NULL,",")); + mesAtual = atoi(strtok(NULL,"")); + + //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)){ + //pc.printf("Passo 3.\r\n"); if(this->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){ + //pc.printf("Passo 5 .\r\n"); //Executa a ação; - this->execAct(this->schedules[indexSchedule].idAct); - this->schedules[indexSchedule].actSent = true; + for(indexScheduleException = 0;indexScheduleException<this->numScheduleExceptions;indexScheduleException++){ + //pc.printf("Passo 6.\r\n"); + if(this->schedules[indexSchedule].id == this->scheduleExceptions[indexScheduleException].id_schedule){ + //pc.printf("Passo 7.\r\n"); + seconds = this->scheduleExceptions[indexScheduleException].timestamp; + strftime(stringTime, 30, "%y,%d,%m", localtime(&seconds)); + anoScheduleException=atoi(strtok(stringTime,",")); + diaScheduleException=atoi(strtok(NULL,",")); + mesScheduleException=atoi(strtok(NULL,"")); + /* + pc.printf("\r\nDebug ScheduleException.\r\n"); + pc.printf("stringTime <%s>.\r\n",stringTime); + pc.printf("anoScheduleException %lu.",anoScheduleException); + pc.printf("diaScheduleException %lu.",diaScheduleException); + pc.printf("mesScheduleException %lu.",mesScheduleException); + pc.printf("anoAtual %lu.",anoAtual); + pc.printf("diaAtual %lu.",diaAtual); + pc.printf("mesAtual %lu.",mesAtual); + pc.printf("\r\n"); + */ + if( + ((anoScheduleException == anoAtual) || (anoScheduleException == 70)) + && + (diaAtual == diaScheduleException) + && + (mesAtual == mesScheduleException) + ) + { + pc.printf("ScheduleException detected.\r\n"); + exceptionDetected=true; + } + } + } + if(!exceptionDetected){ + this->execAct(this->schedules[indexSchedule].idAct); + this->schedules[indexSchedule].actSent = true; + } } }else{ this->schedules[indexSchedule].actSent = false; } - } + } } } @@ -736,34 +788,38 @@ //É 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; //Inicialização de contadores de alarme this->numAlarms++; } - if(strstr(linha,"id:")) { + if(strstr(linha,"idAlarm:")){ strtok(linha,":"); this->alarms[this->numAlarms-1].id = atoi(strtok(NULL,"\r\n")); } - - if(strstr(linha,"seconds:")) { - //sscanf(linha,"seconds:%lu",&this->alarms[this->numAlarms-1].seconds); + if(strstr(linha,"seconds:")) { strtok(linha,":"); pChar = strtok(NULL,"\r\n"); this->alarms[this->numAlarms-1].seconds = atoi(pChar); } - if(strstr(linha,"act:")) { + if(strstr(linha,"on:")) { + strtok(linha,":"); + pChar = strtok(NULL,"\r\n"); + this->alarms[this->numAlarms-1].on = atoi(pChar); + } + + if(strstr(linha,"idAct:")) { strtok(linha,":"); this->alarms[this->numAlarms-1].idAct = atoi(strtok(NULL,"")); return; - } - - - if(strstr(linha,"bit,")){ - this->alarms[this->numAlarms-1].type=modBusType_bit; - + } + + if(strstr(linha,"bit,")){ + this->alarms[this->numAlarms-1].type=modBusType_bit; + pChar = strtok(linha,","); //Parametro1 @@ -787,7 +843,6 @@ this->alarms[this->numAlarms-1].value[0]= (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um } - if(strstr(linha,"float,")) { this->alarms[this->numAlarms-1].type=modBusType_float; strtok(linha,","); @@ -812,25 +867,25 @@ pChar = strtok(NULL,"\r\n"); //floatVar = diversos::stringToFloat(pChar,100); floatVar = atof(pChar); - + this->floatToBin(1,&floatVar,&this->alarms[this->numAlarms-1].value[0]); } - + if(strstr(linha,"uint32_t,")) { this->alarms[this->numAlarms-1].type=modBusType_uint32_t; //sscanf(linha,"max:uint32_t,%lu,%lu,%lu,%lu",&aux1,&aux2,&aux3,&aux4); strtok(linha,","); //parametro1 - pChar = strtok(NULL,","); + pChar = strtok(NULL,","); this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); //parametro2 - pChar = strtok(NULL,","); + pChar = strtok(NULL,","); this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); //parametro3 - pChar = strtok(NULL,","); + pChar = strtok(NULL,","); this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); //Parametro 4; @@ -838,9 +893,9 @@ this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um //parametro5 - pChar = strtok(NULL,"\r\n"); + pChar = strtok(NULL,"\r\n"); aux32_t = atoi(pChar); - + this->uint32_t_ToBin(1,&aux32_t,&this->alarms[this->numAlarms-1].value[0]); } @@ -855,11 +910,11 @@ this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); //parametro2 - pChar = strtok(NULL,","); + pChar = strtok(NULL,","); this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); //parametro3 - pChar = strtok(NULL,","); + pChar = strtok(NULL,","); this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); //Parametro 4; @@ -867,13 +922,11 @@ this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um //parametro5 - pChar = strtok(NULL,"\r\n"); + pChar = strtok(NULL,"\r\n"); aux16_t = atoi(pChar); - + this->uint16_t_ToBin(1,&aux16_t,&this->alarms[this->numAlarms-1].value[0]); - } - - + } } void device::setSchedule(char *linha) @@ -881,8 +934,8 @@ //float floatVar; //uint32_t aux32_t; //uint16_t aux16_t; - char *pChar; - + char *pChar; + 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. @@ -891,25 +944,32 @@ this->numSchedules++; } - if(strstr(linha,"id:")) { + if(strstr(linha,"idSchedule:")) { strtok(linha,":"); - this->schedules[this->numSchedules-1].id = atoi(strtok(NULL,"\r\n")); + pChar = strtok(NULL,""); + this->schedules[this->numSchedules-1].id = atoi(pChar); } - - if(strstr(linha,"dayTime:")) { + if(strstr(linha,"dayTime:")) { strtok(linha,":"); pChar = strtok(NULL,","); this->schedules[this->numSchedules-1].weekday = atoi(pChar); - pChar = strtok(NULL,"\r\n"); + pChar = strtok(NULL,""); this->schedules[this->numSchedules-1].minute = atoi(pChar); } - - if(strstr(linha,"act:")) { + + if(strstr(linha,"idAct:")) { strtok(linha,":"); pChar = strtok(NULL,""); this->schedules[this->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("Debugando erro em scheduleSet.\r\n");*/ } void device::setScheduleException(char *linha) @@ -917,34 +977,32 @@ //float floatVar; //uint32_t aux32_t; //uint16_t aux16_t; - char *pChar; - - if(strstr(linha,"scheduleExceptionSet")) { + char *pChar; + if(strstr(linha,"scheduleExceptionSet")) { //Inicialização de contadores de schedulesExceptions this->numScheduleExceptions++; } - if(strstr(linha,"id_schedule:")) { + if(strstr(linha,"idSchedule:")) { strtok(linha,":"); this->scheduleExceptions[this->numScheduleExceptions-1].id_schedule = atoi(strtok(NULL,"\r\n")); } - - if(strstr(linha,"timestamp:")) { + if(strstr(linha,"timestamp:")) { strtok(linha,":"); pChar = strtok(NULL,","); - this->scheduleExceptions[this->numScheduleExceptions-1].timestamp = atoi(pChar); - } + this->scheduleExceptions[this->numScheduleExceptions-1].timestamp = atoi(pChar); + } } /*void device::writeReadingsToSD(){ //Esta função faz a inserção de todos os dados no arquivo armazenamento. - uint16_t readingIndex=0; - + uint16_t readingIndex=0; + float *floatBuffer; uint32_t *uint32_tBuffer; - uint16_t *uint16_tBuffer; - bool *boolBuffer; + uint16_t *uint16_tBuffer; + bool *boolBuffer; uint32_t seconds_uint32_t; //char registro[1024]; @@ -1025,9 +1083,13 @@ //Abre arquivo char actString[1024]; char indexBusca[15]; + char alarmsSetString[256]; + char alarmsResetString[256]; char *pTipoComando; uint8_t tipoComando; bool endOfFile = false; + uint8_t indexAlarms; + char *pChar; //Variáveis comuns; uint8_t param1; @@ -1036,14 +1098,20 @@ uint32_t param4; //printf("\r\n===================DEBUG(execAct)===================\r\n"); - sprintf(indexBusca,"actId:%lu,",index); + sprintf(indexBusca,"idAct:%lu;",index); + pc.printf("Lido <%s>.\r\n",indexBusca); //printf("Buscando por indexBusca<%s>.\r\n",indexBusca); if(sdCard::abreArquivo(&sdCard::devices,"r")){ do{ endOfFile = feof(sdCard::devices.fp); + if(!endOfFile){fgets(actString,1024,sdCard::devices.fp);} + }while((strstr(actString,"actList")==NULL)&&(endOfFile==0)); + + do{ + endOfFile = feof(sdCard::devices.fp); if(!endOfFile){fgets(actString,1024,sdCard::devices.fp);} - //printf("Linha ACT = <%s>.\r\nendOfFile = %u\r\n",actString,endOfFile); - }while((strstr(actString,indexBusca)==NULL)&&(endOfFile==0)); + //printf("Linha ACT = <%s>.\r\nendOfFile = %u\r\n",actString,endOfFile); + }while((strstr(actString,indexBusca)==NULL)&&(endOfFile==0)); sdCard::fechaArquivo(&sdCard::devices); //Vê se deu tudo certo, se não encontrou, retorna false @@ -1055,9 +1123,16 @@ //printf("\r\nPoint 1\r\n"); //Encontrado o tipo de comando reutilizando a string indexBusca; - strtok(actString,","); - pTipoComando = strtok(NULL,","); - //printf("\r\nPoint 2 comando <%s>\r\n",pTipoComando); + pChar = strtok(actString,";"); + pc.printf("1- <%s>.\r\n",pChar); + pChar = strtok(NULL,";"); + strcpy(alarmsSetString,pChar); + pChar = strtok(NULL,";"); + strcpy(alarmsResetString,pChar); + + pTipoComando = strtok(NULL,","); + + printf("\r\nPoint 2 comando <%s>\r\n",pTipoComando); if(strstr(pTipoComando,"float")){tipoComando = modBusType_float;} if(strstr(pTipoComando,"bit")){tipoComando = modBusType_bit;} if(strstr(pTipoComando,"uint8_t")){tipoComando = modBusType_uint8_t;} @@ -1078,8 +1153,7 @@ param3 = atoi(strtok(NULL,",")); //floatValueToWrite = diversos::stringToFloat(strtok(NULL,","),100); floatValueToWrite = atof(strtok(NULL,",")); - - + //Envio comando via Modbus modBusMaster1::writeFloat( param1, @@ -1168,7 +1242,9 @@ pTipoComando = strtok(NULL,"S");//Reaproveitando variável pTipoComando //printf("\r\nPacote IR <%s>.\r\n",pTipoComando); deserializaPacoteIR(pTipoComando); + //printf("Deserializado.\r\nInicio do envio.\r\n"); enviaComandoIR(param1,param2); + //printf("Fim do envio.\r\n"); break; case PWMCommandType: @@ -1211,10 +1287,69 @@ //printf("comando nao encontrado\r\n"); break; } + //Coletando dados do alarmSet e alarmReset + + pChar = strtok(alarmsSetString,","); + while(pChar!=NULL){ + pc.printf("Lido Alarm Set %lu - <%lu>.\r\n",indexAlarms,atoi(pChar)); + dispositivos[0]->alarmOnOff(atoi(pChar),1); + pChar = strtok(NULL,","); + } + + pChar = strtok(alarmsResetString,","); + while(pChar!=NULL){ + pc.printf("Lido Alarm Reset %lu - <%lu>.\r\n",indexAlarms,atoi(pChar)); + dispositivos[0]->alarmOnOff(atoi(pChar),0); + pChar = strtok(NULL,","); + } + //printf("\r\n===================DEBUG(execAct)===================\r\n"); return true; } +void device::alarmOnOff(uint32_t idAlarm,bool state){ + uint16_t alarmIndex; + char indexBusca[30]; + char alarmString[40]; + uint32_t seekPosition; + bool endOfFile; + char charState; + + for(alarmIndex = 0; alarmIndex < alarmes; alarmIndex++){ + if(this->alarms[alarmIndex].id == idAlarm){ + this->alarms[alarmIndex].on = state; + } + } + + if(!sdCard::abreArquivo(&sdCard::devices,"rb+")){return;} + + sprintf(indexBusca,"idAlarm:%lu",idAlarm); + do{ + endOfFile = feof(sdCard::devices.fp); + if(!endOfFile){fgets(alarmString,40,sdCard::devices.fp);} + }while((strstr(alarmString,indexBusca)==NULL)&&(endOfFile==0)); + + + do{ + endOfFile = feof(sdCard::devices.fp); + if(!endOfFile){fgets(alarmString,40,sdCard::devices.fp);} + }while((strstr(alarmString,"on:")==NULL)&&(endOfFile==0)); + + + if(endOfFile){ + sdCard::fechaArquivo(&sdCard::devices); + return; + } + + seekPosition = ftell(sdCard::devices.fp)-3; + fseek(sdCard::devices.fp,seekPosition,SEEK_SET); + if(state){charState='1';}else{charState='0';} + fputc(charState,sdCard::devices.fp); + //fprintf(sdCard::devices.fp,"%u",state); + fflush(sdCard::devices.fp); + sdCard::fechaArquivo(&sdCard::devices); +} + void device::writeReadingsToSD(){ //Buffering before insertData //Esta função faz a inserção de todos os dados no arquivo armazenamento. @@ -1339,10 +1474,9 @@ } } readingIndex = sdCardBuf.fill("}log",4); //Reaproveitando a variável de 16bit readingIndex - if((!modemCom::status.emComunicacao)&&(!sdCard::arquivoAberto())){ - //Insiro os dados se for possível. - sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),readingIndex); - } + sdCard::arquivoAberto(40000); + //Insiro os dados se for possível. + sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),readingIndex); } @@ -1401,12 +1535,7 @@ //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; - } + } if(estadoConfiguracao == estadoConfiguracao_readingsSet){ leituras++; @@ -1423,9 +1552,9 @@ estadoConfiguracao = estadoConfiguracao_scheduleSet; } - if(strstr(linha,"scheduleExceptionsEnd")) { + if(strstr(linha,"scheduleExceptionsSet")) { //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); - qtdSchedules++; + qtdScheduleExceptions++; estadoConfiguracao = estadoConfiguracao_scheduleSet; } @@ -1439,6 +1568,7 @@ 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); @@ -1477,12 +1607,7 @@ //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); qtdSchedules++; estadoConfiguracao = estadoConfiguracao_scheduleSet; - } - - if(strstr(linha,"schedulesEnd")) { - //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); - estadoConfiguracao = estadoConfiguracao_idle; - } + } if(strstr(linha,">")) { //printf("Fechando dispositivo de indice %u.\n",dispositivoEmConfiguracao); @@ -1596,22 +1721,17 @@ break; case modBusType_uint32_t: printf("Tipo uint32_t "); - break; - + break; case modBusType_uint16_t: printf("Tipo uint16_t "); - break; - + break; case modBusType_uint8_t: printf("Tipo uint8_t "); - break; - + break; case modBusType_bit: printf("Tipo bool "); - break; - + 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); } }