00001
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <errno.h>
00026 #include <ctype.h>
00027 #include <time.h>
00028
00029 #include "gridlabd.h"
00030 #include "../tape/tape.h"
00031 #include "tape_memory.h"
00032
00033
00034
00035
00036
00037 EXPORT CALLBACKS *callback = NULL;
00038
00039 int open_player(struct player *my, char *fname, char *flags)
00040 {
00041
00042 my->memory = (MEMORY*)malloc(sizeof(MEMORY));
00043 if (my->memory==NULL)
00044 {
00045
00046 fprintf(stderr, "player memory %s: %s\n", fname, strerror(errno));
00047 my->status = TS_DONE;
00048 return 0;
00049 }
00050 my->memory->buffer = gl_global_find(fname);
00051 if (my->memory->buffer!=NULL)
00052 {
00053 my->memory->index = 0;
00054 my->loopnum = my->loop;
00055 my->status=TS_OPEN;
00056 my->type = FT_MEMORY;
00057 return 1;
00058 }
00059
00060 fprintf(stderr, "open_player(struct player *my='{...}', char *fname='%s', char *flags='%s'): global '%s' not defined\n", fname,flags,fname);
00061 return 0;
00062 }
00063
00064 char *read_player(struct player *my,char *buffer,unsigned int size)
00065 {
00066 static char temp[256];
00067
00068
00069 if (my->memory->index >= my->memory->buffer->prop->size-1)
00070 return NULL;
00071 else
00072 {
00073 double *ptr = (double*)my->memory->buffer->prop->addr + my->memory->index;
00074 my->memory->index += 2;
00075 sprintf(temp,"%"FMT_INT64"d,%lg",(TIMESTAMP)ptr[0],ptr[1]);
00076 strncpy(buffer,temp,((size < sizeof(temp)) ? size : sizeof(temp)));
00077 return buffer;
00078 }
00079 }
00080
00081 int rewind_player(struct player *my)
00082 {
00083 my->memory->index = 0;
00084 return 1;
00085 }
00086
00087 void close_player(struct player *my)
00088 {
00089 free(my->memory);
00090 }
00091
00092
00093
00094
00095 #define MAPSIZE(N) ((N-1)/8+1)
00096 #define SET(X,B) ((X)[(B)/8]|=(1<<((B)&7)))
00097 #define ISSET(X,B) (((X)[(B)/8]&(1<<((B)&7)))==(1<<((B)&7)))
00098 static char *memory=NULL;
00099 static int linenum=0;
00100 static void setmap(char *spec, unsigned char *map, int size)
00101 {
00102 char *p=spec;
00103 int last=-1;
00104 memset(map,0,MAPSIZE(size));
00105 while (*p!='\0')
00106 {
00107 if (*p=='*')
00108 {
00109 int i;
00110 for (i=0;i<size;i++)
00111 SET(map,i);
00112 p++;
00113 }
00114 else if (isdigit(*p))
00115 {
00116 int i=atoi(p);
00117 if (last<0)
00118
00119 SET(map,i);
00120 else if (i>last)
00121 do {
00122 SET(map,last);
00123 } while (++last<=i);
00124 else
00125 {
00126
00127 do {
00128 SET(map,last);
00129 } while (++last<size);
00130 last=0;
00131 do {
00132 SET(map,last);
00133 } while (++last<=i);
00134 }
00135 last = i;
00136 while (isdigit(*p)) p++;
00137 }
00138 else if (*p=='-')
00139 {
00140 p++;
00141 }
00142 else if (*p==';')
00143 {
00144 last = -1;
00145 p++;
00146 }
00147 }
00148 }
00149 static unsigned char *hourmap(char *spec)
00150 {
00151 static unsigned char hours[MAPSIZE(24)];
00152 setmap(spec,hours,24);
00153 return hours;
00154 }
00155 static unsigned char *daymap(char *spec)
00156 {
00157 static unsigned char days[MAPSIZE(31)];
00158 setmap(spec,days,31);
00159 return days;
00160 }
00161 static unsigned char *monthmap(char *spec)
00162 {
00163 static unsigned char months[MAPSIZE(12)];
00164 setmap(spec,months,12);
00165 return months;
00166 }
00167 static unsigned char *weekdaymap(char *spec)
00168 {
00169 static unsigned char weekdays[MAPSIZE(7)];
00170 setmap(spec,weekdays,7);
00171 return weekdays;
00172 }
00173
00174 int open_shaper(struct shaper *my, char *fname, char *flags)
00175 {
00176 char line[1024], group[256]="(unnamed)";
00177 float sum=0, load=0, peak=0;
00178 float scale[12][31][7][24];
00179
00180
00181 memset(scale,0,sizeof(scale));
00182 linenum=0;
00183 memory=fname;
00184
00186
00187 my->fp = (strcmp(fname,"-")==0?stdin:fopen(fname,flags));
00188 if (my->fp==NULL)
00189 {
00190
00191 fprintf(stderr, "shaper memory %s: %s\n", fname, strerror(errno));
00192 my->status = TS_DONE;
00193 return 0;
00194 }
00195 my->status=TS_OPEN;
00196 my->type = FT_MEMORY;
00198 my->step = 3600;
00199 my->interval = 24;
00200 memset(my->shape,0,sizeof(my->shape));
00201
00202 while (fgets(line,sizeof(line),my->fp)!=NULL)
00203 {
00204 unsigned char *hours, *days, *months, *weekdays;
00205 char min[256],hour[256],day[256],month[256],weekday[256],value[32];
00206 char *p=line;
00207 linenum++;
00208 while (isspace(*p)) p++;
00209 if (p[0]=='\0' || p[0]=='#') continue;
00210 if (strcmp(group,"")!=0 && (isdigit(p[0]) || p[0]=='*'))
00211 {
00212 int h, d, m, w;
00213 if (sscanf(line,"%s %s %s %s %[^,],%[^,\n]",min,hour,day,month,weekday,value)<6)
00214 {
00215
00216 fprintf(stderr, "%s(%d) : shape '%s' has specification '%s'\n", memory, linenum, group, line);
00217 continue;
00218 }
00219
00220 if (min[0]!='*')
00221
00222 hours=hourmap(hour);
00223 days=daymap(day);
00224 months=monthmap(month);
00225 weekdays=weekdaymap(weekday);
00226 load = (float)atof(value);
00227 for (m=0; m<12; m++)
00228 {
00229 if (!ISSET(months,m)) continue;
00230 for (w=0; w<7; w++)
00231 {
00232 if (!ISSET(weekdays,w)) continue;
00233 for (d=0; d<31; d++)
00234 {
00235 if (!ISSET(days,d)) continue;
00236 for (h=0; h<24; h++)
00237 {
00238 if (!ISSET(hours,h)) continue;
00239 scale[m][d][w][h] = -load;
00240 }
00241 }
00242 }
00243 }
00244 sum += load;
00245 if (load>peak) peak=load;
00246 }
00247 else if (p[0]=='}')
00248 {
00249 int h, d, m, w;
00250 my->scale = peak/255/sum;
00251
00252 for (m=0; m<12; m++)
00253 {
00254 for (w=0; w<7; w++)
00255 {
00256 for (d=0; d<31; d++)
00257 {
00258 for (h=0; h<24; h++)
00259 {
00260 if (scale[m][d][w][h]<0)
00261 my->shape[m][d][w][h] = (unsigned char)(-scale[m][d][w][h] / peak * 255 +0.5);
00262 }
00263 }
00264 }
00265 }
00266 strcpy(group,"");
00267 }
00268 else if (sscanf(p,"%s {",group)==1)
00269 {
00270 sum=0;
00271 }
00272 else
00273 {
00274
00275 fprintf(stderr, "%s(%d) : shape specification '%s' is not valid\n", memory, linenum, line);
00276 }
00277 }
00278 return 1;
00279 }
00280
00281 char *read_shaper(struct shaper *my,char *buffer,unsigned int size)
00282 {
00283 return fgets(buffer,size,my->fp);
00284 }
00285
00286 int rewind_shaper(struct shaper *my)
00287 {
00288 return fseek(my->fp,SEEK_SET,0);
00289 }
00290
00291 void close_shaper(struct shaper *my)
00292 {
00293 }
00294
00295
00296
00297
00298 int open_recorder(struct recorder *my, char *fname, char *flags)
00299 {
00300 time_t now=time(NULL);
00301 OBJECT *obj=OBJECTHDR(my);
00302
00303 my->memory = (MEMORY*)malloc(sizeof(MEMORY));
00304 if (my->memory==NULL)
00305 {
00306
00307 fprintf(stderr, "open_recorder(struct recorder *my={...}, char *fname='%s', char *flags='%s'): %s\n", fname, flags, strerror(errno));
00308 my->status = TS_DONE;
00309 return 0;
00310 }
00311 my->memory->buffer = gl_global_find(fname);
00312 if (my->memory->buffer==NULL)
00313 {
00314
00315 fprintf(stderr, "open_recorder(struct recorder *my={...}, char *fname='%s', char *flags='%s'): global '%s' not found\n", fname, flags, fname);
00316 my->status = TS_DONE;
00317 return 0;
00318 }
00319 my->memory->index = 0;
00320 my->type = FT_MEMORY;
00321 my->last.ts = TS_ZERO;
00322 my->status=TS_OPEN;
00323 my->samples=0;
00324 return 1;
00325 }
00326
00327 int write_recorder(struct recorder *my, char *timestamp, char *value)
00328 {
00329 if (my->memory->index >= my->memory->buffer->prop->size-1)
00330 return 0;
00331 else
00332 {
00333 double *ptr = (double*)my->memory->buffer->prop->addr + my->memory->index;
00334 my->memory->index += 2;
00335 ptr[0] = (double)gl_parsetime(timestamp);
00336 ptr[1] = (double)(float)atof(value);
00337 return 2;
00338 }
00339 }
00340
00341 void close_recorder(struct recorder *my)
00342 {
00343 free(my->memory);
00344 }
00345
00346
00347
00348
00349 int open_collector(struct collector *my, char *fname, char *flags)
00350 {
00351 time_t now=time(NULL);
00352 OBJECT *obj=OBJECTHDR(my);
00353
00354 my->memory = (MEMORY*)malloc(sizeof(MEMORY));
00355 if (my->memory==NULL)
00356 {
00357
00358 fprintf(stderr, "open_collector(struct recorder *my={...}, char *fname='%s', char *flags='%s'): %s\n", fname, flags, strerror(errno));
00359 my->status = TS_DONE;
00360 return 0;
00361 }
00362 my->memory->buffer = gl_global_find(fname);
00363 if (my->memory->buffer==NULL)
00364 {
00365
00366 fprintf(stderr, "open_collector(struct recorder *my={...}, char *fname='%s', char *flags='%s'): global '%s' not found\n", fname, flags, fname);
00367 my->status = TS_DONE;
00368 return 0;
00369 }
00370 my->memory->index = 0;
00371 my->type = FT_MEMORY;
00372 my->last.ts = TS_ZERO;
00373 my->status=TS_OPEN;
00374 my->samples=0;
00375 return 1;
00376
00377 }
00378
00379 int write_collector(struct collector *my, char *timestamp, char *value)
00380 {
00381 if (my->memory->index >= my->memory->buffer->prop->size-1)
00382 return 0;
00383 else
00384 {
00385 double *ptr = (double*)my->memory->buffer->prop->addr + my->memory->index;
00386 my->memory->index += 2;
00387 ptr[0] = (double)gl_parsetime(timestamp);
00388 ptr[1] = (double)(float)atof(value);
00389 return 2;
00390 }
00391 }
00392
00393 void close_collector(struct collector *my)
00394 {
00395 free(my->memory);
00396 }
00397