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