teste de publish

Committer:
brunofgc
Date:
Tue May 01 03:27:47 2018 +0000
Revision:
17:c046e4b07ded
Parent:
16:1b43a57ee676
Child:
18:66fa6fcba212
Nova vers?o em teste. (Inclu?ndo a capacidade de ler int16 e 32 com sinal)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
brunofgc 0:8c59a494a451 1 #include "devices.h"
brunofgc 0:8c59a494a451 2
brunofgc 0:8c59a494a451 3 enum {
brunofgc 0:8c59a494a451 4 estadoConfiguracao_idle = 0,
brunofgc 0:8c59a494a451 5 estadoConfiguracao_creatingDevice,
brunofgc 0:8c59a494a451 6 estadoConfiguracao_alarmSet,
brunofgc 0:8c59a494a451 7 estadoConfiguracao_readingsSet,
brunofgc 4:3f92979d1ffb 8 estadoConfiguracao_scheduleSet,
brunofgc 4:3f92979d1ffb 9 estadoConfiguracao_scheduleExceptionSet
brunofgc 0:8c59a494a451 10 };
brunofgc 0:8c59a494a451 11
brunofgc 1:3f9e5b86d9e4 12 /*
brunofgc 1:3f9e5b86d9e4 13 Antigo
brunofgc 0:8c59a494a451 14 uint8_t resizeArray(uint32_t atual, uint32_t novo, char **array,uint16_t sizeUnit)
brunofgc 0:8c59a494a451 15 {
brunofgc 0:8c59a494a451 16 uint32_t i;
brunofgc 0:8c59a494a451 17 char *p = (char *) malloc(sizeUnit*novo);
brunofgc 0:8c59a494a451 18 char *aux;
brunofgc 0:8c59a494a451 19
brunofgc 0:8c59a494a451 20 if(p==NULL) {
brunofgc 0:8c59a494a451 21 return 0;
brunofgc 0:8c59a494a451 22 }
brunofgc 0:8c59a494a451 23
brunofgc 0:8c59a494a451 24 aux = *array;
brunofgc 0:8c59a494a451 25
brunofgc 0:8c59a494a451 26 if(atual) {
brunofgc 0:8c59a494a451 27 //So sigo em frente se tinha algo dentro deste array
brunofgc 0:8c59a494a451 28 for(i=0; i<(novo*sizeUnit); i++) {
brunofgc 0:8c59a494a451 29 p[i]=aux[i];
brunofgc 0:8c59a494a451 30 }
brunofgc 0:8c59a494a451 31 free(aux);
brunofgc 0:8c59a494a451 32 }
brunofgc 0:8c59a494a451 33
brunofgc 0:8c59a494a451 34 *array = p;
brunofgc 0:8c59a494a451 35 return 1;
brunofgc 0:8c59a494a451 36 }
brunofgc 1:3f9e5b86d9e4 37 */
brunofgc 1:3f9e5b86d9e4 38
brunofgc 2:ca3723da6668 39 /*uint8_t resizeArray(uint32_t atual, uint32_t novo, char **array,uint16_t sizeUnit)
brunofgc 1:3f9e5b86d9e4 40 {
brunofgc 1:3f9e5b86d9e4 41 uint32_t i;
brunofgc 1:3f9e5b86d9e4 42 char *p = (char *) malloc(sizeUnit*novo);
brunofgc 1:3f9e5b86d9e4 43
brunofgc 1:3f9e5b86d9e4 44 if(p==NULL) {
brunofgc 1:3f9e5b86d9e4 45 return 0;
brunofgc 1:3f9e5b86d9e4 46 }
brunofgc 1:3f9e5b86d9e4 47
brunofgc 1:3f9e5b86d9e4 48 if(atual) {
brunofgc 1:3f9e5b86d9e4 49 //So sigo em frente se tinha algo dentro deste array
brunofgc 1:3f9e5b86d9e4 50 for(i=0; i<(novo*sizeUnit); i++) {
brunofgc 1:3f9e5b86d9e4 51 p[i]=*array[i];
brunofgc 1:3f9e5b86d9e4 52 }
brunofgc 1:3f9e5b86d9e4 53 free(*array);
brunofgc 1:3f9e5b86d9e4 54 }
brunofgc 1:3f9e5b86d9e4 55
brunofgc 1:3f9e5b86d9e4 56 *array = p;
brunofgc 1:3f9e5b86d9e4 57 return 1;
brunofgc 2:ca3723da6668 58 }*/
brunofgc 0:8c59a494a451 59
brunofgc 0:8c59a494a451 60 void writeReadingsToSD(){
brunofgc 0:8c59a494a451 61 int i;
brunofgc 0:8c59a494a451 62 for(i=0;i<devices;i++){
brunofgc 0:8c59a494a451 63 dispositivos[i]->writeReadingsToSD();
brunofgc 0:8c59a494a451 64 }
brunofgc 0:8c59a494a451 65 }
brunofgc 0:8c59a494a451 66
brunofgc 0:8c59a494a451 67 void verifyAlarms(){
brunofgc 0:8c59a494a451 68 int i;
brunofgc 0:8c59a494a451 69 for(i=0;i<devices;i++){
brunofgc 0:8c59a494a451 70 dispositivos[i]->verifyAlarms();
brunofgc 0:8c59a494a451 71 }
brunofgc 0:8c59a494a451 72 }
brunofgc 0:8c59a494a451 73
brunofgc 0:8c59a494a451 74 void verifySchedules(){
brunofgc 0:8c59a494a451 75 int i;
brunofgc 0:8c59a494a451 76 for(i=0;i<devices;i++){
brunofgc 0:8c59a494a451 77 dispositivos[i]->verifySchedules();
brunofgc 0:8c59a494a451 78 }
brunofgc 0:8c59a494a451 79 }
brunofgc 0:8c59a494a451 80
brunofgc 0:8c59a494a451 81 void device::floatToBin(uint8_t dir, float *v,uint8_t *p)
brunofgc 0:8c59a494a451 82 {
brunofgc 0:8c59a494a451 83 //Variáveis locais
brunofgc 0:8c59a494a451 84 union {
brunofgc 0:8c59a494a451 85 char c[4];
brunofgc 0:8c59a494a451 86 float v;
brunofgc 0:8c59a494a451 87 }u;
brunofgc 0:8c59a494a451 88 int i;
brunofgc 0:8c59a494a451 89
brunofgc 0:8c59a494a451 90 if(dir) {
brunofgc 0:8c59a494a451 91 u.v = *v;
brunofgc 0:8c59a494a451 92 for(i=0; i<4; i++) {
brunofgc 0:8c59a494a451 93 p[i]=u.c[3-i];
brunofgc 0:8c59a494a451 94 }
brunofgc 0:8c59a494a451 95 } else {
brunofgc 0:8c59a494a451 96 for(i=0; i<4; i++) {
brunofgc 0:8c59a494a451 97 u.c[3-i]=p[i];
brunofgc 0:8c59a494a451 98 }
brunofgc 0:8c59a494a451 99 *v = u.v;
brunofgc 0:8c59a494a451 100 }
brunofgc 0:8c59a494a451 101 }
brunofgc 0:8c59a494a451 102
brunofgc 0:8c59a494a451 103 void device::uint32_t_ToBin(uint8_t dir, uint32_t *v,uint8_t *p)
brunofgc 0:8c59a494a451 104 {
brunofgc 0:8c59a494a451 105 //Variáveis locais
brunofgc 0:8c59a494a451 106 union {
brunofgc 0:8c59a494a451 107 char c[4];
brunofgc 0:8c59a494a451 108 uint32_t v;
brunofgc 0:8c59a494a451 109 }u;
brunofgc 0:8c59a494a451 110 int i;
brunofgc 0:8c59a494a451 111
brunofgc 0:8c59a494a451 112 if(dir) {
brunofgc 0:8c59a494a451 113 u.v = *v;
brunofgc 0:8c59a494a451 114 for(i=0; i<4; i++) {
brunofgc 0:8c59a494a451 115 p[i]=u.c[3-i];
brunofgc 0:8c59a494a451 116 }
brunofgc 0:8c59a494a451 117 } else {
brunofgc 0:8c59a494a451 118 for(i=0; i<4; i++) {
brunofgc 0:8c59a494a451 119 u.c[3-i]=p[i];
brunofgc 0:8c59a494a451 120 }
brunofgc 0:8c59a494a451 121 *v = u.v;
brunofgc 0:8c59a494a451 122 }
brunofgc 0:8c59a494a451 123 }
brunofgc 0:8c59a494a451 124
brunofgc 0:8c59a494a451 125 void device::uint16_t_ToBin(uint8_t dir, uint16_t *v,uint8_t *p)
brunofgc 0:8c59a494a451 126 {
brunofgc 0:8c59a494a451 127 //Variáveis locais
brunofgc 0:8c59a494a451 128 union {
brunofgc 0:8c59a494a451 129 char c[2];
brunofgc 0:8c59a494a451 130 uint32_t v;
brunofgc 0:8c59a494a451 131 }u;
brunofgc 0:8c59a494a451 132 int i;
brunofgc 0:8c59a494a451 133
brunofgc 0:8c59a494a451 134 if(dir) {
brunofgc 0:8c59a494a451 135 u.v = *v;
brunofgc 0:8c59a494a451 136 for(i=0; i<2; i++) {
brunofgc 0:8c59a494a451 137 p[i]=u.c[1-i];
brunofgc 0:8c59a494a451 138 }
brunofgc 0:8c59a494a451 139 } else {
brunofgc 0:8c59a494a451 140 for(i=0; i<2; i++) {
brunofgc 0:8c59a494a451 141 u.c[1-i]=p[i];
brunofgc 0:8c59a494a451 142 }
brunofgc 0:8c59a494a451 143 *v = u.v;
brunofgc 0:8c59a494a451 144 }
brunofgc 0:8c59a494a451 145 }
brunofgc 0:8c59a494a451 146
brunofgc 1:3f9e5b86d9e4 147 //Funcao restaurada
brunofgc 0:8c59a494a451 148 void device::verifyAlarms(){
brunofgc 0:8c59a494a451 149 //Inicio da verificação
brunofgc 0:8c59a494a451 150 uint16_t alarmIndex;
brunofgc 0:8c59a494a451 151 float leitura_float,value_float;
brunofgc 0:8c59a494a451 152 uint32_t leitura_uint32_t,value_uint32_t;
brunofgc 0:8c59a494a451 153 uint16_t leitura_uint16_t,value_uint16_t;
brunofgc 0:8c59a494a451 154 bool leitura_bit,value_bit;
brunofgc 0:8c59a494a451 155 bool alarmFound;
brunofgc 0:8c59a494a451 156 char aux[30];
brunofgc 5:8efd609dea7d 157
brunofgc 5:8efd609dea7d 158 for(alarmIndex = 0;alarmIndex < this->numAlarms;alarmIndex++){
brunofgc 5:8efd609dea7d 159
brunofgc 5:8efd609dea7d 160 /*pc.printf("\r\nTeste Alarm, id<%lu>.\r\n",this->alarms[alarmIndex].id);
brunofgc 5:8efd609dea7d 161 pc.printf("ON<%lu>.\r\n",this->alarms[alarmIndex].on);
brunofgc 5:8efd609dea7d 162 pc.printf("secAlarm<%lu>.\r\n",this->alarms[alarmIndex].secAlarm);
brunofgc 5:8efd609dea7d 163 pc.printf("alarmFound<%lu>.\r\n",this->alarms[alarmIndex].alarmFound);*/
brunofgc 5:8efd609dea7d 164 if(this->alarms[alarmIndex].on){
brunofgc 5:8efd609dea7d 165 alarmFound=this->alarms[alarmIndex].alarmFound;
brunofgc 5:8efd609dea7d 166 switch(this->alarms[alarmIndex].type){
brunofgc 5:8efd609dea7d 167 case modBusType_float:
brunofgc 5:8efd609dea7d 168 //Montar float!
brunofgc 5:8efd609dea7d 169 this->floatToBin(0,&value_float,&this->alarms[alarmIndex].value[0]);
brunofgc 5:8efd609dea7d 170
brunofgc 5:8efd609dea7d 171 if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus;
brunofgc 5:8efd609dea7d 172 modBusMaster1::readFloat(
brunofgc 5:8efd609dea7d 173 this->alarms[alarmIndex].addrModbusRead,
brunofgc 5:8efd609dea7d 174 this->alarms[alarmIndex].funcModbusRead,
brunofgc 5:8efd609dea7d 175 this->alarms[alarmIndex].regModbusRead,
brunofgc 5:8efd609dea7d 176 1,
brunofgc 5:8efd609dea7d 177 &leitura_float
brunofgc 5:8efd609dea7d 178 );
brunofgc 5:8efd609dea7d 179 }else{
brunofgc 5:8efd609dea7d 180 //leitura_float = temperaturas[this->alarms[alarmIndex].regModbusRead];
brunofgc 5:8efd609dea7d 181 //Busca Interna
brunofgc 5:8efd609dea7d 182 if(this->alarms[alarmIndex].regModbusRead<100){//Registradores das entradas de temperatura
brunofgc 5:8efd609dea7d 183 //Populando floatBuffer com a temperaturas
brunofgc 5:8efd609dea7d 184 if(this->alarms[alarmIndex].regModbusRead < num_temperatureSensors){
brunofgc 5:8efd609dea7d 185 leitura_float = temperaturas[this->alarms[alarmIndex].regModbusRead];
brunofgc 5:8efd609dea7d 186 }
brunofgc 5:8efd609dea7d 187 }
brunofgc 11:013a2ab0c12e 188 if((this->alarms[alarmIndex].regModbusRead>99)&&(this->alarms[alarmIndex].regModbusRead<104)){//Registradores das entradas analógicas
brunofgc 5:8efd609dea7d 189 leitura_float = aiFiltrada[this->alarms[alarmIndex].regModbusRead - 100];
brunofgc 5:8efd609dea7d 190 }
brunofgc 5:8efd609dea7d 191 }
brunofgc 5:8efd609dea7d 192
brunofgc 5:8efd609dea7d 193 if(this->alarms[alarmIndex].max!=0){
brunofgc 5:8efd609dea7d 194 if(leitura_float > value_float){
brunofgc 5:8efd609dea7d 195 this->alarms[alarmIndex].secAlarm++;
brunofgc 5:8efd609dea7d 196 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 5:8efd609dea7d 197 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 5:8efd609dea7d 198 alarmFound = true;
brunofgc 5:8efd609dea7d 199 }
brunofgc 5:8efd609dea7d 200 }else{
brunofgc 5:8efd609dea7d 201 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 5:8efd609dea7d 202 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 203 alarmFound = false;
brunofgc 5:8efd609dea7d 204 }else{
brunofgc 5:8efd609dea7d 205 if(this->alarms[alarmIndex].secAlarm){
brunofgc 5:8efd609dea7d 206 if(alarmFound){
brunofgc 5:8efd609dea7d 207 this->alarms[alarmIndex].secAlarm--;
brunofgc 5:8efd609dea7d 208 }else{
brunofgc 5:8efd609dea7d 209 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 210 }
brunofgc 5:8efd609dea7d 211 }
brunofgc 5:8efd609dea7d 212 }
brunofgc 5:8efd609dea7d 213 }
brunofgc 5:8efd609dea7d 214 }else{
brunofgc 5:8efd609dea7d 215 if(leitura_float < value_float){
brunofgc 5:8efd609dea7d 216 this->alarms[alarmIndex].secAlarm++;
brunofgc 5:8efd609dea7d 217 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 5:8efd609dea7d 218 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 5:8efd609dea7d 219 alarmFound = true;
brunofgc 5:8efd609dea7d 220 }
brunofgc 5:8efd609dea7d 221 }else{
brunofgc 5:8efd609dea7d 222 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 5:8efd609dea7d 223 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 224 alarmFound = false;
brunofgc 5:8efd609dea7d 225 }else{
brunofgc 5:8efd609dea7d 226 if(this->alarms[alarmIndex].secAlarm){
brunofgc 5:8efd609dea7d 227 if(alarmFound){
brunofgc 5:8efd609dea7d 228 this->alarms[alarmIndex].secAlarm--;
brunofgc 5:8efd609dea7d 229 }else{
brunofgc 5:8efd609dea7d 230 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 231 }
brunofgc 5:8efd609dea7d 232 }
brunofgc 5:8efd609dea7d 233 }
brunofgc 5:8efd609dea7d 234 }
brunofgc 5:8efd609dea7d 235 }
brunofgc 5:8efd609dea7d 236 break;
brunofgc 5:8efd609dea7d 237 case modBusType_uint32_t:
brunofgc 5:8efd609dea7d 238 //Montar uint32_t!
brunofgc 5:8efd609dea7d 239 this->uint32_t_ToBin(0,&value_uint32_t,&this->alarms[alarmIndex].value[0]);
brunofgc 5:8efd609dea7d 240
brunofgc 5:8efd609dea7d 241 if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus;
brunofgc 5:8efd609dea7d 242 //Bloco de leitura da variável modBus;
brunofgc 5:8efd609dea7d 243 modBusMaster1::readRegister32BIT(
brunofgc 5:8efd609dea7d 244 this->alarms[alarmIndex].addrModbusRead,
brunofgc 5:8efd609dea7d 245 this->alarms[alarmIndex].funcModbusRead,
brunofgc 5:8efd609dea7d 246 this->alarms[alarmIndex].regModbusRead,
brunofgc 5:8efd609dea7d 247 1,
brunofgc 5:8efd609dea7d 248 &leitura_uint32_t
brunofgc 5:8efd609dea7d 249 );
brunofgc 5:8efd609dea7d 250 }else{
brunofgc 5:8efd609dea7d 251 leitura_uint32_t = pulsosEDs[this->alarms[alarmIndex].regModbusRead];
brunofgc 5:8efd609dea7d 252 }
brunofgc 5:8efd609dea7d 253
brunofgc 5:8efd609dea7d 254 if(this->alarms[alarmIndex].max!=0){
brunofgc 5:8efd609dea7d 255 if(leitura_uint32_t > value_uint32_t){
brunofgc 5:8efd609dea7d 256 this->alarms[alarmIndex].secAlarm++;
brunofgc 5:8efd609dea7d 257 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 5:8efd609dea7d 258 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 5:8efd609dea7d 259 alarmFound = true;
brunofgc 5:8efd609dea7d 260 pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0;
brunofgc 5:8efd609dea7d 261 }
brunofgc 5:8efd609dea7d 262 }else{
brunofgc 5:8efd609dea7d 263 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 5:8efd609dea7d 264 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 265 alarmFound = false;
brunofgc 5:8efd609dea7d 266 }else{
brunofgc 5:8efd609dea7d 267 if(this->alarms[alarmIndex].secAlarm){
brunofgc 5:8efd609dea7d 268 if(alarmFound){
brunofgc 5:8efd609dea7d 269 this->alarms[alarmIndex].secAlarm--;
brunofgc 5:8efd609dea7d 270 }else{
brunofgc 5:8efd609dea7d 271 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 272 }
brunofgc 5:8efd609dea7d 273 }
brunofgc 5:8efd609dea7d 274 }
brunofgc 5:8efd609dea7d 275 }
brunofgc 5:8efd609dea7d 276 }else{
brunofgc 5:8efd609dea7d 277 if(leitura_uint32_t < value_uint32_t){
brunofgc 5:8efd609dea7d 278 this->alarms[alarmIndex].secAlarm++;
brunofgc 5:8efd609dea7d 279 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 5:8efd609dea7d 280 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 5:8efd609dea7d 281 alarmFound = true;
brunofgc 5:8efd609dea7d 282 pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0;
brunofgc 5:8efd609dea7d 283 }
brunofgc 5:8efd609dea7d 284 }else{
brunofgc 5:8efd609dea7d 285 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 5:8efd609dea7d 286 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 287 alarmFound = false;
brunofgc 5:8efd609dea7d 288 }else{
brunofgc 5:8efd609dea7d 289 if(this->alarms[alarmIndex].secAlarm){
brunofgc 5:8efd609dea7d 290 if(alarmFound){
brunofgc 5:8efd609dea7d 291 this->alarms[alarmIndex].secAlarm--;
brunofgc 5:8efd609dea7d 292 }else{
brunofgc 5:8efd609dea7d 293 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 294 }
brunofgc 5:8efd609dea7d 295 }
brunofgc 5:8efd609dea7d 296 }
brunofgc 5:8efd609dea7d 297 }
brunofgc 5:8efd609dea7d 298 }
brunofgc 5:8efd609dea7d 299 break;
brunofgc 0:8c59a494a451 300
brunofgc 5:8efd609dea7d 301 case modBusType_uint16_t:
brunofgc 5:8efd609dea7d 302 //Montar uint16_t!
brunofgc 5:8efd609dea7d 303 this->uint16_t_ToBin(0,&value_uint16_t,&this->alarms[alarmIndex].value[0]);
brunofgc 5:8efd609dea7d 304
brunofgc 5:8efd609dea7d 305 if(this->alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){
brunofgc 5:8efd609dea7d 306 //Bloco de leitura da variável modBus;
brunofgc 5:8efd609dea7d 307 modBusMaster1::readRegister16BIT(
brunofgc 5:8efd609dea7d 308 this->alarms[alarmIndex].addrModbusRead,
brunofgc 5:8efd609dea7d 309 this->alarms[alarmIndex].funcModbusRead,
brunofgc 5:8efd609dea7d 310 this->alarms[alarmIndex].regModbusRead,
brunofgc 5:8efd609dea7d 311 1,
brunofgc 5:8efd609dea7d 312 &leitura_uint16_t
brunofgc 5:8efd609dea7d 313 );
brunofgc 5:8efd609dea7d 314 }else{
brunofgc 11:013a2ab0c12e 315 if(this->alarms[alarmIndex].regModbusRead<4){
brunofgc 11:013a2ab0c12e 316 leitura_uint16_t = aiFiltrada[this->alarms[alarmIndex].regModbusRead];
brunofgc 5:8efd609dea7d 317 }
brunofgc 5:8efd609dea7d 318 }
brunofgc 5:8efd609dea7d 319
brunofgc 5:8efd609dea7d 320 if(this->alarms[alarmIndex].max!=0){
brunofgc 5:8efd609dea7d 321 if(leitura_uint16_t > value_uint16_t){
brunofgc 5:8efd609dea7d 322 this->alarms[alarmIndex].secAlarm++;
brunofgc 5:8efd609dea7d 323 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 5:8efd609dea7d 324 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 5:8efd609dea7d 325 alarmFound = true;
brunofgc 5:8efd609dea7d 326 }
brunofgc 5:8efd609dea7d 327 }else{
brunofgc 5:8efd609dea7d 328 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 5:8efd609dea7d 329 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 330 alarmFound = false;
brunofgc 5:8efd609dea7d 331 }else{
brunofgc 5:8efd609dea7d 332 if(this->alarms[alarmIndex].secAlarm){
brunofgc 5:8efd609dea7d 333 if(alarmFound){
brunofgc 5:8efd609dea7d 334 this->alarms[alarmIndex].secAlarm--;
brunofgc 5:8efd609dea7d 335 }else{
brunofgc 5:8efd609dea7d 336 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 337 }
brunofgc 5:8efd609dea7d 338 }
brunofgc 5:8efd609dea7d 339 }
brunofgc 5:8efd609dea7d 340 }
brunofgc 5:8efd609dea7d 341 }else{
brunofgc 5:8efd609dea7d 342 if(leitura_uint16_t < value_uint16_t){
brunofgc 5:8efd609dea7d 343 this->alarms[alarmIndex].secAlarm++;
brunofgc 5:8efd609dea7d 344 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 5:8efd609dea7d 345 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 5:8efd609dea7d 346 alarmFound = true;
brunofgc 5:8efd609dea7d 347 }
brunofgc 5:8efd609dea7d 348 }else{
brunofgc 5:8efd609dea7d 349 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 5:8efd609dea7d 350 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 351 alarmFound = false;
brunofgc 5:8efd609dea7d 352 }else{
brunofgc 5:8efd609dea7d 353 if(this->alarms[alarmIndex].secAlarm){
brunofgc 5:8efd609dea7d 354 if(alarmFound){
brunofgc 5:8efd609dea7d 355 this->alarms[alarmIndex].secAlarm--;
brunofgc 5:8efd609dea7d 356 }else{
brunofgc 5:8efd609dea7d 357 this->alarms[alarmIndex].secAlarm=0;
brunofgc 5:8efd609dea7d 358 }
brunofgc 5:8efd609dea7d 359 }
brunofgc 5:8efd609dea7d 360 }
brunofgc 5:8efd609dea7d 361 }
brunofgc 5:8efd609dea7d 362 }
brunofgc 5:8efd609dea7d 363 break;
brunofgc 5:8efd609dea7d 364
brunofgc 17:c046e4b07ded 365 case modBusType_int32_t:
brunofgc 17:c046e4b07ded 366 //Montar uint32_t!
brunofgc 17:c046e4b07ded 367 this->uint32_t_ToBin(0,&value_uint32_t,&this->alarms[alarmIndex].value[0]);
brunofgc 17:c046e4b07ded 368
brunofgc 17:c046e4b07ded 369 if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus;
brunofgc 17:c046e4b07ded 370 //Bloco de leitura da variável modBus;
brunofgc 17:c046e4b07ded 371 modBusMaster1::readRegister32BIT(
brunofgc 17:c046e4b07ded 372 this->alarms[alarmIndex].addrModbusRead,
brunofgc 17:c046e4b07ded 373 this->alarms[alarmIndex].funcModbusRead,
brunofgc 17:c046e4b07ded 374 this->alarms[alarmIndex].regModbusRead,
brunofgc 17:c046e4b07ded 375 1,
brunofgc 17:c046e4b07ded 376 &leitura_uint32_t
brunofgc 17:c046e4b07ded 377 );
brunofgc 17:c046e4b07ded 378 }else{
brunofgc 17:c046e4b07ded 379 leitura_uint32_t = pulsosEDs[this->alarms[alarmIndex].regModbusRead];
brunofgc 17:c046e4b07ded 380 }
brunofgc 17:c046e4b07ded 381
brunofgc 17:c046e4b07ded 382 if(this->alarms[alarmIndex].max!=0){
brunofgc 17:c046e4b07ded 383 if(((int32_t)leitura_uint32_t) > ((int32_t)value_uint32_t)){
brunofgc 17:c046e4b07ded 384 this->alarms[alarmIndex].secAlarm++;
brunofgc 17:c046e4b07ded 385 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 17:c046e4b07ded 386 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 17:c046e4b07ded 387 alarmFound = true;
brunofgc 17:c046e4b07ded 388 pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0;
brunofgc 17:c046e4b07ded 389 }
brunofgc 17:c046e4b07ded 390 }else{
brunofgc 17:c046e4b07ded 391 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 17:c046e4b07ded 392 this->alarms[alarmIndex].secAlarm=0;
brunofgc 17:c046e4b07ded 393 alarmFound = false;
brunofgc 17:c046e4b07ded 394 }else{
brunofgc 17:c046e4b07ded 395 if(this->alarms[alarmIndex].secAlarm){
brunofgc 17:c046e4b07ded 396 if(alarmFound){
brunofgc 17:c046e4b07ded 397 this->alarms[alarmIndex].secAlarm--;
brunofgc 17:c046e4b07ded 398 }else{
brunofgc 17:c046e4b07ded 399 this->alarms[alarmIndex].secAlarm=0;
brunofgc 17:c046e4b07ded 400 }
brunofgc 17:c046e4b07ded 401 }
brunofgc 17:c046e4b07ded 402 }
brunofgc 17:c046e4b07ded 403 }
brunofgc 17:c046e4b07ded 404 }else{
brunofgc 17:c046e4b07ded 405 if(((int32_t)leitura_uint32_t) < ((int32_t)value_uint32_t)){
brunofgc 17:c046e4b07ded 406 this->alarms[alarmIndex].secAlarm++;
brunofgc 17:c046e4b07ded 407 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 17:c046e4b07ded 408 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 17:c046e4b07ded 409 alarmFound = true;
brunofgc 17:c046e4b07ded 410 pulsosEDs[this->alarms[alarmIndex].regModbusRead]=0;
brunofgc 17:c046e4b07ded 411 }
brunofgc 17:c046e4b07ded 412 }else{
brunofgc 17:c046e4b07ded 413 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 17:c046e4b07ded 414 this->alarms[alarmIndex].secAlarm=0;
brunofgc 17:c046e4b07ded 415 alarmFound = false;
brunofgc 17:c046e4b07ded 416 }else{
brunofgc 17:c046e4b07ded 417 if(this->alarms[alarmIndex].secAlarm){
brunofgc 17:c046e4b07ded 418 if(alarmFound){
brunofgc 17:c046e4b07ded 419 this->alarms[alarmIndex].secAlarm--;
brunofgc 17:c046e4b07ded 420 }else{
brunofgc 17:c046e4b07ded 421 this->alarms[alarmIndex].secAlarm=0;
brunofgc 17:c046e4b07ded 422 }
brunofgc 17:c046e4b07ded 423 }
brunofgc 17:c046e4b07ded 424 }
brunofgc 17:c046e4b07ded 425 }
brunofgc 17:c046e4b07ded 426 }
brunofgc 17:c046e4b07ded 427 break;
brunofgc 17:c046e4b07ded 428
brunofgc 17:c046e4b07ded 429 case modBusType_int16_t:
brunofgc 17:c046e4b07ded 430 //Montar uint16_t!
brunofgc 17:c046e4b07ded 431 this->uint16_t_ToBin(0,&value_uint16_t,&this->alarms[alarmIndex].value[0]);
brunofgc 17:c046e4b07ded 432
brunofgc 17:c046e4b07ded 433 if(this->alarms[alarmIndex].addrModbusRead!=enderecoControladoraVirtual){
brunofgc 17:c046e4b07ded 434 //Bloco de leitura da variável modBus;
brunofgc 17:c046e4b07ded 435 modBusMaster1::readRegister16BIT(
brunofgc 17:c046e4b07ded 436 this->alarms[alarmIndex].addrModbusRead,
brunofgc 17:c046e4b07ded 437 this->alarms[alarmIndex].funcModbusRead,
brunofgc 17:c046e4b07ded 438 this->alarms[alarmIndex].regModbusRead,
brunofgc 17:c046e4b07ded 439 1,
brunofgc 17:c046e4b07ded 440 &leitura_uint16_t
brunofgc 17:c046e4b07ded 441 );
brunofgc 17:c046e4b07ded 442 }else{
brunofgc 17:c046e4b07ded 443 if(this->alarms[alarmIndex].regModbusRead<4){
brunofgc 17:c046e4b07ded 444 leitura_uint16_t = aiFiltrada[this->alarms[alarmIndex].regModbusRead];
brunofgc 17:c046e4b07ded 445 }
brunofgc 17:c046e4b07ded 446 }
brunofgc 17:c046e4b07ded 447
brunofgc 17:c046e4b07ded 448 if(this->alarms[alarmIndex].max!=0){
brunofgc 17:c046e4b07ded 449 if(((int16_t)leitura_uint16_t) > ((int16_t)value_uint16_t)){
brunofgc 17:c046e4b07ded 450 this->alarms[alarmIndex].secAlarm++;
brunofgc 17:c046e4b07ded 451 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 17:c046e4b07ded 452 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 17:c046e4b07ded 453 alarmFound = true;
brunofgc 17:c046e4b07ded 454 }
brunofgc 17:c046e4b07ded 455 }else{
brunofgc 17:c046e4b07ded 456 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 17:c046e4b07ded 457 this->alarms[alarmIndex].secAlarm=0;
brunofgc 17:c046e4b07ded 458 alarmFound = false;
brunofgc 17:c046e4b07ded 459 }else{
brunofgc 17:c046e4b07ded 460 if(this->alarms[alarmIndex].secAlarm){
brunofgc 17:c046e4b07ded 461 if(alarmFound){
brunofgc 17:c046e4b07ded 462 this->alarms[alarmIndex].secAlarm--;
brunofgc 17:c046e4b07ded 463 }else{
brunofgc 17:c046e4b07ded 464 this->alarms[alarmIndex].secAlarm=0;
brunofgc 17:c046e4b07ded 465 }
brunofgc 17:c046e4b07ded 466 }
brunofgc 17:c046e4b07ded 467 }
brunofgc 17:c046e4b07ded 468 }
brunofgc 17:c046e4b07ded 469 }else{
brunofgc 17:c046e4b07ded 470 if(((int16_t)leitura_uint16_t) < ((int16_t)value_uint16_t)){
brunofgc 17:c046e4b07ded 471 this->alarms[alarmIndex].secAlarm++;
brunofgc 17:c046e4b07ded 472 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 17:c046e4b07ded 473 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 17:c046e4b07ded 474 alarmFound = true;
brunofgc 17:c046e4b07ded 475 }
brunofgc 17:c046e4b07ded 476 }else{
brunofgc 17:c046e4b07ded 477 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 17:c046e4b07ded 478 this->alarms[alarmIndex].secAlarm=0;
brunofgc 17:c046e4b07ded 479 alarmFound = false;
brunofgc 17:c046e4b07ded 480 }else{
brunofgc 17:c046e4b07ded 481 if(this->alarms[alarmIndex].secAlarm){
brunofgc 17:c046e4b07ded 482 if(alarmFound){
brunofgc 17:c046e4b07ded 483 this->alarms[alarmIndex].secAlarm--;
brunofgc 17:c046e4b07ded 484 }else{
brunofgc 17:c046e4b07ded 485 this->alarms[alarmIndex].secAlarm=0;
brunofgc 17:c046e4b07ded 486 }
brunofgc 17:c046e4b07ded 487 }
brunofgc 17:c046e4b07ded 488 }
brunofgc 17:c046e4b07ded 489 }
brunofgc 17:c046e4b07ded 490 }
brunofgc 17:c046e4b07ded 491 break;
brunofgc 17:c046e4b07ded 492
brunofgc 17:c046e4b07ded 493
brunofgc 6:f20c0e094294 494 case modBusType_bit:
brunofgc 5:8efd609dea7d 495 //Montar bit!
brunofgc 5:8efd609dea7d 496 //O valor de alarm para bit é o value[0]!!!
brunofgc 5:8efd609dea7d 497 value_bit = (this->alarms[alarmIndex].value[0] > 0); //Qualquer valor maior que zero da saída 1
brunofgc 5:8efd609dea7d 498
brunofgc 6:f20c0e094294 499 if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus;
brunofgc 6:f20c0e094294 500 //Bloco de leitura da variável modBus;
brunofgc 6:f20c0e094294 501 modBusMaster1::readCoils(
brunofgc 6:f20c0e094294 502 this->alarms[alarmIndex].addrModbusRead,
brunofgc 6:f20c0e094294 503 this->alarms[alarmIndex].regModbusRead,
brunofgc 6:f20c0e094294 504 1,
brunofgc 6:f20c0e094294 505 &leitura_bit
brunofgc 6:f20c0e094294 506 );
brunofgc 6:f20c0e094294 507 }else{
brunofgc 6:f20c0e094294 508 leitura_bit = entradasDigitais[this->alarms[alarmIndex].regModbusRead];
brunofgc 6:f20c0e094294 509 }
brunofgc 5:8efd609dea7d 510 if(leitura_bit == value_bit){
brunofgc 0:8c59a494a451 511 this->alarms[alarmIndex].secAlarm++;
brunofgc 0:8c59a494a451 512 if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){
brunofgc 0:8c59a494a451 513 this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds;
brunofgc 0:8c59a494a451 514 alarmFound = true;
brunofgc 0:8c59a494a451 515 }
brunofgc 0:8c59a494a451 516 }else{
brunofgc 0:8c59a494a451 517 if(this->alarms[alarmIndex].secAlarm==1){
brunofgc 0:8c59a494a451 518 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 519 alarmFound = false;
brunofgc 0:8c59a494a451 520 }else{
brunofgc 0:8c59a494a451 521 if(this->alarms[alarmIndex].secAlarm){
brunofgc 0:8c59a494a451 522 if(alarmFound){
brunofgc 0:8c59a494a451 523 this->alarms[alarmIndex].secAlarm--;
brunofgc 0:8c59a494a451 524 }else{
brunofgc 0:8c59a494a451 525 this->alarms[alarmIndex].secAlarm=0;
brunofgc 0:8c59a494a451 526 }
brunofgc 0:8c59a494a451 527 }
brunofgc 0:8c59a494a451 528 }
brunofgc 5:8efd609dea7d 529 }
brunofgc 5:8efd609dea7d 530 break;
brunofgc 0:8c59a494a451 531
brunofgc 5:8efd609dea7d 532 }
brunofgc 0:8c59a494a451 533
brunofgc 5:8efd609dea7d 534 //Verifico se houve alarme não tratado
brunofgc 5:8efd609dea7d 535 if(this->alarms[alarmIndex].alarmFound!=alarmFound){
brunofgc 5:8efd609dea7d 536 this->alarms[alarmIndex].alarmFound=alarmFound;
brunofgc 5:8efd609dea7d 537 if(alarmFound){
brunofgc 5:8efd609dea7d 538 printf("Alarm FOUND! Id <%lu>.\n",this->alarms[alarmIndex].id);
brunofgc 5:8efd609dea7d 539 sdCardBuf.fill("log{alarm:event:found;id:",25);
brunofgc 5:8efd609dea7d 540 sprintf(aux,"%lu",this->alarms[alarmIndex].id);
brunofgc 5:8efd609dea7d 541 sdCardBuf.fill(aux,strlen(aux));
brunofgc 5:8efd609dea7d 542
brunofgc 5:8efd609dea7d 543 sdCardBuf.fill(";timestamp:",11);
brunofgc 5:8efd609dea7d 544 sprintf(aux,"%lu",time(NULL));
brunofgc 5:8efd609dea7d 545 sdCardBuf.fill(aux,strlen(aux));
brunofgc 5:8efd609dea7d 546
brunofgc 5:8efd609dea7d 547 sdCardBuf.fill(";reading:",9);
brunofgc 5:8efd609dea7d 548 if(this->alarms[alarmIndex].type == modBusType_float){
brunofgc 5:8efd609dea7d 549 sprintf(aux,"%f",leitura_float);
brunofgc 5:8efd609dea7d 550 }else if(this->alarms[alarmIndex].type == modBusType_uint32_t){
brunofgc 5:8efd609dea7d 551 sprintf(aux,"%lu",leitura_uint32_t);
brunofgc 5:8efd609dea7d 552 }else if(this->alarms[alarmIndex].type == modBusType_uint16_t){
brunofgc 5:8efd609dea7d 553 sprintf(aux,"%lu",leitura_uint16_t);
brunofgc 17:c046e4b07ded 554 }else if(this->alarms[alarmIndex].type == modBusType_int32_t){
brunofgc 17:c046e4b07ded 555 sprintf(aux,"%l",leitura_uint32_t);
brunofgc 17:c046e4b07ded 556 }else if(this->alarms[alarmIndex].type == modBusType_int16_t){
brunofgc 17:c046e4b07ded 557 sprintf(aux,"%l",leitura_uint16_t);
brunofgc 5:8efd609dea7d 558 }else if(this->alarms[alarmIndex].type == modBusType_bit){
brunofgc 5:8efd609dea7d 559 sprintf(aux,"%lu",leitura_bit);
brunofgc 5:8efd609dea7d 560 }
brunofgc 5:8efd609dea7d 561 sdCardBuf.fill(aux,strlen(aux));
brunofgc 5:8efd609dea7d 562 leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit
brunofgc 8:25061210fcc9 563 sdCard::insereDadosBank(sdCardBuf.get(),leitura_uint16_t);
brunofgc 5:8efd609dea7d 564
brunofgc 5:8efd609dea7d 565 boolExecAct=true;
brunofgc 5:8efd609dea7d 566 act = this->alarms[alarmIndex].idAct;
brunofgc 5:8efd609dea7d 567 }else{
brunofgc 5:8efd609dea7d 568 printf("Alarm RELEASE! id <%lu>.\n",this->alarms[alarmIndex].id);
brunofgc 5:8efd609dea7d 569 sdCardBuf.fill("log{alarm:event:release;id:",27);
brunofgc 5:8efd609dea7d 570 sprintf(aux,"%lu",this->alarms[alarmIndex].id);
brunofgc 5:8efd609dea7d 571 sdCardBuf.fill(aux,strlen(aux));
brunofgc 5:8efd609dea7d 572
brunofgc 5:8efd609dea7d 573 sdCardBuf.fill(";timestamp:",11);
brunofgc 5:8efd609dea7d 574 sprintf(aux,"%lu",time(NULL));
brunofgc 5:8efd609dea7d 575 sdCardBuf.fill(aux,strlen(aux));
brunofgc 5:8efd609dea7d 576
brunofgc 5:8efd609dea7d 577 leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit
brunofgc 5:8efd609dea7d 578 sdCard::arquivoAberto(40000);
brunofgc 8:25061210fcc9 579 sdCard::insereDadosBank(sdCardBuf.get(),leitura_uint16_t);
brunofgc 5:8efd609dea7d 580 }
brunofgc 5:8efd609dea7d 581 //atrasa o envio em 3 segundos para que possa ser gravado o registro do alarme.
brunofgc 5:8efd609dea7d 582 enviaDadosPorAlarme=10;
brunofgc 5:8efd609dea7d 583 }
brunofgc 0:8c59a494a451 584 }
brunofgc 0:8c59a494a451 585 }
brunofgc 0:8c59a494a451 586 }
brunofgc 0:8c59a494a451 587
brunofgc 0:8c59a494a451 588 void device::verifySchedules(){
brunofgc 5:8efd609dea7d 589 char stringTime[30];
brunofgc 0:8c59a494a451 590 char *weekdayString;
brunofgc 5:8efd609dea7d 591 time_t seconds = time(NULL)+5;
brunofgc 0:8c59a494a451 592 uint8_t hora;
brunofgc 0:8c59a494a451 593 uint16_t minuto;
brunofgc 0:8c59a494a451 594 uint8_t weekday=0;
brunofgc 5:8efd609dea7d 595 uint16_t anoAtual=0;
brunofgc 5:8efd609dea7d 596 uint16_t diaAtual=0;
brunofgc 5:8efd609dea7d 597 uint16_t mesAtual=0;
brunofgc 5:8efd609dea7d 598 uint16_t anoScheduleException=0;
brunofgc 5:8efd609dea7d 599 uint16_t diaScheduleException=0;
brunofgc 5:8efd609dea7d 600 uint16_t mesScheduleException=0;
brunofgc 0:8c59a494a451 601 const char* weekdayTable[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
brunofgc 0:8c59a494a451 602 uint8_t indexSchedule;
brunofgc 5:8efd609dea7d 603 uint8_t indexScheduleException;
brunofgc 5:8efd609dea7d 604 bool exceptionDetected=false;
brunofgc 0:8c59a494a451 605
brunofgc 5:8efd609dea7d 606 pc.printf("Verifing schedule.\r\n");
brunofgc 0:8c59a494a451 607
brunofgc 5:8efd609dea7d 608 strftime(stringTime, 30, "%a,%R,%y,%d,%m", localtime(&seconds));
brunofgc 5:8efd609dea7d 609 //pc.printf("VerifySchedule <%s>.\r\n",stringTime);
brunofgc 5:8efd609dea7d 610 //Thu,20:48,17,2,7
brunofgc 0:8c59a494a451 611 weekdayString = strtok(stringTime,",");
brunofgc 0:8c59a494a451 612 weekday = 0;
brunofgc 0:8c59a494a451 613 while((weekday<10)&&(strstr(weekdayTable[weekday],weekdayString)==NULL)){
brunofgc 0:8c59a494a451 614 weekday++;
brunofgc 0:8c59a494a451 615 }
brunofgc 0:8c59a494a451 616 hora = atoi(strtok(NULL,":"));
brunofgc 5:8efd609dea7d 617 minuto = atoi(strtok(NULL,","));
brunofgc 5:8efd609dea7d 618 minuto = (hora*60)+minuto;
brunofgc 5:8efd609dea7d 619 anoAtual = atoi(strtok(NULL,","));
brunofgc 5:8efd609dea7d 620 diaAtual = atoi(strtok(NULL,","));
brunofgc 5:8efd609dea7d 621 mesAtual = atoi(strtok(NULL,""));
brunofgc 5:8efd609dea7d 622
brunofgc 5:8efd609dea7d 623 //pc.printf("Passo 1 .\r\n");
brunofgc 5:8efd609dea7d 624 for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){
brunofgc 5:8efd609dea7d 625 //pc.printf("Passo 2.\r\n");
brunofgc 5:8efd609dea7d 626 if(this->schedules[indexSchedule].weekday&(0x1<<weekday)){
brunofgc 5:8efd609dea7d 627 //pc.printf("Passo 3.\r\n");
brunofgc 0:8c59a494a451 628 if(this->schedules[indexSchedule].minute == minuto){
brunofgc 5:8efd609dea7d 629 //pc.printf("Passo 4.\r\n");
brunofgc 0:8c59a494a451 630 //Verifico se já executei e então executo a ação.
brunofgc 0:8c59a494a451 631 if(!this->schedules[indexSchedule].actSent){
brunofgc 5:8efd609dea7d 632 //pc.printf("Passo 5 .\r\n");
brunofgc 0:8c59a494a451 633 //Executa a ação;
brunofgc 5:8efd609dea7d 634 for(indexScheduleException = 0;indexScheduleException<this->numScheduleExceptions;indexScheduleException++){
brunofgc 5:8efd609dea7d 635 //pc.printf("Passo 6.\r\n");
brunofgc 5:8efd609dea7d 636 if(this->schedules[indexSchedule].id == this->scheduleExceptions[indexScheduleException].id_schedule){
brunofgc 5:8efd609dea7d 637 //pc.printf("Passo 7.\r\n");
brunofgc 5:8efd609dea7d 638 seconds = this->scheduleExceptions[indexScheduleException].timestamp;
brunofgc 5:8efd609dea7d 639 strftime(stringTime, 30, "%y,%d,%m", localtime(&seconds));
brunofgc 5:8efd609dea7d 640 anoScheduleException=atoi(strtok(stringTime,","));
brunofgc 5:8efd609dea7d 641 diaScheduleException=atoi(strtok(NULL,","));
brunofgc 5:8efd609dea7d 642 mesScheduleException=atoi(strtok(NULL,""));
brunofgc 5:8efd609dea7d 643 /*
brunofgc 5:8efd609dea7d 644 pc.printf("\r\nDebug ScheduleException.\r\n");
brunofgc 5:8efd609dea7d 645 pc.printf("stringTime <%s>.\r\n",stringTime);
brunofgc 5:8efd609dea7d 646 pc.printf("anoScheduleException %lu.",anoScheduleException);
brunofgc 5:8efd609dea7d 647 pc.printf("diaScheduleException %lu.",diaScheduleException);
brunofgc 5:8efd609dea7d 648 pc.printf("mesScheduleException %lu.",mesScheduleException);
brunofgc 5:8efd609dea7d 649 pc.printf("anoAtual %lu.",anoAtual);
brunofgc 5:8efd609dea7d 650 pc.printf("diaAtual %lu.",diaAtual);
brunofgc 5:8efd609dea7d 651 pc.printf("mesAtual %lu.",mesAtual);
brunofgc 5:8efd609dea7d 652 pc.printf("\r\n");
brunofgc 5:8efd609dea7d 653 */
brunofgc 5:8efd609dea7d 654 if(
brunofgc 5:8efd609dea7d 655 ((anoScheduleException == anoAtual) || (anoScheduleException == 70))
brunofgc 5:8efd609dea7d 656 &&
brunofgc 5:8efd609dea7d 657 (diaAtual == diaScheduleException)
brunofgc 5:8efd609dea7d 658 &&
brunofgc 5:8efd609dea7d 659 (mesAtual == mesScheduleException)
brunofgc 5:8efd609dea7d 660 )
brunofgc 5:8efd609dea7d 661 {
brunofgc 5:8efd609dea7d 662 pc.printf("ScheduleException detected.\r\n");
brunofgc 5:8efd609dea7d 663 exceptionDetected=true;
brunofgc 5:8efd609dea7d 664 }
brunofgc 5:8efd609dea7d 665 }
brunofgc 5:8efd609dea7d 666 }
brunofgc 5:8efd609dea7d 667 if(!exceptionDetected){
brunofgc 16:1b43a57ee676 668 diversos::wdt.kick(90.0);
brunofgc 5:8efd609dea7d 669 this->execAct(this->schedules[indexSchedule].idAct);
brunofgc 5:8efd609dea7d 670 this->schedules[indexSchedule].actSent = true;
brunofgc 5:8efd609dea7d 671 }
brunofgc 0:8c59a494a451 672 }
brunofgc 0:8c59a494a451 673 }else{
brunofgc 0:8c59a494a451 674 this->schedules[indexSchedule].actSent = false;
brunofgc 0:8c59a494a451 675 }
brunofgc 5:8efd609dea7d 676 }
brunofgc 0:8c59a494a451 677 }
brunofgc 0:8c59a494a451 678 }
brunofgc 0:8c59a494a451 679
brunofgc 0:8c59a494a451 680 /*
brunofgc 0:8c59a494a451 681 void device::verifySchedules(){
brunofgc 0:8c59a494a451 682 char stringTime[15];
brunofgc 0:8c59a494a451 683 char *weekdayString;
brunofgc 0:8c59a494a451 684 time_t seconds = time(NULL);
brunofgc 0:8c59a494a451 685 uint8_t hora;
brunofgc 0:8c59a494a451 686 uint16_t minuto;
brunofgc 0:8c59a494a451 687 uint8_t weekday=0;
brunofgc 0:8c59a494a451 688 const char* weekdayTable[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
brunofgc 0:8c59a494a451 689 uint8_t indexSchedule;
brunofgc 0:8c59a494a451 690
brunofgc 0:8c59a494a451 691 //Tue,19:09
brunofgc 0:8c59a494a451 692 strftime(stringTime, 15, "%a,%R", localtime(&seconds));
brunofgc 0:8c59a494a451 693
brunofgc 0:8c59a494a451 694 weekdayString = strtok(stringTime,",");
brunofgc 0:8c59a494a451 695 weekday = 0;
brunofgc 0:8c59a494a451 696 while((weekday<10)&&(strstr(weekdayTable[weekday],weekdayString)==NULL)){
brunofgc 0:8c59a494a451 697 weekday++;
brunofgc 0:8c59a494a451 698 }
brunofgc 0:8c59a494a451 699 weekday++;
brunofgc 0:8c59a494a451 700 hora = atoi(strtok(NULL,":"));
brunofgc 0:8c59a494a451 701 minuto = atoi(strtok(NULL,""));
brunofgc 0:8c59a494a451 702 minuto = (hora*60)+minuto;
brunofgc 0:8c59a494a451 703 //printf("\r\n\r\nMinuto %lu.\r\n\r\n",minuto);
brunofgc 0:8c59a494a451 704 for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){
brunofgc 0:8c59a494a451 705 //printf("Executando item %u do for de schedules.\r\n\r\n",indexSchedule);
brunofgc 0:8c59a494a451 706 if((this->schedules[indexSchedule].weekday == weekday)||(this->schedules[indexSchedule].weekday == 0)){
brunofgc 0:8c59a494a451 707 //Se for um alarme para o dia de hoje ou o alarme independer de dia (Se o dia for zero)
brunofgc 0:8c59a494a451 708 //printf("Hoje eh dia de executar a acão do schedule indice %u.\r\n\r\n",indexSchedule);
brunofgc 0:8c59a494a451 709 if(this->schedules[indexSchedule].minute == minuto){
brunofgc 0:8c59a494a451 710 //Verifico se já executei e então executo a ação.
brunofgc 0:8c59a494a451 711 if(!this->schedules[indexSchedule].actSent){
brunofgc 0:8c59a494a451 712 //Executa a ação;
brunofgc 0:8c59a494a451 713 //printf("Executando a acao!\r\n\r\n");
brunofgc 0:8c59a494a451 714 this->execAct(this->schedules[indexSchedule].idAct);
brunofgc 0:8c59a494a451 715 this->schedules[indexSchedule].actSent = true;
brunofgc 0:8c59a494a451 716 }
brunofgc 0:8c59a494a451 717 }else{
brunofgc 0:8c59a494a451 718 this->schedules[indexSchedule].actSent = false;
brunofgc 0:8c59a494a451 719 }
brunofgc 0:8c59a494a451 720 }
brunofgc 0:8c59a494a451 721 }
brunofgc 0:8c59a494a451 722 }
brunofgc 0:8c59a494a451 723 */
brunofgc 0:8c59a494a451 724 /*
brunofgc 0:8c59a494a451 725 uint8_t device::execActSchedule(uint32_t index){
brunofgc 0:8c59a494a451 726 switch(this->schedules[index].actType){
brunofgc 0:8c59a494a451 727 case modBusType_float:
brunofgc 0:8c59a494a451 728 float floatValueToWrite;
brunofgc 0:8c59a494a451 729 //writeFloat(uint8_t,uint16_t,uint8_t,float*);
brunofgc 0:8c59a494a451 730 this->floatToBin(0,&floatValueToWrite,&this->schedules[index].actValue[0]);
brunofgc 0:8c59a494a451 731 modBusMaster1::writeFloat(
brunofgc 0:8c59a494a451 732 this->schedules[index].param1,
brunofgc 0:8c59a494a451 733 this->schedules[index].param2,
brunofgc 0:8c59a494a451 734 this->schedules[index].param3,
brunofgc 0:8c59a494a451 735 &floatValueToWrite
brunofgc 0:8c59a494a451 736 );
brunofgc 0:8c59a494a451 737 break;
brunofgc 0:8c59a494a451 738 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 739 uint32_t uint32ValueToWrite;
brunofgc 0:8c59a494a451 740 this->uint32_t_ToBin(0,&uint32ValueToWrite,&this->schedules[index].actValue[0]);
brunofgc 0:8c59a494a451 741 modBusMaster1::writeRegister32BIT(
brunofgc 0:8c59a494a451 742 this->schedules[index].param1,
brunofgc 0:8c59a494a451 743 this->schedules[index].param2,
brunofgc 0:8c59a494a451 744 this->schedules[index].param3,
brunofgc 0:8c59a494a451 745 &uint32ValueToWrite
brunofgc 0:8c59a494a451 746 );
brunofgc 0:8c59a494a451 747 break;
brunofgc 0:8c59a494a451 748 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 749 uint16_t uint16ValueToWrite;
brunofgc 0:8c59a494a451 750 this->uint16_t_ToBin(0,&uint16ValueToWrite,&this->schedules[index].actValue[0]);
brunofgc 0:8c59a494a451 751 modBusMaster1::writeRegister16BIT(
brunofgc 0:8c59a494a451 752 this->schedules[index].param1,
brunofgc 0:8c59a494a451 753 this->schedules[index].param2,
brunofgc 0:8c59a494a451 754 this->schedules[index].param3,
brunofgc 0:8c59a494a451 755 &uint16ValueToWrite
brunofgc 0:8c59a494a451 756 );
brunofgc 0:8c59a494a451 757 break;
brunofgc 0:8c59a494a451 758 case modBusType_bit:
brunofgc 0:8c59a494a451 759 bool boolValueToWrite;
brunofgc 0:8c59a494a451 760 boolValueToWrite = (this->schedules[index].actValue[0] != 0);
brunofgc 0:8c59a494a451 761 modBusMaster1::writeSingleCoil(
brunofgc 0:8c59a494a451 762 this->schedules[index].param1,
brunofgc 0:8c59a494a451 763 this->schedules[index].param3,
brunofgc 0:8c59a494a451 764 boolValueToWrite
brunofgc 0:8c59a494a451 765 );
brunofgc 0:8c59a494a451 766 break;
brunofgc 0:8c59a494a451 767 case IrCommandType:
brunofgc 0:8c59a494a451 768 //Busca no arquivo
brunofgc 0:8c59a494a451 769 //Abre arquivo
brunofgc 0:8c59a494a451 770 char comandoIr[1024];
brunofgc 0:8c59a494a451 771 char indexBusca[10];
brunofgc 0:8c59a494a451 772 bool endOfFile = false;
brunofgc 0:8c59a494a451 773
brunofgc 0:8c59a494a451 774 //printf("\r\n===================DEBUG(Schedule)===================\r\n");
brunofgc 0:8c59a494a451 775 //"Ir:0,6E01FFFFS"
brunofgc 0:8c59a494a451 776 sprintf(indexBusca,"Ir:%u,",this->schedules[index].param3);
brunofgc 0:8c59a494a451 777 //printf("indexBusca = <%s>.\r\n",indexBusca);
brunofgc 0:8c59a494a451 778 if(sdCard::abreArquivo(&sdCard::devices,"r")){
brunofgc 0:8c59a494a451 779 do{
brunofgc 0:8c59a494a451 780 fgets(comandoIr,1024,sdCard::devices.fp);
brunofgc 0:8c59a494a451 781 endOfFile = feof(sdCard::devices.fp);
brunofgc 0:8c59a494a451 782 //printf("Linha comando Ir = <%s>.\r\nendOfFile = %u\r\n",comandoIr,endOfFile);
brunofgc 0:8c59a494a451 783 }while((strstr(comandoIr,indexBusca)==NULL)&&(endOfFile==0));
brunofgc 0:8c59a494a451 784 sdCard::fechaArquivo(&sdCard::devices);
brunofgc 0:8c59a494a451 785
brunofgc 0:8c59a494a451 786 //Vê se deu tudo certo, se não encontrou, retorna false
brunofgc 0:8c59a494a451 787 if(!endOfFile){
brunofgc 0:8c59a494a451 788 //Encontrado o comando, vou desempacotar ele.
brunofgc 0:8c59a494a451 789 strtok(comandoIr,",");
brunofgc 0:8c59a494a451 790 //Neste ponto já tenho o meu comando pronto para ser executado dentro de comandoIr;
brunofgc 0:8c59a494a451 791 //printf("\r\n\r\n\r\nEstou enviando o comando %s de dentro do schedule.\r\n\r\n\r\n",strtok(NULL,"S"));
brunofgc 0:8c59a494a451 792 deserializaPacoteIR(strtok(NULL,"S"));
brunofgc 0:8c59a494a451 793 enviaComandoIR(this->schedules[index].param1,this->schedules[index].param2);
brunofgc 0:8c59a494a451 794 }
brunofgc 0:8c59a494a451 795 }else{
brunofgc 0:8c59a494a451 796 return false;
brunofgc 0:8c59a494a451 797 }
brunofgc 0:8c59a494a451 798 //printf("\r\n===================DEBUG(Schedule)===================\r\n");
brunofgc 0:8c59a494a451 799 break;
brunofgc 0:8c59a494a451 800 }
brunofgc 0:8c59a494a451 801 return true;
brunofgc 0:8c59a494a451 802 }
brunofgc 0:8c59a494a451 803 */
brunofgc 0:8c59a494a451 804 /*
brunofgc 0:8c59a494a451 805 uint8_t device::execActAlarm(uint32_t index){
brunofgc 0:8c59a494a451 806 switch(this->alarms[index].actType){
brunofgc 0:8c59a494a451 807 case modBusType_float:
brunofgc 0:8c59a494a451 808 float floatValueToWrite;
brunofgc 0:8c59a494a451 809 //writeFloat(uint8_t,uint16_t,uint8_t,float*);
brunofgc 0:8c59a494a451 810 this->floatToBin(0,&floatValueToWrite,&this->alarms[index].actValue[0]);
brunofgc 0:8c59a494a451 811 modBusMaster1::writeFloat(
brunofgc 0:8c59a494a451 812 this->alarms[index].addrModbusWrite,
brunofgc 0:8c59a494a451 813 this->alarms[index].funcModbusWrite,
brunofgc 0:8c59a494a451 814 this->alarms[index].regModbusWrite,
brunofgc 0:8c59a494a451 815 &floatValueToWrite
brunofgc 0:8c59a494a451 816 );
brunofgc 0:8c59a494a451 817 break;
brunofgc 0:8c59a494a451 818 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 819 uint32_t uint32ValueToWrite;
brunofgc 0:8c59a494a451 820 this->uint32_t_ToBin(0,&uint32ValueToWrite,&this->alarms[index].actValue[0]);
brunofgc 0:8c59a494a451 821 modBusMaster1::writeRegister32BIT(
brunofgc 0:8c59a494a451 822 this->alarms[index].addrModbusWrite,
brunofgc 0:8c59a494a451 823 this->alarms[index].funcModbusWrite,
brunofgc 0:8c59a494a451 824 this->alarms[index].regModbusWrite,
brunofgc 0:8c59a494a451 825 &uint32ValueToWrite
brunofgc 0:8c59a494a451 826 );
brunofgc 0:8c59a494a451 827 break;
brunofgc 0:8c59a494a451 828 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 829 uint16_t uint16ValueToWrite;
brunofgc 0:8c59a494a451 830 this->uint16_t_ToBin(0,&uint16ValueToWrite,&this->alarms[index].actValue[0]);
brunofgc 0:8c59a494a451 831 modBusMaster1::writeRegister16BIT(
brunofgc 0:8c59a494a451 832 this->alarms[index].addrModbusWrite,
brunofgc 0:8c59a494a451 833 this->alarms[index].funcModbusWrite,
brunofgc 0:8c59a494a451 834 this->alarms[index].regModbusWrite,
brunofgc 0:8c59a494a451 835 &uint16ValueToWrite
brunofgc 0:8c59a494a451 836 );
brunofgc 0:8c59a494a451 837 break;
brunofgc 0:8c59a494a451 838 case modBusType_bit:
brunofgc 0:8c59a494a451 839 bool boolValueToWrite;
brunofgc 0:8c59a494a451 840 boolValueToWrite = (this->alarms[index].actValue[0] != 0);
brunofgc 0:8c59a494a451 841 modBusMaster1::writeSingleCoil(
brunofgc 0:8c59a494a451 842 this->alarms[index].addrModbusWrite,
brunofgc 0:8c59a494a451 843 this->alarms[index].regModbusWrite,
brunofgc 0:8c59a494a451 844 boolValueToWrite
brunofgc 0:8c59a494a451 845 );
brunofgc 0:8c59a494a451 846 break;
brunofgc 0:8c59a494a451 847 }
brunofgc 0:8c59a494a451 848 return true;
brunofgc 0:8c59a494a451 849 }
brunofgc 0:8c59a494a451 850 */
brunofgc 0:8c59a494a451 851
brunofgc 0:8c59a494a451 852 void device::setReading(char *linha)
brunofgc 0:8c59a494a451 853 {
brunofgc 0:8c59a494a451 854 uint8_t addr=0;
brunofgc 0:8c59a494a451 855 uint8_t func=0;
brunofgc 0:8c59a494a451 856 uint16_t reg=0;
brunofgc 0:8c59a494a451 857 uint8_t numRegs=0;
brunofgc 0:8c59a494a451 858 char *pChar;
brunofgc 0:8c59a494a451 859 bool inputData = false;
brunofgc 0:8c59a494a451 860
brunofgc 0:8c59a494a451 861 //printf("Linha <%s>.\n",linha);
brunofgc 0:8c59a494a451 862 if(strstr(linha,"readingsSet")) {
brunofgc 0:8c59a494a451 863 return;
brunofgc 0:8c59a494a451 864 }
brunofgc 0:8c59a494a451 865
brunofgc 2:ca3723da6668 866 this->numReadings++;
brunofgc 0:8c59a494a451 867
brunofgc 0:8c59a494a451 868 if(strstr(linha,"float,")) {
brunofgc 0:8c59a494a451 869 this->readings[this->numReadings-1].type=modBusType_float;
brunofgc 0:8c59a494a451 870 inputData = true;
brunofgc 0:8c59a494a451 871 }
brunofgc 0:8c59a494a451 872
brunofgc 0:8c59a494a451 873 if(strstr(linha,"uint32_t,")) {
brunofgc 0:8c59a494a451 874 this->readings[this->numReadings-1].type=modBusType_uint32_t;
brunofgc 0:8c59a494a451 875 inputData = true;
brunofgc 0:8c59a494a451 876 }
brunofgc 0:8c59a494a451 877
brunofgc 0:8c59a494a451 878 if(strstr(linha,"uint16_t,")) {
brunofgc 0:8c59a494a451 879 this->readings[this->numReadings-1].type=modBusType_uint16_t;
brunofgc 0:8c59a494a451 880 inputData = true;
brunofgc 0:8c59a494a451 881 }
brunofgc 0:8c59a494a451 882
brunofgc 17:c046e4b07ded 883 if(strstr(linha,"int32_t,")) {
brunofgc 17:c046e4b07ded 884 this->readings[this->numReadings-1].type=modBusType_int32_t;
brunofgc 17:c046e4b07ded 885 inputData = true;
brunofgc 17:c046e4b07ded 886 }
brunofgc 17:c046e4b07ded 887
brunofgc 17:c046e4b07ded 888 if(strstr(linha,"int16_t,")) {
brunofgc 17:c046e4b07ded 889 this->readings[this->numReadings-1].type=modBusType_int16_t;
brunofgc 17:c046e4b07ded 890 inputData = true;
brunofgc 17:c046e4b07ded 891 }
brunofgc 17:c046e4b07ded 892
brunofgc 0:8c59a494a451 893 if(strstr(linha,"uint8_t,")) {
brunofgc 0:8c59a494a451 894 this->readings[this->numReadings-1].type=modBusType_uint8_t;
brunofgc 0:8c59a494a451 895 inputData = true;
brunofgc 0:8c59a494a451 896 }
brunofgc 0:8c59a494a451 897
brunofgc 0:8c59a494a451 898 if(strstr(linha,"bit,")) {
brunofgc 0:8c59a494a451 899 this->readings[this->numReadings-1].type=modBusType_bit;
brunofgc 0:8c59a494a451 900 inputData = true;
brunofgc 0:8c59a494a451 901 }
brunofgc 0:8c59a494a451 902
brunofgc 0:8c59a494a451 903 if(inputData){
brunofgc 0:8c59a494a451 904 pChar = strtok(linha,",");
brunofgc 0:8c59a494a451 905
brunofgc 0:8c59a494a451 906 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 907 addr = atoi(pChar);
brunofgc 0:8c59a494a451 908
brunofgc 0:8c59a494a451 909 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 910 func = atoi(pChar);
brunofgc 0:8c59a494a451 911
brunofgc 0:8c59a494a451 912 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 913 reg = atoi(pChar);
brunofgc 0:8c59a494a451 914
brunofgc 0:8c59a494a451 915 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 916 numRegs = atoi(pChar);
brunofgc 0:8c59a494a451 917
brunofgc 0:8c59a494a451 918 this->readings[this->numReadings-1].addr=addr;
brunofgc 0:8c59a494a451 919 this->readings[this->numReadings-1].func=func;
brunofgc 0:8c59a494a451 920 this->readings[this->numReadings-1].reg=reg;
brunofgc 0:8c59a494a451 921 this->readings[this->numReadings-1].numRegs=numRegs;
brunofgc 0:8c59a494a451 922 }
brunofgc 0:8c59a494a451 923
brunofgc 0:8c59a494a451 924 }
brunofgc 0:8c59a494a451 925
brunofgc 0:8c59a494a451 926 void device::setAlarm(char *linha)
brunofgc 0:8c59a494a451 927 {
brunofgc 0:8c59a494a451 928 float floatVar;
brunofgc 0:8c59a494a451 929 uint32_t aux32_t;
brunofgc 0:8c59a494a451 930 uint16_t aux16_t;
brunofgc 0:8c59a494a451 931 char *pChar;
brunofgc 0:8c59a494a451 932
brunofgc 0:8c59a494a451 933 if(strstr(linha,"alarmSet")) {
brunofgc 2:ca3723da6668 934 //Inicialização de contadores de alarme
brunofgc 2:ca3723da6668 935 //É necessário inicializar estes dados para quando a máquina de alarmes rodar ter um inicio correto.
brunofgc 2:ca3723da6668 936 this->alarms[this->numAlarms].secAlarm=0;
brunofgc 2:ca3723da6668 937 this->alarms[this->numAlarms].idAct=0;
brunofgc 5:8efd609dea7d 938 this->alarms[this->numAlarms].on=0;
brunofgc 2:ca3723da6668 939 this->alarms[this->numAlarms].alarmFound = false;
brunofgc 0:8c59a494a451 940 //Inicialização de contadores de alarme
brunofgc 0:8c59a494a451 941 this->numAlarms++;
brunofgc 0:8c59a494a451 942 }
brunofgc 0:8c59a494a451 943
brunofgc 5:8efd609dea7d 944 if(strstr(linha,"idAlarm:")){
brunofgc 0:8c59a494a451 945 strtok(linha,":");
brunofgc 0:8c59a494a451 946 this->alarms[this->numAlarms-1].id = atoi(strtok(NULL,"\r\n"));
brunofgc 0:8c59a494a451 947 }
brunofgc 0:8c59a494a451 948
brunofgc 5:8efd609dea7d 949 if(strstr(linha,"seconds:")) {
brunofgc 0:8c59a494a451 950 strtok(linha,":");
brunofgc 0:8c59a494a451 951 pChar = strtok(NULL,"\r\n");
brunofgc 0:8c59a494a451 952 this->alarms[this->numAlarms-1].seconds = atoi(pChar);
brunofgc 0:8c59a494a451 953 }
brunofgc 0:8c59a494a451 954
brunofgc 5:8efd609dea7d 955 if(strstr(linha,"on:")) {
brunofgc 5:8efd609dea7d 956 strtok(linha,":");
brunofgc 5:8efd609dea7d 957 pChar = strtok(NULL,"\r\n");
brunofgc 5:8efd609dea7d 958 this->alarms[this->numAlarms-1].on = atoi(pChar);
brunofgc 5:8efd609dea7d 959 }
brunofgc 5:8efd609dea7d 960
brunofgc 5:8efd609dea7d 961 if(strstr(linha,"idAct:")) {
brunofgc 0:8c59a494a451 962 strtok(linha,":");
brunofgc 0:8c59a494a451 963 this->alarms[this->numAlarms-1].idAct = atoi(strtok(NULL,""));
brunofgc 0:8c59a494a451 964 return;
brunofgc 5:8efd609dea7d 965 }
brunofgc 5:8efd609dea7d 966
brunofgc 5:8efd609dea7d 967 if(strstr(linha,"bit,")){
brunofgc 5:8efd609dea7d 968 this->alarms[this->numAlarms-1].type=modBusType_bit;
brunofgc 5:8efd609dea7d 969
brunofgc 0:8c59a494a451 970 pChar = strtok(linha,",");
brunofgc 0:8c59a494a451 971
brunofgc 0:8c59a494a451 972 //Parametro1
brunofgc 0:8c59a494a451 973 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 974 this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 975
brunofgc 0:8c59a494a451 976 //Parametro2
brunofgc 0:8c59a494a451 977 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 978 this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 979
brunofgc 0:8c59a494a451 980 //Parametro3
brunofgc 0:8c59a494a451 981 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 982 this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 983
brunofgc 0:8c59a494a451 984 //Parametro4
brunofgc 0:8c59a494a451 985 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 986 this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 0:8c59a494a451 987
brunofgc 0:8c59a494a451 988 //Parametro5
brunofgc 0:8c59a494a451 989 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 990 this->alarms[this->numAlarms-1].value[0]= (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 0:8c59a494a451 991 }
brunofgc 0:8c59a494a451 992
brunofgc 0:8c59a494a451 993 if(strstr(linha,"float,")) {
brunofgc 0:8c59a494a451 994 this->alarms[this->numAlarms-1].type=modBusType_float;
brunofgc 0:8c59a494a451 995 strtok(linha,",");
brunofgc 0:8c59a494a451 996
brunofgc 0:8c59a494a451 997 //Parametro 1;
brunofgc 0:8c59a494a451 998 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 999 this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 1000
brunofgc 0:8c59a494a451 1001 //Parametro 2;
brunofgc 0:8c59a494a451 1002 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1003 this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 1004
brunofgc 0:8c59a494a451 1005 //Parametro 3;
brunofgc 0:8c59a494a451 1006 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1007 this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 1008
brunofgc 0:8c59a494a451 1009 //Parametro 4;
brunofgc 0:8c59a494a451 1010 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1011 this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 0:8c59a494a451 1012
brunofgc 0:8c59a494a451 1013 //Parametro 5;
brunofgc 0:8c59a494a451 1014 pChar = strtok(NULL,"\r\n");
brunofgc 0:8c59a494a451 1015 //floatVar = diversos::stringToFloat(pChar,100);
brunofgc 0:8c59a494a451 1016 floatVar = atof(pChar);
brunofgc 5:8efd609dea7d 1017
brunofgc 0:8c59a494a451 1018 this->floatToBin(1,&floatVar,&this->alarms[this->numAlarms-1].value[0]);
brunofgc 0:8c59a494a451 1019 }
brunofgc 5:8efd609dea7d 1020
brunofgc 0:8c59a494a451 1021 if(strstr(linha,"uint32_t,")) {
brunofgc 0:8c59a494a451 1022 this->alarms[this->numAlarms-1].type=modBusType_uint32_t;
brunofgc 0:8c59a494a451 1023 //sscanf(linha,"max:uint32_t,%lu,%lu,%lu,%lu",&aux1,&aux2,&aux3,&aux4);
brunofgc 0:8c59a494a451 1024 strtok(linha,",");
brunofgc 0:8c59a494a451 1025
brunofgc 0:8c59a494a451 1026 //parametro1
brunofgc 5:8efd609dea7d 1027 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1028 this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 1029
brunofgc 0:8c59a494a451 1030 //parametro2
brunofgc 5:8efd609dea7d 1031 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1032 this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 1033
brunofgc 0:8c59a494a451 1034 //parametro3
brunofgc 5:8efd609dea7d 1035 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1036 this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 1037
brunofgc 0:8c59a494a451 1038 //Parametro 4;
brunofgc 0:8c59a494a451 1039 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1040 this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 0:8c59a494a451 1041
brunofgc 0:8c59a494a451 1042 //parametro5
brunofgc 5:8efd609dea7d 1043 pChar = strtok(NULL,"\r\n");
brunofgc 0:8c59a494a451 1044 aux32_t = atoi(pChar);
brunofgc 5:8efd609dea7d 1045
brunofgc 0:8c59a494a451 1046 this->uint32_t_ToBin(1,&aux32_t,&this->alarms[this->numAlarms-1].value[0]);
brunofgc 0:8c59a494a451 1047 }
brunofgc 0:8c59a494a451 1048
brunofgc 0:8c59a494a451 1049
brunofgc 0:8c59a494a451 1050
brunofgc 0:8c59a494a451 1051 if(strstr(linha,"uint16_t,")) {
brunofgc 0:8c59a494a451 1052 this->alarms[this->numAlarms-1].type=modBusType_uint16_t;
brunofgc 0:8c59a494a451 1053 strtok(linha,",");
brunofgc 0:8c59a494a451 1054
brunofgc 0:8c59a494a451 1055 //parametro1
brunofgc 0:8c59a494a451 1056 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1057 this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 1058
brunofgc 0:8c59a494a451 1059 //parametro2
brunofgc 5:8efd609dea7d 1060 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1061 this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 1062
brunofgc 0:8c59a494a451 1063 //parametro3
brunofgc 5:8efd609dea7d 1064 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1065 this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar);
brunofgc 0:8c59a494a451 1066
brunofgc 0:8c59a494a451 1067 //Parametro 4;
brunofgc 0:8c59a494a451 1068 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1069 this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 0:8c59a494a451 1070
brunofgc 0:8c59a494a451 1071 //parametro5
brunofgc 5:8efd609dea7d 1072 pChar = strtok(NULL,"\r\n");
brunofgc 0:8c59a494a451 1073 aux16_t = atoi(pChar);
brunofgc 5:8efd609dea7d 1074
brunofgc 0:8c59a494a451 1075 this->uint16_t_ToBin(1,&aux16_t,&this->alarms[this->numAlarms-1].value[0]);
brunofgc 5:8efd609dea7d 1076 }
brunofgc 17:c046e4b07ded 1077
brunofgc 17:c046e4b07ded 1078 if(strstr(linha,"int32_t,")) {
brunofgc 17:c046e4b07ded 1079 this->alarms[this->numAlarms-1].type=modBusType_int32_t;
brunofgc 17:c046e4b07ded 1080 //sscanf(linha,"max:uint32_t,%lu,%lu,%lu,%lu",&aux1,&aux2,&aux3,&aux4);
brunofgc 17:c046e4b07ded 1081 strtok(linha,",");
brunofgc 17:c046e4b07ded 1082
brunofgc 17:c046e4b07ded 1083 //parametro1
brunofgc 17:c046e4b07ded 1084 pChar = strtok(NULL,",");
brunofgc 17:c046e4b07ded 1085 this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar);
brunofgc 17:c046e4b07ded 1086
brunofgc 17:c046e4b07ded 1087 //parametro2
brunofgc 17:c046e4b07ded 1088 pChar = strtok(NULL,",");
brunofgc 17:c046e4b07ded 1089 this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar);
brunofgc 17:c046e4b07ded 1090
brunofgc 17:c046e4b07ded 1091 //parametro3
brunofgc 17:c046e4b07ded 1092 pChar = strtok(NULL,",");
brunofgc 17:c046e4b07ded 1093 this->alarms[this->numAlarms-1].regModbusRead = atol(pChar);
brunofgc 17:c046e4b07ded 1094
brunofgc 17:c046e4b07ded 1095 //Parametro 4;
brunofgc 17:c046e4b07ded 1096 pChar = strtok(NULL,",");
brunofgc 17:c046e4b07ded 1097 this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 17:c046e4b07ded 1098
brunofgc 17:c046e4b07ded 1099 //parametro5
brunofgc 17:c046e4b07ded 1100 pChar = strtok(NULL,"\r\n");
brunofgc 17:c046e4b07ded 1101 aux32_t = atol(pChar);
brunofgc 17:c046e4b07ded 1102
brunofgc 17:c046e4b07ded 1103 this->uint32_t_ToBin(1,&aux32_t,&this->alarms[this->numAlarms-1].value[0]);
brunofgc 17:c046e4b07ded 1104 }
brunofgc 17:c046e4b07ded 1105
brunofgc 17:c046e4b07ded 1106
brunofgc 17:c046e4b07ded 1107
brunofgc 17:c046e4b07ded 1108 if(strstr(linha,"int16_t,")) {
brunofgc 17:c046e4b07ded 1109 this->alarms[this->numAlarms-1].type=modBusType_uint16_t;
brunofgc 17:c046e4b07ded 1110 strtok(linha,",");
brunofgc 17:c046e4b07ded 1111
brunofgc 17:c046e4b07ded 1112 //parametro1
brunofgc 17:c046e4b07ded 1113 pChar = strtok(NULL,",");
brunofgc 17:c046e4b07ded 1114 this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar);
brunofgc 17:c046e4b07ded 1115
brunofgc 17:c046e4b07ded 1116 //parametro2
brunofgc 17:c046e4b07ded 1117 pChar = strtok(NULL,",");
brunofgc 17:c046e4b07ded 1118 this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar);
brunofgc 17:c046e4b07ded 1119
brunofgc 17:c046e4b07ded 1120 //parametro3
brunofgc 17:c046e4b07ded 1121 pChar = strtok(NULL,",");
brunofgc 17:c046e4b07ded 1122 this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar);
brunofgc 17:c046e4b07ded 1123
brunofgc 17:c046e4b07ded 1124 //Parametro 4;
brunofgc 17:c046e4b07ded 1125 pChar = strtok(NULL,",");
brunofgc 17:c046e4b07ded 1126 this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um
brunofgc 17:c046e4b07ded 1127
brunofgc 17:c046e4b07ded 1128 //parametro5
brunofgc 17:c046e4b07ded 1129 pChar = strtok(NULL,"\r\n");
brunofgc 17:c046e4b07ded 1130 aux16_t = atoi(pChar);
brunofgc 17:c046e4b07ded 1131
brunofgc 17:c046e4b07ded 1132 this->uint16_t_ToBin(1,&aux16_t,&this->alarms[this->numAlarms-1].value[0]);
brunofgc 17:c046e4b07ded 1133 }
brunofgc 0:8c59a494a451 1134 }
brunofgc 0:8c59a494a451 1135
brunofgc 0:8c59a494a451 1136 void device::setSchedule(char *linha)
brunofgc 0:8c59a494a451 1137 {
brunofgc 0:8c59a494a451 1138 //float floatVar;
brunofgc 0:8c59a494a451 1139 //uint32_t aux32_t;
brunofgc 0:8c59a494a451 1140 //uint16_t aux16_t;
brunofgc 5:8efd609dea7d 1141 char *pChar;
brunofgc 5:8efd609dea7d 1142
brunofgc 2:ca3723da6668 1143 if(strstr(linha,"scheduleSet")) {
brunofgc 0:8c59a494a451 1144 //Inicialização de contadores de schedules
brunofgc 2:ca3723da6668 1145 //É necessário inicializar estes dados para quando a máquina de schedules rodar ter um inicio correto.
brunofgc 2:ca3723da6668 1146 this->schedules[this->numSchedules].actSent = false;
brunofgc 2:ca3723da6668 1147 //Inicialização de contadores de schedules
brunofgc 2:ca3723da6668 1148 this->numSchedules++;
brunofgc 0:8c59a494a451 1149 }
brunofgc 0:8c59a494a451 1150
brunofgc 5:8efd609dea7d 1151 if(strstr(linha,"idSchedule:")) {
brunofgc 0:8c59a494a451 1152 strtok(linha,":");
brunofgc 5:8efd609dea7d 1153 pChar = strtok(NULL,"");
brunofgc 5:8efd609dea7d 1154 this->schedules[this->numSchedules-1].id = atoi(pChar);
brunofgc 0:8c59a494a451 1155 }
brunofgc 0:8c59a494a451 1156
brunofgc 5:8efd609dea7d 1157 if(strstr(linha,"dayTime:")) {
brunofgc 0:8c59a494a451 1158 strtok(linha,":");
brunofgc 0:8c59a494a451 1159 pChar = strtok(NULL,",");
brunofgc 0:8c59a494a451 1160 this->schedules[this->numSchedules-1].weekday = atoi(pChar);
brunofgc 5:8efd609dea7d 1161 pChar = strtok(NULL,"");
brunofgc 0:8c59a494a451 1162 this->schedules[this->numSchedules-1].minute = atoi(pChar);
brunofgc 0:8c59a494a451 1163 }
brunofgc 5:8efd609dea7d 1164
brunofgc 5:8efd609dea7d 1165 if(strstr(linha,"idAct:")) {
brunofgc 0:8c59a494a451 1166 strtok(linha,":");
brunofgc 0:8c59a494a451 1167 pChar = strtok(NULL,"");
brunofgc 0:8c59a494a451 1168 this->schedules[this->numSchedules-1].idAct = atoi(pChar);
brunofgc 5:8efd609dea7d 1169 }
brunofgc 5:8efd609dea7d 1170 /*pc.printf("Debugando erro em scheduleSet.\r\n");
brunofgc 5:8efd609dea7d 1171 pc.printf("Linha <%s>\r\n",linha);
brunofgc 5:8efd609dea7d 1172 pc.printf("actSent = %lu",this->schedules[this->numSchedules-1].actSent);
brunofgc 5:8efd609dea7d 1173 pc.printf("idSchedule = %lu",this->schedules[this->numSchedules-1].id);
brunofgc 5:8efd609dea7d 1174 pc.printf("dayTime = %lu",this->schedules[this->numSchedules-1].weekday);
brunofgc 5:8efd609dea7d 1175 pc.printf("idAct = %lu",this->schedules[this->numSchedules-1].idAct);
brunofgc 5:8efd609dea7d 1176 pc.printf("Debugando erro em scheduleSet.\r\n");*/
brunofgc 0:8c59a494a451 1177 }
brunofgc 0:8c59a494a451 1178
brunofgc 4:3f92979d1ffb 1179 void device::setScheduleException(char *linha)
brunofgc 4:3f92979d1ffb 1180 {
brunofgc 4:3f92979d1ffb 1181 //float floatVar;
brunofgc 4:3f92979d1ffb 1182 //uint32_t aux32_t;
brunofgc 4:3f92979d1ffb 1183 //uint16_t aux16_t;
brunofgc 5:8efd609dea7d 1184 char *pChar;
brunofgc 5:8efd609dea7d 1185 if(strstr(linha,"scheduleExceptionSet")) {
brunofgc 4:3f92979d1ffb 1186 //Inicialização de contadores de schedulesExceptions
brunofgc 4:3f92979d1ffb 1187 this->numScheduleExceptions++;
brunofgc 4:3f92979d1ffb 1188 }
brunofgc 4:3f92979d1ffb 1189
brunofgc 5:8efd609dea7d 1190 if(strstr(linha,"idSchedule:")) {
brunofgc 4:3f92979d1ffb 1191 strtok(linha,":");
brunofgc 4:3f92979d1ffb 1192 this->scheduleExceptions[this->numScheduleExceptions-1].id_schedule = atoi(strtok(NULL,"\r\n"));
brunofgc 4:3f92979d1ffb 1193 }
brunofgc 4:3f92979d1ffb 1194
brunofgc 5:8efd609dea7d 1195 if(strstr(linha,"timestamp:")) {
brunofgc 4:3f92979d1ffb 1196 strtok(linha,":");
brunofgc 4:3f92979d1ffb 1197 pChar = strtok(NULL,",");
brunofgc 5:8efd609dea7d 1198 this->scheduleExceptions[this->numScheduleExceptions-1].timestamp = atoi(pChar);
brunofgc 5:8efd609dea7d 1199 }
brunofgc 4:3f92979d1ffb 1200 }
brunofgc 4:3f92979d1ffb 1201
brunofgc 0:8c59a494a451 1202 /*void device::writeReadingsToSD(){
brunofgc 0:8c59a494a451 1203 //Esta função faz a inserção de todos os dados no arquivo armazenamento.
brunofgc 5:8efd609dea7d 1204 uint16_t readingIndex=0;
brunofgc 5:8efd609dea7d 1205
brunofgc 0:8c59a494a451 1206 float *floatBuffer;
brunofgc 0:8c59a494a451 1207 uint32_t *uint32_tBuffer;
brunofgc 5:8efd609dea7d 1208 uint16_t *uint16_tBuffer;
brunofgc 5:8efd609dea7d 1209 bool *boolBuffer;
brunofgc 0:8c59a494a451 1210 uint32_t seconds_uint32_t;
brunofgc 0:8c59a494a451 1211
brunofgc 0:8c59a494a451 1212 //char registro[1024];
brunofgc 0:8c59a494a451 1213 //uint16_t indexRegistro;
brunofgc 0:8c59a494a451 1214
brunofgc 0:8c59a494a451 1215 seconds_uint32_t = (uint32_t) time(NULL);
brunofgc 0:8c59a494a451 1216
brunofgc 0:8c59a494a451 1217 //Inicio da string dentro do arquivo binario
brunofgc 8:25061210fcc9 1218 sdCard::insereDadosBank("log{data:",9);
brunofgc 0:8c59a494a451 1219
brunofgc 0:8c59a494a451 1220 //O timestamp é posto nos 4 primeiros bytes (segundos)
brunofgc 8:25061210fcc9 1221 sdCard::insereDadosBank(&sdCard::armazenamento,(char *)&seconds_uint32_t,4);
brunofgc 0:8c59a494a451 1222
brunofgc 0:8c59a494a451 1223 for(readingIndex = 0;readingIndex < this->numReadings;readingIndex++){
brunofgc 0:8c59a494a451 1224 switch(this->readings[readingIndex].type){
brunofgc 0:8c59a494a451 1225 case modBusType_float:
brunofgc 0:8c59a494a451 1226 floatBuffer = (float*) malloc(this->readings[readingIndex].numRegs*sizeof(float));
brunofgc 0:8c59a494a451 1227 modBusMaster1::readFloat(
brunofgc 0:8c59a494a451 1228 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1229 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 1230 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1231 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1232 floatBuffer
brunofgc 0:8c59a494a451 1233 );
brunofgc 0:8c59a494a451 1234 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 8:25061210fcc9 1235 sdCard::insereDadosBank((char *)floatBuffer,sizeof(float)*this->readings[readingIndex].numRegs);
brunofgc 0:8c59a494a451 1236 free(floatBuffer);
brunofgc 0:8c59a494a451 1237 break;
brunofgc 0:8c59a494a451 1238 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 1239 uint32_tBuffer = (uint32_t*) malloc(this->readings[readingIndex].numRegs*sizeof(uint32_t));
brunofgc 0:8c59a494a451 1240 modBusMaster1::readRegister32BIT(
brunofgc 0:8c59a494a451 1241 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1242 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 1243 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1244 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1245 uint32_tBuffer
brunofgc 0:8c59a494a451 1246 );
brunofgc 0:8c59a494a451 1247 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 8:25061210fcc9 1248 sdCard::insereDadosBank((char *)uint32_tBuffer,sizeof(uint32_t)*this->readings[readingIndex].numRegs);
brunofgc 0:8c59a494a451 1249 free(uint32_tBuffer);
brunofgc 0:8c59a494a451 1250 break;
brunofgc 0:8c59a494a451 1251
brunofgc 0:8c59a494a451 1252 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 1253 uint16_tBuffer = (uint16_t*) malloc(this->readings[readingIndex].numRegs*sizeof(uint16_t));
brunofgc 0:8c59a494a451 1254 modBusMaster1::readRegister16BIT(
brunofgc 0:8c59a494a451 1255 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1256 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 1257 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1258 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1259 uint16_tBuffer
brunofgc 0:8c59a494a451 1260 );
brunofgc 0:8c59a494a451 1261 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 8:25061210fcc9 1262 sdCard::insereDadosBank((char *)uint16_tBuffer,sizeof(uint16_t)*this->readings[readingIndex].numRegs);
brunofgc 0:8c59a494a451 1263 free(uint16_tBuffer);
brunofgc 0:8c59a494a451 1264 break;
brunofgc 0:8c59a494a451 1265
brunofgc 0:8c59a494a451 1266 case modBusType_bit:
brunofgc 0:8c59a494a451 1267 boolBuffer = (bool*) malloc(this->readings[readingIndex].numRegs*sizeof(bool));
brunofgc 0:8c59a494a451 1268 modBusMaster1::readCoils(
brunofgc 0:8c59a494a451 1269 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1270 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1271 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1272 boolBuffer
brunofgc 0:8c59a494a451 1273 );
brunofgc 0:8c59a494a451 1274 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 8:25061210fcc9 1275 sdCard::insereDadosBank((char *)boolBuffer,sizeof(bool)*this->readings[readingIndex].numRegs);
brunofgc 0:8c59a494a451 1276 free(boolBuffer);
brunofgc 0:8c59a494a451 1277 break;
brunofgc 0:8c59a494a451 1278
brunofgc 0:8c59a494a451 1279 }
brunofgc 0:8c59a494a451 1280 }
brunofgc 8:25061210fcc9 1281 sdCard::insereDadosBank("}log",4);
brunofgc 0:8c59a494a451 1282 }*/
brunofgc 0:8c59a494a451 1283
brunofgc 0:8c59a494a451 1284 uint8_t device::execAct(uint32_t index){
brunofgc 0:8c59a494a451 1285 //Primeiro preciso decidir que tipo de act estou lidando para tanto preciso pegar a ACT inteira.
brunofgc 0:8c59a494a451 1286 //Busca no arquivo
brunofgc 0:8c59a494a451 1287 //Abre arquivo
brunofgc 0:8c59a494a451 1288 char actString[1024];
brunofgc 0:8c59a494a451 1289 char indexBusca[15];
brunofgc 5:8efd609dea7d 1290 char alarmsSetString[256];
brunofgc 5:8efd609dea7d 1291 char alarmsResetString[256];
brunofgc 0:8c59a494a451 1292 char *pTipoComando;
brunofgc 0:8c59a494a451 1293 uint8_t tipoComando;
brunofgc 0:8c59a494a451 1294 bool endOfFile = false;
brunofgc 5:8efd609dea7d 1295 uint8_t indexAlarms;
brunofgc 5:8efd609dea7d 1296 char *pChar;
brunofgc 9:0230ec4d2e68 1297 bool commandExecOk = true;
brunofgc 12:254c32b4ece7 1298 uint8_t i;
brunofgc 12:254c32b4ece7 1299
brunofgc 0:8c59a494a451 1300 //Variáveis comuns;
brunofgc 0:8c59a494a451 1301 uint8_t param1;
brunofgc 0:8c59a494a451 1302 uint8_t param2;
brunofgc 0:8c59a494a451 1303 uint16_t param3;
brunofgc 0:8c59a494a451 1304 uint32_t param4;
brunofgc 0:8c59a494a451 1305
brunofgc 0:8c59a494a451 1306 //printf("\r\n===================DEBUG(execAct)===================\r\n");
brunofgc 5:8efd609dea7d 1307 sprintf(indexBusca,"idAct:%lu;",index);
brunofgc 5:8efd609dea7d 1308 pc.printf("Lido <%s>.\r\n",indexBusca);
brunofgc 0:8c59a494a451 1309 //printf("Buscando por indexBusca<%s>.\r\n",indexBusca);
brunofgc 0:8c59a494a451 1310 if(sdCard::abreArquivo(&sdCard::devices,"r")){
brunofgc 0:8c59a494a451 1311 do{
brunofgc 0:8c59a494a451 1312 endOfFile = feof(sdCard::devices.fp);
brunofgc 5:8efd609dea7d 1313 if(!endOfFile){fgets(actString,1024,sdCard::devices.fp);}
brunofgc 5:8efd609dea7d 1314 }while((strstr(actString,"actList")==NULL)&&(endOfFile==0));
brunofgc 6:f20c0e094294 1315 pc.printf("Achei o actList.\r\n");
brunofgc 5:8efd609dea7d 1316 do{
brunofgc 5:8efd609dea7d 1317 endOfFile = feof(sdCard::devices.fp);
brunofgc 0:8c59a494a451 1318 if(!endOfFile){fgets(actString,1024,sdCard::devices.fp);}
brunofgc 5:8efd609dea7d 1319 //printf("Linha ACT = <%s>.\r\nendOfFile = %u\r\n",actString,endOfFile);
brunofgc 5:8efd609dea7d 1320 }while((strstr(actString,indexBusca)==NULL)&&(endOfFile==0));
brunofgc 0:8c59a494a451 1321 sdCard::fechaArquivo(&sdCard::devices);
brunofgc 0:8c59a494a451 1322
brunofgc 0:8c59a494a451 1323 //Vê se deu tudo certo, se não encontrou, retorna false
brunofgc 0:8c59a494a451 1324 if(endOfFile){
brunofgc 0:8c59a494a451 1325 return false;
brunofgc 0:8c59a494a451 1326 }
brunofgc 0:8c59a494a451 1327 //Tudo correndo bem.
brunofgc 0:8c59a494a451 1328
brunofgc 0:8c59a494a451 1329 //printf("\r\nPoint 1\r\n");
brunofgc 0:8c59a494a451 1330
brunofgc 0:8c59a494a451 1331 //Encontrado o tipo de comando reutilizando a string indexBusca;
brunofgc 5:8efd609dea7d 1332 pChar = strtok(actString,";");
brunofgc 5:8efd609dea7d 1333 pc.printf("1- <%s>.\r\n",pChar);
brunofgc 5:8efd609dea7d 1334 pChar = strtok(NULL,";");
brunofgc 5:8efd609dea7d 1335 strcpy(alarmsSetString,pChar);
brunofgc 5:8efd609dea7d 1336 pChar = strtok(NULL,";");
brunofgc 5:8efd609dea7d 1337 strcpy(alarmsResetString,pChar);
brunofgc 5:8efd609dea7d 1338
brunofgc 5:8efd609dea7d 1339 pTipoComando = strtok(NULL,",");
brunofgc 5:8efd609dea7d 1340
brunofgc 5:8efd609dea7d 1341 printf("\r\nPoint 2 comando <%s>\r\n",pTipoComando);
brunofgc 0:8c59a494a451 1342 if(strstr(pTipoComando,"float")){tipoComando = modBusType_float;}
brunofgc 0:8c59a494a451 1343 if(strstr(pTipoComando,"bit")){tipoComando = modBusType_bit;}
brunofgc 0:8c59a494a451 1344 if(strstr(pTipoComando,"uint8_t")){tipoComando = modBusType_uint8_t;}
brunofgc 0:8c59a494a451 1345 if(strstr(pTipoComando,"uint16_t")){tipoComando = modBusType_uint16_t;}
brunofgc 0:8c59a494a451 1346 if(strstr(pTipoComando,"uint32_t")){tipoComando = modBusType_uint32_t;}
brunofgc 17:c046e4b07ded 1347 if(strstr(pTipoComando,"int16_t")){tipoComando = modBusType_int16_t;}
brunofgc 17:c046e4b07ded 1348 if(strstr(pTipoComando,"int32_t")){tipoComando = modBusType_int32_t;}
brunofgc 1:3f9e5b86d9e4 1349 if(strstr(pTipoComando,"IR")){tipoComando = IrCommandType;}
brunofgc 1:3f9e5b86d9e4 1350 if(strstr(pTipoComando,"PWM")){tipoComando = PWMCommandType;}
brunofgc 0:8c59a494a451 1351 }else{
brunofgc 0:8c59a494a451 1352 return false;
brunofgc 0:8c59a494a451 1353 }
brunofgc 0:8c59a494a451 1354
brunofgc 0:8c59a494a451 1355 switch(tipoComando){
brunofgc 0:8c59a494a451 1356 case modBusType_float:
brunofgc 0:8c59a494a451 1357 float floatValueToWrite;
brunofgc 0:8c59a494a451 1358 uint32_t auxMod;
brunofgc 0:8c59a494a451 1359 param1 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1360 param2 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1361 param3 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1362 //floatValueToWrite = diversos::stringToFloat(strtok(NULL,","),100);
brunofgc 0:8c59a494a451 1363 floatValueToWrite = atof(strtok(NULL,","));
brunofgc 5:8efd609dea7d 1364
brunofgc 1:3f9e5b86d9e4 1365 //Envio comando via Modbus
brunofgc 9:0230ec4d2e68 1366 if(modBusMaster1::writeFloat(
brunofgc 1:3f9e5b86d9e4 1367 param1,
brunofgc 1:3f9e5b86d9e4 1368 param3,
brunofgc 10:b61bb770f7b2 1369 1,
brunofgc 1:3f9e5b86d9e4 1370 &floatValueToWrite
brunofgc 9:0230ec4d2e68 1371 )){commandExecOk = false;}
brunofgc 0:8c59a494a451 1372 break;
brunofgc 17:c046e4b07ded 1373 case modBusType_uint32_t:
brunofgc 17:c046e4b07ded 1374 case modBusType_int32_t:
brunofgc 0:8c59a494a451 1375 param1 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1376 param2 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1377 param3 = atoi(strtok(NULL,","));
brunofgc 17:c046e4b07ded 1378 param4 = atol(strtok(NULL,","));
brunofgc 0:8c59a494a451 1379
brunofgc 9:0230ec4d2e68 1380 if(modBusMaster1::writeRegister32BIT(
brunofgc 0:8c59a494a451 1381 param1,
brunofgc 0:8c59a494a451 1382 param3,
brunofgc 10:b61bb770f7b2 1383 1,
brunofgc 0:8c59a494a451 1384 &param4
brunofgc 9:0230ec4d2e68 1385 )){commandExecOk = false;}
brunofgc 0:8c59a494a451 1386 break;
brunofgc 0:8c59a494a451 1387 case modBusType_uint16_t:
brunofgc 17:c046e4b07ded 1388 case modBusType_int16_t:
brunofgc 0:8c59a494a451 1389 uint16_t param4_uint16_t;
brunofgc 0:8c59a494a451 1390 param1 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1391 param2 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1392 param3 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1393 param4_uint16_t = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1394
brunofgc 9:0230ec4d2e68 1395 if(modBusMaster1::writeRegister16BIT(
brunofgc 0:8c59a494a451 1396 param1,
brunofgc 0:8c59a494a451 1397 param3,
brunofgc 10:b61bb770f7b2 1398 1,
brunofgc 0:8c59a494a451 1399 &param4_uint16_t
brunofgc 9:0230ec4d2e68 1400 )){commandExecOk = false;}
brunofgc 0:8c59a494a451 1401 break;
brunofgc 0:8c59a494a451 1402 case modBusType_bit:
brunofgc 0:8c59a494a451 1403 param1 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1404 param2 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1405 param3 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1406 param4 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1407
brunofgc 0:8c59a494a451 1408 if(param1 != 254){
brunofgc 0:8c59a494a451 1409 //Envia comando via Modbus
brunofgc 9:0230ec4d2e68 1410 if(modBusMaster1::writeSingleCoil(
brunofgc 0:8c59a494a451 1411 param1,
brunofgc 0:8c59a494a451 1412 param3,
brunofgc 0:8c59a494a451 1413 (bool)param4
brunofgc 9:0230ec4d2e68 1414 )){commandExecOk = false;}
brunofgc 0:8c59a494a451 1415 }else{
brunofgc 0:8c59a494a451 1416 //Executa comando interno;
brunofgc 0:8c59a494a451 1417 switch(param3){
brunofgc 0:8c59a494a451 1418 case 0:
brunofgc 0:8c59a494a451 1419 SD1.write(param4*1.0f);
brunofgc 0:8c59a494a451 1420 break;
brunofgc 0:8c59a494a451 1421 case 1:
brunofgc 0:8c59a494a451 1422 SD2.write(param4*1.0f);
brunofgc 0:8c59a494a451 1423 break;
brunofgc 0:8c59a494a451 1424 case 2:
brunofgc 0:8c59a494a451 1425 SD3.write(param4*1.0f);
brunofgc 0:8c59a494a451 1426 break;
brunofgc 0:8c59a494a451 1427 case 3:
brunofgc 0:8c59a494a451 1428 SD4.write(param4*1.0f);
brunofgc 0:8c59a494a451 1429 break;
brunofgc 0:8c59a494a451 1430 case 4:
brunofgc 0:8c59a494a451 1431 SD5.write(param4*1.0f);
brunofgc 0:8c59a494a451 1432 break;
brunofgc 0:8c59a494a451 1433 case 5:
brunofgc 0:8c59a494a451 1434 SD6.write(param4*1.0f);
brunofgc 0:8c59a494a451 1435 break;
brunofgc 0:8c59a494a451 1436 case 6:
brunofgc 0:8c59a494a451 1437 SD7 = param4;
brunofgc 0:8c59a494a451 1438 break;
brunofgc 0:8c59a494a451 1439 case 7:
brunofgc 0:8c59a494a451 1440 SD8 = param4;
brunofgc 0:8c59a494a451 1441 break;
brunofgc 0:8c59a494a451 1442 }
brunofgc 0:8c59a494a451 1443 }
brunofgc 0:8c59a494a451 1444 break;
brunofgc 0:8c59a494a451 1445 case IrCommandType:
brunofgc 0:8c59a494a451 1446 //Busca no arquivo
brunofgc 0:8c59a494a451 1447 //Abre arquivo
brunofgc 0:8c59a494a451 1448 //printf("\r\nIniciando execucao de comando IR\r\n");
brunofgc 0:8c59a494a451 1449 param1 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1450 param2 = atoi(strtok(NULL,","));
brunofgc 0:8c59a494a451 1451 //printf("\r\nparam 1 %lu, param2 %lu\r\n",param1,param2);
brunofgc 0:8c59a494a451 1452 pTipoComando = strtok(NULL,"S");//Reaproveitando variável pTipoComando
brunofgc 0:8c59a494a451 1453 //printf("\r\nPacote IR <%s>.\r\n",pTipoComando);
brunofgc 0:8c59a494a451 1454 deserializaPacoteIR(pTipoComando);
brunofgc 5:8efd609dea7d 1455 //printf("Deserializado.\r\nInicio do envio.\r\n");
brunofgc 12:254c32b4ece7 1456 if(param1){
brunofgc 12:254c32b4ece7 1457 enviaComandoIR(param1,param2);
brunofgc 12:254c32b4ece7 1458 }else{
brunofgc 12:254c32b4ece7 1459 for(i=33;i<41;i++){
brunofgc 12:254c32b4ece7 1460 enviaComandoIR(i,param2);
brunofgc 12:254c32b4ece7 1461 if(i!=40){osDelay(100);}
brunofgc 12:254c32b4ece7 1462 }
brunofgc 12:254c32b4ece7 1463 }
brunofgc 5:8efd609dea7d 1464 //printf("Fim do envio.\r\n");
brunofgc 0:8c59a494a451 1465 break;
brunofgc 1:3f9e5b86d9e4 1466
brunofgc 1:3f9e5b86d9e4 1467 case PWMCommandType:
brunofgc 1:3f9e5b86d9e4 1468 //float floatValuePWM;
brunofgc 1:3f9e5b86d9e4 1469 //uint32_t auxPeriod;
brunofgc 1:3f9e5b86d9e4 1470 param1 = atoi(strtok(NULL,","));
brunofgc 1:3f9e5b86d9e4 1471 param2 = atoi(strtok(NULL,","));
brunofgc 1:3f9e5b86d9e4 1472 param3 = atoi(strtok(NULL,","));
brunofgc 1:3f9e5b86d9e4 1473 //floatValueToWrite = diversos::stringToFloat(strtok(NULL,","),100);
brunofgc 1:3f9e5b86d9e4 1474 floatValueToWrite = atof(strtok(NULL,","));
brunofgc 1:3f9e5b86d9e4 1475
brunofgc 1:3f9e5b86d9e4 1476 //Executa comando interno;
brunofgc 1:3f9e5b86d9e4 1477 auxMod = floatValueToWrite/10;
brunofgc 1:3f9e5b86d9e4 1478 floatValueToWrite = floatValueToWrite-(auxMod*10);
brunofgc 1:3f9e5b86d9e4 1479 SD1.period_us(auxMod);
brunofgc 1:3f9e5b86d9e4 1480 pwmPeriod = auxMod;
brunofgc 1:3f9e5b86d9e4 1481 switch(param3){
brunofgc 1:3f9e5b86d9e4 1482 case 0:
brunofgc 1:3f9e5b86d9e4 1483 SD1.write(floatValueToWrite);
brunofgc 1:3f9e5b86d9e4 1484 break;
brunofgc 1:3f9e5b86d9e4 1485 case 1:
brunofgc 1:3f9e5b86d9e4 1486 SD2.write(floatValueToWrite);
brunofgc 1:3f9e5b86d9e4 1487 break;
brunofgc 1:3f9e5b86d9e4 1488 case 2:
brunofgc 1:3f9e5b86d9e4 1489 SD3.write(floatValueToWrite);
brunofgc 1:3f9e5b86d9e4 1490 break;
brunofgc 1:3f9e5b86d9e4 1491 case 3:
brunofgc 1:3f9e5b86d9e4 1492 SD4.write(floatValueToWrite);
brunofgc 1:3f9e5b86d9e4 1493 break;
brunofgc 1:3f9e5b86d9e4 1494 case 4:
brunofgc 1:3f9e5b86d9e4 1495 SD5.write(floatValueToWrite);
brunofgc 1:3f9e5b86d9e4 1496 break;
brunofgc 1:3f9e5b86d9e4 1497 case 5:
brunofgc 1:3f9e5b86d9e4 1498 SD6.write(floatValueToWrite);
brunofgc 1:3f9e5b86d9e4 1499 break;
brunofgc 1:3f9e5b86d9e4 1500 }
brunofgc 1:3f9e5b86d9e4 1501 break;
brunofgc 1:3f9e5b86d9e4 1502
brunofgc 0:8c59a494a451 1503 default:
brunofgc 0:8c59a494a451 1504 //printf("comando nao encontrado\r\n");
brunofgc 0:8c59a494a451 1505 break;
brunofgc 0:8c59a494a451 1506 }
brunofgc 5:8efd609dea7d 1507 //Coletando dados do alarmSet e alarmReset
brunofgc 5:8efd609dea7d 1508
brunofgc 5:8efd609dea7d 1509 pChar = strtok(alarmsSetString,",");
brunofgc 5:8efd609dea7d 1510 while(pChar!=NULL){
brunofgc 5:8efd609dea7d 1511 pc.printf("Lido Alarm Set %lu - <%lu>.\r\n",indexAlarms,atoi(pChar));
brunofgc 13:c1122e7b0b36 1512 if(atoi(pChar)){
brunofgc 13:c1122e7b0b36 1513 dispositivos[0]->alarmOnOff(atoi(pChar),1);
brunofgc 13:c1122e7b0b36 1514 }
brunofgc 5:8efd609dea7d 1515 pChar = strtok(NULL,",");
brunofgc 5:8efd609dea7d 1516 }
brunofgc 5:8efd609dea7d 1517
brunofgc 5:8efd609dea7d 1518 pChar = strtok(alarmsResetString,",");
brunofgc 5:8efd609dea7d 1519 while(pChar!=NULL){
brunofgc 5:8efd609dea7d 1520 pc.printf("Lido Alarm Reset %lu - <%lu>.\r\n",indexAlarms,atoi(pChar));
brunofgc 13:c1122e7b0b36 1521 if(atoi(pChar)){
brunofgc 13:c1122e7b0b36 1522 dispositivos[0]->alarmOnOff(atoi(pChar),0);
brunofgc 13:c1122e7b0b36 1523 }
brunofgc 5:8efd609dea7d 1524 pChar = strtok(NULL,",");
brunofgc 5:8efd609dea7d 1525 }
brunofgc 5:8efd609dea7d 1526
brunofgc 0:8c59a494a451 1527 //printf("\r\n===================DEBUG(execAct)===================\r\n");
brunofgc 9:0230ec4d2e68 1528 return commandExecOk;
brunofgc 0:8c59a494a451 1529 }
brunofgc 0:8c59a494a451 1530
brunofgc 5:8efd609dea7d 1531 void device::alarmOnOff(uint32_t idAlarm,bool state){
brunofgc 5:8efd609dea7d 1532 uint16_t alarmIndex;
brunofgc 5:8efd609dea7d 1533 char indexBusca[30];
brunofgc 5:8efd609dea7d 1534 char alarmString[40];
brunofgc 5:8efd609dea7d 1535 uint32_t seekPosition;
brunofgc 5:8efd609dea7d 1536 bool endOfFile;
brunofgc 5:8efd609dea7d 1537 char charState;
brunofgc 5:8efd609dea7d 1538
brunofgc 5:8efd609dea7d 1539 for(alarmIndex = 0; alarmIndex < alarmes; alarmIndex++){
brunofgc 5:8efd609dea7d 1540 if(this->alarms[alarmIndex].id == idAlarm){
brunofgc 5:8efd609dea7d 1541 this->alarms[alarmIndex].on = state;
brunofgc 5:8efd609dea7d 1542 }
brunofgc 5:8efd609dea7d 1543 }
brunofgc 5:8efd609dea7d 1544
brunofgc 5:8efd609dea7d 1545 if(!sdCard::abreArquivo(&sdCard::devices,"rb+")){return;}
brunofgc 5:8efd609dea7d 1546
brunofgc 7:b4b2b8720166 1547 sprintf(indexBusca,"idAlarm:%lu\r",idAlarm);
brunofgc 5:8efd609dea7d 1548 do{
brunofgc 5:8efd609dea7d 1549 endOfFile = feof(sdCard::devices.fp);
brunofgc 5:8efd609dea7d 1550 if(!endOfFile){fgets(alarmString,40,sdCard::devices.fp);}
brunofgc 5:8efd609dea7d 1551 }while((strstr(alarmString,indexBusca)==NULL)&&(endOfFile==0));
brunofgc 5:8efd609dea7d 1552
brunofgc 5:8efd609dea7d 1553
brunofgc 5:8efd609dea7d 1554 do{
brunofgc 5:8efd609dea7d 1555 endOfFile = feof(sdCard::devices.fp);
brunofgc 5:8efd609dea7d 1556 if(!endOfFile){fgets(alarmString,40,sdCard::devices.fp);}
brunofgc 5:8efd609dea7d 1557 }while((strstr(alarmString,"on:")==NULL)&&(endOfFile==0));
brunofgc 5:8efd609dea7d 1558
brunofgc 5:8efd609dea7d 1559
brunofgc 5:8efd609dea7d 1560 if(endOfFile){
brunofgc 5:8efd609dea7d 1561 sdCard::fechaArquivo(&sdCard::devices);
brunofgc 5:8efd609dea7d 1562 return;
brunofgc 5:8efd609dea7d 1563 }
brunofgc 5:8efd609dea7d 1564
brunofgc 5:8efd609dea7d 1565 seekPosition = ftell(sdCard::devices.fp)-3;
brunofgc 5:8efd609dea7d 1566 fseek(sdCard::devices.fp,seekPosition,SEEK_SET);
brunofgc 5:8efd609dea7d 1567 if(state){charState='1';}else{charState='0';}
brunofgc 5:8efd609dea7d 1568 fputc(charState,sdCard::devices.fp);
brunofgc 5:8efd609dea7d 1569 //fprintf(sdCard::devices.fp,"%u",state);
brunofgc 5:8efd609dea7d 1570 fflush(sdCard::devices.fp);
brunofgc 5:8efd609dea7d 1571 sdCard::fechaArquivo(&sdCard::devices);
brunofgc 5:8efd609dea7d 1572 }
brunofgc 5:8efd609dea7d 1573
brunofgc 0:8c59a494a451 1574 void device::writeReadingsToSD(){
brunofgc 0:8c59a494a451 1575 //Buffering before insertData
brunofgc 0:8c59a494a451 1576 //Esta função faz a inserção de todos os dados no arquivo armazenamento.
brunofgc 0:8c59a494a451 1577 uint16_t readingIndex=0;
brunofgc 0:8c59a494a451 1578 /*
brunofgc 0:8c59a494a451 1579 union {
brunofgc 0:8c59a494a451 1580 char c[4];
brunofgc 0:8c59a494a451 1581 float v;
brunofgc 0:8c59a494a451 1582 }uFloat;
brunofgc 0:8c59a494a451 1583 */
brunofgc 0:8c59a494a451 1584 float *floatBuffer;
brunofgc 0:8c59a494a451 1585 uint32_t *uint32_tBuffer;
brunofgc 0:8c59a494a451 1586 uint16_t *uint16_tBuffer;
brunofgc 0:8c59a494a451 1587 bool *boolBuffer;
brunofgc 0:8c59a494a451 1588 uint32_t seconds_uint32_t;
brunofgc 0:8c59a494a451 1589 char seconds_char[5];
brunofgc 0:8c59a494a451 1590
brunofgc 0:8c59a494a451 1591 seconds_uint32_t = (uint32_t) time(NULL);
brunofgc 0:8c59a494a451 1592 seconds_char[0]=(seconds_uint32_t >> 0) & 0xFF;
brunofgc 0:8c59a494a451 1593 seconds_char[1]=(seconds_uint32_t >> 8) & 0xFF;
brunofgc 0:8c59a494a451 1594 seconds_char[2]=(seconds_uint32_t >> 16) & 0xFF;
brunofgc 0:8c59a494a451 1595 seconds_char[3]=(seconds_uint32_t >> 24) & 0xFF;
brunofgc 0:8c59a494a451 1596 seconds_char[4]=0;
brunofgc 0:8c59a494a451 1597
brunofgc 0:8c59a494a451 1598
brunofgc 0:8c59a494a451 1599 //Inicio da string dentro do arquivo binario
brunofgc 16:1b43a57ee676 1600 sdCardBuf.del();
brunofgc 0:8c59a494a451 1601 sdCardBuf.fill("log{data:",9);
brunofgc 0:8c59a494a451 1602
brunofgc 0:8c59a494a451 1603 //O timestamp é posto nos 4 primeiros bytes (segundos)
brunofgc 0:8c59a494a451 1604 sdCardBuf.fill(seconds_char,5);
brunofgc 0:8c59a494a451 1605
brunofgc 0:8c59a494a451 1606 for(readingIndex = 0;readingIndex < this->numReadings;readingIndex++){
brunofgc 0:8c59a494a451 1607 switch(this->readings[readingIndex].type){
brunofgc 0:8c59a494a451 1608 case modBusType_float:
brunofgc 1:3f9e5b86d9e4 1609 if(this->readings[readingIndex].addr != enderecoControladoraVirtual){
brunofgc 0:8c59a494a451 1610 //Busca externa via Modbus
brunofgc 0:8c59a494a451 1611 floatBuffer = (float*) malloc(((this->readings[readingIndex].numRegs*sizeof(float))+1)); //Alocando mais um byte para status
brunofgc 15:6d9e9cafe138 1612 ((char *)floatBuffer)[(this->readings[readingIndex].numRegs*sizeof(float))] = modBusMaster1::readFloat(
brunofgc 0:8c59a494a451 1613 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1614 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 1615 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1616 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1617 floatBuffer
brunofgc 0:8c59a494a451 1618 );
brunofgc 0:8c59a494a451 1619 }else{
brunofgc 0:8c59a494a451 1620 //Busca Interna
brunofgc 0:8c59a494a451 1621 floatBuffer = (float*) malloc(sizeof(float));
brunofgc 3:75223647cf3d 1622 if((this->readings[readingIndex].reg<100)&&(this->readings[readingIndex].reg < num_temperatureSensors)){//Registradores das entradas de temperatura
brunofgc 3:75223647cf3d 1623 //Populando floatBuffer com a temperaturas
brunofgc 3:75223647cf3d 1624 floatBuffer[0] = temperaturas[this->readings[readingIndex].reg];
brunofgc 1:3f9e5b86d9e4 1625 }
brunofgc 15:6d9e9cafe138 1626 if((this->readings[readingIndex].reg>=100)&&(this->readings[readingIndex].reg<104)){
brunofgc 3:75223647cf3d 1627 //Registradores das entradas analógicas
brunofgc 3:75223647cf3d 1628 floatBuffer[0] = aiFiltrada[this->readings[readingIndex].reg - 100];
brunofgc 3:75223647cf3d 1629 }
brunofgc 3:75223647cf3d 1630 this->readings[readingIndex].numRegs = 1;
brunofgc 0:8c59a494a451 1631 }
brunofgc 0:8c59a494a451 1632
brunofgc 0:8c59a494a451 1633 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 1634 sdCardBuf.fill((char *)floatBuffer,((sizeof(float)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status
brunofgc 0:8c59a494a451 1635 free(floatBuffer);
brunofgc 0:8c59a494a451 1636 break;
brunofgc 17:c046e4b07ded 1637 case modBusType_uint32_t:
brunofgc 17:c046e4b07ded 1638 case modBusType_int32_t:
brunofgc 3:75223647cf3d 1639 if(this->readings[readingIndex].addr != enderecoControladoraVirtual){
brunofgc 15:6d9e9cafe138 1640 uint32_tBuffer = (uint32_t*) malloc((this->readings[readingIndex].numRegs*sizeof(uint32_t))+1); //Alocando mais um byte para status
brunofgc 15:6d9e9cafe138 1641 ((char *)uint32_tBuffer)[(this->readings[readingIndex].numRegs*sizeof(uint32_t))] = modBusMaster1::readRegister32BIT(//Ponho o status no ultimo byte
brunofgc 3:75223647cf3d 1642 this->readings[readingIndex].addr,
brunofgc 3:75223647cf3d 1643 this->readings[readingIndex].func,
brunofgc 3:75223647cf3d 1644 this->readings[readingIndex].reg,
brunofgc 3:75223647cf3d 1645 this->readings[readingIndex].numRegs,
brunofgc 3:75223647cf3d 1646 uint32_tBuffer
brunofgc 3:75223647cf3d 1647 );
brunofgc 3:75223647cf3d 1648 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 3:75223647cf3d 1649 }else{
brunofgc 15:6d9e9cafe138 1650 uint32_tBuffer = (uint32_t*) malloc(sizeof(uint32_t)+1); //Alocando mais um byte para status
brunofgc 3:75223647cf3d 1651 uint32_tBuffer[0] = pulsosEDs[this->readings[readingIndex].reg];
brunofgc 3:75223647cf3d 1652 this->readings[readingIndex].numRegs = 1;
brunofgc 3:75223647cf3d 1653 pulsosEDs[this->readings[readingIndex].reg] = 0;
brunofgc 3:75223647cf3d 1654 }
brunofgc 0:8c59a494a451 1655 sdCardBuf.fill((char *)uint32_tBuffer,((sizeof(uint32_t)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status
brunofgc 0:8c59a494a451 1656 free(uint32_tBuffer);
brunofgc 0:8c59a494a451 1657 break;
brunofgc 0:8c59a494a451 1658
brunofgc 17:c046e4b07ded 1659 case modBusType_uint16_t:
brunofgc 17:c046e4b07ded 1660 case modBusType_int16_t:
brunofgc 1:3f9e5b86d9e4 1661 if(this->readings[readingIndex].addr!=enderecoControladoraVirtual){
brunofgc 0:8c59a494a451 1662 uint16_tBuffer = (uint16_t*) malloc(((this->readings[readingIndex].numRegs*sizeof(uint16_t))+1));//Alocando mais um byte para status
brunofgc 15:6d9e9cafe138 1663 ((char *)uint16_tBuffer)[(this->readings[readingIndex].numRegs*sizeof(uint16_t))] = modBusMaster1::readRegister16BIT(//
brunofgc 0:8c59a494a451 1664 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1665 this->readings[readingIndex].func,
brunofgc 0:8c59a494a451 1666 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1667 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1668 uint16_tBuffer
brunofgc 0:8c59a494a451 1669 );
brunofgc 0:8c59a494a451 1670 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 1671 sdCardBuf.fill((char *)uint16_tBuffer,((sizeof(uint16_t)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status
brunofgc 0:8c59a494a451 1672 free(uint16_tBuffer);
brunofgc 15:6d9e9cafe138 1673 }else{
brunofgc 0:8c59a494a451 1674 if((this->readings[readingIndex].reg>0)&&((this->readings[readingIndex].reg + this->readings[readingIndex].numRegs)<=4)){
brunofgc 15:6d9e9cafe138 1675 sdCardBuf.fill((char *)&aiFiltrada[this->readings[readingIndex].reg-1],sizeof(uint16_t)*this->readings[readingIndex].numRegs);
brunofgc 15:6d9e9cafe138 1676 seconds_char[4]=0;
brunofgc 15:6d9e9cafe138 1677 sdCardBuf.fill((char *)&seconds_char[4],1);
brunofgc 0:8c59a494a451 1678 }
brunofgc 0:8c59a494a451 1679 }
brunofgc 0:8c59a494a451 1680 break;
brunofgc 0:8c59a494a451 1681
brunofgc 0:8c59a494a451 1682 case modBusType_bit:
brunofgc 15:6d9e9cafe138 1683 boolBuffer = (bool*) malloc(sizeof(bool)+1);//Alocando mais um byte para status
brunofgc 15:6d9e9cafe138 1684 if(this->readings[readingIndex].addr != enderecoControladoraVirtual){
brunofgc 15:6d9e9cafe138 1685 ((char *)boolBuffer)[(this->readings[readingIndex].numRegs*sizeof(bool))] = modBusMaster1::readCoils(
brunofgc 0:8c59a494a451 1686 this->readings[readingIndex].addr,
brunofgc 0:8c59a494a451 1687 this->readings[readingIndex].reg,
brunofgc 0:8c59a494a451 1688 this->readings[readingIndex].numRegs,
brunofgc 0:8c59a494a451 1689 boolBuffer
brunofgc 0:8c59a494a451 1690 );
brunofgc 15:6d9e9cafe138 1691 }else{
brunofgc 0:8c59a494a451 1692 boolBuffer[0] = entradasDigitais[this->readings[readingIndex].reg];
brunofgc 15:6d9e9cafe138 1693 ((char *)boolBuffer)[1]=0;
brunofgc 0:8c59a494a451 1694 }
brunofgc 0:8c59a494a451 1695 //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste());
brunofgc 0:8c59a494a451 1696 sdCardBuf.fill((char *)boolBuffer,((sizeof(bool)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status
brunofgc 0:8c59a494a451 1697 free(boolBuffer);
brunofgc 0:8c59a494a451 1698 break;
brunofgc 0:8c59a494a451 1699
brunofgc 0:8c59a494a451 1700 }
brunofgc 0:8c59a494a451 1701 }
brunofgc 0:8c59a494a451 1702 readingIndex = sdCardBuf.fill("}log",4); //Reaproveitando a variável de 16bit readingIndex
brunofgc 5:8efd609dea7d 1703 sdCard::arquivoAberto(40000);
brunofgc 5:8efd609dea7d 1704 //Insiro os dados se for possível.
brunofgc 8:25061210fcc9 1705 sdCard::insereDadosBank(sdCardBuf.get(),readingIndex);
brunofgc 0:8c59a494a451 1706 }
brunofgc 0:8c59a494a451 1707
brunofgc 0:8c59a494a451 1708
brunofgc 0:8c59a494a451 1709
brunofgc 0:8c59a494a451 1710
brunofgc 0:8c59a494a451 1711
brunofgc 0:8c59a494a451 1712
brunofgc 0:8c59a494a451 1713
brunofgc 0:8c59a494a451 1714
brunofgc 0:8c59a494a451 1715 device::device()
brunofgc 0:8c59a494a451 1716 {
brunofgc 0:8c59a494a451 1717 this->numAlarms=0;
brunofgc 0:8c59a494a451 1718 this->numReadings=0;
brunofgc 0:8c59a494a451 1719 }
brunofgc 0:8c59a494a451 1720
brunofgc 0:8c59a494a451 1721 int criaDevices(FILE *devicesCfg)
brunofgc 0:8c59a494a451 1722 {
brunofgc 0:8c59a494a451 1723 char linha[maxCaractereLeLinha];
brunofgc 0:8c59a494a451 1724 int i;
brunofgc 0:8c59a494a451 1725 int linhas = 0;
brunofgc 0:8c59a494a451 1726 devices = 0;
brunofgc 0:8c59a494a451 1727
brunofgc 0:8c59a494a451 1728 while(fgets(linha,maxCaractereLeLinha,devicesCfg)) {
brunofgc 0:8c59a494a451 1729 if(strstr(linha,"deviceCreate\\")) {
brunofgc 0:8c59a494a451 1730 devices++;
brunofgc 0:8c59a494a451 1731 }
brunofgc 0:8c59a494a451 1732 linhas++;
brunofgc 0:8c59a494a451 1733 }
brunofgc 0:8c59a494a451 1734
brunofgc 0:8c59a494a451 1735 printf("Contei %u linha(s), %u device(s).\n",linhas,devices);
brunofgc 0:8c59a494a451 1736
brunofgc 0:8c59a494a451 1737 //Criando vetor de objetos dispositivos;
brunofgc 0:8c59a494a451 1738
brunofgc 0:8c59a494a451 1739 dispositivos = (device **) malloc(sizeof(device)*devices);
brunofgc 0:8c59a494a451 1740
brunofgc 0:8c59a494a451 1741 for(i=0; i<devices; i++) {
brunofgc 0:8c59a494a451 1742 dispositivos[i] = new device();
brunofgc 0:8c59a494a451 1743 //dispositivos[i]->id=i;
brunofgc 0:8c59a494a451 1744 }
brunofgc 0:8c59a494a451 1745
brunofgc 2:ca3723da6668 1746 rewind(devicesCfg);
brunofgc 0:8c59a494a451 1747 return 1;
brunofgc 0:8c59a494a451 1748 }
brunofgc 0:8c59a494a451 1749
brunofgc 0:8c59a494a451 1750
brunofgc 0:8c59a494a451 1751 int configuraDevices(FILE *devicesCfg)
brunofgc 0:8c59a494a451 1752 {
brunofgc 0:8c59a494a451 1753 uint16_t linhas=0;
brunofgc 0:8c59a494a451 1754 //uint16_t leituras = 0;
brunofgc 2:ca3723da6668 1755 char linha[maxCaractereLeLinha];
brunofgc 0:8c59a494a451 1756 uint8_t estadoConfiguracao=estadoConfiguracao_idle;
brunofgc 2:ca3723da6668 1757 int8_t dispositivoEmConfiguracao = -1;
brunofgc 2:ca3723da6668 1758
brunofgc 2:ca3723da6668 1759 while(fgets(linha,maxCaractereLeLinha,devicesCfg)){
brunofgc 2:ca3723da6668 1760 if(strstr(linha,"alarmSet")) {
brunofgc 2:ca3723da6668 1761 //printf("Criando alarme no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 2:ca3723da6668 1762 alarmes++;
brunofgc 2:ca3723da6668 1763 estadoConfiguracao = estadoConfiguracao_alarmSet;
brunofgc 5:8efd609dea7d 1764 }
brunofgc 15:6d9e9cafe138 1765
brunofgc 15:6d9e9cafe138 1766 if(strstr(linha,"readingsEnd")) {
brunofgc 15:6d9e9cafe138 1767 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 15:6d9e9cafe138 1768 estadoConfiguracao = estadoConfiguracao_idle;
brunofgc 15:6d9e9cafe138 1769 leituras++;
brunofgc 15:6d9e9cafe138 1770 }
brunofgc 2:ca3723da6668 1771
brunofgc 15:6d9e9cafe138 1772 if(estadoConfiguracao == estadoConfiguracao_readingsSet){
brunofgc 2:ca3723da6668 1773 leituras++;
brunofgc 15:6d9e9cafe138 1774 }
brunofgc 0:8c59a494a451 1775
brunofgc 2:ca3723da6668 1776 if(strstr(linha,"readingsSet")) {
brunofgc 2:ca3723da6668 1777 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 15:6d9e9cafe138 1778 estadoConfiguracao = estadoConfiguracao_readingsSet;
brunofgc 2:ca3723da6668 1779 }
brunofgc 2:ca3723da6668 1780
brunofgc 2:ca3723da6668 1781 if(strstr(linha,"scheduleSet")) {
brunofgc 2:ca3723da6668 1782 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 2:ca3723da6668 1783 qtdSchedules++;
brunofgc 2:ca3723da6668 1784 estadoConfiguracao = estadoConfiguracao_scheduleSet;
brunofgc 2:ca3723da6668 1785 }
brunofgc 4:3f92979d1ffb 1786
brunofgc 5:8efd609dea7d 1787 if(strstr(linha,"scheduleExceptionsSet")) {
brunofgc 4:3f92979d1ffb 1788 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 5:8efd609dea7d 1789 qtdScheduleExceptions++;
brunofgc 4:3f92979d1ffb 1790 estadoConfiguracao = estadoConfiguracao_scheduleSet;
brunofgc 4:3f92979d1ffb 1791 }
brunofgc 2:ca3723da6668 1792
brunofgc 2:ca3723da6668 1793 linhas++;
brunofgc 2:ca3723da6668 1794 }
brunofgc 15:6d9e9cafe138 1795 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);
brunofgc 2:ca3723da6668 1796 //Reservar memoria aqui.
brunofgc 2:ca3723da6668 1797
brunofgc 2:ca3723da6668 1798 //if(resizeArray(this->numReadings, this->numReadings+1, (char **)&this->readings,sizeof(reading))) {
brunofgc 2:ca3723da6668 1799 //(char *) malloc(sizeUnit*novo);
brunofgc 2:ca3723da6668 1800 dispositivos[0]->readings = (reading*) malloc(sizeof(reading)*leituras);
brunofgc 2:ca3723da6668 1801 dispositivos[0]->alarms = (alarm*) malloc(sizeof(alarm)*alarmes);
brunofgc 2:ca3723da6668 1802 dispositivos[0]->schedules = (schedule*) malloc(sizeof(schedule)*qtdSchedules);
brunofgc 5:8efd609dea7d 1803 dispositivos[0]->scheduleExceptions = (scheduleException*) malloc(sizeof(scheduleException)*qtdScheduleExceptions);
brunofgc 2:ca3723da6668 1804
brunofgc 2:ca3723da6668 1805 //Retornando ao inicio o arquivo
brunofgc 2:ca3723da6668 1806 rewind(devicesCfg);
brunofgc 2:ca3723da6668 1807
brunofgc 0:8c59a494a451 1808 while(fgets(linha,maxCaractereLeLinha,devicesCfg)) {
brunofgc 0:8c59a494a451 1809 if(strstr(linha,"deviceCreate\\")) {
brunofgc 0:8c59a494a451 1810 dispositivoEmConfiguracao++;
brunofgc 0:8c59a494a451 1811 //printf("Configurando dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1812 estadoConfiguracao = estadoConfiguracao_creatingDevice;
brunofgc 0:8c59a494a451 1813 }
brunofgc 0:8c59a494a451 1814
brunofgc 0:8c59a494a451 1815 if(strstr(linha,"alarmSet")) {
brunofgc 0:8c59a494a451 1816 //printf("Criando alarme no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1817 alarmes++;
brunofgc 0:8c59a494a451 1818 estadoConfiguracao = estadoConfiguracao_alarmSet;
brunofgc 0:8c59a494a451 1819 }
brunofgc 0:8c59a494a451 1820
brunofgc 15:6d9e9cafe138 1821 if(strstr(linha,"readingsEnd")) {
brunofgc 0:8c59a494a451 1822 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 15:6d9e9cafe138 1823 estadoConfiguracao = estadoConfiguracao_idle;
brunofgc 15:6d9e9cafe138 1824 }
brunofgc 15:6d9e9cafe138 1825
brunofgc 15:6d9e9cafe138 1826 if(estadoConfiguracao == estadoConfiguracao_readingsSet){
brunofgc 2:ca3723da6668 1827 leituras++;
brunofgc 15:6d9e9cafe138 1828 }
brunofgc 15:6d9e9cafe138 1829
brunofgc 15:6d9e9cafe138 1830
brunofgc 15:6d9e9cafe138 1831 if(strstr(linha,"readingsSet")) {
brunofgc 15:6d9e9cafe138 1832 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1833 estadoConfiguracao = estadoConfiguracao_readingsSet;
brunofgc 0:8c59a494a451 1834 }
brunofgc 0:8c59a494a451 1835
brunofgc 4:3f92979d1ffb 1836 if(strstr(linha,"scheduleExceptionSet")) {
brunofgc 4:3f92979d1ffb 1837 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 4:3f92979d1ffb 1838 qtdScheduleExceptions++;
brunofgc 4:3f92979d1ffb 1839 estadoConfiguracao = estadoConfiguracao_scheduleExceptionSet;
brunofgc 15:6d9e9cafe138 1840 }
brunofgc 0:8c59a494a451 1841
brunofgc 0:8c59a494a451 1842 if(strstr(linha,"scheduleSet")) {
brunofgc 0:8c59a494a451 1843 //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1844 qtdSchedules++;
brunofgc 0:8c59a494a451 1845 estadoConfiguracao = estadoConfiguracao_scheduleSet;
brunofgc 5:8efd609dea7d 1846 }
brunofgc 0:8c59a494a451 1847
brunofgc 0:8c59a494a451 1848 if(strstr(linha,">")) {
brunofgc 0:8c59a494a451 1849 //printf("Fechando dispositivo de indice %u.\n",dispositivoEmConfiguracao);
brunofgc 0:8c59a494a451 1850 estadoConfiguracao = estadoConfiguracao_idle;
brunofgc 0:8c59a494a451 1851 }
brunofgc 0:8c59a494a451 1852
brunofgc 0:8c59a494a451 1853 switch(estadoConfiguracao) {
brunofgc 0:8c59a494a451 1854 case estadoConfiguracao_creatingDevice:
brunofgc 0:8c59a494a451 1855 /*if(strstr(linha,"id:")) {
brunofgc 0:8c59a494a451 1856 sscanf(linha,"id:%u",&dispositivos[dispositivoEmConfiguracao]->id);
brunofgc 0:8c59a494a451 1857 }*/
brunofgc 0:8c59a494a451 1858 break;
brunofgc 0:8c59a494a451 1859 case estadoConfiguracao_alarmSet:
brunofgc 0:8c59a494a451 1860 //Inclue linha nos alarmes do dispositivo em configuração
brunofgc 0:8c59a494a451 1861 dispositivos[dispositivoEmConfiguracao]->setAlarm(linha);
brunofgc 0:8c59a494a451 1862 break;
brunofgc 0:8c59a494a451 1863 case estadoConfiguracao_readingsSet:
brunofgc 0:8c59a494a451 1864 //Inclue linha nas leituras do dispositivo em configuração
brunofgc 0:8c59a494a451 1865 dispositivos[dispositivoEmConfiguracao]->setReading(linha);
brunofgc 0:8c59a494a451 1866 break;
brunofgc 0:8c59a494a451 1867 case estadoConfiguracao_scheduleSet:
brunofgc 4:3f92979d1ffb 1868 //Inclue linha nos agendamentos do dispositivo em configuração
brunofgc 0:8c59a494a451 1869 dispositivos[dispositivoEmConfiguracao]->setSchedule(linha);
brunofgc 4:3f92979d1ffb 1870 break;
brunofgc 4:3f92979d1ffb 1871 case estadoConfiguracao_scheduleExceptionSet:
brunofgc 4:3f92979d1ffb 1872 //Inclue linha nas excessões dos agendamentos do dispositivo em configuração
brunofgc 4:3f92979d1ffb 1873 dispositivos[dispositivoEmConfiguracao]->setScheduleException(linha);
brunofgc 0:8c59a494a451 1874 break;
brunofgc 0:8c59a494a451 1875 }
brunofgc 0:8c59a494a451 1876 }
brunofgc 2:ca3723da6668 1877
brunofgc 0:8c59a494a451 1878 return 1;
brunofgc 0:8c59a494a451 1879 }
brunofgc 0:8c59a494a451 1880
brunofgc 0:8c59a494a451 1881
brunofgc 0:8c59a494a451 1882
brunofgc 0:8c59a494a451 1883 void testaTudoDevices()
brunofgc 0:8c59a494a451 1884 {
brunofgc 0:8c59a494a451 1885 int i;
brunofgc 0:8c59a494a451 1886 int j;
brunofgc 0:8c59a494a451 1887
brunofgc 0:8c59a494a451 1888 for(i=0; i<devices; i++) {
brunofgc 0:8c59a494a451 1889 printf("Dispositivo %u tem %u alarmes sendo eles:\n",i,dispositivos[i]->numAlarms);
brunofgc 0:8c59a494a451 1890
brunofgc 0:8c59a494a451 1891 for(j=0; j<dispositivos[i]->numAlarms; j++) {
brunofgc 0:8c59a494a451 1892 printf("Alarme %u:\n",j);
brunofgc 0:8c59a494a451 1893 printf("\tId <%lu>.\n",dispositivos[i]->alarms[j].id);
brunofgc 0:8c59a494a451 1894 printf("\tSeconds <%lu>.\n",dispositivos[i]->alarms[j].seconds);
brunofgc 0:8c59a494a451 1895 printf("\tAddr %u, func %u, reg %lu.\n",dispositivos[i]->alarms[j].addrModbusRead,dispositivos[i]->alarms[j].funcModbusRead,dispositivos[i]->alarms[j].regModbusRead);
brunofgc 0:8c59a494a451 1896 if(dispositivos[i]->alarms[j].type==modBusType_float) {
brunofgc 0:8c59a494a451 1897 float value;
brunofgc 0:8c59a494a451 1898 dispositivos[i]->floatToBin(0,&value,&dispositivos[i]->alarms[j].value[0]);
brunofgc 0:8c59a494a451 1899 if(dispositivos[i]->alarms[j].max){
brunofgc 0:8c59a494a451 1900 printf("\tFloat max <%f>.\n",value);
brunofgc 0:8c59a494a451 1901 }
brunofgc 0:8c59a494a451 1902 else{printf("\tFloat min <%f>.\n",value);}
brunofgc 0:8c59a494a451 1903 }
brunofgc 0:8c59a494a451 1904
brunofgc 0:8c59a494a451 1905 if(dispositivos[i]->alarms[j].type==modBusType_uint32_t) {
brunofgc 0:8c59a494a451 1906 uint32_t value;
brunofgc 0:8c59a494a451 1907 dispositivos[i]->uint32_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]);
brunofgc 0:8c59a494a451 1908 if(dispositivos[i]->alarms[j].max){
brunofgc 0:8c59a494a451 1909 printf("\tuint32_t max <%lu>.\n",value);
brunofgc 0:8c59a494a451 1910 }
brunofgc 0:8c59a494a451 1911 else{printf("\tuint32_t min <%lu>.\n",value);}
brunofgc 0:8c59a494a451 1912 }
brunofgc 0:8c59a494a451 1913
brunofgc 0:8c59a494a451 1914 if(dispositivos[i]->alarms[j].type==modBusType_uint16_t) {
brunofgc 0:8c59a494a451 1915 uint16_t value;
brunofgc 0:8c59a494a451 1916 dispositivos[i]->uint16_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]);
brunofgc 0:8c59a494a451 1917 if(dispositivos[i]->alarms[j].max){
brunofgc 0:8c59a494a451 1918 printf("\tuint16_t max <%lu>.\n",value);
brunofgc 0:8c59a494a451 1919 }
brunofgc 0:8c59a494a451 1920 else{printf("\tuint16_t min <%lu>.\n",value);}
brunofgc 0:8c59a494a451 1921 }
brunofgc 0:8c59a494a451 1922
brunofgc 17:c046e4b07ded 1923 if(dispositivos[i]->alarms[j].type==modBusType_int32_t) {
brunofgc 17:c046e4b07ded 1924 uint32_t value;
brunofgc 17:c046e4b07ded 1925 dispositivos[i]->uint32_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]);
brunofgc 17:c046e4b07ded 1926 if(dispositivos[i]->alarms[j].max){
brunofgc 17:c046e4b07ded 1927 printf("\tint32_t max <%l>.\n",value);
brunofgc 17:c046e4b07ded 1928 }
brunofgc 17:c046e4b07ded 1929 else{printf("\tint32_t min <%l>.\n",value);}
brunofgc 17:c046e4b07ded 1930 }
brunofgc 17:c046e4b07ded 1931
brunofgc 17:c046e4b07ded 1932 if(dispositivos[i]->alarms[j].type==modBusType_int16_t) {
brunofgc 17:c046e4b07ded 1933 uint16_t value;
brunofgc 17:c046e4b07ded 1934 dispositivos[i]->uint16_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]);
brunofgc 17:c046e4b07ded 1935 if(dispositivos[i]->alarms[j].max){
brunofgc 17:c046e4b07ded 1936 printf("\tuint16_t max <%l>.\n",value);
brunofgc 17:c046e4b07ded 1937 }
brunofgc 17:c046e4b07ded 1938 else{printf("\tuint16_t min <%l>.\n",value);}
brunofgc 17:c046e4b07ded 1939 }
brunofgc 17:c046e4b07ded 1940
brunofgc 0:8c59a494a451 1941 if(dispositivos[i]->alarms[j].type==modBusType_bit) {
brunofgc 0:8c59a494a451 1942 uint8_t value;
brunofgc 0:8c59a494a451 1943 value=dispositivos[i]->alarms[j].value[0];
brunofgc 0:8c59a494a451 1944 if(dispositivos[i]->alarms[j].max){
brunofgc 0:8c59a494a451 1945 printf("\tBIT max <%lu>.\n",value);
brunofgc 0:8c59a494a451 1946 }
brunofgc 0:8c59a494a451 1947 else{printf("\tBIT min <%lu>.\n",value);}
brunofgc 0:8c59a494a451 1948 }
brunofgc 0:8c59a494a451 1949
brunofgc 0:8c59a494a451 1950 printf("\tact:%lu\n",dispositivos[i]->alarms[j].idAct);
brunofgc 0:8c59a494a451 1951 }
brunofgc 0:8c59a494a451 1952
brunofgc 4:3f92979d1ffb 1953 printf("Dispositivo %u tem %u schedules sendo eles:\n",i,dispositivos[i]->numSchedules);
brunofgc 0:8c59a494a451 1954 for(j=0; j<dispositivos[i]->numSchedules; j++) {
brunofgc 0:8c59a494a451 1955 printf("Schedule %u:\n",j);
brunofgc 0:8c59a494a451 1956 printf("\tId <%lu>.\n",dispositivos[i]->schedules[j].id);
brunofgc 0:8c59a494a451 1957 printf("\tweekday <%lu>.\n",dispositivos[i]->schedules[j].weekday);
brunofgc 0:8c59a494a451 1958 printf("\tminute <%lu>.\n",dispositivos[i]->schedules[j].minute);
brunofgc 0:8c59a494a451 1959 printf("\tidAct <%lu>.\n",dispositivos[i]->schedules[j].idAct);
brunofgc 0:8c59a494a451 1960 }
brunofgc 4:3f92979d1ffb 1961
brunofgc 4:3f92979d1ffb 1962 printf("Dispositivo %u tem %u scheduleExceptions sendo eles:\n",i,dispositivos[i]->numScheduleExceptions);
brunofgc 4:3f92979d1ffb 1963 for(j=0; j<dispositivos[i]->numScheduleExceptions; j++) {
brunofgc 4:3f92979d1ffb 1964 printf("scheduleException %u:\n",j);
brunofgc 4:3f92979d1ffb 1965 printf("\tid_schedule <%lu>.\n",dispositivos[i]->scheduleExceptions[j].id_schedule);
brunofgc 4:3f92979d1ffb 1966 printf("\ttimestamp <%lu>.\n",dispositivos[i]->scheduleExceptions[j].timestamp);
brunofgc 4:3f92979d1ffb 1967 }
brunofgc 0:8c59a494a451 1968
brunofgc 0:8c59a494a451 1969 printf("Dispositivo %u tem %u leituras sendo elas:\n",i,dispositivos[i]->numReadings);
brunofgc 0:8c59a494a451 1970 for(j=0; j<dispositivos[i]->numReadings; j++) {
brunofgc 0:8c59a494a451 1971 printf("Leitura %u:",j);
brunofgc 0:8c59a494a451 1972 switch(dispositivos[i]->readings[j].type) {
brunofgc 0:8c59a494a451 1973 case modBusType_float:
brunofgc 0:8c59a494a451 1974 printf("Tipo float ");
brunofgc 0:8c59a494a451 1975 break;
brunofgc 0:8c59a494a451 1976 case modBusType_uint32_t:
brunofgc 0:8c59a494a451 1977 printf("Tipo uint32_t ");
brunofgc 5:8efd609dea7d 1978 break;
brunofgc 0:8c59a494a451 1979 case modBusType_uint16_t:
brunofgc 0:8c59a494a451 1980 printf("Tipo uint16_t ");
brunofgc 5:8efd609dea7d 1981 break;
brunofgc 17:c046e4b07ded 1982 case modBusType_int32_t:
brunofgc 17:c046e4b07ded 1983 printf("Tipo int32_t ");
brunofgc 17:c046e4b07ded 1984 break;
brunofgc 17:c046e4b07ded 1985 case modBusType_int16_t:
brunofgc 17:c046e4b07ded 1986 printf("Tipo int16_t ");
brunofgc 17:c046e4b07ded 1987 break;
brunofgc 0:8c59a494a451 1988 case modBusType_uint8_t:
brunofgc 0:8c59a494a451 1989 printf("Tipo uint8_t ");
brunofgc 5:8efd609dea7d 1990 break;
brunofgc 0:8c59a494a451 1991 case modBusType_bit:
brunofgc 0:8c59a494a451 1992 printf("Tipo bool ");
brunofgc 5:8efd609dea7d 1993 break;
brunofgc 0:8c59a494a451 1994 }
brunofgc 0:8c59a494a451 1995 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);
brunofgc 0:8c59a494a451 1996 }
brunofgc 0:8c59a494a451 1997 }
brunofgc 0:8c59a494a451 1998 }