00001 #include "billdump.h"
00002
00003
00011 #include <stdlib.h>
00012 #include <stdio.h>
00013 #include <errno.h>
00014 #include <math.h>
00015
00016 #include "billdump.h"
00017 #include "node.h"
00018
00020
00022
00023 CLASS* billdump::oclass = NULL;
00024
00025 billdump::billdump(MODULE *mod)
00026 {
00027 if (oclass==NULL)
00028 {
00029
00030 oclass = gl_register_class(mod,"billdump",sizeof(billdump),PC_BOTTOMUP);
00031 if (oclass==NULL)
00032 GL_THROW("unable to register object class implemented by %s",__FILE__);
00033
00034
00035 if (gl_publish_variable(oclass,
00036 PT_char32,"group",PADDR(group),PT_DESCRIPTION,"the group ID to output data for (all nodes if empty)",
00037 PT_timestamp,"runtime",PADDR(runtime),PT_DESCRIPTION,"the time to check voltage data",
00038 PT_char32,"filename",PADDR(filename),PT_DESCRIPTION,"the file to dump the voltage data into",
00039 PT_int32,"runcount",PADDR(runcount),PT_ACCESS,PA_REFERENCE,PT_DESCRIPTION,"the number of times the file has been written to",
00040 NULL)<1) GL_THROW("unable to publish properties in %s",__FILE__);
00041
00042 }
00043 }
00044
00045
00046 int billdump::create(void)
00047 {
00048 memset(group, 0, sizeof(char32));
00049 runtime = TS_NEVER;
00050 runcount = 0;
00051 return 1;
00052 }
00053
00054 int billdump::init(OBJECT *parent)
00055 {
00056 return 1;
00057 }
00058
00059 int billdump::isa(char *classname)
00060 {
00061 return strcmp(classname,"billdump")==0;
00062 }
00063
00064 void billdump::dump(TIMESTAMP t){
00065 char namestr[64];
00066 char timestr[64];
00067 FINDLIST *nodes = NULL;
00068 OBJECT *obj = NULL;
00069 FILE *outfile = NULL;
00070 triplex_meter *pnode;
00071
00072
00073
00074 if(group[0] == 0){
00075 nodes = gl_find_objects(FL_NEW,FT_CLASS,SAME,"triplex_meter",FT_END);
00076 } else {
00077 nodes = gl_find_objects(FL_NEW,FT_CLASS,SAME,"triplex_meter",AND,FT_GROUPID,SAME,group,FT_END);
00078 }
00079
00080 if(nodes == NULL){
00081 gl_warning("no nodes were found to dump");
00082 return;
00083 }
00084
00085 outfile = fopen(filename, "w");
00086 if(outfile == NULL){
00087 gl_error("billdump unable to open %s for output", filename);
00088 return;
00089 }
00090
00091
00092
00093
00094
00095 gl_printtime(t, timestr, 64);
00096 fprintf(outfile,"# %s run at %s on %i triplex meters\n", filename, timestr, nodes->hit_count);
00097
00098 fprintf(outfile,"meter_name,previous_monthly_bill,previous_monthly_energy\n");
00099 while (obj=gl_find_next(nodes,obj)){
00100 if(gl_object_isa(obj, "triplex_meter", "powerflow")){
00101 pnode = OBJECTDATA(obj,triplex_meter);
00102 if(obj->name == NULL){
00103 sprintf(namestr, "%s:%i", obj->oclass->name, obj->id);
00104 }
00105 fprintf(outfile,"%s,%f,%f\n",(obj->name ? obj->name : namestr),pnode->previous_monthly_bill,pnode->previous_monthly_energy);
00106 }
00107 }
00108 fclose(outfile);
00109 }
00110
00111 int billdump::commit(TIMESTAMP t){
00112 if(runtime == 0){
00113 runtime = t;
00114 }
00115 if((t == runtime || runtime == TS_NEVER) && (runcount < 1)){
00116
00117 dump(t);
00118 ++runcount;
00119 }
00120 return 1;
00121 }
00122
00124
00126
00134 EXPORT int create_billdump(OBJECT **obj, OBJECT *parent)
00135 {
00136 try
00137 {
00138 *obj = gl_create_object(billdump::oclass);
00139 if (*obj!=NULL)
00140 {
00141 billdump *my = OBJECTDATA(*obj,billdump);
00142 gl_set_parent(*obj,parent);
00143 return my->create();
00144 }
00145 }
00146 catch (const char *msg)
00147 {
00148 gl_error("create_billdump: %s", msg);
00149 }
00150 return 0;
00151 }
00152
00153 EXPORT int init_billdump(OBJECT *obj)
00154 {
00155 billdump *my = OBJECTDATA(obj,billdump);
00156 try {
00157 return my->init(obj->parent);
00158 }
00159 catch (const char *msg)
00160 {
00161 GL_THROW("%s (billdump:%d): %s", obj->name, obj->id, msg);
00162 return 0;
00163 }
00164 }
00165
00166 EXPORT TIMESTAMP sync_billdump(OBJECT *obj, TIMESTAMP t1, PASSCONFIG pass)
00167 {
00168 billdump *my = OBJECTDATA(obj,billdump);
00169 TIMESTAMP rv;
00170 obj->clock = t1;
00171 rv = my->runtime > t1 ? my->runtime : TS_NEVER;
00172 return rv;
00173 }
00174
00175 EXPORT int commit_billdump(OBJECT *obj){
00176 billdump *my = OBJECTDATA(obj,billdump);
00177 try {
00178 return my->commit(obj->clock);
00179 } catch(char *msg){
00180 GL_THROW("%s (billdump:%d): %s", obj->name, obj->id, msg);
00181 return 0;
00182 }
00183 }
00184
00185 EXPORT int isa_billdump(OBJECT *obj, char *classname)
00186 {
00187 return OBJECTDATA(obj,billdump)->isa(classname);
00188 }
00189
00193