00001
00002
00003
00004
00005 #include <stdlib.h>
00006 #include <stdio.h>
00007 #include <errno.h>
00008 #include <ctype.h>
00009 #include <time.h>
00010
00011 #include "gridlabd.h"
00012 #include "tape.h"
00013 #include "file.h"
00014
00015
00016
00017
00018 int file_open_player(struct player *my, char *fname, char *flags)
00019 {
00020 char ff[1024];
00021
00022
00023 my->fp = (strcmp(fname,"-")==0?stdin:(gl_findfile(fname,NULL,R_OK,ff,sizeof(ff))?fopen(ff,flags):NULL));
00024 if (my->fp==NULL)
00025 {
00026 gl_error("player file %s: %s", fname, strerror(errno));
00027 my->status = TS_DONE;
00028 return 0;
00029 }
00030 else
00031 {
00032 my->loopnum = my->loop;
00033 my->status=TS_OPEN;
00034 my->type = FT_FILE;
00035 return 1;
00036 }
00037 }
00038
00039 char *file_read_player(struct player *my,char *buffer,unsigned int size)
00040 {
00041 return fgets(buffer,size,my->fp);
00042 }
00043
00044 int file_rewind_player(struct player *my)
00045 {
00046 return fseek(my->fp,SEEK_SET,0);
00047 }
00048
00049 void file_close_player(struct player *my)
00050 {
00051 }
00052
00053
00054
00055
00056 #define MAPSIZE(N) ((N-1)/8+1)
00057 #define SET(X,B) ((X)[(B)/8]|=(1<<((B)&7)))
00058 #define ISSET(X,B) (((X)[(B)/8]&(1<<((B)&7)))==(1<<((B)&7)))
00059 char *file=NULL;
00060 int linenum=0;
00061 static void setmap(char *spec, unsigned char *map, int size)
00062 {
00063 char *p=spec;
00064 int last=-1;
00065 memset(map,0,MAPSIZE(size));
00066 while (*p!='\0')
00067 {
00068 if (*p=='*')
00069 {
00070 int i;
00071 for (i=0;i<size;i++)
00072 SET(map,i);
00073 p++;
00074 }
00075 else if (isdigit(*p))
00076 {
00077 int i=atoi(p);
00078 if (last<0)
00079
00080 SET(map,i);
00081 else if (i>last)
00082 do {
00083 SET(map,last);
00084 } while (++last<=i);
00085 else
00086 {
00087
00088 do {
00089 SET(map,last);
00090 } while (++last<size);
00091 last=0;
00092 do {
00093 SET(map,last);
00094 } while (++last<=i);
00095 }
00096 last = i;
00097 while (isdigit(*p)) p++;
00098 }
00099 else if (*p=='-')
00100 {
00101 p++;
00102 }
00103 else if (*p==';')
00104 {
00105 last = -1;
00106 p++;
00107 }
00108 }
00109 }
00110 static unsigned char *hourmap(char *spec)
00111 {
00112 static unsigned char hours[MAPSIZE(24)];
00113 setmap(spec,hours,24);
00114 return hours;
00115 }
00116 static unsigned char *daymap(char *spec)
00117 {
00118 static unsigned char days[MAPSIZE(31)];
00119 setmap(spec,days,31);
00120 return days;
00121 }
00122 static unsigned char *monthmap(char *spec)
00123 {
00124 static unsigned char months[MAPSIZE(12)];
00125 setmap(spec,months,12);
00126 return months;
00127 }
00128 static unsigned char *weekdaymap(char *spec)
00129 {
00130 static unsigned char weekdays[MAPSIZE(7)];
00131 setmap(spec,weekdays,7);
00132 return weekdays;
00133 }
00134
00135 int file_open_shaper(struct shaper *my, char *fname, char *flags)
00136 {
00137 char line[1024], group[256]="(unnamed)";
00138 float sum=0, load=0, peak=0;
00139 float scale[12][31][7][24];
00140 char ff[1024];
00141
00142
00143 memset(scale,0,sizeof(scale));
00144 linenum=0;
00145 file=fname;
00146
00147
00148 my->fp = (strcmp(fname,"-")==0?stdin:(gl_findfile(fname,NULL,R_OK,ff,sizeof(ff))?fopen(ff,flags):NULL));
00149 if (my->fp==NULL)
00150 {
00151 gl_error("shaper file %s: %s", fname, strerror(errno));
00152 my->status = TS_DONE;
00153 return 0;
00154 }
00155 my->status=TS_OPEN;
00156 my->type = FT_FILE;
00157
00158 my->step = 3600;
00159 my->interval = 24;
00160 memset(my->shape,0,sizeof(my->shape));
00161
00162 while (fgets(line,sizeof(line),my->fp)!=NULL)
00163 {
00164 unsigned char *hours, *days, *months, *weekdays;
00165 char min[256],hour[256],day[256],month[256],weekday[256],value[32];
00166 char *p=line;
00167 linenum++;
00168 while (isspace(*p)) p++;
00169 if (p[0]=='\0' || p[0]=='#') continue;
00170 if (strcmp(group,"")!=0 && (isdigit(p[0]) || p[0]=='*'))
00171 {
00172 int h, d, m, w;
00173 if (sscanf(line,"%s %s %s %s %[^,],%[^,\n]",min,hour,day,month,weekday,value)<6)
00174 {
00175 gl_error("%s(%d) : shape '%s' has specification '%s'", file, linenum, group, line);
00176 continue;
00177 }
00178
00179 if (min[0]!='*') gl_warning("%s(%d) : minutes are ignored in '%s'", file, linenum, line);
00180 hours=hourmap(hour);
00181 days=daymap(day);
00182 months=monthmap(month);
00183 weekdays=weekdaymap(weekday);
00184 load = (float)atof(value);
00185 for (m=0; m<12; m++)
00186 {
00187 if (!ISSET(months,m)) continue;
00188 for (w=0; w<7; w++)
00189 {
00190 if (!ISSET(weekdays,w)) continue;
00191 for (d=0; d<31; d++)
00192 {
00193 if (!ISSET(days,d)) continue;
00194 for (h=0; h<24; h++)
00195 {
00196 if (!ISSET(hours,h)) continue;
00197 scale[m][d][w][h] = -load;
00198 }
00199 }
00200 }
00201 }
00202 sum += load;
00203 if (load>peak) peak=load;
00204 }
00205 else if (p[0]=='}')
00206 {
00207 int h, d, m, w;
00208 my->scale = peak/255/sum;
00209
00210 for (m=0; m<12; m++)
00211 {
00212 for (w=0; w<7; w++)
00213 {
00214 for (d=0; d<31; d++)
00215 {
00216 for (h=0; h<24; h++)
00217 {
00218 if (scale[m][d][w][h]<0)
00219 my->shape[m][d][w][h] = (unsigned char)(-scale[m][d][w][h] / peak * 255 +0.5);
00220 }
00221 }
00222 }
00223 }
00224 strcpy(group,"");
00225 }
00226 else if (sscanf(p,"%s {",group)==1)
00227 {
00228 sum=0;
00229 }
00230 else
00231 {
00232 gl_error("%s(%d) : shape specification '%s' is not valid", file, linenum, line);
00233 }
00234 }
00235 return 1;
00236 }
00237
00238 char *file_read_shaper(struct shaper *my,char *buffer,unsigned int size)
00239 {
00240 return fgets(buffer,size,my->fp);
00241 }
00242
00243 int file_rewind_shaper(struct shaper *my)
00244 {
00245 return fseek(my->fp,SEEK_SET,0);
00246 }
00247
00248 void file_close_shaper(struct shaper *my)
00249 {
00250 }
00251
00252
00253
00254
00255 int file_open_recorder(struct recorder *my, char *fname, char *flags)
00256 {
00257 time_t now=time(NULL);
00258 OBJECT *obj=OBJECTHDR(my);
00259
00260 my->fp = (strcmp(fname,"-")==0?stdout:fopen(fname,flags));
00261 if (my->fp==NULL)
00262 {
00263 gl_error("recorder file %s: %s", fname, strerror(errno));
00264 my->status = TS_DONE;
00265 return 0;
00266 }
00267 my->type = FT_FILE;
00268 my->last.ts = TS_ZERO;
00269 my->status=TS_OPEN;
00270 my->samples=0;
00271
00272
00273 fprintf(my->fp,"# file...... %s\n", my->file);
00274 fprintf(my->fp,"# date...... %s", asctime(localtime(&now)));
00275 #ifdef WIN32
00276 fprintf(my->fp,"# user...... %s\n", getenv("USERNAME"));
00277 fprintf(my->fp,"# host...... %s\n", getenv("MACHINENAME"));
00278 #else
00279 fprintf(my->fp,"# user...... %s\n", getenv("USER"));
00280 fprintf(my->fp,"# host...... %s\n", getenv("HOST"));
00281 #endif
00282 fprintf(my->fp,"# target.... %s %d\n", obj->parent->oclass->name, obj->parent->id);
00283 fprintf(my->fp,"# trigger... %s\n", my->trigger[0]=='\0'?"(none)":my->trigger);
00284 fprintf(my->fp,"# interval.. %d\n", my->interval);
00285 fprintf(my->fp,"# limit..... %d\n", my->limit);
00286 fprintf(my->fp,"# timestamp,%s\n", my->property);
00287
00288 return 1;
00289 }
00290
00291 int file_write_recorder(struct recorder *my, char *timestamp, char *value)
00292 {
00293 return fprintf(my->fp,"%s,%s\n", timestamp, value);
00294 }
00295
00296 void file_close_recorder(struct recorder *my)
00297 {
00298 fprintf(my->fp,"# end of tape\n");
00299 fclose(my->fp);
00300 }
00301 void file_flush_recorder(struct recorder *my)
00302 {
00303 fflush(my->fp);
00304 }
00305
00306
00307
00308
00309 int file_open_collector(struct collector *my, char *fname, char *flags)
00310 {
00311 unsigned int count=0;
00312 time_t now=time(NULL);
00313
00314 my->fp = (strcmp(fname,"-")==0?stdout:fopen(fname,flags));
00315 if (my->fp==NULL)
00316 {
00317 gl_error("collector file %s: %s", fname, strerror(errno));
00318 my->status = TS_DONE;
00319 return 0;
00320 }
00321 my->last.ts = TS_ZERO;
00322 my->status=TS_OPEN;
00323 my->type = FT_FILE;
00324 my->samples=0;
00325
00326
00327 count += fprintf(my->fp,"# file...... %s\n", my->file);
00328 count += fprintf(my->fp,"# date...... %s", asctime(localtime(&now)));
00329 #ifdef WIN32
00330 count += fprintf(my->fp,"# user...... %s\n", getenv("USERNAME"));
00331 count += fprintf(my->fp,"# host...... %s\n", getenv("MACHINENAME"));
00332 #else
00333 count += fprintf(my->fp,"# user...... %s\n", getenv("USER"));
00334 count += fprintf(my->fp,"# host...... %s\n", getenv("HOST"));
00335 #endif
00336 count += fprintf(my->fp,"# group..... %s\n", my->group);
00337 count += fprintf(my->fp,"# trigger... %s\n", my->trigger[0]=='\0'?"(none)":my->trigger);
00338 count += fprintf(my->fp,"# interval.. %d\n", my->interval);
00339 count += fprintf(my->fp,"# limit..... %d\n", my->limit);
00340 count += fprintf(my->fp,"# property.. timestamp,%s\n", my->property);
00341
00342 return count;
00343 }
00344
00345 int file_write_collector(struct collector *my, char *timestamp, char *value)
00346 {
00347 return fprintf(my->fp,"%s,%s\n", timestamp, value);
00348 }
00349
00350 void file_close_collector(struct collector *my)
00351 {
00352 fprintf(my->fp,"# end of tape\n");
00353 fclose(my->fp);
00354
00355 }
00356
00357 void file_flush_collector(struct collector *my)
00358 {
00359 fflush(my->fp);
00360 }