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