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