Subversion Repositories HomeAutomation

Rev

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