Subversion Repositories HomeAutomation

Rev

Rev 2141 | Rev 2219 | 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
                }
68
                cmd += names + " "+RRD_Tool_StoredData[name]["rrd"]["file"]+" "+values;
69
                //Log("Command: "+cmd);
70
                Execute(cmd);
71
                break;
1770 linlun 72
            }
73
        }
74
    }
75
}
2042 arune 76
 
77
/* Tab complete stuff */
78
RRD_Tool_Aliases        = function() { return Module_GetAliasNames(); };
79
RRD_Tool_Intervals      = function() { return [ 5, 10, 30, 60, 120, 150, 300 ]; };
2218 arune 80
RRD_Tool_MinValues      = function() { return [ -40, 0 ]; };
81
RRD_Tool_MaxValues      = function() { return [ 50, 100, 10000, 20000 ]; };
2042 arune 82
/* Function returns an array with all CAN variables existing in LastValues storage */
83
RRD_Tool_Variables      = function()
84
{
85
    var last_value_string = Storage_GetParameters("LastValues");
86
    var last_values = new Array();
87
    var last_values_variables = [];
88
    for (var name in last_value_string)
89
    {
90
        last_values[name] = eval("(" + last_value_string[name] + ")");
91
        for (var name2 in last_values[name])
92
        {
93
            /* Only get unique variables */
94
            if (last_values_variables.indexOf(name2) == -1)
95
            {
96
                last_values_variables.push(name2);
97
            }
98
        }
99
    }
100
    return last_values_variables;
101
};
102
 
103
/*
104
electricPower={"rrd":{"file":"/var/www/appdata/rrd-data-new/electricPower.rrd","Period_s":"60"}, "rrdNames":{"power":{"Module":"powerMeter","data":"Power32","value":"value"}}}
105
 
106
<store_name>={"rrd":{"file":"<rrd_filename>","Period_s":"<push_interval>"}, "rrdNames":{"<rrd_data_source_name>":{"Module":"<alias_name>","data":"<variable_name>","value":"value"}}}
107
*/
108
 
109
/*
110
TODO:
111
handle more than one rrd data source (not possible with different alias/variables)
112
function RRDTool_AddConfig(alias_name, variable_name, push_interval, store_name, rrd_filename, rrd_data_source_name_array)
113
*/
114
 
115
/* Function to add new rrd config on atomic command line interface */
116
function RRDTool_AddConfig(alias_name, variable_name, push_interval, store_name, rrd_filename, rrd_data_source_name)
117
{
2046 arune 118
    if (arguments.length < 6)
119
    {
120
        Log("\033[31mNot enough parameters given.\033[0m\n");
121
        return false;
122
    }
123
 
2042 arune 124
    if (RRD_Tool_Config == null || RRD_Tool_StoredData == null || RRD_Tool_Timers == null)
125
    {
126
        RRD_Tool_Config = "RRD_Tool_config";
127
        RRD_Tool_StoredData = new Array();
128
        RRD_Tool_Timers = new Array();
129
    }
130
 
131
    //Log("rrd data source array length: "+rrd_data_source_name_array.length);
132
    //if (rrd_data_source_name_array.length > 0)
133
    //{
134
        var newString = "{\"rrd\":{\"file\":\"" + rrd_filename + "\",\"Period_s\":\"" + push_interval + "\"},\"rrdNames\":{\"" + rrd_data_source_name + "\":{\"Module\":\""+alias_name+"\",\"data\":\""+variable_name+"\",\"value\":\"value\"}}";
135
        //for (var rrd_data_source in rrd_data_source_name_array)
136
        //{
137
        //  newString += "";
138
        //}
139
        newString += "}";
140
 
141
        /* Push new config line to storage */
142
        Storage_SetParameter(RRD_Tool_Config, store_name, newString);
143
        //Log("New config: "+newString + " configname " + RRD_Tool_Config +"\n");
144
    //}
145
 
146
    /* Start this new config now */
147
    RRD_Tool_StoredData[store_name] = eval("(" + newString + ")");
148
    RRD_Tool_Timers[store_name] = Timer_SetTimer(function(timer) {RRD_Tool_timerUpdate(timer)}, RRD_Tool_StoredData[store_name]["rrd"]["Period_s"]*1000, true);
149
    Log("\033[33mCreated: "+store_name+" with update rate of "+RRD_Tool_StoredData[store_name]["rrd"]["Period_s"]+"s.\033[0m\n");
150
}
151
Console_RegisterCommand(RRDTool_AddConfig, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, RRD_Tool_Aliases(), RRD_Tool_Variables(), RRD_Tool_Intervals() ); });
152
 
2218 arune 153
 
154
/* Function to add new rrd config and create rrd file on atomic command line interface */
155
function RRDTool_AddConfigCreateRrd(alias_name, variable_name, push_interval, data_max, data_min, store_name, rrd_filename, rrd_data_source_name)
156
{
157
    /* Create new rrd file */
158
    /* Two avarages, one with 1 value avarage and one with 12 values avarage */
159
    /* Make sure 1 value avarage last 14 days and 12 value avarage last 10 years, independant of push interval */
160
 
161
    /* set data timeout to double push interval */
162
    /* set step to push interval */
163
 
164
}
165
//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() ); });
166