Subversion Repositories HomeAutomation

Rev

Rev 2218 | Rev 2220 | 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();
1770 linlun 27
 
2042 arune 28
RRD_Tool_timerUpdate = function(timer)
1770 linlun 29
{
30
    var alias_string = Storage_GetParameters("LastValues");
2218 arune 31
    timer_loop:
2042 arune 32
    for (var name in RRD_Tool_Timers)
1770 linlun 33
    {  
2218 arune 34
        if (RRD_Tool_Timers[name] == timer)
35
        {
2042 arune 36
            //Log("Timer: "+timer+", named: "+name+" with data: "+RRD_Tool_StoredData[name]["rrd"]["file"]+"\n");
1770 linlun 37
            /* Create command to execute...*/
38
            var names = "";
39
            var values = "N";
40
            var cmd = "/usr/bin/rrdtool update --template ";
41
            var first = 1;
2042 arune 42
            for (var label in RRD_Tool_StoredData[name]["rrdNames"])
1770 linlun 43
            {
44
                //Log("Found label: "+label+"\n");
2042 arune 45
                var data = eval("(" + alias_string[RRD_Tool_StoredData[name]["rrdNames"][label]["Module"]] + ")");
2218 arune 46
                var datatype=RRD_Tool_StoredData[name]["rrdNames"][label]["data"];
47
                var periodicity=RRD_Tool_StoredData[name]["rrd"]["Period_s"]*1000;
48
                if ((get_time() - periodicity*2) > data[datatype]["timestamp"])
49
                {
50
                    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");
51
                    break timer_loop;
52
                }
1770 linlun 53
                if (first == 1) {
54
                    names += label;
55
                    first = 0;
56
                } else {
57
                    names += ":"+label;  
58
                }
2141 linlun 59
                if (typeof data[RRD_Tool_StoredData[name]["rrdNames"][label]["data"]]["value"] == "string")
60
                {
2218 arune 61
                    values += ":"+data[RRD_Tool_StoredData[name]["rrdNames"][label]["data"]]["value"].toString;
2141 linlun 62
                }
63
                else
64
                {
2218 arune 65
                    values += ":"+data[RRD_Tool_StoredData[name]["rrdNames"][label]["data"]]["value"][RRD_Tool_StoredData[name]["rrdNames"][label]["value"]].toString;
2141 linlun 66
                }  
2218 arune 67
                cmd += names + " "+RRD_Tool_StoredData[name]["rrd"]["file"]+" "+values;
68
                //Log("Command: "+cmd);
69
                Execute(cmd);
70
                break;
1770 linlun 71
            }
72
        }
73
    }
74
}
2042 arune 75
 
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