teste de publish

Committer:
brunofgc
Date:
Tue Jul 11 11:55:44 2017 +0000
Revision:
5:8efd609dea7d
Parent:
4:3f92979d1ffb
Child:
6:f20c0e094294
Vers?o UCSal piloto 1

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