teste de publish

Committer:
brunofgc
Date:
Thu Nov 09 12:53:30 2017 +0000
Revision:
10:b61bb770f7b2
Parent:
9:0230ec4d2e68
Child:
11:013a2ab0c12e
Ultima vers?o instalada no HSR com bootloader

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