Subversion Repositories heizung

Rev

Rev 3 | Rev 5 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 4
Line 41... Line 41...
41
}HEIZUNG;
41
}HEIZUNG;
42
 
42
 
43
typedef struct HEIZINDEX
43
typedef struct HEIZINDEX
44
{
44
{
45
      HEIZUNG *heizung;
45
      HEIZUNG *heizung;
46
      HEIZUNG *next;
46
      struct HEIZINDEX *next;
47
}HEIZINDEX;
47
}HEIZINDEX;
48
 
48
 
49
CONFIGURE configs;
49
CONFIGURE configs;
50
HEIZINDEX *HeizFirst;
50
HEIZINDEX *HeizFirst;
51
float ActTemperature;
51
float ActTemperature;
Line 56... Line 56...
56
// Prototypes
56
// Prototypes
57
void daemon_start(int ignsigcld);
57
void daemon_start(int ignsigcld);
58
void *pthr_parser(void *pV_data);
58
void *pthr_parser(void *pV_data);
59
void sig_child(void);
59
void sig_child(void);
60
int parseCommand(int s1, char *cmd);
60
int parseCommand(int s1, char *cmd);
-
 
61
void changeToUser(char *, char *);
61
 
62
 
62
HEIZINDEX *allocateMemory(void);
63
HEIZINDEX *allocateMemory(void);
-
 
64
HEIZINDEX *insertMemory(HEIZINDEX *pos);
63
void freeMemory(void);
65
void freeMemory(void);
64
void freeChainMember(HEIZINDEX *member);
66
HEIZINDEX *freeChainMember(HEIZINDEX *member);
-
 
67
void freeDay(int wday);
-
 
68
void insertMember(int wday, long endt, float temp);
65
int readHeizPlan(void);
69
int readHeizPlan(void);
66
int writeHeizPlan(void);
70
int writeHeizPlan(void);
67
void readConf(void);
71
void readConf(void);
68
 
72
 
69
char *readLine(int fd, long *offset, char *buf, int bufLen);
73
char *readLine(int fd, char *buf, int bufLen);
70
char *trim(char *str);
74
char *trim(char *str);
71
char *remove_string(char *str, char *search, char *ret);
75
char *remove_string(char *str, char *search, char *ret);
72
 
76
 
73
static pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
77
static pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
74
 
78
 
Line 81... Line 85...
81
 
85
 
82
	HeizFirst = NULL;
86
	HeizFirst = NULL;
83
	readConf();
87
	readConf();
84
	// Now daemonize this application
88
	// Now daemonize this application
85
	daemon_start(0);
89
	daemon_start(0);
86
	changeToUser(configs.User, configs.Grp);
90
	changeToUser(&configs.User[0], &configs.Grp[0]);
87
	// Now start our Thread
91
	// Now start our Thread
88
	if (pthread_create(&pthr_pars, NULL, pthr_parser, (void *)0) != 0)
92
	if (pthread_create(&pthr_pars, NULL, pthr_parser, (void *)0) != 0)
89
	{
93
	{
90
	   syslog (LOG_DAEMON,"Create of thread \"pthr_parser\" failed!");
94
	   syslog (LOG_DAEMON,"Create of thread \"pthr_parser\" failed!");
91
	   return 1;
95
	   return 1;
Line 278... Line 282...
278
	   {
282
	   {
279
	      if (i < (sizeof(buf) - 1))
283
	      if (i < (sizeof(buf) - 1))
280
	      {
284
	      {
281
		 buf[i] = ch;
285
		 buf[i] = ch;
282
 
286
 
283
		 if (ch == ';')
287
		 if (ch == ';' || ch == 0x0d)
284
		 {
288
		 {
-
 
289
		    if (!strncmp(buf, "quit", 4))
285
//		    buf[i+1] = 0;
290
		       break;
286
 
291
 
287
		    if (!parseCommand(s1,buf))
292
		    if (!parseCommand(s1,buf))
288
		    {
293
		    {
289
		    char hv0[128];
294
		    char hv0[128];
290
 
295
 
Line 310... Line 315...
310
	return NULL;
315
	return NULL;
311
}
316
}
312
 
317
 
313
int parseCommand(int s1, char *cmd)
318
int parseCommand(int s1, char *cmd)
314
{
319
{
315
char bef[128],par[32], *p;
320
char bef[32],par[1024], *p;
316
char hv0[32];
321
char hv0[256];
317
int i,j;
322
int i,j;
318
HEIZINDEX *act;
323
HEIZINDEX *act, *last;
319
 
324
 
320
	memset(bef,0,sizeof(bef));
325
	memset(bef,0,sizeof(bef));
321
	memset(par,0,sizeof(par));
326
	memset(par,0,sizeof(par));
322
 
327
 
323
	if ((p = strchr(cmd,':')) != NULL)		// do we have a parameter?
328
	if ((p = strchr(cmd,':')) != NULL)		// do we have a parameter?
324
	{
329
	{
325
	   strncpy(bef,cmd,(p - cmd) - 1);
330
	   strncpy(bef,cmd,p - cmd);
326
	   strncpy(par,p,strlen(p)-1);
331
	   strncpy(par,p+1,strlen(p+1));
327
	}
332
	}
328
	else
333
	else
329
	   strncpy(bef,cmd,strlen(cmd)-1);
334
	   strncpy(bef,cmd,strlen(cmd)-1);
330
 
-
 
-
 
335
sprintf(&hv0[0], "%s --> %s\n", bef, par);
-
 
336
write(s1, hv0, strlen(hv0));
331
	if (strcasecmp(bef, "LIST"))		// Write out current list
337
	if (!strcasecmp(bef, "LIST"))		// Write out current list
332
	{
338
	{
333
	   act = HeizFirst;
339
	   act = HeizFirst;
334
	   i = 1;
340
	   i = 1;
335
 
341
 
336
	   while (act)
342
	   while (act)
337
	   {
343
	   {
338
	      sprintf(&hv0[0], "LINE:%d:%d:%d:%d:%f;", i, act->heizung->wday,
344
	      sprintf(&hv0[0], "LINE:%d:%d:%lu:%lu:%f;", i, act->heizung->wday,
339
		      act->heizung->start, act->heizung->end, act->heizung->temp);
345
		      act->heizung->start, act->heizung->end, act->heizung->temp);
340
	      write(s1, &hv0[0], strlen(hv0));
346
	      write(s1, &hv0[0], strlen(hv0));
341
	      i++;
347
	      i++;
342
	      act = act->next;
348
	      act = act->next;
343
	   }
349
	   }
344
	}
350
	}
345
 
351
 
346
	if (strcasecmp(bef, "GET WDAY"))		// Write out a particular week day
352
	if (!strcasecmp(bef, "GET WDAY"))		// Write out a particular week day
347
	{
353
	{
348
	int wday = atoi(cmd);
354
	int wday = atoi(par);
349
 
355
 
350
	   if (wday < 1 || wday > 7)
356
	   if (wday < 1 || wday > 7)
351
	   {
357
	   {
352
	      strcpy(&hv0[0], "ERROR:Invalid week day");
358
	      strcpy(&hv0[0], "ERROR:Invalid week day");
353
	      write(s1, &hv0[0], strlen(hv0));
359
	      write(s1, &hv0[0], strlen(hv0));
Line 368... Line 374...
368
 
374
 
369
	   i = 1;
375
	   i = 1;
370
 
376
 
371
	   while (act && act->heizung->wday == wday)
377
	   while (act && act->heizung->wday == wday)
372
	   {
378
	   {
373
	      sprintf(&hv0[0], "LINE:%d:%d:%d:%d:%f;", i, act->heizung->wday,
379
	      sprintf(&hv0[0], "LINE:%d:%d:%lu:%lu:%f;", i, act->heizung->wday,
374
		      act->heizung->start, act->heizung->end, act->heizung->temp);
380
		      act->heizung->start, act->heizung->end, act->heizung->temp);
375
	      write(s1, &hv0[0], strlen(hv0));
381
	      write(s1, &hv0[0], strlen(hv0));
376
	      i++;
382
	      i++;
377
	      act = act->next;
383
	      act = act->next;
378
	   }
384
	   }
379
	}
385
	}
380
 
386
 
381
	if (strcasecmp(bef, "GET TEMP"))	// Return actual temperature
387
	if (!strcasecmp(bef, "GET TEMP"))	// Return actual temperature
382
	{
388
	{
383
	   sprintf(&hv0[0], "TEMP:%f;", ActTemperature);
389
	   sprintf(&hv0[0], "TEMP:%f;", ActTemperature);
384
	   write(s1, &hv0[0], strlen(hv0));
390
	   write(s1, &hv0[0], strlen(hv0));
385
	}
391
	}
386
 
392
 
387
	if (strcasecmp(bef, "GET PRESSURE"))	// Return the actual air pressure
393
	if (!strcasecmp(bef, "GET PRESSURE"))	// Return the actual air pressure
388
	{
394
	{
389
	   sprintf(&hv0[0], "PRESSURE:%f;", ActPressure);
395
	   sprintf(&hv0[0], "PRESSURE:%f;", ActPressure);
390
	   write(s1, &hv0[0], strlen(hv0));
396
	   write(s1, &hv0[0], strlen(hv0));
391
	}
397
	}
392
 
398
 
Line 394... Line 400...
394
	// <count>   number of entries following
400
	// <count>   number of entries following
395
	// <day>     The day of the week
401
	// <day>     The day of the week
396
	// <end1>    The end time
402
	// <end1>    The end time
397
	// <temp>    The temperature wanted until end time is reached
403
	// <temp>    The temperature wanted until end time is reached
398
	//
404
	//
399
	if (strcasecmp(bef, "SET DAY"))		// Set the plan for a particular day
405
	if (!strcasecmp(bef, "SET DAY"))		// Set the plan for a particular day
400
	{
406
	{
401
	int count, wday, i;
407
	int count, wday, i;
402
	long endt;
408
	long endt;
403
	float temp;
409
	float temp;
404
 
410
 
-
 
411
	   count = atoi(par);
405
	   remove_string(bef, "SET DAY:", &hv0[0]);
412
	   remove_string(par, ":", &hv0[0]);
406
	   count = atoi(bef);
413
	   wday = atoi(par);
-
 
414
	   freeDay(wday);
-
 
415
	   last = NULL;
407
 
416
 
408
	   if (count > 0)
417
	   if (count > 0)
409
	   {
418
	   {
410
	      for (i = 0; i < count; i++)
419
	      for (i = 0; i < count; i++)
411
	      {
420
	      {
412
		 remove_string(bef, ":", &hv0[0]);
421
		 remove_string(par, ":", &hv0[0]);
413
		 wday = atoi(bef);
-
 
414
		 remove_string(bef, ":", &hv0[0]);
-
 
415
		 endt = atol(bef);
422
		 endt = atol(par);
416
		 remove_string(bef, ":", &hv0[0]);
423
		 remove_string(par, ":", &hv0[0]);
417
		 temp = atof(bef);
424
		 temp = atof(par);
418
 
-
 
419
		 act = HeizFirst;
-
 
420
 
425
 
421
		 while(act)
426
		 if ((act = allocateMemory()) == NULL)
422
		 {
427
		 {
-
 
428
		    syslog(LOG_DAEMON,"Error allocating memory for a temperature line.");
-
 
429
		    sprintf(&hv0[0], "ERROR:Not enough memory!");
-
 
430
		    write(s1, &hv0[0], strlen(hv0));
-
 
431
		    return 0;
-
 
432
		 }
-
 
433
 
423
		    if (act->heizung->wday == wday)
434
		 act->heizung->wday = wday;
-
 
435
		 act->heizung->end = endt;
-
 
436
		 act->heizung->temp = temp;
-
 
437
 
-
 
438
		 if (last)
-
 
439
		    act->heizung->start = act->heizung->end;
-
 
440
		 else
-
 
441
		    act->heizung->start = 0L;
-
 
442
 
-
 
443
		 last = act;
424
	      }
444
	      }
425
	   }
445
	   }
426
	}
446
	}
427
 
447
 
428
	if (strcasecmp(bef, "SET PLAN"))	// Set the complete plan
-
 
429
	{
-
 
430
	}
-
 
431
 
-
 
432
	// SET TEMP:<wday>:<end>:<temp>;
448
	// SET TEMP:<wday>:<end>:<temp>;
433
	if (strcasecmp(bef, "SET TEMP"))	// Set the temperature for a particular day and line
449
	if (!strcasecmp(bef, "SET TEMP:"))	// Set the temperature for a particular day and line
434
	{
450
	{
435
	int wday, line;
451
	int wday;
-
 
452
	unsigned long endt;
436
	float tmp;
453
	float tmp;
437
 
454
 
438
	   if ((p = strchr(cmd, ':')) != NULL)
-
 
439
	   {
-
 
440
	      wday = atoi(cmd);
455
	   wday = atoi(par);
441
	      line = atoi(p+1);
456
	   remove_string(par, ":", &hv0[0]);
442
 
-
 
443
	      if (wday > 0 && wday <= 7 && line > 0 && (p = strchr(p+1, ':')) != NULL)
-
 
444
	      {
-
 
445
		 tmp = atof(p+1);
457
	   endt = atol(par);
446
 
-
 
447
		 if (tmp < 5.0)
-
 
448
		    tmp = 5.0;
-
 
449
 
-
 
450
		 if (tmp > 30.0)
458
	   remove_string(par, ":", &hv0[0]);
451
		    tmp = 30.0;
459
	   tmp = atof(par);
452
 
-
 
453
		 act = HeizFirst;
-
 
454
		 i = 1;
-
 
455
 
-
 
456
		 while(act)
-
 
457
		 {
-
 
458
		    if (act->heizung->wday == wday)
-
 
459
		    {
-
 
460
		       if (i == line)
-
 
461
		       {
-
 
462
			  act->heizung->temp = tmp;
460
	   insertMember(wday, endt, tmp);
463
			  writeHeizPlan();
-
 
464
		       }
-
 
465
 
-
 
466
		       i++;
-
 
467
		    }
-
 
468
 
-
 
469
		    act = act->next;
-
 
470
		 }
-
 
471
	      }
-
 
472
	   }
-
 
473
	}
461
	}
474
 
462
 
475
	return 1;
463
	return 1;
476
}
464
}
477
 
465
 
Line 486... Line 474...
486
	prev = NULL;
474
	prev = NULL;
487
 
475
 
488
	while(act)
476
	while(act)
489
	{
477
	{
490
	   if (act->heizung->wday == wday)
478
	   if (act->heizung->wday == wday)
491
	   {
-
 
492
	      act = freeChainMember(act);
479
	      act = freeChainMember(act);
493
	      continue;
-
 
494
	   }
-
 
495
 
480
 
496
	   act = act->next;
481
	   act = act->next;
497
	}  
482
	}  
498
}
483
}
499
 
484
 
Line 566... Line 551...
566
 
551
 
567
	if (act == member)
552
	if (act == member)
568
	{
553
	{
569
	   if (prev)
554
	   if (prev)
570
	      prev->next = act->next;
555
	      prev->next = act->next;
-
 
556
	   else
-
 
557
	   {
-
 
558
	      prev = act->next;
-
 
559
 
-
 
560
	      if (act == HeizFirst)
-
 
561
		 HeizFirst = act->next;
-
 
562
	   }
571
 
563
 
572
	   if (act->heizung)
564
	   if (act->heizung)
573
	      free(act->heizung);
565
	      free(act->heizung);
574
 
566
 
575
	   free(act);
567
	   free(act);
576
	   return prev->next;
568
	   return prev;
577
	}
569
	}
578
 
570
 
579
	return NULL;
571
	return NULL;
580
}
572
}
581
 
573
 
582
/*
574
/*
583
 * Allocate the memory for the actual heizung plan,
575
 * Allocate the memory for the actual heizung plan.
-
 
576
 * This function appends an element to the end of the chain.
584
 */
577
 */
585
HEIZINDEX *allocateMemory()
578
HEIZINDEX *allocateMemory()
586
{
579
{
587
HEIZINDEX *act, *last;
580
HEIZINDEX *act, *last;
588
 
581
 
Line 624... Line 617...
624
 
617
 
625
	return NULL;
618
	return NULL;
626
}
619
}
627
 
620
 
628
/*
621
/*
-
 
622
 * Allocate the memory for the actual heizung plan.
-
 
623
 * This function inserts an element into the chain.
-
 
624
 */
-
 
625
HEIZINDEX *insertMemory(HEIZINDEX *pos)
-
 
626
{
-
 
627
HEIZINDEX *act;
-
 
628
 
-
 
629
	if (!HeizFirst)
-
 
630
	{
-
 
631
	   HeizFirst = malloc(sizeof(HEIZINDEX));
-
 
632
 
-
 
633
	   if (HeizFirst)
-
 
634
	   {
-
 
635
	      HeizFirst->heizung = malloc(sizeof(HEIZUNG));
-
 
636
	      HeizFirst->next = NULL;
-
 
637
	   }
-
 
638
	   else
-
 
639
	      return NULL;
-
 
640
 
-
 
641
	   return HeizFirst;
-
 
642
	}
-
 
643
	else
-
 
644
	{
-
 
645
	   act = malloc(sizeof(HEIZINDEX));
-
 
646
 
-
 
647
	   if (act)
-
 
648
	   {
-
 
649
	      act->heizung = malloc(sizeof(HEIZUNG));
-
 
650
	      act->next = pos->next;
-
 
651
	      pos->next = act;
-
 
652
	   }
-
 
653
	   else
-
 
654
	      return NULL;
-
 
655
 
-
 
656
	   return act;
-
 
657
	}
-
 
658
 
-
 
659
	return NULL;
-
 
660
}
-
 
661
 
-
 
662
/*
629
 * The following functions read a config file and put the
663
 * The following functions read a config file and put the
630
 * contents into a structure.
664
 * contents into a structure.
631
 */
665
 */
632
void readConf(void)
666
void readConf(void)
633
{
667
{
634
int fd;
668
int fd;
635
char confFile[512], line[512];
669
char confFile[512], line[512];
636
char *home, *p;
670
char *home, *p;
637
char hv0[64], hv1[128];
671
char hv0[64], hv1[128];
638
long offset = 0;
-
 
639
 
672
 
640
	home = getenv("HOME");
673
	home = getenv("HOME");
641
	fd = -1;
674
	fd = -1;
642
 
675
 
643
	if (!access("/etc/heizung.conf",R_OK))
676
	if (!access("/etc/heizung.conf",R_OK))
Line 650... Line 683...
650
	{
683
	{
651
	   strcpy(confFile,home);
684
	   strcpy(confFile,home);
652
	   strcat(confFile,"/.heizung.conf");
685
	   strcat(confFile,"/.heizung.conf");
653
 
686
 
654
	   if (access(confFile,R_OK))
687
	   if (access(confFile,R_OK))
-
 
688
	   {
-
 
689
	      syslog(LOG_WARNING,"Even config file %s was not found!", confFile);
655
	      confFile[0] = 0;
690
	      confFile[0] = 0;
-
 
691
	   }
656
	}
692
	}
657
	else
693
	else
658
	   confFile[0] = 0;
694
	   confFile[0] = 0;
659
 
695
 
660
        memset(&configs, 0, sizeof(configs));
696
        memset(&configs, 0, sizeof(configs));
-
 
697
        strcpy(configs.User,"nobody");
-
 
698
        strcpy(configs.Grp,"nobody");
-
 
699
        strcpy(configs.HeizPath, "/var/www/.HeizPlan.conf");
-
 
700
        configs.port = 11001;
661
 
701
 
662
	if (!confFile[0] && (fd = open(confFile,O_RDONLY)) == -1)
702
	if (!confFile[0] || (fd = open(confFile,O_RDONLY)) == -1)
663
	{
703
	{
-
 
704
	   if (confFile[0])
664
	   syslog(LOG_WARNING,"Error opening the config file! Using built in defaults.");
705
	      syslog(LOG_WARNING,"Error opening the config file %s! Using built in defaults. (%s)", confFile, strerror(errno));
665
	   strcpy(configs.User,"nobody");
706
	   else
666
	   strcpy(configs.Grp,"nobody");
-
 
667
	   strcpy(configs.HeizPath, "/var/www/.HeizPlan.conf");
707
	      syslog(LOG_WARNING,"Error opening the config file! Using built in defaults.");
668
	   configs.port = 11001;
-
 
-
 
708
 
669
	   return;
709
	   return;
670
	}
710
	}
671
 
711
 
672
	while (readLine(fd, &offset, &line[0], sizeof(line)) != NULL)
712
	while (readLine(fd, &line[0], sizeof(line)) != NULL)
673
	{
713
	{
674
	   int len;
714
	   int len;
675
 
715
 
676
	   trim (&line[0]);
716
	   trim (&line[0]);
677
 
717
 
678
	   if (line[0] == '#')
718
	   if (line[0] == '#' || !strlen(line))
679
	      continue;
719
	      continue;
680
 
720
 
681
	   if ((p = strchr (line, '=')) == NULL)
721
	   if ((p = strchr (line, '=')) == NULL)
682
	      continue;
722
	      continue;
683
 
723
 
Line 687... Line 727...
687
 
727
 
688
	   if (len > sizeof(hv0))
728
	   if (len > sizeof(hv0))
689
	      len = sizeof(hv0) - 1;
729
	      len = sizeof(hv0) - 1;
690
 
730
 
691
	   strncpy (hv0, line, len);
731
	   strncpy (hv0, line, len);
-
 
732
	   hv0[len] = 0;
692
	   trim (hv0);
733
	   trim (hv0);
693
	   len = strlen(p);
734
	   len = strlen(p);
694
 
735
 
695
	   if (len > sizeof(hv1))
736
	   if (len > sizeof(hv1))
696
	      len = sizeof(hv0) - 1;
737
	      len = sizeof(hv0) - 1;
697
 
738
 
698
	   strncpy (hv1, p, len);
739
	   strncpy (hv1, p, len);
-
 
740
	   hv1[len] = 0;
699
	   trim (hv1);
741
	   trim (hv1);
700
 
742
 
701
	   if (!strcasecmp(hv0, "user"))
743
	   if (!strcasecmp(hv0, "user"))
-
 
744
	   {
-
 
745
	      syslog(LOG_INFO,"Found \"user\": %s", hv1);
702
	      strncpy (configs.User, hv1, sizeof(configs.User));
746
	      strncpy (configs.User, hv1, sizeof(configs.User));
-
 
747
	   }
703
 
748
 
704
	   if (!strcasecmp(hv0, "group"))
749
	   if (!strcasecmp(hv0, "group"))
-
 
750
	   {
-
 
751
	      syslog(LOG_INFO,"Found \"group\": %s", hv1);
705
	      strncpy (configs.Grp, hv1, sizeof(configs.Grp));
752
	      strncpy (configs.Grp, hv1, sizeof(configs.Grp));
-
 
753
	   }
706
 
754
 
707
	   if (!strcasecmp(hv0, "port"))
755
	   if (!strcasecmp(hv0, "port"))
-
 
756
	   {
-
 
757
	      syslog(LOG_INFO,"Found \"port\": %s", hv1);
708
	      configs.port = atoi (hv1);
758
	      configs.port = atoi (hv1);
-
 
759
	   }
709
 
760
 
710
	   if (!strcasecmp(hv0, "heizpath"))
761
	   if (!strcasecmp(hv0, "heizpath"))
-
 
762
	   {
-
 
763
	      syslog(LOG_INFO,"Found \"heizpath\": %s", hv1);
711
	      strncpy (configs.HeizPath, hv1, sizeof(configs.HeizPath));
764
	      strncpy (configs.HeizPath, hv1, sizeof(configs.HeizPath));
-
 
765
	   }
712
	}
766
	}
713
 
767
 
714
	close (fd);
768
	close (fd);
715
}
769
}
716
 
770
 
Line 721... Line 775...
721
char line[512];
775
char line[512];
722
char *p, *xp;
776
char *p, *xp;
723
char hv0[64], hv1[128];
777
char hv0[64], hv1[128];
724
float temperature;
778
float temperature;
725
long offset = 0;
779
long offset = 0;
726
HEIZINDEX *act;
780
HEIZINDEX *act, *prev;
727
 
781
 
728
	fd = -1;
782
	fd = -1;
729
 
783
 
730
	if (!access(configs.HeizPath, R_OK))
784
	if (access(configs.HeizPath, R_OK))
-
 
785
	{
-
 
786
	   syslog(LOG_DAEMON,"Access to file %s denied: %s", configs.HeizPath, strerror(errno));
731
	   return 0;
787
	   return 0;
-
 
788
	}
732
 
789
 
733
	if ((fd = open(configs.HeizPath, O_RDONLY)) == -1)
790
	if ((fd = open(configs.HeizPath, O_RDONLY)) == -1)
-
 
791
	{
-
 
792
	   syslog(LOG_DAEMON,"Error opening file %s: %s", configs.HeizPath, strerror(errno));
734
	   return 0;
793
	   return 0;
-
 
794
	}
-
 
795
 
-
 
796
	act = prev = NULL;
-
 
797
	freeMemory();
735
 
798
 
736
	while (readLine(fd, &offset, &line[0], sizeof(line)) != NULL)
799
	while (readLine(fd, &line[0], sizeof(line)) != NULL)
737
	{
800
	{
738
	   int len;
801
	   int len;
739
 
802
 
740
	   trim (&line[0]);
803
	   trim (&line[0]);
741
 
804
 
742
	   if (line[0] == '#')
805
	   if (line[0] == '#')
743
	      continue;
806
	      continue;
744
 
807
 
-
 
808
	   // We need a place to store the information
-
 
809
	   prev = act;
-
 
810
 
-
 
811
	   if ((act = allocateMemory()) == NULL)
-
 
812
	   {
-
 
813
	      close(fd);
-
 
814
	      syslog(LOG_DAEMON,"Error allocating memory for a temperature line! Stopped reading file %s.", configs.HeizPath);
-
 
815
	      return 0;
-
 
816
	   }
-
 
817
 
-
 
818
	   memset(act->heizung, 0, sizeof(HEIZUNG));
745
	   // Parse a line. The line has the format:
819
	   // Parse a line. The line has the format:
746
	   // <wday>,<end>,<temperature>
820
	   // <wday>,<end>,<temperature>
747
	   p = strtok(line, ';');
821
	   p = strtok(line, ",");
748
	   i = 1;
822
	   i = 1;
749
 
823
 
750
	   while(p)
824
	   while(p)
751
	   {
825
	   {
752
	      switch(i)
826
	      switch(i)
Line 754... Line 828...
754
		 case 1:	// Week day
828
		 case 1:	// Week day
755
		    wday = atoi(p);
829
		    wday = atoi(p);
756
 
830
 
757
		    if (wday < 1 || wday > 7)	// valid line?
831
		    if (wday < 1 || wday > 7)	// valid line?
758
		    {
832
		    {
759
		       p = strtok(NULL, ',');
833
		       if (prev)
760
		       continue;
834
			  wday = prev->heizung->wday;
761
		    }
-
 
762
 
835
 
763
		    // We need a place to store the information
836
		       if (wday < 1 || wday > 7)
-
 
837
		       {
764
		    if ((act = allocateMemory()) == NULL)
838
			  p = strtok(NULL, ",");
765
		    {
839
			  i++;
766
		       close(fd);
840
			  continue;
767
		       return 0;
841
		       }
768
		    }
842
		    }
769
 
843
 
770
		    act->heizung->wday = wday;
844
		    act->heizung->wday = wday;
771
		    act->heizung->start = 0;
845
		    act->heizung->start = 0;
772
		 break;
846
		 break;
Line 779... Line 853...
779
		       hour = atoi(p);
853
		       hour = atoi(p);
780
		       min = atoi(xp+1);
854
		       min = atoi(xp+1);
781
 
855
 
782
		       if (hour >= 0 && hour <= 23 && min >= 0 && min <= 59)
856
		       if (hour >= 0 && hour <= 23 && min >= 0 && min <= 59)
783
		       {
857
		       {
784
			  act->heizung->end = hour * 3600 + min;
858
			  act->heizung->end = hour * 3600 + (min + 1) * 60;
785
 
859
 
786
			  if (act->next)
860
			  if (prev && prev->heizung->wday == act->heizung->wday)
787
			     act->next->start = hour * 3600 + min;
861
			     act->heizung->start = prev->heizung->end;
788
		       }
862
		       }
789
		    }
863
		    }
790
		 break;
864
		 break;
791
 
865
 
792
		 case 3:	// temperature
866
		 case 3:	// temperature
793
		    temperature = atof(p);
867
		    temperature = atof(p);
794
 
868
 
795
		    if (temperature < 5.0 || temperature > 30.0)
869
		    if (temperature < 5.0 || temperature > 30.0)
796
		    {
870
		    {
797
		       p = strtok(NULL,',');
871
		       p = strtok(NULL,",");
-
 
872
		       i++;
798
		       continue;
873
		       continue;
799
		    }
874
		    }
800
 
875
 
801
		    act->heizung->temp = temperature;
876
		    act->heizung->temp = temperature;
802
		 break;
877
		 break;
803
	      }
878
	      }
804
 
879
 
805
	      p = strtok(NULL, ',');
880
	      p = strtok(NULL, ",");
-
 
881
	      i++;
806
	   }
882
	   }
807
	}
883
	}
808
 
884
 
809
	close (fd);
885
	close (fd);
810
	return 1;
886
	return 1;
Line 830... Line 906...
830
 
906
 
831
	act = HeizFirst;
907
	act = HeizFirst;
832
 
908
 
833
	while(act)
909
	while(act)
834
	{
910
	{
835
	   sprintf(&hv0[0], "%d,%d,%f\n",
911
	   sprintf(&hv0[0], "%d,%lu,%f\n",
836
		   act->heizung->wday, act->heizung->end,
912
		   act->heizung->wday, act->heizung->end,
837
		   act->heizung->temp);
913
		   act->heizung->temp);
838
	   write(fd, &hv0[0], strlen(hv0));
914
	   write(fd, &hv0[0], strlen(hv0));
839
	   act = act->next;
915
	   act = act->next;
840
	}
916
	}
841
 
917
 
842
	close(fd);
918
	close(fd);
843
	return 1;
919
	return 1;
844
}
920
}
845
 
921
 
846
char *readLine(int fd, long *offset, char *buf, int bufLen)
922
char *readLine(int fd, char *buf, int bufLen)
847
{
923
{
848
int i;
924
int i, end;
849
char ch, *p;
925
char ch, *p;
850
 
926
 
851
        if (fd <= 0)
927
        if (fd <= 0)
852
	   return NULL;
-
 
853
 
-
 
854
	lseek(fd, *offset, SEEK_SET);
-
 
855
	i = 0;
-
 
856
	p = buf;
-
 
857
 
-
 
858
	while (read(fd, &ch, 1) > 0)
-
 
859
	{
928
	{
860
	   if (ch == 0x0a)
929
	   syslog(LOG_DAEMON,"Function readLine was called with an invalid file descriptor of %d!", fd);
861
	   {
-
 
862
	     *p = 0;
-
 
863
	     *offset++;
-
 
864
	     return buf;
930
           return NULL;
865
	   }
931
	}
866
 
932
 
867
	   if (ch == 0x0d)	// ignore this!
-
 
868
	   {
-
 
869
	      *offset++;
933
        i = end = 0;
870
	      continue;
934
        p = buf;
871
	   }
-
 
872
 
935
 
873
	   if (i < bufLen)
936
        while (read(fd, &ch, 1) > 0)
874
	   {
937
        {
875
	      *p = ch;
938
           end = 1;
876
	      p++;
-
 
877
	      i++;
-
 
878
	   }
-
 
879
 
939
 
-
 
940
           if (ch == 0x0a)
-
 
941
           {
-
 
942
             *p = 0;
-
 
943
             return buf;
880
	   *offset++;
944
           }
-
 
945
 
-
 
946
           if (ch == 0x0d)      // ignore this!
-
 
947
              continue;
881
	}
948
 
-
 
949
           if (i < (bufLen - 1))
-
 
950
           {
-
 
951
              *p = ch;
-
 
952
              p++;
-
 
953
              i++;
-
 
954
           }
-
 
955
        }
882
 
956
 
883
	*p = 0;
957
        *p = 0;
-
 
958
 
-
 
959
        if (end)
884
	return buf;
960
           return buf;
-
 
961
        else
-
 
962
           return NULL;
885
}
963
}
886
 
964
 
887
char *trim(char *str)
965
char *trim(char *str)
888
{
966
{
889
char *p1, *p2, *p;
967
char *p1, *p2, *p;
890
 
968
 
891
	if (!str)
969
	if (!str)
892
	   return NULL;
970
	   return NULL;
893
 
971
 
-
 
972
	if (!strlen(str))
-
 
973
	   return str;
-
 
974
 
894
	p = str;
975
	p = str;
895
	p1 = p2 = NULL;
976
	p1 = p2 = NULL;
896
 
977
 
897
	while (*p)
978
	while (*p)
898
	{
979
	{
Line 932... Line 1013...
932
 
1013
 
933
	if ((p = strstr(str, search)) != NULL)
1014
	if ((p = strstr(str, search)) != NULL)
934
	{
1015
	{
935
	int len = strlen(search);
1016
	int len = strlen(search);
936
 
1017
 
937
	   strcpy(ret, str, p - str + len);
1018
	   strncpy(ret, str, p - str + len);
938
	   memmove(str, p + len + 1, strlen(p+len));
1019
	   memmove(str, p + len + 1, strlen(p+len));
939
	   return ret;
1020
	   return ret;
940
	}
1021
	}
941
 
1022
 
942
	return NULL;
1023
	return NULL;
943
}
-
 
944
 
1024
}
-
 
1025