teste de publish
devices.cpp@6:f20c0e094294, 2017-08-09 (annotated)
- Committer:
- brunofgc
- Date:
- Wed Aug 09 20:37:54 2017 +0000
- Revision:
- 6:f20c0e094294
- Parent:
- 5:8efd609dea7d
- Child:
- 7:b4b2b8720166
Ultima vers?o com envia dados na thread comunicacoes
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:8efd609dea7d | 188 | if((this->alarms[alarmIndex].regModbusRead>100)&&(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 | 5:8efd609dea7d | 315 | if((this->alarms[alarmIndex].regModbusRead > 0) &&(this->alarms[alarmIndex].regModbusRead<4)){ |
brunofgc | 5:8efd609dea7d | 316 | leitura_uint16_t = aiFiltrada[this->alarms[alarmIndex].regModbusRead-1]; |
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 | 6:f20c0e094294 | 365 | case modBusType_bit: |
brunofgc | 5:8efd609dea7d | 366 | //Montar bit! |
brunofgc | 5:8efd609dea7d | 367 | //O valor de alarm para bit é o value[0]!!! |
brunofgc | 5:8efd609dea7d | 368 | value_bit = (this->alarms[alarmIndex].value[0] > 0); //Qualquer valor maior que zero da saída 1 |
brunofgc | 5:8efd609dea7d | 369 | |
brunofgc | 6:f20c0e094294 | 370 | if(this->alarms[alarmIndex].addrModbusRead != enderecoControladoraVirtual){//Bloco de leitura da variável modBus; |
brunofgc | 6:f20c0e094294 | 371 | //Bloco de leitura da variável modBus; |
brunofgc | 6:f20c0e094294 | 372 | modBusMaster1::readCoils( |
brunofgc | 6:f20c0e094294 | 373 | this->alarms[alarmIndex].addrModbusRead, |
brunofgc | 6:f20c0e094294 | 374 | this->alarms[alarmIndex].regModbusRead, |
brunofgc | 6:f20c0e094294 | 375 | 1, |
brunofgc | 6:f20c0e094294 | 376 | &leitura_bit |
brunofgc | 6:f20c0e094294 | 377 | ); |
brunofgc | 6:f20c0e094294 | 378 | }else{ |
brunofgc | 6:f20c0e094294 | 379 | leitura_bit = entradasDigitais[this->alarms[alarmIndex].regModbusRead]; |
brunofgc | 6:f20c0e094294 | 380 | } |
brunofgc | 5:8efd609dea7d | 381 | if(leitura_bit == value_bit){ |
brunofgc | 0:8c59a494a451 | 382 | this->alarms[alarmIndex].secAlarm++; |
brunofgc | 0:8c59a494a451 | 383 | if(this->alarms[alarmIndex].secAlarm >= this->alarms[alarmIndex].seconds){ |
brunofgc | 0:8c59a494a451 | 384 | this->alarms[alarmIndex].secAlarm = this->alarms[alarmIndex].seconds; |
brunofgc | 0:8c59a494a451 | 385 | alarmFound = true; |
brunofgc | 0:8c59a494a451 | 386 | } |
brunofgc | 0:8c59a494a451 | 387 | }else{ |
brunofgc | 0:8c59a494a451 | 388 | if(this->alarms[alarmIndex].secAlarm==1){ |
brunofgc | 0:8c59a494a451 | 389 | this->alarms[alarmIndex].secAlarm=0; |
brunofgc | 0:8c59a494a451 | 390 | alarmFound = false; |
brunofgc | 0:8c59a494a451 | 391 | }else{ |
brunofgc | 0:8c59a494a451 | 392 | if(this->alarms[alarmIndex].secAlarm){ |
brunofgc | 0:8c59a494a451 | 393 | if(alarmFound){ |
brunofgc | 0:8c59a494a451 | 394 | this->alarms[alarmIndex].secAlarm--; |
brunofgc | 0:8c59a494a451 | 395 | }else{ |
brunofgc | 0:8c59a494a451 | 396 | this->alarms[alarmIndex].secAlarm=0; |
brunofgc | 0:8c59a494a451 | 397 | } |
brunofgc | 0:8c59a494a451 | 398 | } |
brunofgc | 0:8c59a494a451 | 399 | } |
brunofgc | 5:8efd609dea7d | 400 | } |
brunofgc | 5:8efd609dea7d | 401 | break; |
brunofgc | 0:8c59a494a451 | 402 | |
brunofgc | 5:8efd609dea7d | 403 | } |
brunofgc | 0:8c59a494a451 | 404 | |
brunofgc | 5:8efd609dea7d | 405 | //Verifico se houve alarme não tratado |
brunofgc | 5:8efd609dea7d | 406 | if(this->alarms[alarmIndex].alarmFound!=alarmFound){ |
brunofgc | 5:8efd609dea7d | 407 | this->alarms[alarmIndex].alarmFound=alarmFound; |
brunofgc | 5:8efd609dea7d | 408 | if(alarmFound){ |
brunofgc | 5:8efd609dea7d | 409 | printf("Alarm FOUND! Id <%lu>.\n",this->alarms[alarmIndex].id); |
brunofgc | 5:8efd609dea7d | 410 | sdCardBuf.fill("log{alarm:event:found;id:",25); |
brunofgc | 5:8efd609dea7d | 411 | sprintf(aux,"%lu",this->alarms[alarmIndex].id); |
brunofgc | 5:8efd609dea7d | 412 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 5:8efd609dea7d | 413 | |
brunofgc | 5:8efd609dea7d | 414 | sdCardBuf.fill(";timestamp:",11); |
brunofgc | 5:8efd609dea7d | 415 | sprintf(aux,"%lu",time(NULL)); |
brunofgc | 5:8efd609dea7d | 416 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 5:8efd609dea7d | 417 | |
brunofgc | 5:8efd609dea7d | 418 | sdCardBuf.fill(";reading:",9); |
brunofgc | 5:8efd609dea7d | 419 | if(this->alarms[alarmIndex].type == modBusType_float){ |
brunofgc | 5:8efd609dea7d | 420 | sprintf(aux,"%f",leitura_float); |
brunofgc | 5:8efd609dea7d | 421 | }else if(this->alarms[alarmIndex].type == modBusType_uint32_t){ |
brunofgc | 5:8efd609dea7d | 422 | sprintf(aux,"%lu",leitura_uint32_t); |
brunofgc | 5:8efd609dea7d | 423 | }else if(this->alarms[alarmIndex].type == modBusType_uint16_t){ |
brunofgc | 5:8efd609dea7d | 424 | sprintf(aux,"%lu",leitura_uint16_t); |
brunofgc | 5:8efd609dea7d | 425 | }else if(this->alarms[alarmIndex].type == modBusType_bit){ |
brunofgc | 5:8efd609dea7d | 426 | sprintf(aux,"%lu",leitura_bit); |
brunofgc | 5:8efd609dea7d | 427 | } |
brunofgc | 5:8efd609dea7d | 428 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 5:8efd609dea7d | 429 | leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit |
brunofgc | 5:8efd609dea7d | 430 | sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),leitura_uint16_t); |
brunofgc | 5:8efd609dea7d | 431 | |
brunofgc | 5:8efd609dea7d | 432 | boolExecAct=true; |
brunofgc | 5:8efd609dea7d | 433 | act = this->alarms[alarmIndex].idAct; |
brunofgc | 5:8efd609dea7d | 434 | }else{ |
brunofgc | 5:8efd609dea7d | 435 | printf("Alarm RELEASE! id <%lu>.\n",this->alarms[alarmIndex].id); |
brunofgc | 5:8efd609dea7d | 436 | sdCardBuf.fill("log{alarm:event:release;id:",27); |
brunofgc | 5:8efd609dea7d | 437 | sprintf(aux,"%lu",this->alarms[alarmIndex].id); |
brunofgc | 5:8efd609dea7d | 438 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 5:8efd609dea7d | 439 | |
brunofgc | 5:8efd609dea7d | 440 | sdCardBuf.fill(";timestamp:",11); |
brunofgc | 5:8efd609dea7d | 441 | sprintf(aux,"%lu",time(NULL)); |
brunofgc | 5:8efd609dea7d | 442 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 5:8efd609dea7d | 443 | |
brunofgc | 5:8efd609dea7d | 444 | leitura_uint16_t = sdCardBuf.fill("}log",4); //Reaproveitando uma variável de 16bit |
brunofgc | 5:8efd609dea7d | 445 | sdCard::arquivoAberto(40000); |
brunofgc | 5:8efd609dea7d | 446 | sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),leitura_uint16_t); |
brunofgc | 5:8efd609dea7d | 447 | } |
brunofgc | 5:8efd609dea7d | 448 | //atrasa o envio em 3 segundos para que possa ser gravado o registro do alarme. |
brunofgc | 5:8efd609dea7d | 449 | enviaDadosPorAlarme=10; |
brunofgc | 5:8efd609dea7d | 450 | } |
brunofgc | 0:8c59a494a451 | 451 | } |
brunofgc | 0:8c59a494a451 | 452 | } |
brunofgc | 0:8c59a494a451 | 453 | } |
brunofgc | 0:8c59a494a451 | 454 | |
brunofgc | 0:8c59a494a451 | 455 | void device::verifySchedules(){ |
brunofgc | 5:8efd609dea7d | 456 | char stringTime[30]; |
brunofgc | 0:8c59a494a451 | 457 | char *weekdayString; |
brunofgc | 5:8efd609dea7d | 458 | time_t seconds = time(NULL)+5; |
brunofgc | 0:8c59a494a451 | 459 | uint8_t hora; |
brunofgc | 0:8c59a494a451 | 460 | uint16_t minuto; |
brunofgc | 0:8c59a494a451 | 461 | uint8_t weekday=0; |
brunofgc | 5:8efd609dea7d | 462 | uint16_t anoAtual=0; |
brunofgc | 5:8efd609dea7d | 463 | uint16_t diaAtual=0; |
brunofgc | 5:8efd609dea7d | 464 | uint16_t mesAtual=0; |
brunofgc | 5:8efd609dea7d | 465 | uint16_t anoScheduleException=0; |
brunofgc | 5:8efd609dea7d | 466 | uint16_t diaScheduleException=0; |
brunofgc | 5:8efd609dea7d | 467 | uint16_t mesScheduleException=0; |
brunofgc | 0:8c59a494a451 | 468 | const char* weekdayTable[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; |
brunofgc | 0:8c59a494a451 | 469 | uint8_t indexSchedule; |
brunofgc | 5:8efd609dea7d | 470 | uint8_t indexScheduleException; |
brunofgc | 5:8efd609dea7d | 471 | bool exceptionDetected=false; |
brunofgc | 0:8c59a494a451 | 472 | |
brunofgc | 5:8efd609dea7d | 473 | pc.printf("Verifing schedule.\r\n"); |
brunofgc | 0:8c59a494a451 | 474 | |
brunofgc | 5:8efd609dea7d | 475 | strftime(stringTime, 30, "%a,%R,%y,%d,%m", localtime(&seconds)); |
brunofgc | 5:8efd609dea7d | 476 | //pc.printf("VerifySchedule <%s>.\r\n",stringTime); |
brunofgc | 5:8efd609dea7d | 477 | //Thu,20:48,17,2,7 |
brunofgc | 0:8c59a494a451 | 478 | weekdayString = strtok(stringTime,","); |
brunofgc | 0:8c59a494a451 | 479 | weekday = 0; |
brunofgc | 0:8c59a494a451 | 480 | while((weekday<10)&&(strstr(weekdayTable[weekday],weekdayString)==NULL)){ |
brunofgc | 0:8c59a494a451 | 481 | weekday++; |
brunofgc | 0:8c59a494a451 | 482 | } |
brunofgc | 0:8c59a494a451 | 483 | hora = atoi(strtok(NULL,":")); |
brunofgc | 5:8efd609dea7d | 484 | minuto = atoi(strtok(NULL,",")); |
brunofgc | 5:8efd609dea7d | 485 | minuto = (hora*60)+minuto; |
brunofgc | 5:8efd609dea7d | 486 | anoAtual = atoi(strtok(NULL,",")); |
brunofgc | 5:8efd609dea7d | 487 | diaAtual = atoi(strtok(NULL,",")); |
brunofgc | 5:8efd609dea7d | 488 | mesAtual = atoi(strtok(NULL,"")); |
brunofgc | 5:8efd609dea7d | 489 | |
brunofgc | 5:8efd609dea7d | 490 | //pc.printf("Passo 1 .\r\n"); |
brunofgc | 5:8efd609dea7d | 491 | for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){ |
brunofgc | 5:8efd609dea7d | 492 | //pc.printf("Passo 2.\r\n"); |
brunofgc | 5:8efd609dea7d | 493 | if(this->schedules[indexSchedule].weekday&(0x1<<weekday)){ |
brunofgc | 5:8efd609dea7d | 494 | //pc.printf("Passo 3.\r\n"); |
brunofgc | 0:8c59a494a451 | 495 | if(this->schedules[indexSchedule].minute == minuto){ |
brunofgc | 5:8efd609dea7d | 496 | //pc.printf("Passo 4.\r\n"); |
brunofgc | 0:8c59a494a451 | 497 | //Verifico se já executei e então executo a ação. |
brunofgc | 0:8c59a494a451 | 498 | if(!this->schedules[indexSchedule].actSent){ |
brunofgc | 5:8efd609dea7d | 499 | //pc.printf("Passo 5 .\r\n"); |
brunofgc | 0:8c59a494a451 | 500 | //Executa a ação; |
brunofgc | 5:8efd609dea7d | 501 | for(indexScheduleException = 0;indexScheduleException<this->numScheduleExceptions;indexScheduleException++){ |
brunofgc | 5:8efd609dea7d | 502 | //pc.printf("Passo 6.\r\n"); |
brunofgc | 5:8efd609dea7d | 503 | if(this->schedules[indexSchedule].id == this->scheduleExceptions[indexScheduleException].id_schedule){ |
brunofgc | 5:8efd609dea7d | 504 | //pc.printf("Passo 7.\r\n"); |
brunofgc | 5:8efd609dea7d | 505 | seconds = this->scheduleExceptions[indexScheduleException].timestamp; |
brunofgc | 5:8efd609dea7d | 506 | strftime(stringTime, 30, "%y,%d,%m", localtime(&seconds)); |
brunofgc | 5:8efd609dea7d | 507 | anoScheduleException=atoi(strtok(stringTime,",")); |
brunofgc | 5:8efd609dea7d | 508 | diaScheduleException=atoi(strtok(NULL,",")); |
brunofgc | 5:8efd609dea7d | 509 | mesScheduleException=atoi(strtok(NULL,"")); |
brunofgc | 5:8efd609dea7d | 510 | /* |
brunofgc | 5:8efd609dea7d | 511 | pc.printf("\r\nDebug ScheduleException.\r\n"); |
brunofgc | 5:8efd609dea7d | 512 | pc.printf("stringTime <%s>.\r\n",stringTime); |
brunofgc | 5:8efd609dea7d | 513 | pc.printf("anoScheduleException %lu.",anoScheduleException); |
brunofgc | 5:8efd609dea7d | 514 | pc.printf("diaScheduleException %lu.",diaScheduleException); |
brunofgc | 5:8efd609dea7d | 515 | pc.printf("mesScheduleException %lu.",mesScheduleException); |
brunofgc | 5:8efd609dea7d | 516 | pc.printf("anoAtual %lu.",anoAtual); |
brunofgc | 5:8efd609dea7d | 517 | pc.printf("diaAtual %lu.",diaAtual); |
brunofgc | 5:8efd609dea7d | 518 | pc.printf("mesAtual %lu.",mesAtual); |
brunofgc | 5:8efd609dea7d | 519 | pc.printf("\r\n"); |
brunofgc | 5:8efd609dea7d | 520 | */ |
brunofgc | 5:8efd609dea7d | 521 | if( |
brunofgc | 5:8efd609dea7d | 522 | ((anoScheduleException == anoAtual) || (anoScheduleException == 70)) |
brunofgc | 5:8efd609dea7d | 523 | && |
brunofgc | 5:8efd609dea7d | 524 | (diaAtual == diaScheduleException) |
brunofgc | 5:8efd609dea7d | 525 | && |
brunofgc | 5:8efd609dea7d | 526 | (mesAtual == mesScheduleException) |
brunofgc | 5:8efd609dea7d | 527 | ) |
brunofgc | 5:8efd609dea7d | 528 | { |
brunofgc | 5:8efd609dea7d | 529 | pc.printf("ScheduleException detected.\r\n"); |
brunofgc | 5:8efd609dea7d | 530 | exceptionDetected=true; |
brunofgc | 5:8efd609dea7d | 531 | } |
brunofgc | 5:8efd609dea7d | 532 | } |
brunofgc | 5:8efd609dea7d | 533 | } |
brunofgc | 5:8efd609dea7d | 534 | if(!exceptionDetected){ |
brunofgc | 5:8efd609dea7d | 535 | this->execAct(this->schedules[indexSchedule].idAct); |
brunofgc | 5:8efd609dea7d | 536 | this->schedules[indexSchedule].actSent = true; |
brunofgc | 5:8efd609dea7d | 537 | } |
brunofgc | 0:8c59a494a451 | 538 | } |
brunofgc | 0:8c59a494a451 | 539 | }else{ |
brunofgc | 0:8c59a494a451 | 540 | this->schedules[indexSchedule].actSent = false; |
brunofgc | 0:8c59a494a451 | 541 | } |
brunofgc | 5:8efd609dea7d | 542 | } |
brunofgc | 0:8c59a494a451 | 543 | } |
brunofgc | 0:8c59a494a451 | 544 | } |
brunofgc | 0:8c59a494a451 | 545 | |
brunofgc | 0:8c59a494a451 | 546 | /* |
brunofgc | 0:8c59a494a451 | 547 | void device::verifySchedules(){ |
brunofgc | 0:8c59a494a451 | 548 | char stringTime[15]; |
brunofgc | 0:8c59a494a451 | 549 | char *weekdayString; |
brunofgc | 0:8c59a494a451 | 550 | time_t seconds = time(NULL); |
brunofgc | 0:8c59a494a451 | 551 | uint8_t hora; |
brunofgc | 0:8c59a494a451 | 552 | uint16_t minuto; |
brunofgc | 0:8c59a494a451 | 553 | uint8_t weekday=0; |
brunofgc | 0:8c59a494a451 | 554 | const char* weekdayTable[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; |
brunofgc | 0:8c59a494a451 | 555 | uint8_t indexSchedule; |
brunofgc | 0:8c59a494a451 | 556 | |
brunofgc | 0:8c59a494a451 | 557 | //Tue,19:09 |
brunofgc | 0:8c59a494a451 | 558 | strftime(stringTime, 15, "%a,%R", localtime(&seconds)); |
brunofgc | 0:8c59a494a451 | 559 | |
brunofgc | 0:8c59a494a451 | 560 | weekdayString = strtok(stringTime,","); |
brunofgc | 0:8c59a494a451 | 561 | weekday = 0; |
brunofgc | 0:8c59a494a451 | 562 | while((weekday<10)&&(strstr(weekdayTable[weekday],weekdayString)==NULL)){ |
brunofgc | 0:8c59a494a451 | 563 | weekday++; |
brunofgc | 0:8c59a494a451 | 564 | } |
brunofgc | 0:8c59a494a451 | 565 | weekday++; |
brunofgc | 0:8c59a494a451 | 566 | hora = atoi(strtok(NULL,":")); |
brunofgc | 0:8c59a494a451 | 567 | minuto = atoi(strtok(NULL,"")); |
brunofgc | 0:8c59a494a451 | 568 | minuto = (hora*60)+minuto; |
brunofgc | 0:8c59a494a451 | 569 | //printf("\r\n\r\nMinuto %lu.\r\n\r\n",minuto); |
brunofgc | 0:8c59a494a451 | 570 | for(indexSchedule = 0; indexSchedule < qtdSchedules; indexSchedule++){ |
brunofgc | 0:8c59a494a451 | 571 | //printf("Executando item %u do for de schedules.\r\n\r\n",indexSchedule); |
brunofgc | 0:8c59a494a451 | 572 | if((this->schedules[indexSchedule].weekday == weekday)||(this->schedules[indexSchedule].weekday == 0)){ |
brunofgc | 0:8c59a494a451 | 573 | //Se for um alarme para o dia de hoje ou o alarme independer de dia (Se o dia for zero) |
brunofgc | 0:8c59a494a451 | 574 | //printf("Hoje eh dia de executar a acão do schedule indice %u.\r\n\r\n",indexSchedule); |
brunofgc | 0:8c59a494a451 | 575 | if(this->schedules[indexSchedule].minute == minuto){ |
brunofgc | 0:8c59a494a451 | 576 | //Verifico se já executei e então executo a ação. |
brunofgc | 0:8c59a494a451 | 577 | if(!this->schedules[indexSchedule].actSent){ |
brunofgc | 0:8c59a494a451 | 578 | //Executa a ação; |
brunofgc | 0:8c59a494a451 | 579 | //printf("Executando a acao!\r\n\r\n"); |
brunofgc | 0:8c59a494a451 | 580 | this->execAct(this->schedules[indexSchedule].idAct); |
brunofgc | 0:8c59a494a451 | 581 | this->schedules[indexSchedule].actSent = true; |
brunofgc | 0:8c59a494a451 | 582 | } |
brunofgc | 0:8c59a494a451 | 583 | }else{ |
brunofgc | 0:8c59a494a451 | 584 | this->schedules[indexSchedule].actSent = false; |
brunofgc | 0:8c59a494a451 | 585 | } |
brunofgc | 0:8c59a494a451 | 586 | } |
brunofgc | 0:8c59a494a451 | 587 | } |
brunofgc | 0:8c59a494a451 | 588 | } |
brunofgc | 0:8c59a494a451 | 589 | */ |
brunofgc | 0:8c59a494a451 | 590 | /* |
brunofgc | 0:8c59a494a451 | 591 | uint8_t device::execActSchedule(uint32_t index){ |
brunofgc | 0:8c59a494a451 | 592 | switch(this->schedules[index].actType){ |
brunofgc | 0:8c59a494a451 | 593 | case modBusType_float: |
brunofgc | 0:8c59a494a451 | 594 | float floatValueToWrite; |
brunofgc | 0:8c59a494a451 | 595 | //writeFloat(uint8_t,uint16_t,uint8_t,float*); |
brunofgc | 0:8c59a494a451 | 596 | this->floatToBin(0,&floatValueToWrite,&this->schedules[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 597 | modBusMaster1::writeFloat( |
brunofgc | 0:8c59a494a451 | 598 | this->schedules[index].param1, |
brunofgc | 0:8c59a494a451 | 599 | this->schedules[index].param2, |
brunofgc | 0:8c59a494a451 | 600 | this->schedules[index].param3, |
brunofgc | 0:8c59a494a451 | 601 | &floatValueToWrite |
brunofgc | 0:8c59a494a451 | 602 | ); |
brunofgc | 0:8c59a494a451 | 603 | break; |
brunofgc | 0:8c59a494a451 | 604 | case modBusType_uint32_t: |
brunofgc | 0:8c59a494a451 | 605 | uint32_t uint32ValueToWrite; |
brunofgc | 0:8c59a494a451 | 606 | this->uint32_t_ToBin(0,&uint32ValueToWrite,&this->schedules[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 607 | modBusMaster1::writeRegister32BIT( |
brunofgc | 0:8c59a494a451 | 608 | this->schedules[index].param1, |
brunofgc | 0:8c59a494a451 | 609 | this->schedules[index].param2, |
brunofgc | 0:8c59a494a451 | 610 | this->schedules[index].param3, |
brunofgc | 0:8c59a494a451 | 611 | &uint32ValueToWrite |
brunofgc | 0:8c59a494a451 | 612 | ); |
brunofgc | 0:8c59a494a451 | 613 | break; |
brunofgc | 0:8c59a494a451 | 614 | case modBusType_uint16_t: |
brunofgc | 0:8c59a494a451 | 615 | uint16_t uint16ValueToWrite; |
brunofgc | 0:8c59a494a451 | 616 | this->uint16_t_ToBin(0,&uint16ValueToWrite,&this->schedules[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 617 | modBusMaster1::writeRegister16BIT( |
brunofgc | 0:8c59a494a451 | 618 | this->schedules[index].param1, |
brunofgc | 0:8c59a494a451 | 619 | this->schedules[index].param2, |
brunofgc | 0:8c59a494a451 | 620 | this->schedules[index].param3, |
brunofgc | 0:8c59a494a451 | 621 | &uint16ValueToWrite |
brunofgc | 0:8c59a494a451 | 622 | ); |
brunofgc | 0:8c59a494a451 | 623 | break; |
brunofgc | 0:8c59a494a451 | 624 | case modBusType_bit: |
brunofgc | 0:8c59a494a451 | 625 | bool boolValueToWrite; |
brunofgc | 0:8c59a494a451 | 626 | boolValueToWrite = (this->schedules[index].actValue[0] != 0); |
brunofgc | 0:8c59a494a451 | 627 | modBusMaster1::writeSingleCoil( |
brunofgc | 0:8c59a494a451 | 628 | this->schedules[index].param1, |
brunofgc | 0:8c59a494a451 | 629 | this->schedules[index].param3, |
brunofgc | 0:8c59a494a451 | 630 | boolValueToWrite |
brunofgc | 0:8c59a494a451 | 631 | ); |
brunofgc | 0:8c59a494a451 | 632 | break; |
brunofgc | 0:8c59a494a451 | 633 | case IrCommandType: |
brunofgc | 0:8c59a494a451 | 634 | //Busca no arquivo |
brunofgc | 0:8c59a494a451 | 635 | //Abre arquivo |
brunofgc | 0:8c59a494a451 | 636 | char comandoIr[1024]; |
brunofgc | 0:8c59a494a451 | 637 | char indexBusca[10]; |
brunofgc | 0:8c59a494a451 | 638 | bool endOfFile = false; |
brunofgc | 0:8c59a494a451 | 639 | |
brunofgc | 0:8c59a494a451 | 640 | //printf("\r\n===================DEBUG(Schedule)===================\r\n"); |
brunofgc | 0:8c59a494a451 | 641 | //"Ir:0,6E01FFFFS" |
brunofgc | 0:8c59a494a451 | 642 | sprintf(indexBusca,"Ir:%u,",this->schedules[index].param3); |
brunofgc | 0:8c59a494a451 | 643 | //printf("indexBusca = <%s>.\r\n",indexBusca); |
brunofgc | 0:8c59a494a451 | 644 | if(sdCard::abreArquivo(&sdCard::devices,"r")){ |
brunofgc | 0:8c59a494a451 | 645 | do{ |
brunofgc | 0:8c59a494a451 | 646 | fgets(comandoIr,1024,sdCard::devices.fp); |
brunofgc | 0:8c59a494a451 | 647 | endOfFile = feof(sdCard::devices.fp); |
brunofgc | 0:8c59a494a451 | 648 | //printf("Linha comando Ir = <%s>.\r\nendOfFile = %u\r\n",comandoIr,endOfFile); |
brunofgc | 0:8c59a494a451 | 649 | }while((strstr(comandoIr,indexBusca)==NULL)&&(endOfFile==0)); |
brunofgc | 0:8c59a494a451 | 650 | sdCard::fechaArquivo(&sdCard::devices); |
brunofgc | 0:8c59a494a451 | 651 | |
brunofgc | 0:8c59a494a451 | 652 | //Vê se deu tudo certo, se não encontrou, retorna false |
brunofgc | 0:8c59a494a451 | 653 | if(!endOfFile){ |
brunofgc | 0:8c59a494a451 | 654 | //Encontrado o comando, vou desempacotar ele. |
brunofgc | 0:8c59a494a451 | 655 | strtok(comandoIr,","); |
brunofgc | 0:8c59a494a451 | 656 | //Neste ponto já tenho o meu comando pronto para ser executado dentro de comandoIr; |
brunofgc | 0:8c59a494a451 | 657 | //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 | 658 | deserializaPacoteIR(strtok(NULL,"S")); |
brunofgc | 0:8c59a494a451 | 659 | enviaComandoIR(this->schedules[index].param1,this->schedules[index].param2); |
brunofgc | 0:8c59a494a451 | 660 | } |
brunofgc | 0:8c59a494a451 | 661 | }else{ |
brunofgc | 0:8c59a494a451 | 662 | return false; |
brunofgc | 0:8c59a494a451 | 663 | } |
brunofgc | 0:8c59a494a451 | 664 | //printf("\r\n===================DEBUG(Schedule)===================\r\n"); |
brunofgc | 0:8c59a494a451 | 665 | break; |
brunofgc | 0:8c59a494a451 | 666 | } |
brunofgc | 0:8c59a494a451 | 667 | return true; |
brunofgc | 0:8c59a494a451 | 668 | } |
brunofgc | 0:8c59a494a451 | 669 | */ |
brunofgc | 0:8c59a494a451 | 670 | /* |
brunofgc | 0:8c59a494a451 | 671 | uint8_t device::execActAlarm(uint32_t index){ |
brunofgc | 0:8c59a494a451 | 672 | switch(this->alarms[index].actType){ |
brunofgc | 0:8c59a494a451 | 673 | case modBusType_float: |
brunofgc | 0:8c59a494a451 | 674 | float floatValueToWrite; |
brunofgc | 0:8c59a494a451 | 675 | //writeFloat(uint8_t,uint16_t,uint8_t,float*); |
brunofgc | 0:8c59a494a451 | 676 | this->floatToBin(0,&floatValueToWrite,&this->alarms[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 677 | modBusMaster1::writeFloat( |
brunofgc | 0:8c59a494a451 | 678 | this->alarms[index].addrModbusWrite, |
brunofgc | 0:8c59a494a451 | 679 | this->alarms[index].funcModbusWrite, |
brunofgc | 0:8c59a494a451 | 680 | this->alarms[index].regModbusWrite, |
brunofgc | 0:8c59a494a451 | 681 | &floatValueToWrite |
brunofgc | 0:8c59a494a451 | 682 | ); |
brunofgc | 0:8c59a494a451 | 683 | break; |
brunofgc | 0:8c59a494a451 | 684 | case modBusType_uint32_t: |
brunofgc | 0:8c59a494a451 | 685 | uint32_t uint32ValueToWrite; |
brunofgc | 0:8c59a494a451 | 686 | this->uint32_t_ToBin(0,&uint32ValueToWrite,&this->alarms[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 687 | modBusMaster1::writeRegister32BIT( |
brunofgc | 0:8c59a494a451 | 688 | this->alarms[index].addrModbusWrite, |
brunofgc | 0:8c59a494a451 | 689 | this->alarms[index].funcModbusWrite, |
brunofgc | 0:8c59a494a451 | 690 | this->alarms[index].regModbusWrite, |
brunofgc | 0:8c59a494a451 | 691 | &uint32ValueToWrite |
brunofgc | 0:8c59a494a451 | 692 | ); |
brunofgc | 0:8c59a494a451 | 693 | break; |
brunofgc | 0:8c59a494a451 | 694 | case modBusType_uint16_t: |
brunofgc | 0:8c59a494a451 | 695 | uint16_t uint16ValueToWrite; |
brunofgc | 0:8c59a494a451 | 696 | this->uint16_t_ToBin(0,&uint16ValueToWrite,&this->alarms[index].actValue[0]); |
brunofgc | 0:8c59a494a451 | 697 | modBusMaster1::writeRegister16BIT( |
brunofgc | 0:8c59a494a451 | 698 | this->alarms[index].addrModbusWrite, |
brunofgc | 0:8c59a494a451 | 699 | this->alarms[index].funcModbusWrite, |
brunofgc | 0:8c59a494a451 | 700 | this->alarms[index].regModbusWrite, |
brunofgc | 0:8c59a494a451 | 701 | &uint16ValueToWrite |
brunofgc | 0:8c59a494a451 | 702 | ); |
brunofgc | 0:8c59a494a451 | 703 | break; |
brunofgc | 0:8c59a494a451 | 704 | case modBusType_bit: |
brunofgc | 0:8c59a494a451 | 705 | bool boolValueToWrite; |
brunofgc | 0:8c59a494a451 | 706 | boolValueToWrite = (this->alarms[index].actValue[0] != 0); |
brunofgc | 0:8c59a494a451 | 707 | modBusMaster1::writeSingleCoil( |
brunofgc | 0:8c59a494a451 | 708 | this->alarms[index].addrModbusWrite, |
brunofgc | 0:8c59a494a451 | 709 | this->alarms[index].regModbusWrite, |
brunofgc | 0:8c59a494a451 | 710 | boolValueToWrite |
brunofgc | 0:8c59a494a451 | 711 | ); |
brunofgc | 0:8c59a494a451 | 712 | break; |
brunofgc | 0:8c59a494a451 | 713 | } |
brunofgc | 0:8c59a494a451 | 714 | return true; |
brunofgc | 0:8c59a494a451 | 715 | } |
brunofgc | 0:8c59a494a451 | 716 | */ |
brunofgc | 0:8c59a494a451 | 717 | |
brunofgc | 0:8c59a494a451 | 718 | void device::setReading(char *linha) |
brunofgc | 0:8c59a494a451 | 719 | { |
brunofgc | 0:8c59a494a451 | 720 | uint8_t addr=0; |
brunofgc | 0:8c59a494a451 | 721 | uint8_t func=0; |
brunofgc | 0:8c59a494a451 | 722 | uint16_t reg=0; |
brunofgc | 0:8c59a494a451 | 723 | uint8_t numRegs=0; |
brunofgc | 0:8c59a494a451 | 724 | char *pChar; |
brunofgc | 0:8c59a494a451 | 725 | bool inputData = false; |
brunofgc | 0:8c59a494a451 | 726 | |
brunofgc | 0:8c59a494a451 | 727 | //printf("Linha <%s>.\n",linha); |
brunofgc | 0:8c59a494a451 | 728 | if(strstr(linha,"readingsSet")) { |
brunofgc | 0:8c59a494a451 | 729 | return; |
brunofgc | 0:8c59a494a451 | 730 | } |
brunofgc | 0:8c59a494a451 | 731 | |
brunofgc | 2:ca3723da6668 | 732 | this->numReadings++; |
brunofgc | 0:8c59a494a451 | 733 | |
brunofgc | 0:8c59a494a451 | 734 | if(strstr(linha,"float,")) { |
brunofgc | 0:8c59a494a451 | 735 | this->readings[this->numReadings-1].type=modBusType_float; |
brunofgc | 0:8c59a494a451 | 736 | inputData = true; |
brunofgc | 0:8c59a494a451 | 737 | } |
brunofgc | 0:8c59a494a451 | 738 | |
brunofgc | 0:8c59a494a451 | 739 | if(strstr(linha,"uint32_t,")) { |
brunofgc | 0:8c59a494a451 | 740 | this->readings[this->numReadings-1].type=modBusType_uint32_t; |
brunofgc | 0:8c59a494a451 | 741 | inputData = true; |
brunofgc | 0:8c59a494a451 | 742 | } |
brunofgc | 0:8c59a494a451 | 743 | |
brunofgc | 0:8c59a494a451 | 744 | if(strstr(linha,"uint16_t,")) { |
brunofgc | 0:8c59a494a451 | 745 | this->readings[this->numReadings-1].type=modBusType_uint16_t; |
brunofgc | 0:8c59a494a451 | 746 | inputData = true; |
brunofgc | 0:8c59a494a451 | 747 | } |
brunofgc | 0:8c59a494a451 | 748 | |
brunofgc | 0:8c59a494a451 | 749 | if(strstr(linha,"uint8_t,")) { |
brunofgc | 0:8c59a494a451 | 750 | this->readings[this->numReadings-1].type=modBusType_uint8_t; |
brunofgc | 0:8c59a494a451 | 751 | inputData = true; |
brunofgc | 0:8c59a494a451 | 752 | } |
brunofgc | 0:8c59a494a451 | 753 | |
brunofgc | 0:8c59a494a451 | 754 | if(strstr(linha,"bit,")) { |
brunofgc | 0:8c59a494a451 | 755 | this->readings[this->numReadings-1].type=modBusType_bit; |
brunofgc | 0:8c59a494a451 | 756 | inputData = true; |
brunofgc | 0:8c59a494a451 | 757 | } |
brunofgc | 0:8c59a494a451 | 758 | |
brunofgc | 0:8c59a494a451 | 759 | if(inputData){ |
brunofgc | 0:8c59a494a451 | 760 | pChar = strtok(linha,","); |
brunofgc | 0:8c59a494a451 | 761 | |
brunofgc | 0:8c59a494a451 | 762 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 763 | addr = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 764 | |
brunofgc | 0:8c59a494a451 | 765 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 766 | func = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 767 | |
brunofgc | 0:8c59a494a451 | 768 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 769 | reg = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 770 | |
brunofgc | 0:8c59a494a451 | 771 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 772 | numRegs = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 773 | |
brunofgc | 0:8c59a494a451 | 774 | this->readings[this->numReadings-1].addr=addr; |
brunofgc | 0:8c59a494a451 | 775 | this->readings[this->numReadings-1].func=func; |
brunofgc | 0:8c59a494a451 | 776 | this->readings[this->numReadings-1].reg=reg; |
brunofgc | 0:8c59a494a451 | 777 | this->readings[this->numReadings-1].numRegs=numRegs; |
brunofgc | 0:8c59a494a451 | 778 | } |
brunofgc | 0:8c59a494a451 | 779 | |
brunofgc | 0:8c59a494a451 | 780 | } |
brunofgc | 0:8c59a494a451 | 781 | |
brunofgc | 0:8c59a494a451 | 782 | void device::setAlarm(char *linha) |
brunofgc | 0:8c59a494a451 | 783 | { |
brunofgc | 0:8c59a494a451 | 784 | float floatVar; |
brunofgc | 0:8c59a494a451 | 785 | uint32_t aux32_t; |
brunofgc | 0:8c59a494a451 | 786 | uint16_t aux16_t; |
brunofgc | 0:8c59a494a451 | 787 | char *pChar; |
brunofgc | 0:8c59a494a451 | 788 | |
brunofgc | 0:8c59a494a451 | 789 | if(strstr(linha,"alarmSet")) { |
brunofgc | 2:ca3723da6668 | 790 | //Inicialização de contadores de alarme |
brunofgc | 2:ca3723da6668 | 791 | //É necessário inicializar estes dados para quando a máquina de alarmes rodar ter um inicio correto. |
brunofgc | 2:ca3723da6668 | 792 | this->alarms[this->numAlarms].secAlarm=0; |
brunofgc | 2:ca3723da6668 | 793 | this->alarms[this->numAlarms].idAct=0; |
brunofgc | 5:8efd609dea7d | 794 | this->alarms[this->numAlarms].on=0; |
brunofgc | 2:ca3723da6668 | 795 | this->alarms[this->numAlarms].alarmFound = false; |
brunofgc | 0:8c59a494a451 | 796 | //Inicialização de contadores de alarme |
brunofgc | 0:8c59a494a451 | 797 | this->numAlarms++; |
brunofgc | 0:8c59a494a451 | 798 | } |
brunofgc | 0:8c59a494a451 | 799 | |
brunofgc | 5:8efd609dea7d | 800 | if(strstr(linha,"idAlarm:")){ |
brunofgc | 0:8c59a494a451 | 801 | strtok(linha,":"); |
brunofgc | 0:8c59a494a451 | 802 | this->alarms[this->numAlarms-1].id = atoi(strtok(NULL,"\r\n")); |
brunofgc | 0:8c59a494a451 | 803 | } |
brunofgc | 0:8c59a494a451 | 804 | |
brunofgc | 5:8efd609dea7d | 805 | if(strstr(linha,"seconds:")) { |
brunofgc | 0:8c59a494a451 | 806 | strtok(linha,":"); |
brunofgc | 0:8c59a494a451 | 807 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 0:8c59a494a451 | 808 | this->alarms[this->numAlarms-1].seconds = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 809 | } |
brunofgc | 0:8c59a494a451 | 810 | |
brunofgc | 5:8efd609dea7d | 811 | if(strstr(linha,"on:")) { |
brunofgc | 5:8efd609dea7d | 812 | strtok(linha,":"); |
brunofgc | 5:8efd609dea7d | 813 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 5:8efd609dea7d | 814 | this->alarms[this->numAlarms-1].on = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 815 | } |
brunofgc | 5:8efd609dea7d | 816 | |
brunofgc | 5:8efd609dea7d | 817 | if(strstr(linha,"idAct:")) { |
brunofgc | 0:8c59a494a451 | 818 | strtok(linha,":"); |
brunofgc | 0:8c59a494a451 | 819 | this->alarms[this->numAlarms-1].idAct = atoi(strtok(NULL,"")); |
brunofgc | 0:8c59a494a451 | 820 | return; |
brunofgc | 5:8efd609dea7d | 821 | } |
brunofgc | 5:8efd609dea7d | 822 | |
brunofgc | 5:8efd609dea7d | 823 | if(strstr(linha,"bit,")){ |
brunofgc | 5:8efd609dea7d | 824 | this->alarms[this->numAlarms-1].type=modBusType_bit; |
brunofgc | 5:8efd609dea7d | 825 | |
brunofgc | 0:8c59a494a451 | 826 | pChar = strtok(linha,","); |
brunofgc | 0:8c59a494a451 | 827 | |
brunofgc | 0:8c59a494a451 | 828 | //Parametro1 |
brunofgc | 0:8c59a494a451 | 829 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 830 | this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 831 | |
brunofgc | 0:8c59a494a451 | 832 | //Parametro2 |
brunofgc | 0:8c59a494a451 | 833 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 834 | this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 835 | |
brunofgc | 0:8c59a494a451 | 836 | //Parametro3 |
brunofgc | 0:8c59a494a451 | 837 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 838 | this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 839 | |
brunofgc | 0:8c59a494a451 | 840 | //Parametro4 |
brunofgc | 0:8c59a494a451 | 841 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 842 | this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 0:8c59a494a451 | 843 | |
brunofgc | 0:8c59a494a451 | 844 | //Parametro5 |
brunofgc | 0:8c59a494a451 | 845 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 846 | this->alarms[this->numAlarms-1].value[0]= (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 0:8c59a494a451 | 847 | } |
brunofgc | 0:8c59a494a451 | 848 | |
brunofgc | 0:8c59a494a451 | 849 | if(strstr(linha,"float,")) { |
brunofgc | 0:8c59a494a451 | 850 | this->alarms[this->numAlarms-1].type=modBusType_float; |
brunofgc | 0:8c59a494a451 | 851 | strtok(linha,","); |
brunofgc | 0:8c59a494a451 | 852 | |
brunofgc | 0:8c59a494a451 | 853 | //Parametro 1; |
brunofgc | 0:8c59a494a451 | 854 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 855 | this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 856 | |
brunofgc | 0:8c59a494a451 | 857 | //Parametro 2; |
brunofgc | 0:8c59a494a451 | 858 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 859 | this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 860 | |
brunofgc | 0:8c59a494a451 | 861 | //Parametro 3; |
brunofgc | 0:8c59a494a451 | 862 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 863 | this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 864 | |
brunofgc | 0:8c59a494a451 | 865 | //Parametro 4; |
brunofgc | 0:8c59a494a451 | 866 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 867 | this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 0:8c59a494a451 | 868 | |
brunofgc | 0:8c59a494a451 | 869 | //Parametro 5; |
brunofgc | 0:8c59a494a451 | 870 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 0:8c59a494a451 | 871 | //floatVar = diversos::stringToFloat(pChar,100); |
brunofgc | 0:8c59a494a451 | 872 | floatVar = atof(pChar); |
brunofgc | 5:8efd609dea7d | 873 | |
brunofgc | 0:8c59a494a451 | 874 | this->floatToBin(1,&floatVar,&this->alarms[this->numAlarms-1].value[0]); |
brunofgc | 0:8c59a494a451 | 875 | } |
brunofgc | 5:8efd609dea7d | 876 | |
brunofgc | 0:8c59a494a451 | 877 | if(strstr(linha,"uint32_t,")) { |
brunofgc | 0:8c59a494a451 | 878 | this->alarms[this->numAlarms-1].type=modBusType_uint32_t; |
brunofgc | 0:8c59a494a451 | 879 | //sscanf(linha,"max:uint32_t,%lu,%lu,%lu,%lu",&aux1,&aux2,&aux3,&aux4); |
brunofgc | 0:8c59a494a451 | 880 | strtok(linha,","); |
brunofgc | 0:8c59a494a451 | 881 | |
brunofgc | 0:8c59a494a451 | 882 | //parametro1 |
brunofgc | 5:8efd609dea7d | 883 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 884 | this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 885 | |
brunofgc | 0:8c59a494a451 | 886 | //parametro2 |
brunofgc | 5:8efd609dea7d | 887 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 888 | this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 889 | |
brunofgc | 0:8c59a494a451 | 890 | //parametro3 |
brunofgc | 5:8efd609dea7d | 891 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 892 | this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 893 | |
brunofgc | 0:8c59a494a451 | 894 | //Parametro 4; |
brunofgc | 0:8c59a494a451 | 895 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 896 | this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 0:8c59a494a451 | 897 | |
brunofgc | 0:8c59a494a451 | 898 | //parametro5 |
brunofgc | 5:8efd609dea7d | 899 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 0:8c59a494a451 | 900 | aux32_t = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 901 | |
brunofgc | 0:8c59a494a451 | 902 | this->uint32_t_ToBin(1,&aux32_t,&this->alarms[this->numAlarms-1].value[0]); |
brunofgc | 0:8c59a494a451 | 903 | } |
brunofgc | 0:8c59a494a451 | 904 | |
brunofgc | 0:8c59a494a451 | 905 | |
brunofgc | 0:8c59a494a451 | 906 | |
brunofgc | 0:8c59a494a451 | 907 | if(strstr(linha,"uint16_t,")) { |
brunofgc | 0:8c59a494a451 | 908 | this->alarms[this->numAlarms-1].type=modBusType_uint16_t; |
brunofgc | 0:8c59a494a451 | 909 | strtok(linha,","); |
brunofgc | 0:8c59a494a451 | 910 | |
brunofgc | 0:8c59a494a451 | 911 | //parametro1 |
brunofgc | 0:8c59a494a451 | 912 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 913 | this->alarms[this->numAlarms-1].addrModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 914 | |
brunofgc | 0:8c59a494a451 | 915 | //parametro2 |
brunofgc | 5:8efd609dea7d | 916 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 917 | this->alarms[this->numAlarms-1].funcModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 918 | |
brunofgc | 0:8c59a494a451 | 919 | //parametro3 |
brunofgc | 5:8efd609dea7d | 920 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 921 | this->alarms[this->numAlarms-1].regModbusRead = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 922 | |
brunofgc | 0:8c59a494a451 | 923 | //Parametro 4; |
brunofgc | 0:8c59a494a451 | 924 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 925 | this->alarms[this->numAlarms-1].max = (atoi(pChar) != 0); //Qualquer valor diferente de 0 é um |
brunofgc | 0:8c59a494a451 | 926 | |
brunofgc | 0:8c59a494a451 | 927 | //parametro5 |
brunofgc | 5:8efd609dea7d | 928 | pChar = strtok(NULL,"\r\n"); |
brunofgc | 0:8c59a494a451 | 929 | aux16_t = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 930 | |
brunofgc | 0:8c59a494a451 | 931 | this->uint16_t_ToBin(1,&aux16_t,&this->alarms[this->numAlarms-1].value[0]); |
brunofgc | 5:8efd609dea7d | 932 | } |
brunofgc | 0:8c59a494a451 | 933 | } |
brunofgc | 0:8c59a494a451 | 934 | |
brunofgc | 0:8c59a494a451 | 935 | void device::setSchedule(char *linha) |
brunofgc | 0:8c59a494a451 | 936 | { |
brunofgc | 0:8c59a494a451 | 937 | //float floatVar; |
brunofgc | 0:8c59a494a451 | 938 | //uint32_t aux32_t; |
brunofgc | 0:8c59a494a451 | 939 | //uint16_t aux16_t; |
brunofgc | 5:8efd609dea7d | 940 | char *pChar; |
brunofgc | 5:8efd609dea7d | 941 | |
brunofgc | 2:ca3723da6668 | 942 | if(strstr(linha,"scheduleSet")) { |
brunofgc | 0:8c59a494a451 | 943 | //Inicialização de contadores de schedules |
brunofgc | 2:ca3723da6668 | 944 | //É necessário inicializar estes dados para quando a máquina de schedules rodar ter um inicio correto. |
brunofgc | 2:ca3723da6668 | 945 | this->schedules[this->numSchedules].actSent = false; |
brunofgc | 2:ca3723da6668 | 946 | //Inicialização de contadores de schedules |
brunofgc | 2:ca3723da6668 | 947 | this->numSchedules++; |
brunofgc | 0:8c59a494a451 | 948 | } |
brunofgc | 0:8c59a494a451 | 949 | |
brunofgc | 5:8efd609dea7d | 950 | if(strstr(linha,"idSchedule:")) { |
brunofgc | 0:8c59a494a451 | 951 | strtok(linha,":"); |
brunofgc | 5:8efd609dea7d | 952 | pChar = strtok(NULL,""); |
brunofgc | 5:8efd609dea7d | 953 | this->schedules[this->numSchedules-1].id = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 954 | } |
brunofgc | 0:8c59a494a451 | 955 | |
brunofgc | 5:8efd609dea7d | 956 | if(strstr(linha,"dayTime:")) { |
brunofgc | 0:8c59a494a451 | 957 | strtok(linha,":"); |
brunofgc | 0:8c59a494a451 | 958 | pChar = strtok(NULL,","); |
brunofgc | 0:8c59a494a451 | 959 | this->schedules[this->numSchedules-1].weekday = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 960 | pChar = strtok(NULL,""); |
brunofgc | 0:8c59a494a451 | 961 | this->schedules[this->numSchedules-1].minute = atoi(pChar); |
brunofgc | 0:8c59a494a451 | 962 | } |
brunofgc | 5:8efd609dea7d | 963 | |
brunofgc | 5:8efd609dea7d | 964 | if(strstr(linha,"idAct:")) { |
brunofgc | 0:8c59a494a451 | 965 | strtok(linha,":"); |
brunofgc | 0:8c59a494a451 | 966 | pChar = strtok(NULL,""); |
brunofgc | 0:8c59a494a451 | 967 | this->schedules[this->numSchedules-1].idAct = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 968 | } |
brunofgc | 5:8efd609dea7d | 969 | /*pc.printf("Debugando erro em scheduleSet.\r\n"); |
brunofgc | 5:8efd609dea7d | 970 | pc.printf("Linha <%s>\r\n",linha); |
brunofgc | 5:8efd609dea7d | 971 | pc.printf("actSent = %lu",this->schedules[this->numSchedules-1].actSent); |
brunofgc | 5:8efd609dea7d | 972 | pc.printf("idSchedule = %lu",this->schedules[this->numSchedules-1].id); |
brunofgc | 5:8efd609dea7d | 973 | pc.printf("dayTime = %lu",this->schedules[this->numSchedules-1].weekday); |
brunofgc | 5:8efd609dea7d | 974 | pc.printf("idAct = %lu",this->schedules[this->numSchedules-1].idAct); |
brunofgc | 5:8efd609dea7d | 975 | pc.printf("Debugando erro em scheduleSet.\r\n");*/ |
brunofgc | 0:8c59a494a451 | 976 | } |
brunofgc | 0:8c59a494a451 | 977 | |
brunofgc | 4:3f92979d1ffb | 978 | void device::setScheduleException(char *linha) |
brunofgc | 4:3f92979d1ffb | 979 | { |
brunofgc | 4:3f92979d1ffb | 980 | //float floatVar; |
brunofgc | 4:3f92979d1ffb | 981 | //uint32_t aux32_t; |
brunofgc | 4:3f92979d1ffb | 982 | //uint16_t aux16_t; |
brunofgc | 5:8efd609dea7d | 983 | char *pChar; |
brunofgc | 5:8efd609dea7d | 984 | if(strstr(linha,"scheduleExceptionSet")) { |
brunofgc | 4:3f92979d1ffb | 985 | //Inicialização de contadores de schedulesExceptions |
brunofgc | 4:3f92979d1ffb | 986 | this->numScheduleExceptions++; |
brunofgc | 4:3f92979d1ffb | 987 | } |
brunofgc | 4:3f92979d1ffb | 988 | |
brunofgc | 5:8efd609dea7d | 989 | if(strstr(linha,"idSchedule:")) { |
brunofgc | 4:3f92979d1ffb | 990 | strtok(linha,":"); |
brunofgc | 4:3f92979d1ffb | 991 | this->scheduleExceptions[this->numScheduleExceptions-1].id_schedule = atoi(strtok(NULL,"\r\n")); |
brunofgc | 4:3f92979d1ffb | 992 | } |
brunofgc | 4:3f92979d1ffb | 993 | |
brunofgc | 5:8efd609dea7d | 994 | if(strstr(linha,"timestamp:")) { |
brunofgc | 4:3f92979d1ffb | 995 | strtok(linha,":"); |
brunofgc | 4:3f92979d1ffb | 996 | pChar = strtok(NULL,","); |
brunofgc | 5:8efd609dea7d | 997 | this->scheduleExceptions[this->numScheduleExceptions-1].timestamp = atoi(pChar); |
brunofgc | 5:8efd609dea7d | 998 | } |
brunofgc | 4:3f92979d1ffb | 999 | } |
brunofgc | 4:3f92979d1ffb | 1000 | |
brunofgc | 0:8c59a494a451 | 1001 | /*void device::writeReadingsToSD(){ |
brunofgc | 0:8c59a494a451 | 1002 | //Esta função faz a inserção de todos os dados no arquivo armazenamento. |
brunofgc | 5:8efd609dea7d | 1003 | uint16_t readingIndex=0; |
brunofgc | 5:8efd609dea7d | 1004 | |
brunofgc | 0:8c59a494a451 | 1005 | float *floatBuffer; |
brunofgc | 0:8c59a494a451 | 1006 | uint32_t *uint32_tBuffer; |
brunofgc | 5:8efd609dea7d | 1007 | uint16_t *uint16_tBuffer; |
brunofgc | 5:8efd609dea7d | 1008 | bool *boolBuffer; |
brunofgc | 0:8c59a494a451 | 1009 | uint32_t seconds_uint32_t; |
brunofgc | 0:8c59a494a451 | 1010 | |
brunofgc | 0:8c59a494a451 | 1011 | //char registro[1024]; |
brunofgc | 0:8c59a494a451 | 1012 | //uint16_t indexRegistro; |
brunofgc | 0:8c59a494a451 | 1013 | |
brunofgc | 0:8c59a494a451 | 1014 | seconds_uint32_t = (uint32_t) time(NULL); |
brunofgc | 0:8c59a494a451 | 1015 | |
brunofgc | 0:8c59a494a451 | 1016 | //Inicio da string dentro do arquivo binario |
brunofgc | 0:8c59a494a451 | 1017 | sdCard::insereDadosArquivo(&sdCard::armazenamento,"log{data:",9); |
brunofgc | 0:8c59a494a451 | 1018 | |
brunofgc | 0:8c59a494a451 | 1019 | //O timestamp é posto nos 4 primeiros bytes (segundos) |
brunofgc | 0:8c59a494a451 | 1020 | sdCard::insereDadosArquivo(&sdCard::armazenamento,(char *)&seconds_uint32_t,4); |
brunofgc | 0:8c59a494a451 | 1021 | |
brunofgc | 0:8c59a494a451 | 1022 | for(readingIndex = 0;readingIndex < this->numReadings;readingIndex++){ |
brunofgc | 0:8c59a494a451 | 1023 | switch(this->readings[readingIndex].type){ |
brunofgc | 0:8c59a494a451 | 1024 | case modBusType_float: |
brunofgc | 0:8c59a494a451 | 1025 | floatBuffer = (float*) malloc(this->readings[readingIndex].numRegs*sizeof(float)); |
brunofgc | 0:8c59a494a451 | 1026 | modBusMaster1::readFloat( |
brunofgc | 0:8c59a494a451 | 1027 | this->readings[readingIndex].addr, |
brunofgc | 0:8c59a494a451 | 1028 | this->readings[readingIndex].func, |
brunofgc | 0:8c59a494a451 | 1029 | this->readings[readingIndex].reg, |
brunofgc | 0:8c59a494a451 | 1030 | this->readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1031 | floatBuffer |
brunofgc | 0:8c59a494a451 | 1032 | ); |
brunofgc | 0:8c59a494a451 | 1033 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 0:8c59a494a451 | 1034 | sdCard::insereDadosArquivo(&sdCard::armazenamento,(char *)floatBuffer,sizeof(float)*this->readings[readingIndex].numRegs); |
brunofgc | 0:8c59a494a451 | 1035 | free(floatBuffer); |
brunofgc | 0:8c59a494a451 | 1036 | break; |
brunofgc | 0:8c59a494a451 | 1037 | case modBusType_uint32_t: |
brunofgc | 0:8c59a494a451 | 1038 | uint32_tBuffer = (uint32_t*) malloc(this->readings[readingIndex].numRegs*sizeof(uint32_t)); |
brunofgc | 0:8c59a494a451 | 1039 | modBusMaster1::readRegister32BIT( |
brunofgc | 0:8c59a494a451 | 1040 | this->readings[readingIndex].addr, |
brunofgc | 0:8c59a494a451 | 1041 | this->readings[readingIndex].func, |
brunofgc | 0:8c59a494a451 | 1042 | this->readings[readingIndex].reg, |
brunofgc | 0:8c59a494a451 | 1043 | this->readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1044 | uint32_tBuffer |
brunofgc | 0:8c59a494a451 | 1045 | ); |
brunofgc | 0:8c59a494a451 | 1046 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 0:8c59a494a451 | 1047 | sdCard::insereDadosArquivo(&sdCard::armazenamento,(char *)uint32_tBuffer,sizeof(uint32_t)*this->readings[readingIndex].numRegs); |
brunofgc | 0:8c59a494a451 | 1048 | free(uint32_tBuffer); |
brunofgc | 0:8c59a494a451 | 1049 | break; |
brunofgc | 0:8c59a494a451 | 1050 | |
brunofgc | 0:8c59a494a451 | 1051 | case modBusType_uint16_t: |
brunofgc | 0:8c59a494a451 | 1052 | uint16_tBuffer = (uint16_t*) malloc(this->readings[readingIndex].numRegs*sizeof(uint16_t)); |
brunofgc | 0:8c59a494a451 | 1053 | modBusMaster1::readRegister16BIT( |
brunofgc | 0:8c59a494a451 | 1054 | this->readings[readingIndex].addr, |
brunofgc | 0:8c59a494a451 | 1055 | this->readings[readingIndex].func, |
brunofgc | 0:8c59a494a451 | 1056 | this->readings[readingIndex].reg, |
brunofgc | 0:8c59a494a451 | 1057 | this->readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1058 | uint16_tBuffer |
brunofgc | 0:8c59a494a451 | 1059 | ); |
brunofgc | 0:8c59a494a451 | 1060 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 0:8c59a494a451 | 1061 | sdCard::insereDadosArquivo(&sdCard::armazenamento,(char *)uint16_tBuffer,sizeof(uint16_t)*this->readings[readingIndex].numRegs); |
brunofgc | 0:8c59a494a451 | 1062 | free(uint16_tBuffer); |
brunofgc | 0:8c59a494a451 | 1063 | break; |
brunofgc | 0:8c59a494a451 | 1064 | |
brunofgc | 0:8c59a494a451 | 1065 | case modBusType_bit: |
brunofgc | 0:8c59a494a451 | 1066 | boolBuffer = (bool*) malloc(this->readings[readingIndex].numRegs*sizeof(bool)); |
brunofgc | 0:8c59a494a451 | 1067 | modBusMaster1::readCoils( |
brunofgc | 0:8c59a494a451 | 1068 | this->readings[readingIndex].addr, |
brunofgc | 0:8c59a494a451 | 1069 | this->readings[readingIndex].reg, |
brunofgc | 0:8c59a494a451 | 1070 | this->readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1071 | boolBuffer |
brunofgc | 0:8c59a494a451 | 1072 | ); |
brunofgc | 0:8c59a494a451 | 1073 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 0:8c59a494a451 | 1074 | sdCard::insereDadosArquivo(&sdCard::armazenamento,(char *)boolBuffer,sizeof(bool)*this->readings[readingIndex].numRegs); |
brunofgc | 0:8c59a494a451 | 1075 | free(boolBuffer); |
brunofgc | 0:8c59a494a451 | 1076 | break; |
brunofgc | 0:8c59a494a451 | 1077 | |
brunofgc | 0:8c59a494a451 | 1078 | } |
brunofgc | 0:8c59a494a451 | 1079 | } |
brunofgc | 0:8c59a494a451 | 1080 | sdCard::insereDadosArquivo(&sdCard::armazenamento,"}log",4); |
brunofgc | 0:8c59a494a451 | 1081 | }*/ |
brunofgc | 0:8c59a494a451 | 1082 | |
brunofgc | 0:8c59a494a451 | 1083 | uint8_t device::execAct(uint32_t index){ |
brunofgc | 0:8c59a494a451 | 1084 | //Primeiro preciso decidir que tipo de act estou lidando para tanto preciso pegar a ACT inteira. |
brunofgc | 0:8c59a494a451 | 1085 | //Busca no arquivo |
brunofgc | 0:8c59a494a451 | 1086 | //Abre arquivo |
brunofgc | 0:8c59a494a451 | 1087 | char actString[1024]; |
brunofgc | 0:8c59a494a451 | 1088 | char indexBusca[15]; |
brunofgc | 5:8efd609dea7d | 1089 | char alarmsSetString[256]; |
brunofgc | 5:8efd609dea7d | 1090 | char alarmsResetString[256]; |
brunofgc | 0:8c59a494a451 | 1091 | char *pTipoComando; |
brunofgc | 0:8c59a494a451 | 1092 | uint8_t tipoComando; |
brunofgc | 0:8c59a494a451 | 1093 | bool endOfFile = false; |
brunofgc | 5:8efd609dea7d | 1094 | uint8_t indexAlarms; |
brunofgc | 5:8efd609dea7d | 1095 | char *pChar; |
brunofgc | 0:8c59a494a451 | 1096 | |
brunofgc | 0:8c59a494a451 | 1097 | //Variáveis comuns; |
brunofgc | 0:8c59a494a451 | 1098 | uint8_t param1; |
brunofgc | 0:8c59a494a451 | 1099 | uint8_t param2; |
brunofgc | 0:8c59a494a451 | 1100 | uint16_t param3; |
brunofgc | 0:8c59a494a451 | 1101 | uint32_t param4; |
brunofgc | 0:8c59a494a451 | 1102 | |
brunofgc | 0:8c59a494a451 | 1103 | //printf("\r\n===================DEBUG(execAct)===================\r\n"); |
brunofgc | 5:8efd609dea7d | 1104 | sprintf(indexBusca,"idAct:%lu;",index); |
brunofgc | 5:8efd609dea7d | 1105 | pc.printf("Lido <%s>.\r\n",indexBusca); |
brunofgc | 0:8c59a494a451 | 1106 | //printf("Buscando por indexBusca<%s>.\r\n",indexBusca); |
brunofgc | 0:8c59a494a451 | 1107 | if(sdCard::abreArquivo(&sdCard::devices,"r")){ |
brunofgc | 0:8c59a494a451 | 1108 | do{ |
brunofgc | 0:8c59a494a451 | 1109 | endOfFile = feof(sdCard::devices.fp); |
brunofgc | 5:8efd609dea7d | 1110 | if(!endOfFile){fgets(actString,1024,sdCard::devices.fp);} |
brunofgc | 5:8efd609dea7d | 1111 | }while((strstr(actString,"actList")==NULL)&&(endOfFile==0)); |
brunofgc | 6:f20c0e094294 | 1112 | pc.printf("Achei o actList.\r\n"); |
brunofgc | 5:8efd609dea7d | 1113 | do{ |
brunofgc | 5:8efd609dea7d | 1114 | endOfFile = feof(sdCard::devices.fp); |
brunofgc | 0:8c59a494a451 | 1115 | if(!endOfFile){fgets(actString,1024,sdCard::devices.fp);} |
brunofgc | 5:8efd609dea7d | 1116 | //printf("Linha ACT = <%s>.\r\nendOfFile = %u\r\n",actString,endOfFile); |
brunofgc | 5:8efd609dea7d | 1117 | }while((strstr(actString,indexBusca)==NULL)&&(endOfFile==0)); |
brunofgc | 0:8c59a494a451 | 1118 | sdCard::fechaArquivo(&sdCard::devices); |
brunofgc | 0:8c59a494a451 | 1119 | |
brunofgc | 0:8c59a494a451 | 1120 | //Vê se deu tudo certo, se não encontrou, retorna false |
brunofgc | 0:8c59a494a451 | 1121 | if(endOfFile){ |
brunofgc | 0:8c59a494a451 | 1122 | return false; |
brunofgc | 0:8c59a494a451 | 1123 | } |
brunofgc | 0:8c59a494a451 | 1124 | //Tudo correndo bem. |
brunofgc | 0:8c59a494a451 | 1125 | |
brunofgc | 0:8c59a494a451 | 1126 | //printf("\r\nPoint 1\r\n"); |
brunofgc | 0:8c59a494a451 | 1127 | |
brunofgc | 0:8c59a494a451 | 1128 | //Encontrado o tipo de comando reutilizando a string indexBusca; |
brunofgc | 5:8efd609dea7d | 1129 | pChar = strtok(actString,";"); |
brunofgc | 5:8efd609dea7d | 1130 | pc.printf("1- <%s>.\r\n",pChar); |
brunofgc | 5:8efd609dea7d | 1131 | pChar = strtok(NULL,";"); |
brunofgc | 5:8efd609dea7d | 1132 | strcpy(alarmsSetString,pChar); |
brunofgc | 5:8efd609dea7d | 1133 | pChar = strtok(NULL,";"); |
brunofgc | 5:8efd609dea7d | 1134 | strcpy(alarmsResetString,pChar); |
brunofgc | 5:8efd609dea7d | 1135 | |
brunofgc | 5:8efd609dea7d | 1136 | pTipoComando = strtok(NULL,","); |
brunofgc | 5:8efd609dea7d | 1137 | |
brunofgc | 5:8efd609dea7d | 1138 | printf("\r\nPoint 2 comando <%s>\r\n",pTipoComando); |
brunofgc | 0:8c59a494a451 | 1139 | if(strstr(pTipoComando,"float")){tipoComando = modBusType_float;} |
brunofgc | 0:8c59a494a451 | 1140 | if(strstr(pTipoComando,"bit")){tipoComando = modBusType_bit;} |
brunofgc | 0:8c59a494a451 | 1141 | if(strstr(pTipoComando,"uint8_t")){tipoComando = modBusType_uint8_t;} |
brunofgc | 0:8c59a494a451 | 1142 | if(strstr(pTipoComando,"uint16_t")){tipoComando = modBusType_uint16_t;} |
brunofgc | 0:8c59a494a451 | 1143 | if(strstr(pTipoComando,"uint32_t")){tipoComando = modBusType_uint32_t;} |
brunofgc | 1:3f9e5b86d9e4 | 1144 | if(strstr(pTipoComando,"IR")){tipoComando = IrCommandType;} |
brunofgc | 1:3f9e5b86d9e4 | 1145 | if(strstr(pTipoComando,"PWM")){tipoComando = PWMCommandType;} |
brunofgc | 0:8c59a494a451 | 1146 | }else{ |
brunofgc | 0:8c59a494a451 | 1147 | return false; |
brunofgc | 0:8c59a494a451 | 1148 | } |
brunofgc | 0:8c59a494a451 | 1149 | |
brunofgc | 0:8c59a494a451 | 1150 | switch(tipoComando){ |
brunofgc | 0:8c59a494a451 | 1151 | case modBusType_float: |
brunofgc | 0:8c59a494a451 | 1152 | float floatValueToWrite; |
brunofgc | 0:8c59a494a451 | 1153 | uint32_t auxMod; |
brunofgc | 0:8c59a494a451 | 1154 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1155 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1156 | param3 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1157 | //floatValueToWrite = diversos::stringToFloat(strtok(NULL,","),100); |
brunofgc | 0:8c59a494a451 | 1158 | floatValueToWrite = atof(strtok(NULL,",")); |
brunofgc | 5:8efd609dea7d | 1159 | |
brunofgc | 1:3f9e5b86d9e4 | 1160 | //Envio comando via Modbus |
brunofgc | 1:3f9e5b86d9e4 | 1161 | modBusMaster1::writeFloat( |
brunofgc | 1:3f9e5b86d9e4 | 1162 | param1, |
brunofgc | 1:3f9e5b86d9e4 | 1163 | param2, |
brunofgc | 1:3f9e5b86d9e4 | 1164 | param3, |
brunofgc | 1:3f9e5b86d9e4 | 1165 | &floatValueToWrite |
brunofgc | 1:3f9e5b86d9e4 | 1166 | ); |
brunofgc | 0:8c59a494a451 | 1167 | break; |
brunofgc | 0:8c59a494a451 | 1168 | case modBusType_uint32_t: |
brunofgc | 0:8c59a494a451 | 1169 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1170 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1171 | param3 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1172 | param4 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1173 | |
brunofgc | 0:8c59a494a451 | 1174 | modBusMaster1::writeRegister32BIT( |
brunofgc | 0:8c59a494a451 | 1175 | param1, |
brunofgc | 0:8c59a494a451 | 1176 | param2, |
brunofgc | 0:8c59a494a451 | 1177 | param3, |
brunofgc | 0:8c59a494a451 | 1178 | ¶m4 |
brunofgc | 0:8c59a494a451 | 1179 | ); |
brunofgc | 0:8c59a494a451 | 1180 | break; |
brunofgc | 0:8c59a494a451 | 1181 | case modBusType_uint16_t: |
brunofgc | 0:8c59a494a451 | 1182 | uint16_t param4_uint16_t; |
brunofgc | 0:8c59a494a451 | 1183 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1184 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1185 | param3 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1186 | param4_uint16_t = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1187 | |
brunofgc | 0:8c59a494a451 | 1188 | modBusMaster1::writeRegister16BIT( |
brunofgc | 0:8c59a494a451 | 1189 | param1, |
brunofgc | 0:8c59a494a451 | 1190 | param2, |
brunofgc | 0:8c59a494a451 | 1191 | param3, |
brunofgc | 0:8c59a494a451 | 1192 | ¶m4_uint16_t |
brunofgc | 0:8c59a494a451 | 1193 | ); |
brunofgc | 0:8c59a494a451 | 1194 | break; |
brunofgc | 0:8c59a494a451 | 1195 | case modBusType_bit: |
brunofgc | 0:8c59a494a451 | 1196 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1197 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1198 | param3 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1199 | param4 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1200 | |
brunofgc | 0:8c59a494a451 | 1201 | if(param1 != 254){ |
brunofgc | 0:8c59a494a451 | 1202 | //Envia comando via Modbus |
brunofgc | 0:8c59a494a451 | 1203 | modBusMaster1::writeSingleCoil( |
brunofgc | 0:8c59a494a451 | 1204 | param1, |
brunofgc | 0:8c59a494a451 | 1205 | param3, |
brunofgc | 0:8c59a494a451 | 1206 | (bool)param4 |
brunofgc | 0:8c59a494a451 | 1207 | ); |
brunofgc | 0:8c59a494a451 | 1208 | }else{ |
brunofgc | 0:8c59a494a451 | 1209 | //Executa comando interno; |
brunofgc | 0:8c59a494a451 | 1210 | switch(param3){ |
brunofgc | 0:8c59a494a451 | 1211 | case 0: |
brunofgc | 0:8c59a494a451 | 1212 | SD1.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1213 | break; |
brunofgc | 0:8c59a494a451 | 1214 | case 1: |
brunofgc | 0:8c59a494a451 | 1215 | SD2.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1216 | break; |
brunofgc | 0:8c59a494a451 | 1217 | case 2: |
brunofgc | 0:8c59a494a451 | 1218 | SD3.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1219 | break; |
brunofgc | 0:8c59a494a451 | 1220 | case 3: |
brunofgc | 0:8c59a494a451 | 1221 | SD4.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1222 | break; |
brunofgc | 0:8c59a494a451 | 1223 | case 4: |
brunofgc | 0:8c59a494a451 | 1224 | SD5.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1225 | break; |
brunofgc | 0:8c59a494a451 | 1226 | case 5: |
brunofgc | 0:8c59a494a451 | 1227 | SD6.write(param4*1.0f); |
brunofgc | 0:8c59a494a451 | 1228 | break; |
brunofgc | 0:8c59a494a451 | 1229 | case 6: |
brunofgc | 0:8c59a494a451 | 1230 | SD7 = param4; |
brunofgc | 0:8c59a494a451 | 1231 | break; |
brunofgc | 0:8c59a494a451 | 1232 | case 7: |
brunofgc | 0:8c59a494a451 | 1233 | SD8 = param4; |
brunofgc | 0:8c59a494a451 | 1234 | break; |
brunofgc | 0:8c59a494a451 | 1235 | } |
brunofgc | 0:8c59a494a451 | 1236 | } |
brunofgc | 0:8c59a494a451 | 1237 | break; |
brunofgc | 0:8c59a494a451 | 1238 | case IrCommandType: |
brunofgc | 0:8c59a494a451 | 1239 | //Busca no arquivo |
brunofgc | 0:8c59a494a451 | 1240 | //Abre arquivo |
brunofgc | 0:8c59a494a451 | 1241 | //printf("\r\nIniciando execucao de comando IR\r\n"); |
brunofgc | 0:8c59a494a451 | 1242 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1243 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 0:8c59a494a451 | 1244 | //printf("\r\nparam 1 %lu, param2 %lu\r\n",param1,param2); |
brunofgc | 0:8c59a494a451 | 1245 | pTipoComando = strtok(NULL,"S");//Reaproveitando variável pTipoComando |
brunofgc | 0:8c59a494a451 | 1246 | //printf("\r\nPacote IR <%s>.\r\n",pTipoComando); |
brunofgc | 0:8c59a494a451 | 1247 | deserializaPacoteIR(pTipoComando); |
brunofgc | 5:8efd609dea7d | 1248 | //printf("Deserializado.\r\nInicio do envio.\r\n"); |
brunofgc | 1:3f9e5b86d9e4 | 1249 | enviaComandoIR(param1,param2); |
brunofgc | 5:8efd609dea7d | 1250 | //printf("Fim do envio.\r\n"); |
brunofgc | 0:8c59a494a451 | 1251 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1252 | |
brunofgc | 1:3f9e5b86d9e4 | 1253 | case PWMCommandType: |
brunofgc | 1:3f9e5b86d9e4 | 1254 | //float floatValuePWM; |
brunofgc | 1:3f9e5b86d9e4 | 1255 | //uint32_t auxPeriod; |
brunofgc | 1:3f9e5b86d9e4 | 1256 | param1 = atoi(strtok(NULL,",")); |
brunofgc | 1:3f9e5b86d9e4 | 1257 | param2 = atoi(strtok(NULL,",")); |
brunofgc | 1:3f9e5b86d9e4 | 1258 | param3 = atoi(strtok(NULL,",")); |
brunofgc | 1:3f9e5b86d9e4 | 1259 | //floatValueToWrite = diversos::stringToFloat(strtok(NULL,","),100); |
brunofgc | 1:3f9e5b86d9e4 | 1260 | floatValueToWrite = atof(strtok(NULL,",")); |
brunofgc | 1:3f9e5b86d9e4 | 1261 | |
brunofgc | 1:3f9e5b86d9e4 | 1262 | //Executa comando interno; |
brunofgc | 1:3f9e5b86d9e4 | 1263 | auxMod = floatValueToWrite/10; |
brunofgc | 1:3f9e5b86d9e4 | 1264 | floatValueToWrite = floatValueToWrite-(auxMod*10); |
brunofgc | 1:3f9e5b86d9e4 | 1265 | SD1.period_us(auxMod); |
brunofgc | 1:3f9e5b86d9e4 | 1266 | pwmPeriod = auxMod; |
brunofgc | 1:3f9e5b86d9e4 | 1267 | switch(param3){ |
brunofgc | 1:3f9e5b86d9e4 | 1268 | case 0: |
brunofgc | 1:3f9e5b86d9e4 | 1269 | SD1.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1270 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1271 | case 1: |
brunofgc | 1:3f9e5b86d9e4 | 1272 | SD2.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1273 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1274 | case 2: |
brunofgc | 1:3f9e5b86d9e4 | 1275 | SD3.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1276 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1277 | case 3: |
brunofgc | 1:3f9e5b86d9e4 | 1278 | SD4.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1279 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1280 | case 4: |
brunofgc | 1:3f9e5b86d9e4 | 1281 | SD5.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1282 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1283 | case 5: |
brunofgc | 1:3f9e5b86d9e4 | 1284 | SD6.write(floatValueToWrite); |
brunofgc | 1:3f9e5b86d9e4 | 1285 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1286 | } |
brunofgc | 1:3f9e5b86d9e4 | 1287 | break; |
brunofgc | 1:3f9e5b86d9e4 | 1288 | |
brunofgc | 0:8c59a494a451 | 1289 | default: |
brunofgc | 0:8c59a494a451 | 1290 | //printf("comando nao encontrado\r\n"); |
brunofgc | 0:8c59a494a451 | 1291 | break; |
brunofgc | 0:8c59a494a451 | 1292 | } |
brunofgc | 5:8efd609dea7d | 1293 | //Coletando dados do alarmSet e alarmReset |
brunofgc | 5:8efd609dea7d | 1294 | |
brunofgc | 5:8efd609dea7d | 1295 | pChar = strtok(alarmsSetString,","); |
brunofgc | 5:8efd609dea7d | 1296 | while(pChar!=NULL){ |
brunofgc | 5:8efd609dea7d | 1297 | pc.printf("Lido Alarm Set %lu - <%lu>.\r\n",indexAlarms,atoi(pChar)); |
brunofgc | 5:8efd609dea7d | 1298 | dispositivos[0]->alarmOnOff(atoi(pChar),1); |
brunofgc | 5:8efd609dea7d | 1299 | pChar = strtok(NULL,","); |
brunofgc | 5:8efd609dea7d | 1300 | } |
brunofgc | 5:8efd609dea7d | 1301 | |
brunofgc | 5:8efd609dea7d | 1302 | pChar = strtok(alarmsResetString,","); |
brunofgc | 5:8efd609dea7d | 1303 | while(pChar!=NULL){ |
brunofgc | 5:8efd609dea7d | 1304 | pc.printf("Lido Alarm Reset %lu - <%lu>.\r\n",indexAlarms,atoi(pChar)); |
brunofgc | 5:8efd609dea7d | 1305 | dispositivos[0]->alarmOnOff(atoi(pChar),0); |
brunofgc | 5:8efd609dea7d | 1306 | pChar = strtok(NULL,","); |
brunofgc | 5:8efd609dea7d | 1307 | } |
brunofgc | 5:8efd609dea7d | 1308 | |
brunofgc | 0:8c59a494a451 | 1309 | //printf("\r\n===================DEBUG(execAct)===================\r\n"); |
brunofgc | 0:8c59a494a451 | 1310 | return true; |
brunofgc | 0:8c59a494a451 | 1311 | } |
brunofgc | 0:8c59a494a451 | 1312 | |
brunofgc | 5:8efd609dea7d | 1313 | void device::alarmOnOff(uint32_t idAlarm,bool state){ |
brunofgc | 5:8efd609dea7d | 1314 | uint16_t alarmIndex; |
brunofgc | 5:8efd609dea7d | 1315 | char indexBusca[30]; |
brunofgc | 5:8efd609dea7d | 1316 | char alarmString[40]; |
brunofgc | 5:8efd609dea7d | 1317 | uint32_t seekPosition; |
brunofgc | 5:8efd609dea7d | 1318 | bool endOfFile; |
brunofgc | 5:8efd609dea7d | 1319 | char charState; |
brunofgc | 5:8efd609dea7d | 1320 | |
brunofgc | 5:8efd609dea7d | 1321 | for(alarmIndex = 0; alarmIndex < alarmes; alarmIndex++){ |
brunofgc | 5:8efd609dea7d | 1322 | if(this->alarms[alarmIndex].id == idAlarm){ |
brunofgc | 5:8efd609dea7d | 1323 | this->alarms[alarmIndex].on = state; |
brunofgc | 5:8efd609dea7d | 1324 | } |
brunofgc | 5:8efd609dea7d | 1325 | } |
brunofgc | 5:8efd609dea7d | 1326 | |
brunofgc | 5:8efd609dea7d | 1327 | if(!sdCard::abreArquivo(&sdCard::devices,"rb+")){return;} |
brunofgc | 5:8efd609dea7d | 1328 | |
brunofgc | 5:8efd609dea7d | 1329 | sprintf(indexBusca,"idAlarm:%lu",idAlarm); |
brunofgc | 5:8efd609dea7d | 1330 | do{ |
brunofgc | 5:8efd609dea7d | 1331 | endOfFile = feof(sdCard::devices.fp); |
brunofgc | 5:8efd609dea7d | 1332 | if(!endOfFile){fgets(alarmString,40,sdCard::devices.fp);} |
brunofgc | 5:8efd609dea7d | 1333 | }while((strstr(alarmString,indexBusca)==NULL)&&(endOfFile==0)); |
brunofgc | 5:8efd609dea7d | 1334 | |
brunofgc | 5:8efd609dea7d | 1335 | |
brunofgc | 5:8efd609dea7d | 1336 | do{ |
brunofgc | 5:8efd609dea7d | 1337 | endOfFile = feof(sdCard::devices.fp); |
brunofgc | 5:8efd609dea7d | 1338 | if(!endOfFile){fgets(alarmString,40,sdCard::devices.fp);} |
brunofgc | 5:8efd609dea7d | 1339 | }while((strstr(alarmString,"on:")==NULL)&&(endOfFile==0)); |
brunofgc | 5:8efd609dea7d | 1340 | |
brunofgc | 5:8efd609dea7d | 1341 | |
brunofgc | 5:8efd609dea7d | 1342 | if(endOfFile){ |
brunofgc | 5:8efd609dea7d | 1343 | sdCard::fechaArquivo(&sdCard::devices); |
brunofgc | 5:8efd609dea7d | 1344 | return; |
brunofgc | 5:8efd609dea7d | 1345 | } |
brunofgc | 5:8efd609dea7d | 1346 | |
brunofgc | 5:8efd609dea7d | 1347 | seekPosition = ftell(sdCard::devices.fp)-3; |
brunofgc | 5:8efd609dea7d | 1348 | fseek(sdCard::devices.fp,seekPosition,SEEK_SET); |
brunofgc | 5:8efd609dea7d | 1349 | if(state){charState='1';}else{charState='0';} |
brunofgc | 5:8efd609dea7d | 1350 | fputc(charState,sdCard::devices.fp); |
brunofgc | 5:8efd609dea7d | 1351 | //fprintf(sdCard::devices.fp,"%u",state); |
brunofgc | 5:8efd609dea7d | 1352 | fflush(sdCard::devices.fp); |
brunofgc | 5:8efd609dea7d | 1353 | sdCard::fechaArquivo(&sdCard::devices); |
brunofgc | 5:8efd609dea7d | 1354 | } |
brunofgc | 5:8efd609dea7d | 1355 | |
brunofgc | 0:8c59a494a451 | 1356 | void device::writeReadingsToSD(){ |
brunofgc | 0:8c59a494a451 | 1357 | //Buffering before insertData |
brunofgc | 0:8c59a494a451 | 1358 | //Esta função faz a inserção de todos os dados no arquivo armazenamento. |
brunofgc | 0:8c59a494a451 | 1359 | uint16_t readingIndex=0; |
brunofgc | 0:8c59a494a451 | 1360 | /* |
brunofgc | 0:8c59a494a451 | 1361 | union { |
brunofgc | 0:8c59a494a451 | 1362 | char c[4]; |
brunofgc | 0:8c59a494a451 | 1363 | float v; |
brunofgc | 0:8c59a494a451 | 1364 | }uFloat; |
brunofgc | 0:8c59a494a451 | 1365 | */ |
brunofgc | 0:8c59a494a451 | 1366 | float *floatBuffer; |
brunofgc | 0:8c59a494a451 | 1367 | uint32_t *uint32_tBuffer; |
brunofgc | 0:8c59a494a451 | 1368 | uint16_t *uint16_tBuffer; |
brunofgc | 0:8c59a494a451 | 1369 | bool *boolBuffer; |
brunofgc | 0:8c59a494a451 | 1370 | uint32_t seconds_uint32_t; |
brunofgc | 0:8c59a494a451 | 1371 | char seconds_char[5]; |
brunofgc | 0:8c59a494a451 | 1372 | |
brunofgc | 0:8c59a494a451 | 1373 | seconds_uint32_t = (uint32_t) time(NULL); |
brunofgc | 0:8c59a494a451 | 1374 | seconds_char[0]=(seconds_uint32_t >> 0) & 0xFF; |
brunofgc | 0:8c59a494a451 | 1375 | seconds_char[1]=(seconds_uint32_t >> 8) & 0xFF; |
brunofgc | 0:8c59a494a451 | 1376 | seconds_char[2]=(seconds_uint32_t >> 16) & 0xFF; |
brunofgc | 0:8c59a494a451 | 1377 | seconds_char[3]=(seconds_uint32_t >> 24) & 0xFF; |
brunofgc | 0:8c59a494a451 | 1378 | seconds_char[4]=0; |
brunofgc | 0:8c59a494a451 | 1379 | |
brunofgc | 0:8c59a494a451 | 1380 | |
brunofgc | 0:8c59a494a451 | 1381 | //Inicio da string dentro do arquivo binario |
brunofgc | 0:8c59a494a451 | 1382 | //sdCardBuf.del(); |
brunofgc | 0:8c59a494a451 | 1383 | sdCardBuf.fill("log{data:",9); |
brunofgc | 0:8c59a494a451 | 1384 | |
brunofgc | 0:8c59a494a451 | 1385 | //O timestamp é posto nos 4 primeiros bytes (segundos) |
brunofgc | 0:8c59a494a451 | 1386 | sdCardBuf.fill(seconds_char,5); |
brunofgc | 0:8c59a494a451 | 1387 | |
brunofgc | 0:8c59a494a451 | 1388 | for(readingIndex = 0;readingIndex < this->numReadings;readingIndex++){ |
brunofgc | 0:8c59a494a451 | 1389 | switch(this->readings[readingIndex].type){ |
brunofgc | 0:8c59a494a451 | 1390 | case modBusType_float: |
brunofgc | 1:3f9e5b86d9e4 | 1391 | if(this->readings[readingIndex].addr != enderecoControladoraVirtual){ |
brunofgc | 0:8c59a494a451 | 1392 | //Busca externa via Modbus |
brunofgc | 0:8c59a494a451 | 1393 | floatBuffer = (float*) malloc(((this->readings[readingIndex].numRegs*sizeof(float))+1)); //Alocando mais um byte para status |
brunofgc | 1:3f9e5b86d9e4 | 1394 | modBusMaster1::readFloat( |
brunofgc | 0:8c59a494a451 | 1395 | this->readings[readingIndex].addr, |
brunofgc | 0:8c59a494a451 | 1396 | this->readings[readingIndex].func, |
brunofgc | 0:8c59a494a451 | 1397 | this->readings[readingIndex].reg, |
brunofgc | 0:8c59a494a451 | 1398 | this->readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1399 | floatBuffer |
brunofgc | 0:8c59a494a451 | 1400 | ); |
brunofgc | 0:8c59a494a451 | 1401 | }else{ |
brunofgc | 0:8c59a494a451 | 1402 | //Busca Interna |
brunofgc | 0:8c59a494a451 | 1403 | floatBuffer = (float*) malloc(sizeof(float)); |
brunofgc | 3:75223647cf3d | 1404 | if((this->readings[readingIndex].reg<100)&&(this->readings[readingIndex].reg < num_temperatureSensors)){//Registradores das entradas de temperatura |
brunofgc | 3:75223647cf3d | 1405 | //Populando floatBuffer com a temperaturas |
brunofgc | 3:75223647cf3d | 1406 | floatBuffer[0] = temperaturas[this->readings[readingIndex].reg]; |
brunofgc | 1:3f9e5b86d9e4 | 1407 | } |
brunofgc | 3:75223647cf3d | 1408 | if((this->readings[readingIndex].reg>100)&&(this->readings[readingIndex].reg<104)){ |
brunofgc | 3:75223647cf3d | 1409 | //Registradores das entradas analógicas |
brunofgc | 3:75223647cf3d | 1410 | floatBuffer[0] = aiFiltrada[this->readings[readingIndex].reg - 100]; |
brunofgc | 3:75223647cf3d | 1411 | } |
brunofgc | 3:75223647cf3d | 1412 | this->readings[readingIndex].numRegs = 1; |
brunofgc | 0:8c59a494a451 | 1413 | } |
brunofgc | 0:8c59a494a451 | 1414 | |
brunofgc | 0:8c59a494a451 | 1415 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 0:8c59a494a451 | 1416 | sdCardBuf.fill((char *)floatBuffer,((sizeof(float)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status |
brunofgc | 0:8c59a494a451 | 1417 | free(floatBuffer); |
brunofgc | 0:8c59a494a451 | 1418 | break; |
brunofgc | 0:8c59a494a451 | 1419 | case modBusType_uint32_t: |
brunofgc | 0:8c59a494a451 | 1420 | uint32_tBuffer = (uint32_t*) malloc((this->readings[readingIndex].numRegs*sizeof(uint32_t))+1); //Alocando mais um byte para status |
brunofgc | 3:75223647cf3d | 1421 | if(this->readings[readingIndex].addr != enderecoControladoraVirtual){ |
brunofgc | 3:75223647cf3d | 1422 | modBusMaster1::readRegister32BIT(//Ponho o status no ultimo byte |
brunofgc | 3:75223647cf3d | 1423 | this->readings[readingIndex].addr, |
brunofgc | 3:75223647cf3d | 1424 | this->readings[readingIndex].func, |
brunofgc | 3:75223647cf3d | 1425 | this->readings[readingIndex].reg, |
brunofgc | 3:75223647cf3d | 1426 | this->readings[readingIndex].numRegs, |
brunofgc | 3:75223647cf3d | 1427 | uint32_tBuffer |
brunofgc | 3:75223647cf3d | 1428 | ); |
brunofgc | 3:75223647cf3d | 1429 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 3:75223647cf3d | 1430 | }else{ |
brunofgc | 3:75223647cf3d | 1431 | uint32_tBuffer[0] = pulsosEDs[this->readings[readingIndex].reg]; |
brunofgc | 3:75223647cf3d | 1432 | this->readings[readingIndex].numRegs = 1; |
brunofgc | 3:75223647cf3d | 1433 | pulsosEDs[this->readings[readingIndex].reg] = 0; |
brunofgc | 3:75223647cf3d | 1434 | } |
brunofgc | 0:8c59a494a451 | 1435 | sdCardBuf.fill((char *)uint32_tBuffer,((sizeof(uint32_t)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status |
brunofgc | 0:8c59a494a451 | 1436 | free(uint32_tBuffer); |
brunofgc | 0:8c59a494a451 | 1437 | break; |
brunofgc | 0:8c59a494a451 | 1438 | |
brunofgc | 0:8c59a494a451 | 1439 | case modBusType_uint16_t: |
brunofgc | 1:3f9e5b86d9e4 | 1440 | if(this->readings[readingIndex].addr!=enderecoControladoraVirtual){ |
brunofgc | 0:8c59a494a451 | 1441 | uint16_tBuffer = (uint16_t*) malloc(((this->readings[readingIndex].numRegs*sizeof(uint16_t))+1));//Alocando mais um byte para status |
brunofgc | 1:3f9e5b86d9e4 | 1442 | modBusMaster1::readRegister16BIT(// |
brunofgc | 0:8c59a494a451 | 1443 | this->readings[readingIndex].addr, |
brunofgc | 0:8c59a494a451 | 1444 | this->readings[readingIndex].func, |
brunofgc | 0:8c59a494a451 | 1445 | this->readings[readingIndex].reg, |
brunofgc | 0:8c59a494a451 | 1446 | this->readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1447 | uint16_tBuffer |
brunofgc | 0:8c59a494a451 | 1448 | ); |
brunofgc | 0:8c59a494a451 | 1449 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 0:8c59a494a451 | 1450 | sdCardBuf.fill((char *)uint16_tBuffer,((sizeof(uint16_t)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status |
brunofgc | 0:8c59a494a451 | 1451 | free(uint16_tBuffer); |
brunofgc | 0:8c59a494a451 | 1452 | }else{ |
brunofgc | 0:8c59a494a451 | 1453 | if((this->readings[readingIndex].reg>0)&&((this->readings[readingIndex].reg + this->readings[readingIndex].numRegs)<=4)){ |
brunofgc | 0:8c59a494a451 | 1454 | sdCardBuf.fill((char *)&aiFiltrada[this->readings[readingIndex].reg-1],sizeof(uint16_t)*this->readings[readingIndex].numRegs); |
brunofgc | 0:8c59a494a451 | 1455 | } |
brunofgc | 0:8c59a494a451 | 1456 | } |
brunofgc | 0:8c59a494a451 | 1457 | break; |
brunofgc | 0:8c59a494a451 | 1458 | |
brunofgc | 0:8c59a494a451 | 1459 | case modBusType_bit: |
brunofgc | 1:3f9e5b86d9e4 | 1460 | if(this->readings[readingIndex].addr != enderecoControladoraVirtual){ |
brunofgc | 0:8c59a494a451 | 1461 | boolBuffer = (bool*) malloc(((this->readings[readingIndex].numRegs*sizeof(bool))+1));//Alocando mais um byte para status |
brunofgc | 1:3f9e5b86d9e4 | 1462 | modBusMaster1::readCoils( |
brunofgc | 0:8c59a494a451 | 1463 | this->readings[readingIndex].addr, |
brunofgc | 0:8c59a494a451 | 1464 | this->readings[readingIndex].reg, |
brunofgc | 0:8c59a494a451 | 1465 | this->readings[readingIndex].numRegs, |
brunofgc | 0:8c59a494a451 | 1466 | boolBuffer |
brunofgc | 0:8c59a494a451 | 1467 | ); |
brunofgc | 0:8c59a494a451 | 1468 | }else{ |
brunofgc | 0:8c59a494a451 | 1469 | boolBuffer = (bool*) malloc(sizeof(bool)); |
brunofgc | 0:8c59a494a451 | 1470 | boolBuffer[0] = entradasDigitais[this->readings[readingIndex].reg]; |
brunofgc | 0:8c59a494a451 | 1471 | } |
brunofgc | 0:8c59a494a451 | 1472 | //pc.printf("Lido dentro de modBusMaster1::teste() <%f>.\n",modBusMaster1::teste()); |
brunofgc | 0:8c59a494a451 | 1473 | sdCardBuf.fill((char *)boolBuffer,((sizeof(bool)*this->readings[readingIndex].numRegs)+1));//Ao fim appendo o byte de status |
brunofgc | 0:8c59a494a451 | 1474 | free(boolBuffer); |
brunofgc | 0:8c59a494a451 | 1475 | break; |
brunofgc | 0:8c59a494a451 | 1476 | |
brunofgc | 0:8c59a494a451 | 1477 | } |
brunofgc | 0:8c59a494a451 | 1478 | } |
brunofgc | 0:8c59a494a451 | 1479 | readingIndex = sdCardBuf.fill("}log",4); //Reaproveitando a variável de 16bit readingIndex |
brunofgc | 5:8efd609dea7d | 1480 | sdCard::arquivoAberto(40000); |
brunofgc | 5:8efd609dea7d | 1481 | //Insiro os dados se for possível. |
brunofgc | 5:8efd609dea7d | 1482 | sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),readingIndex); |
brunofgc | 0:8c59a494a451 | 1483 | } |
brunofgc | 0:8c59a494a451 | 1484 | |
brunofgc | 0:8c59a494a451 | 1485 | |
brunofgc | 0:8c59a494a451 | 1486 | |
brunofgc | 0:8c59a494a451 | 1487 | |
brunofgc | 0:8c59a494a451 | 1488 | |
brunofgc | 0:8c59a494a451 | 1489 | |
brunofgc | 0:8c59a494a451 | 1490 | |
brunofgc | 0:8c59a494a451 | 1491 | |
brunofgc | 0:8c59a494a451 | 1492 | device::device() |
brunofgc | 0:8c59a494a451 | 1493 | { |
brunofgc | 0:8c59a494a451 | 1494 | this->numAlarms=0; |
brunofgc | 0:8c59a494a451 | 1495 | this->numReadings=0; |
brunofgc | 0:8c59a494a451 | 1496 | } |
brunofgc | 0:8c59a494a451 | 1497 | |
brunofgc | 0:8c59a494a451 | 1498 | int criaDevices(FILE *devicesCfg) |
brunofgc | 0:8c59a494a451 | 1499 | { |
brunofgc | 0:8c59a494a451 | 1500 | char linha[maxCaractereLeLinha]; |
brunofgc | 0:8c59a494a451 | 1501 | int i; |
brunofgc | 0:8c59a494a451 | 1502 | int linhas = 0; |
brunofgc | 0:8c59a494a451 | 1503 | devices = 0; |
brunofgc | 0:8c59a494a451 | 1504 | |
brunofgc | 0:8c59a494a451 | 1505 | while(fgets(linha,maxCaractereLeLinha,devicesCfg)) { |
brunofgc | 0:8c59a494a451 | 1506 | if(strstr(linha,"deviceCreate\\")) { |
brunofgc | 0:8c59a494a451 | 1507 | devices++; |
brunofgc | 0:8c59a494a451 | 1508 | } |
brunofgc | 0:8c59a494a451 | 1509 | linhas++; |
brunofgc | 0:8c59a494a451 | 1510 | } |
brunofgc | 0:8c59a494a451 | 1511 | |
brunofgc | 0:8c59a494a451 | 1512 | printf("Contei %u linha(s), %u device(s).\n",linhas,devices); |
brunofgc | 0:8c59a494a451 | 1513 | |
brunofgc | 0:8c59a494a451 | 1514 | //Criando vetor de objetos dispositivos; |
brunofgc | 0:8c59a494a451 | 1515 | |
brunofgc | 0:8c59a494a451 | 1516 | dispositivos = (device **) malloc(sizeof(device)*devices); |
brunofgc | 0:8c59a494a451 | 1517 | |
brunofgc | 0:8c59a494a451 | 1518 | for(i=0; i<devices; i++) { |
brunofgc | 0:8c59a494a451 | 1519 | dispositivos[i] = new device(); |
brunofgc | 0:8c59a494a451 | 1520 | //dispositivos[i]->id=i; |
brunofgc | 0:8c59a494a451 | 1521 | } |
brunofgc | 0:8c59a494a451 | 1522 | |
brunofgc | 2:ca3723da6668 | 1523 | rewind(devicesCfg); |
brunofgc | 0:8c59a494a451 | 1524 | return 1; |
brunofgc | 0:8c59a494a451 | 1525 | } |
brunofgc | 0:8c59a494a451 | 1526 | |
brunofgc | 0:8c59a494a451 | 1527 | |
brunofgc | 0:8c59a494a451 | 1528 | int configuraDevices(FILE *devicesCfg) |
brunofgc | 0:8c59a494a451 | 1529 | { |
brunofgc | 0:8c59a494a451 | 1530 | uint16_t linhas=0; |
brunofgc | 0:8c59a494a451 | 1531 | //uint16_t leituras = 0; |
brunofgc | 2:ca3723da6668 | 1532 | char linha[maxCaractereLeLinha]; |
brunofgc | 0:8c59a494a451 | 1533 | uint8_t estadoConfiguracao=estadoConfiguracao_idle; |
brunofgc | 2:ca3723da6668 | 1534 | int8_t dispositivoEmConfiguracao = -1; |
brunofgc | 2:ca3723da6668 | 1535 | |
brunofgc | 2:ca3723da6668 | 1536 | while(fgets(linha,maxCaractereLeLinha,devicesCfg)){ |
brunofgc | 2:ca3723da6668 | 1537 | if(strstr(linha,"alarmSet")) { |
brunofgc | 2:ca3723da6668 | 1538 | //printf("Criando alarme no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 2:ca3723da6668 | 1539 | alarmes++; |
brunofgc | 2:ca3723da6668 | 1540 | estadoConfiguracao = estadoConfiguracao_alarmSet; |
brunofgc | 5:8efd609dea7d | 1541 | } |
brunofgc | 2:ca3723da6668 | 1542 | |
brunofgc | 2:ca3723da6668 | 1543 | if(estadoConfiguracao == estadoConfiguracao_readingsSet){ |
brunofgc | 2:ca3723da6668 | 1544 | leituras++; |
brunofgc | 2:ca3723da6668 | 1545 | } |
brunofgc | 0:8c59a494a451 | 1546 | |
brunofgc | 2:ca3723da6668 | 1547 | if(strstr(linha,"readingsSet")) { |
brunofgc | 2:ca3723da6668 | 1548 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 2:ca3723da6668 | 1549 | estadoConfiguracao = estadoConfiguracao_readingsSet; |
brunofgc | 2:ca3723da6668 | 1550 | } |
brunofgc | 2:ca3723da6668 | 1551 | |
brunofgc | 2:ca3723da6668 | 1552 | if(strstr(linha,"scheduleSet")) { |
brunofgc | 2:ca3723da6668 | 1553 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 2:ca3723da6668 | 1554 | qtdSchedules++; |
brunofgc | 2:ca3723da6668 | 1555 | estadoConfiguracao = estadoConfiguracao_scheduleSet; |
brunofgc | 2:ca3723da6668 | 1556 | } |
brunofgc | 4:3f92979d1ffb | 1557 | |
brunofgc | 5:8efd609dea7d | 1558 | if(strstr(linha,"scheduleExceptionsSet")) { |
brunofgc | 4:3f92979d1ffb | 1559 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 5:8efd609dea7d | 1560 | qtdScheduleExceptions++; |
brunofgc | 4:3f92979d1ffb | 1561 | estadoConfiguracao = estadoConfiguracao_scheduleSet; |
brunofgc | 4:3f92979d1ffb | 1562 | } |
brunofgc | 2:ca3723da6668 | 1563 | |
brunofgc | 2:ca3723da6668 | 1564 | linhas++; |
brunofgc | 2:ca3723da6668 | 1565 | } |
brunofgc | 2:ca3723da6668 | 1566 | printf("Contei %u linha(s), configurei %u dispositivo(s), instalei %u alarme(s), %u leituras(s) e %u schedule(s) nos dispositivo(s).\n",linhas,devices,alarmes,leituras,qtdSchedules); |
brunofgc | 2:ca3723da6668 | 1567 | //Reservar memoria aqui. |
brunofgc | 2:ca3723da6668 | 1568 | |
brunofgc | 2:ca3723da6668 | 1569 | //if(resizeArray(this->numReadings, this->numReadings+1, (char **)&this->readings,sizeof(reading))) { |
brunofgc | 2:ca3723da6668 | 1570 | //(char *) malloc(sizeUnit*novo); |
brunofgc | 2:ca3723da6668 | 1571 | dispositivos[0]->readings = (reading*) malloc(sizeof(reading)*leituras); |
brunofgc | 2:ca3723da6668 | 1572 | dispositivos[0]->alarms = (alarm*) malloc(sizeof(alarm)*alarmes); |
brunofgc | 2:ca3723da6668 | 1573 | dispositivos[0]->schedules = (schedule*) malloc(sizeof(schedule)*qtdSchedules); |
brunofgc | 5:8efd609dea7d | 1574 | dispositivos[0]->scheduleExceptions = (scheduleException*) malloc(sizeof(scheduleException)*qtdScheduleExceptions); |
brunofgc | 2:ca3723da6668 | 1575 | |
brunofgc | 2:ca3723da6668 | 1576 | //Retornando ao inicio o arquivo |
brunofgc | 2:ca3723da6668 | 1577 | rewind(devicesCfg); |
brunofgc | 2:ca3723da6668 | 1578 | |
brunofgc | 0:8c59a494a451 | 1579 | while(fgets(linha,maxCaractereLeLinha,devicesCfg)) { |
brunofgc | 0:8c59a494a451 | 1580 | if(strstr(linha,"deviceCreate\\")) { |
brunofgc | 0:8c59a494a451 | 1581 | dispositivoEmConfiguracao++; |
brunofgc | 0:8c59a494a451 | 1582 | //printf("Configurando dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 0:8c59a494a451 | 1583 | estadoConfiguracao = estadoConfiguracao_creatingDevice; |
brunofgc | 0:8c59a494a451 | 1584 | } |
brunofgc | 0:8c59a494a451 | 1585 | |
brunofgc | 0:8c59a494a451 | 1586 | if(strstr(linha,"alarmSet")) { |
brunofgc | 0:8c59a494a451 | 1587 | //printf("Criando alarme no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 0:8c59a494a451 | 1588 | alarmes++; |
brunofgc | 0:8c59a494a451 | 1589 | estadoConfiguracao = estadoConfiguracao_alarmSet; |
brunofgc | 0:8c59a494a451 | 1590 | } |
brunofgc | 0:8c59a494a451 | 1591 | |
brunofgc | 0:8c59a494a451 | 1592 | if(strstr(linha,"readingsSet")) { |
brunofgc | 0:8c59a494a451 | 1593 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 2:ca3723da6668 | 1594 | leituras++; |
brunofgc | 0:8c59a494a451 | 1595 | estadoConfiguracao = estadoConfiguracao_readingsSet; |
brunofgc | 0:8c59a494a451 | 1596 | } |
brunofgc | 0:8c59a494a451 | 1597 | |
brunofgc | 4:3f92979d1ffb | 1598 | if(strstr(linha,"scheduleExceptionSet")) { |
brunofgc | 4:3f92979d1ffb | 1599 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 4:3f92979d1ffb | 1600 | qtdScheduleExceptions++; |
brunofgc | 4:3f92979d1ffb | 1601 | estadoConfiguracao = estadoConfiguracao_scheduleExceptionSet; |
brunofgc | 4:3f92979d1ffb | 1602 | } |
brunofgc | 4:3f92979d1ffb | 1603 | |
brunofgc | 0:8c59a494a451 | 1604 | if(strstr(linha,"readingsEnd")) { |
brunofgc | 0:8c59a494a451 | 1605 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 0:8c59a494a451 | 1606 | estadoConfiguracao = estadoConfiguracao_idle; |
brunofgc | 0:8c59a494a451 | 1607 | } |
brunofgc | 0:8c59a494a451 | 1608 | |
brunofgc | 0:8c59a494a451 | 1609 | if(strstr(linha,"scheduleSet")) { |
brunofgc | 0:8c59a494a451 | 1610 | //printf("Criando leituras no dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 0:8c59a494a451 | 1611 | qtdSchedules++; |
brunofgc | 0:8c59a494a451 | 1612 | estadoConfiguracao = estadoConfiguracao_scheduleSet; |
brunofgc | 5:8efd609dea7d | 1613 | } |
brunofgc | 0:8c59a494a451 | 1614 | |
brunofgc | 0:8c59a494a451 | 1615 | if(strstr(linha,">")) { |
brunofgc | 0:8c59a494a451 | 1616 | //printf("Fechando dispositivo de indice %u.\n",dispositivoEmConfiguracao); |
brunofgc | 0:8c59a494a451 | 1617 | estadoConfiguracao = estadoConfiguracao_idle; |
brunofgc | 0:8c59a494a451 | 1618 | } |
brunofgc | 0:8c59a494a451 | 1619 | |
brunofgc | 0:8c59a494a451 | 1620 | switch(estadoConfiguracao) { |
brunofgc | 0:8c59a494a451 | 1621 | case estadoConfiguracao_creatingDevice: |
brunofgc | 0:8c59a494a451 | 1622 | /*if(strstr(linha,"id:")) { |
brunofgc | 0:8c59a494a451 | 1623 | sscanf(linha,"id:%u",&dispositivos[dispositivoEmConfiguracao]->id); |
brunofgc | 0:8c59a494a451 | 1624 | }*/ |
brunofgc | 0:8c59a494a451 | 1625 | break; |
brunofgc | 0:8c59a494a451 | 1626 | case estadoConfiguracao_alarmSet: |
brunofgc | 0:8c59a494a451 | 1627 | //Inclue linha nos alarmes do dispositivo em configuração |
brunofgc | 0:8c59a494a451 | 1628 | dispositivos[dispositivoEmConfiguracao]->setAlarm(linha); |
brunofgc | 0:8c59a494a451 | 1629 | break; |
brunofgc | 0:8c59a494a451 | 1630 | case estadoConfiguracao_readingsSet: |
brunofgc | 0:8c59a494a451 | 1631 | //Inclue linha nas leituras do dispositivo em configuração |
brunofgc | 0:8c59a494a451 | 1632 | dispositivos[dispositivoEmConfiguracao]->setReading(linha); |
brunofgc | 0:8c59a494a451 | 1633 | break; |
brunofgc | 0:8c59a494a451 | 1634 | case estadoConfiguracao_scheduleSet: |
brunofgc | 4:3f92979d1ffb | 1635 | //Inclue linha nos agendamentos do dispositivo em configuração |
brunofgc | 0:8c59a494a451 | 1636 | dispositivos[dispositivoEmConfiguracao]->setSchedule(linha); |
brunofgc | 4:3f92979d1ffb | 1637 | break; |
brunofgc | 4:3f92979d1ffb | 1638 | case estadoConfiguracao_scheduleExceptionSet: |
brunofgc | 4:3f92979d1ffb | 1639 | //Inclue linha nas excessões dos agendamentos do dispositivo em configuração |
brunofgc | 4:3f92979d1ffb | 1640 | dispositivos[dispositivoEmConfiguracao]->setScheduleException(linha); |
brunofgc | 0:8c59a494a451 | 1641 | break; |
brunofgc | 0:8c59a494a451 | 1642 | } |
brunofgc | 0:8c59a494a451 | 1643 | } |
brunofgc | 2:ca3723da6668 | 1644 | |
brunofgc | 0:8c59a494a451 | 1645 | return 1; |
brunofgc | 0:8c59a494a451 | 1646 | } |
brunofgc | 0:8c59a494a451 | 1647 | |
brunofgc | 0:8c59a494a451 | 1648 | |
brunofgc | 0:8c59a494a451 | 1649 | |
brunofgc | 0:8c59a494a451 | 1650 | void testaTudoDevices() |
brunofgc | 0:8c59a494a451 | 1651 | { |
brunofgc | 0:8c59a494a451 | 1652 | int i; |
brunofgc | 0:8c59a494a451 | 1653 | int j; |
brunofgc | 0:8c59a494a451 | 1654 | |
brunofgc | 0:8c59a494a451 | 1655 | for(i=0; i<devices; i++) { |
brunofgc | 0:8c59a494a451 | 1656 | printf("Dispositivo %u tem %u alarmes sendo eles:\n",i,dispositivos[i]->numAlarms); |
brunofgc | 0:8c59a494a451 | 1657 | |
brunofgc | 0:8c59a494a451 | 1658 | for(j=0; j<dispositivos[i]->numAlarms; j++) { |
brunofgc | 0:8c59a494a451 | 1659 | printf("Alarme %u:\n",j); |
brunofgc | 0:8c59a494a451 | 1660 | printf("\tId <%lu>.\n",dispositivos[i]->alarms[j].id); |
brunofgc | 0:8c59a494a451 | 1661 | printf("\tSeconds <%lu>.\n",dispositivos[i]->alarms[j].seconds); |
brunofgc | 0:8c59a494a451 | 1662 | 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 | 1663 | if(dispositivos[i]->alarms[j].type==modBusType_float) { |
brunofgc | 0:8c59a494a451 | 1664 | float value; |
brunofgc | 0:8c59a494a451 | 1665 | dispositivos[i]->floatToBin(0,&value,&dispositivos[i]->alarms[j].value[0]); |
brunofgc | 0:8c59a494a451 | 1666 | if(dispositivos[i]->alarms[j].max){ |
brunofgc | 0:8c59a494a451 | 1667 | printf("\tFloat max <%f>.\n",value); |
brunofgc | 0:8c59a494a451 | 1668 | } |
brunofgc | 0:8c59a494a451 | 1669 | else{printf("\tFloat min <%f>.\n",value);} |
brunofgc | 0:8c59a494a451 | 1670 | } |
brunofgc | 0:8c59a494a451 | 1671 | |
brunofgc | 0:8c59a494a451 | 1672 | if(dispositivos[i]->alarms[j].type==modBusType_uint32_t) { |
brunofgc | 0:8c59a494a451 | 1673 | uint32_t value; |
brunofgc | 0:8c59a494a451 | 1674 | dispositivos[i]->uint32_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]); |
brunofgc | 0:8c59a494a451 | 1675 | if(dispositivos[i]->alarms[j].max){ |
brunofgc | 0:8c59a494a451 | 1676 | printf("\tuint32_t max <%lu>.\n",value); |
brunofgc | 0:8c59a494a451 | 1677 | } |
brunofgc | 0:8c59a494a451 | 1678 | else{printf("\tuint32_t min <%lu>.\n",value);} |
brunofgc | 0:8c59a494a451 | 1679 | } |
brunofgc | 0:8c59a494a451 | 1680 | |
brunofgc | 0:8c59a494a451 | 1681 | if(dispositivos[i]->alarms[j].type==modBusType_uint16_t) { |
brunofgc | 0:8c59a494a451 | 1682 | uint16_t value; |
brunofgc | 0:8c59a494a451 | 1683 | dispositivos[i]->uint16_t_ToBin(0,&value,&dispositivos[i]->alarms[j].value[0]); |
brunofgc | 0:8c59a494a451 | 1684 | if(dispositivos[i]->alarms[j].max){ |
brunofgc | 0:8c59a494a451 | 1685 | printf("\tuint16_t max <%lu>.\n",value); |
brunofgc | 0:8c59a494a451 | 1686 | } |
brunofgc | 0:8c59a494a451 | 1687 | else{printf("\tuint16_t min <%lu>.\n",value);} |
brunofgc | 0:8c59a494a451 | 1688 | } |
brunofgc | 0:8c59a494a451 | 1689 | |
brunofgc | 0:8c59a494a451 | 1690 | if(dispositivos[i]->alarms[j].type==modBusType_bit) { |
brunofgc | 0:8c59a494a451 | 1691 | uint8_t value; |
brunofgc | 0:8c59a494a451 | 1692 | value=dispositivos[i]->alarms[j].value[0]; |
brunofgc | 0:8c59a494a451 | 1693 | if(dispositivos[i]->alarms[j].max){ |
brunofgc | 0:8c59a494a451 | 1694 | printf("\tBIT max <%lu>.\n",value); |
brunofgc | 0:8c59a494a451 | 1695 | } |
brunofgc | 0:8c59a494a451 | 1696 | else{printf("\tBIT min <%lu>.\n",value);} |
brunofgc | 0:8c59a494a451 | 1697 | } |
brunofgc | 0:8c59a494a451 | 1698 | |
brunofgc | 0:8c59a494a451 | 1699 | printf("\tact:%lu\n",dispositivos[i]->alarms[j].idAct); |
brunofgc | 0:8c59a494a451 | 1700 | } |
brunofgc | 0:8c59a494a451 | 1701 | |
brunofgc | 4:3f92979d1ffb | 1702 | printf("Dispositivo %u tem %u schedules sendo eles:\n",i,dispositivos[i]->numSchedules); |
brunofgc | 0:8c59a494a451 | 1703 | for(j=0; j<dispositivos[i]->numSchedules; j++) { |
brunofgc | 0:8c59a494a451 | 1704 | printf("Schedule %u:\n",j); |
brunofgc | 0:8c59a494a451 | 1705 | printf("\tId <%lu>.\n",dispositivos[i]->schedules[j].id); |
brunofgc | 0:8c59a494a451 | 1706 | printf("\tweekday <%lu>.\n",dispositivos[i]->schedules[j].weekday); |
brunofgc | 0:8c59a494a451 | 1707 | printf("\tminute <%lu>.\n",dispositivos[i]->schedules[j].minute); |
brunofgc | 0:8c59a494a451 | 1708 | printf("\tidAct <%lu>.\n",dispositivos[i]->schedules[j].idAct); |
brunofgc | 0:8c59a494a451 | 1709 | } |
brunofgc | 4:3f92979d1ffb | 1710 | |
brunofgc | 4:3f92979d1ffb | 1711 | printf("Dispositivo %u tem %u scheduleExceptions sendo eles:\n",i,dispositivos[i]->numScheduleExceptions); |
brunofgc | 4:3f92979d1ffb | 1712 | for(j=0; j<dispositivos[i]->numScheduleExceptions; j++) { |
brunofgc | 4:3f92979d1ffb | 1713 | printf("scheduleException %u:\n",j); |
brunofgc | 4:3f92979d1ffb | 1714 | printf("\tid_schedule <%lu>.\n",dispositivos[i]->scheduleExceptions[j].id_schedule); |
brunofgc | 4:3f92979d1ffb | 1715 | printf("\ttimestamp <%lu>.\n",dispositivos[i]->scheduleExceptions[j].timestamp); |
brunofgc | 4:3f92979d1ffb | 1716 | } |
brunofgc | 0:8c59a494a451 | 1717 | |
brunofgc | 0:8c59a494a451 | 1718 | printf("Dispositivo %u tem %u leituras sendo elas:\n",i,dispositivos[i]->numReadings); |
brunofgc | 0:8c59a494a451 | 1719 | for(j=0; j<dispositivos[i]->numReadings; j++) { |
brunofgc | 0:8c59a494a451 | 1720 | printf("Leitura %u:",j); |
brunofgc | 0:8c59a494a451 | 1721 | switch(dispositivos[i]->readings[j].type) { |
brunofgc | 0:8c59a494a451 | 1722 | case modBusType_float: |
brunofgc | 0:8c59a494a451 | 1723 | printf("Tipo float "); |
brunofgc | 0:8c59a494a451 | 1724 | break; |
brunofgc | 0:8c59a494a451 | 1725 | case modBusType_uint32_t: |
brunofgc | 0:8c59a494a451 | 1726 | printf("Tipo uint32_t "); |
brunofgc | 5:8efd609dea7d | 1727 | break; |
brunofgc | 0:8c59a494a451 | 1728 | case modBusType_uint16_t: |
brunofgc | 0:8c59a494a451 | 1729 | printf("Tipo uint16_t "); |
brunofgc | 5:8efd609dea7d | 1730 | break; |
brunofgc | 0:8c59a494a451 | 1731 | case modBusType_uint8_t: |
brunofgc | 0:8c59a494a451 | 1732 | printf("Tipo uint8_t "); |
brunofgc | 5:8efd609dea7d | 1733 | break; |
brunofgc | 0:8c59a494a451 | 1734 | case modBusType_bit: |
brunofgc | 0:8c59a494a451 | 1735 | printf("Tipo bool "); |
brunofgc | 5:8efd609dea7d | 1736 | break; |
brunofgc | 0:8c59a494a451 | 1737 | } |
brunofgc | 0:8c59a494a451 | 1738 | 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 | 1739 | } |
brunofgc | 0:8c59a494a451 | 1740 | } |
brunofgc | 0:8c59a494a451 | 1741 | } |