teste de publish

Committer:
brunofgc
Date:
Tue Feb 06 16:33:21 2018 +0000
Revision:
15:6d9e9cafe138
Parent:
13:c1122e7b0b36
Child:
16:1b43a57ee676
Ultima Vers?o sem usar o URLDecode2 para parsear a string vinda do GET de config.bin

Who changed what in which revision?

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