Subversion Repositories HomeAutomation

Rev

Rev 2283 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
2047 arune 1
function RRD_Tool() {Log("\033[31mCall to RRD_Tool() can be removed from autostart.js!\033[0m\n");}
2
 
2048 arune 3
/* Declaration of static variables */
4
RRD_Tool_myInterval = null;
5
RRD_Tool_StoredData = null;
6
RRD_Tool_Timers = null;
7
/* Constant config name */
8
RRD_Tool_Config = "RRD_Tool_config";
9
 
2047 arune 10
function RRD_Tool_Init()
1770 linlun 11
{
2047 arune 12
    //Log("\033[33mRRD Tool updater starting...\033[0m");
1770 linlun 13
 
2042 arune 14
    var last_value_string = Storage_GetParameters(RRD_Tool_Config);
15
    RRD_Tool_StoredData = new Array();
16
    RRD_Tool_Timers = new Array();
1770 linlun 17
    for (var name in last_value_string)
18
    {
2042 arune 19
        RRD_Tool_StoredData[name] = eval("(" + last_value_string[name] + ")");
20
        Log("\033[33mFound: "+name+" with update rate of "+RRD_Tool_StoredData[name]["rrd"]["Period_s"]+"s.\033[0m");
21
        RRD_Tool_Timers[name] = Timer_SetTimer(function(timer) {RRD_Tool_timerUpdate(timer)}, RRD_Tool_StoredData[name]["rrd"]["Period_s"]*1000, true);
1770 linlun 22
    }
2047 arune 23
    //Log("\033[33mRRD Tool updater created.\033[0m");
1770 linlun 24
}
25
 
2048 arune 26
RRD_Tool_Init();
2042 arune 27
RRD_Tool_timerUpdate = function(timer)
1770 linlun 28
{
29
    var alias_string = Storage_GetParameters("LastValues");
2042 arune 30
    for (var name in RRD_Tool_Timers)
1770 linlun 31
    {  
2279 linlun 32
        if (RRD_Tool_Timers[name] == timer) {
2042 arune 33
            //Log("Timer: "+timer+", named: "+name+" with data: "+RRD_Tool_StoredData[name]["rrd"]["file"]+"\n");
1770 linlun 34
            /* Create command to execute...*/
35
            var names = "";
36
            var values = "N";
37
            var cmd = "/usr/bin/rrdtool update --template ";
38
            var first = 1;
2042 arune 39
            for (var label in RRD_Tool_StoredData[name]["rrdNames"])
1770 linlun 40
            {
41
                //Log("Found label: "+label+"\n");
2042 arune 42
                var data = eval("(" + alias_string[RRD_Tool_StoredData[name]["rrdNames"][label]["Module"]] + ")");
2284 linlun 43
                var datatype=RRD_Tool_StoredData[name]["rrdNames"][label]["data"];
44
                var periodicity=RRD_Tool_StoredData[name]["rrd"]["Period_s"];
45
                if ((get_time() - periodicity*2) > data[datatype]["timestamp"])
46
                {
47
                    //Log("\033[31mError: RRD-store "+name+" could not store data, too old. Current time="+get_time()+", periodicity="+periodicity+", data timestamp="+data[datatype]["timestamp"]+".\033[0m");
48
                    continue;
49
                }
1770 linlun 50
                if (first == 1) {
51
                    names += label;
52
                    first = 0;
53
                } else {
54
                    names += ":"+label;  
55
                }
2141 linlun 56
                if (typeof data[RRD_Tool_StoredData[name]["rrdNames"][label]["data"]]["value"] == "string")
57
                {
2220 arune 58
                    values += ":"+data[RRD_Tool_StoredData[name]["rrdNames"][label]["data"]]["value"];
2141 linlun 59
                }
60
                else
61
                {
2284 linlun 62
                    values += ":"+data[RRD_Tool_StoredData[name]["rrdNames"][label]["data"]]["value"][RRD_Tool_StoredData[name]["rrdNames"][label]["value"]];
2141 linlun 63
                }  
1770 linlun 64
            }
2284 linlun 65
            if (first != 1) {
66
                cmd += names + " "+RRD_Tool_StoredData[name]["rrd"]["file"]+" "+values;
67
                //Log("Command: "+cmd);
68
                Execute(cmd);
69
            }
2279 linlun 70
            break;
1770 linlun 71
        }
72
    }
73
}
2042 arune 74
 
2279 linlun 75
 
2042 arune 76
/* Tab complete stuff */
77
RRD_Tool_Aliases        = function() { return Module_GetAliasNames(); };
78
RRD_Tool_Intervals      = function() { return [ 5, 10, 30, 60, 120, 150, 300 ]; };
2218 arune 79
RRD_Tool_MinValues      = function() { return [ -40, 0 ]; };
80
RRD_Tool_MaxValues      = function() { return [ 50, 100, 10000, 20000 ]; };
2042 arune 81
/* Function returns an array with all CAN variables existing in LastValues storage */
82
RRD_Tool_Variables      = function()
83
{
84
    var last_value_string = Storage_GetParameters("LastValues");
85
    var last_values = new Array();
86
    var last_values_variables = [];
87
    for (var name in last_value_string)
88
    {
89
        last_values[name] = eval("(" + last_value_string[name] + ")");
90
        for (var name2 in last_values[name])
91
        {
92
            /* Only get unique variables */
93
            if (last_values_variables.indexOf(name2) == -1)
94
            {
95
                last_values_variables.push(name2);
96
            }
97
        }
98
    }
99
    return last_values_variables;
100
};
101
 
102
/*
103
electricPower={"rrd":{"file":"/var/www/appdata/rrd-data-new/electricPower.rrd","Period_s":"60"}, "rrdNames":{"power":{"Module":"powerMeter","data":"Power32","value":"value"}}}
104
 
105
<store_name>={"rrd":{"file":"<rrd_filename>","Period_s":"<push_interval>"}, "rrdNames":{"<rrd_data_source_name>":{"Module":"<alias_name>","data":"<variable_name>","value":"value"}}}
106
*/
107
 
108
/*
109
TODO:
110
handle more than one rrd data source (not possible with different alias/variables)
111
function RRDTool_AddConfig(alias_name, variable_name, push_interval, store_name, rrd_filename, rrd_data_source_name_array)
112
*/
113
 
114
/* Function to add new rrd config on atomic command line interface */
115
function RRDTool_AddConfig(alias_name, variable_name, push_interval, store_name, rrd_filename, rrd_data_source_name)
116
{
2046 arune 117
    if (arguments.length < 6)
118
    {
119
        Log("\033[31mNot enough parameters given.\033[0m\n");
120
        return false;
121
    }
122
 
2042 arune 123
    if (RRD_Tool_Config == null || RRD_Tool_StoredData == null || RRD_Tool_Timers == null)
124
    {
125
        RRD_Tool_Config = "RRD_Tool_config";
126
        RRD_Tool_StoredData = new Array();
127
        RRD_Tool_Timers = new Array();
128
    }
129
 
130
    //Log("rrd data source array length: "+rrd_data_source_name_array.length);
131
    //if (rrd_data_source_name_array.length > 0)
132
    //{
133
        var newString = "{\"rrd\":{\"file\":\"" + rrd_filename + "\",\"Period_s\":\"" + push_interval + "\"},\"rrdNames\":{\"" + rrd_data_source_name + "\":{\"Module\":\""+alias_name+"\",\"data\":\""+variable_name+"\",\"value\":\"value\"}}";
134
        //for (var rrd_data_source in rrd_data_source_name_array)
135
        //{
136
        //  newString += "";
137
        //}
138
        newString += "}";
139
 
140
        /* Push new config line to storage */
141
        Storage_SetParameter(RRD_Tool_Config, store_name, newString);
142
        //Log("New config: "+newString + " configname " + RRD_Tool_Config +"\n");
143
    //}
144
 
145
    /* Start this new config now */
146
    RRD_Tool_StoredData[store_name] = eval("(" + newString + ")");
147
    RRD_Tool_Timers[store_name] = Timer_SetTimer(function(timer) {RRD_Tool_timerUpdate(timer)}, RRD_Tool_StoredData[store_name]["rrd"]["Period_s"]*1000, true);
148
    Log("\033[33mCreated: "+store_name+" with update rate of "+RRD_Tool_StoredData[store_name]["rrd"]["Period_s"]+"s.\033[0m\n");
149
}
150
Console_RegisterCommand(RRDTool_AddConfig, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, RRD_Tool_Aliases(), RRD_Tool_Variables(), RRD_Tool_Intervals() ); });
151
 
2218 arune 152
 
153
/* Function to add new rrd config and create rrd file on atomic command line interface */
154
function RRDTool_AddConfigCreateRrd(alias_name, variable_name, push_interval, data_max, data_min, store_name, rrd_filename, rrd_data_source_name)
155
{
156
    /* Create new rrd file */
157
    /* Two avarages, one with 1 value avarage and one with 12 values avarage */
158
    /* Make sure 1 value avarage last 14 days and 12 value avarage last 10 years, independant of push interval */
159
 
160
    /* set data timeout to double push interval */
161
    /* set step to push interval */
162
 
163
}
164
//Console_RegisterCommand(RRDTool_AddConfigCreateRrd, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, RRD_Tool_Aliases(), RRD_Tool_Variables(), RRD_Tool_Intervals(), RRD_Tool_MaxValues(), RRD_Tool_MinValues() ); });
165