Subversion Repositories HomeAutomation

Rev

Rev 2041 | Rev 2046 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 2041 Rev 2042
Line 1... Line -...
1
 
-
 
2
 
-
 
-
 
1
/* RRD_Tool() must be called from autostart.js to start timers */
3
function RRD_Tool(config)
2
function RRD_Tool()
4
{
3
{
5
 
-
 
6
/* Declaration of instance variables, for static variables remove prototype */
-
 
7
 
-
 
8
    /* We must always call the parent constructor, initialization
4
    /* We must always call the parent constructor, initialization
9
       of variables could be done here, but initialize is a better place */
5
       of variables could be done here, but initialize is a better place */
10
    var self = this;
6
    var self = this;
11
   
7
   
-
 
8
    /* Constant config name */
12
    Log("\033[33mRRD Tool updater starting...\033[0m");
9
    RRD_Tool_Config = "RRD_Tool_config";
13
   
10
   
-
 
11
    Log("\033[33mRRD Tool updater starting...\033[0m");
-
 
12
   
14
    var last_value_string = Storage_GetParameters(config);
13
    var last_value_string = Storage_GetParameters(RRD_Tool_Config);
15
    this.StoredData = new Array();
14
    RRD_Tool_StoredData = new Array();
16
    this.Timers = new Array();
15
    RRD_Tool_Timers = new Array();
17
    for (var name in last_value_string)
16
    for (var name in last_value_string)
18
    {
17
    {
19
        this.StoredData[name] = eval("(" + last_value_string[name] + ")");
18
        RRD_Tool_StoredData[name] = eval("(" + last_value_string[name] + ")");
20
        Log("\033[33mFound: "+name+" with update rate of "+this.StoredData[name]["rrd"]["Period_s"]+"s.\033[0m");
19
        Log("\033[33mFound: "+name+" with update rate of "+RRD_Tool_StoredData[name]["rrd"]["Period_s"]+"s.\033[0m");
21
        this.Timers[name] = Timer_SetTimer(function(timer) {self.timerUpdate2(timer)}, this.StoredData[name]["rrd"]["Period_s"]*1000, true);
20
        RRD_Tool_Timers[name] = Timer_SetTimer(function(timer) {RRD_Tool_timerUpdate(timer)}, RRD_Tool_StoredData[name]["rrd"]["Period_s"]*1000, true);
22
    }
21
    }
23
    Log("\033[33mRRD Tool updater created.\033[0m");
22
    Log("\033[33mRRD Tool updater created.\033[0m");
-
 
23
}
24
   
24
 
25
}
-
 
26
 
-
 
27
/* Declaration of instance variables, for static variables remove prototype */
25
/* Declaration of static variables */
28
RRD_Tool.prototype.myInterval = null;
26
RRD_Tool_myInterval = null;
29
RRD_Tool.prototype.StoredData = null;
27
RRD_Tool_StoredData = null;
30
RRD_Tool.prototype.Timers = null;
28
RRD_Tool_Timers = null;
31
 
-
 
-
 
29
RRD_Tool_Config = null;
32
 
30
 
33
RRD_Tool.prototype.timerUpdate2 = function(timer)
31
RRD_Tool_timerUpdate = function(timer)
34
{
32
{
35
    var alias_string = Storage_GetParameters("LastValues");
33
    var alias_string = Storage_GetParameters("LastValues");
36
    for (var name in this.Timers)
34
    for (var name in RRD_Tool_Timers)
37
    {  
35
    {  
38
        if (this.Timers[name] == timer) {
36
        if (RRD_Tool_Timers[name] == timer) {
39
            //Log("Timer: "+timer+", named: "+name+" with data: "+this.StoredData[name]["rrd"]["file"]+"\n");
37
            //Log("Timer: "+timer+", named: "+name+" with data: "+RRD_Tool_StoredData[name]["rrd"]["file"]+"\n");
40
            /* Create command to execute...*/
38
            /* Create command to execute...*/
41
            var names = "";
39
            var names = "";
42
            var values = "N";
40
            var values = "N";
43
            var cmd = "/usr/bin/rrdtool update --template ";
41
            var cmd = "/usr/bin/rrdtool update --template ";
44
            var first = 1;
42
            var first = 1;
45
            for (var label in this.StoredData[name]["rrdNames"])
43
            for (var label in RRD_Tool_StoredData[name]["rrdNames"])
46
            {
44
            {
47
                //Log("Found label: "+label+"\n");
45
                //Log("Found label: "+label+"\n");
48
                var data = eval("(" + alias_string[this.StoredData[name]["rrdNames"][label]["Module"]] + ")");
46
                var data = eval("(" + alias_string[RRD_Tool_StoredData[name]["rrdNames"][label]["Module"]] + ")");
49
                if (first == 1) {
47
                if (first == 1) {
50
                    names += label;
48
                    names += label;
51
                    first = 0;
49
                    first = 0;
52
                } else {
50
                } else {
53
                    names += ":"+label;  
51
                    names += ":"+label;  
54
                }
52
                }
55
                values += ":"+data[this.StoredData[name]["rrdNames"][label]["data"]][this.StoredData[name]["rrdNames"][label]["value"]];
53
                values += ":"+data[RRD_Tool_StoredData[name]["rrdNames"][label]["data"]][RRD_Tool_StoredData[name]["rrdNames"][label]["value"]];
56
            }
54
            }
57
            cmd += names + " "+this.StoredData[name]["rrd"]["file"]+" "+values;
55
            cmd += names + " "+RRD_Tool_StoredData[name]["rrd"]["file"]+" "+values;
58
            //Log("Command: "+cmd);
56
            //Log("Command: "+cmd);
59
            Execute(cmd);
57
            Execute(cmd);
60
            break;
58
            break;
61
        }
59
        }
62
    }
60
    }
63
}
61
}
-
 
62
 
-
 
63
/* Tab complete stuff */
-
 
64
RRD_Tool_Aliases        = function() { return Module_GetAliasNames(); };
-
 
65
RRD_Tool_Intervals      = function() { return [ 5, 10, 30, 60, 120, 150, 300 ]; };
-
 
66
/* Function returns an array with all CAN variables existing in LastValues storage */
-
 
67
RRD_Tool_Variables      = function()
-
 
68
{
-
 
69
    var last_value_string = Storage_GetParameters("LastValues");
-
 
70
    var last_values = new Array();
-
 
71
    var last_values_variables = [];
-
 
72
    for (var name in last_value_string)
-
 
73
    {
-
 
74
        last_values[name] = eval("(" + last_value_string[name] + ")");
-
 
75
        for (var name2 in last_values[name])
-
 
76
        {
-
 
77
            /* Only get unique variables */
-
 
78
            if (last_values_variables.indexOf(name2) == -1)
-
 
79
            {
-
 
80
                last_values_variables.push(name2);
-
 
81
            }
-
 
82
        }
-
 
83
    }
-
 
84
    return last_values_variables;
-
 
85
};
-
 
86
 
-
 
87
/*
-
 
88
electricPower={"rrd":{"file":"/var/www/appdata/rrd-data-new/electricPower.rrd","Period_s":"60"}, "rrdNames":{"power":{"Module":"powerMeter","data":"Power32","value":"value"}}}
-
 
89
 
-
 
90
<store_name>={"rrd":{"file":"<rrd_filename>","Period_s":"<push_interval>"}, "rrdNames":{"<rrd_data_source_name>":{"Module":"<alias_name>","data":"<variable_name>","value":"value"}}}
-
 
91
*/
-
 
92
 
-
 
93
/*
-
 
94
TODO:
-
 
95
handle more than one rrd data source (not possible with different alias/variables)
-
 
96
function RRDTool_AddConfig(alias_name, variable_name, push_interval, store_name, rrd_filename, rrd_data_source_name_array)
-
 
97
*/
-
 
98
 
-
 
99
/* Function to add new rrd config on atomic command line interface */
-
 
100
function RRDTool_AddConfig(alias_name, variable_name, push_interval, store_name, rrd_filename, rrd_data_source_name)
-
 
101
{
-
 
102
    /* In case RRD_Tool() is not called from autostart.js yet */
-
 
103
    if (RRD_Tool_Config == null || RRD_Tool_StoredData == null || RRD_Tool_Timers == null)
-
 
104
    {
-
 
105
        RRD_Tool_Config = "RRD_Tool_config";
-
 
106
        RRD_Tool_StoredData = new Array();
-
 
107
        RRD_Tool_Timers = new Array();
-
 
108
       
-
 
109
        Log("\033[31mRRD_Tool() must be called from autostart.js!.\033[0m\n");
-
 
110
    }
-
 
111
   
-
 
112
    //Log("rrd data source array length: "+rrd_data_source_name_array.length);
-
 
113
    //if (rrd_data_source_name_array.length > 0)
-
 
114
    //{
-
 
115
        var newString = "{\"rrd\":{\"file\":\"" + rrd_filename + "\",\"Period_s\":\"" + push_interval + "\"},\"rrdNames\":{\"" + rrd_data_source_name + "\":{\"Module\":\""+alias_name+"\",\"data\":\""+variable_name+"\",\"value\":\"value\"}}";
-
 
116
        //for (var rrd_data_source in rrd_data_source_name_array)
-
 
117
        //{
-
 
118
        //  newString += "";
-
 
119
        //}
-
 
120
        newString += "}";
-
 
121
       
-
 
122
        /* Push new config line to storage */
-
 
123
        Storage_SetParameter(RRD_Tool_Config, store_name, newString);
-
 
124
        //Log("New config: "+newString + " configname " + RRD_Tool_Config +"\n");
-
 
125
    //}
-
 
126
 
-
 
127
    /* Start this new config now */
-
 
128
    RRD_Tool_StoredData[store_name] = eval("(" + newString + ")");
-
 
129
    RRD_Tool_Timers[store_name] = Timer_SetTimer(function(timer) {RRD_Tool_timerUpdate(timer)}, RRD_Tool_StoredData[store_name]["rrd"]["Period_s"]*1000, true);
-
 
130
    Log("\033[33mCreated: "+store_name+" with update rate of "+RRD_Tool_StoredData[store_name]["rrd"]["Period_s"]+"s.\033[0m\n");
-
 
131
}
-
 
132
 
-
 
133
Console_RegisterCommand(RRDTool_AddConfig, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, RRD_Tool_Aliases(), RRD_Tool_Variables(), RRD_Tool_Intervals() ); });
-
 
134