Subversion Repositories HomeAutomation

Rev

Rev 1952 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1601 runge 1
 
1759 runge 2
/*
3
String.pad(length: Integer, [substring: String = " "], [type: Integer = 0]): String
4
      Returns the string with a substring padded on the left, right or both sides.
5
    length
6
        amount of characters that the string must have
7
    substring
8
        string that will be concatenated
9
    type
10
        specifies the side where the concatenation will happen, where: 0 = left, 1 = right and 2 = both sides
11
*/
12
String.prototype.pad = function(l, s, t)
13
{
14
  return  s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
15
    + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
16
    + this + s.substr(0, l - t) : this;
17
}
1679 runge 18
 
1759 runge 19
String.prototype.ltrim = function(charlist)
20
{
21
  charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
22
  var re = new RegExp('^[' + charlist + ']+', 'g');
23
  return this.replace(re, '');
24
}
1679 runge 25
 
1759 runge 26
String.prototype.rtrim = function(charlist)
27
{
28
  charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
29
  var re = new RegExp('[' + charlist + ']+$', 'g');
30
  return this.replace(re, '');
31
}
32
 
33
String.prototype.trim = function(charlist)
34
{
35
  var str = this.ltrim(charlist);
36
  return str.rtrim(charlist);
37
}
38
 
39
String.prototype.html_entity_decode = function()
40
{
41
  var str = this.replace(/Å/g, "Ä");
42
  str = str.replace(/Ä/g, "Ä");
43
 
44
  str = str.replace(/Ä/g, "Å");
45
  str = str.replace(/Å/g, "Å");
46
 
47
  str = str.replace(/Ö/g, "Ö");
48
  str = str.replace(/Ö/g, "Ö");
49
 
50
  str = str.replace(/ä/g, "ä");
51
  str = str.replace(/ä/g, "ä");
52
 
53
  str = str.replace(/å/g, "å");
54
  str = str.replace(/å/g, "å");
55
 
56
  str = str.replace(/ö/g, "ö");
57
  str = str.replace(/ö/g, "ö");
58
 
59
  //FIXME: Add more characters
60
 
61
  return str;
62
}
63
 
64
Date.prototype.getTimestamp = function()
65
{
66
  return this.getTime()/1000;
67
}
68
 
69
Date.prototype.getDateFormated = function()
70
{
71
  return this.getFullYear() + "-" + (this.getMonth()+1).toString().pad(2, "0", 0) + "-" + this.getDate().toString().pad(2, "0", 0);
72
}
73
 
74
Date.prototype.getTimeFormated = function()
75
{
76
  return this.getHours().toString().pad(2, "0", 0) + ":" + this.getMinutes().toString().pad(2, "0", 0) + ":" + this.getSeconds().toString().pad(2, "0", 0);
77
}
78
 
79
Date.prototype.getTimeShortFormated = function()
80
{
81
  return this.getHours().toString().pad(2, "0", 0) + ":" + this.getMinutes().toString().pad(2, "0", 0);
82
}
83
 
84
Date.prototype.getDateTimeFormated = function()
85
{
86
  return this.getDateFormated() + " " + this.getTimeFormated();
87
}
88
 
1649 runge 89
function get_time()
90
{
1679 runge 91
    return Math.round(new Date().getTime() / 1000);
1649 runge 92
}
93
 
1647 runge 94
function to_array(obj)
95
{
1679 runge 96
    var result = [];
97
 
98
    for (var n = 0; n < obj.length; n++)
99
    {
100
        result.push(obj[n]);
101
    }
102
 
103
    return result;
1647 runge 104
}
105
 
1646 runge 106
function array_length(array)
107
{
108
    var count = 0;
1679 runge 109
 
1646 runge 110
    for (var n in array)
111
    {
112
        count++;
113
    }
1679 runge 114
 
1646 runge 115
    return count;
116
}
117
 
1660 runge 118
function array_remove_empty(array)
119
{
1679 runge 120
    var result = [];
121
 
122
    for (var n in array)
123
    {
124
        if (array[n] && (typeof array[n] == 'string' && array[n].length > 0))
125
        {
126
            result.push(array[n]);
127
        }
128
    }
129
 
130
    return result;
1660 runge 131
}
132
 
1644 runge 133
function in_array(array, item)
1601 runge 134
{
1679 runge 135
    for (var n in array)
136
    {
137
        if (array[n] == item)
138
        {
139
            return true;
140
        }
141
    }
142
 
143
    return false;
1603 runge 144
}
145
 
1644 runge 146
function key_in_array(array, key)
1614 runge 147
{
1679 runge 148
    for (var n in array)
149
    {
150
        if (n == key)
151
        {
152
            return true;
153
        }
154
    }
155
 
156
    return false;
1614 runge 157
}
158
 
1647 runge 159
function get_keys(array)
160
{
1679 runge 161
    var result = [];
162
 
163
    for (var name in array)
164
    {
165
        result.push(name);
166
    }
167
 
168
    return result;
1647 runge 169
}
170
 
1603 runge 171
String.prototype.ltrim = function(charlist)
172
{
1679 runge 173
    charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
174
    var re = new RegExp('^[' + charlist + ']+', 'g');
175
    return this.replace(re, '');
1603 runge 176
}
177
 
178
String.prototype.rtrim = function(charlist)
179
{
1679 runge 180
    charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
181
    var re = new RegExp('[' + charlist + ']+$', 'g');
182
    return this.replace(re, '');
1603 runge 183
}
184
 
185
String.prototype.trim = function(charlist)
186
{
1679 runge 187
    var str = this.ltrim(charlist);
188
    return str.rtrim(charlist);
1603 runge 189
}
1660 runge 190
 
191
String.prototype.rpad = function(char, length)
192
{
1679 runge 193
    var result = this;
194
 
195
    while (result.length < length)
196
    {
197
        result += char;
198
    }
199
 
200
    return result;
1660 runge 201
}
202
 
203
function create_table(table_lines)
204
{
1679 runge 205
    var table_rows = [];
206
 
207
    if (table_lines.length == 0)
208
    {
209
        return table_rows;
210
    }
211
 
212
    for (var col = 0; col < table_lines[0].length; col++)
213
    {
214
        var len = 0;
215
 
216
        for (var row = 0; row < table_lines.length; row++)
217
        {
218
            if (table_lines[row][col].length > len)
219
            {
220
                len = table_lines[row][col].length;
221
            }
222
        }
223
 
224
        len += 6;
225
 
226
        for (var row = 0; row < table_lines.length; row++)
227
        {
228
            if (!table_rows[row])
229
            {
230
                table_rows[row] = "";
231
            }
232
 
233
            var str = table_lines[row][col] + '';
234
            str = str.rpad(" ", len);
235
            //Log("col=" + col + ", row=" + row + " len=" + len + ", str=\"" + str + "\", str.length=" + str.length + "\n");
236
            table_rows[row] += str;
237
        }
238
    }
239
 
240
    return table_rows;
1660 runge 241
}
1952 runge 242
 
243
function parse_json_rpc(data)
244
{
245
  var items = [];
246
  var position = -1;
247
 
248
  //Log(data);
249
 
250
  while ((position = data.indexOf("}{")) != -1)
251
  {
252
    //Log(data.substr(0, position + 1));
253
 
254
    items.push(eval("(" + data.substr(0, position + 1) + ")"));
255
 
256
    data = data.substring(position + 1);
257
 
258
    //Log(data);
259
  }
260
 
261
  items.push(eval("(" + data + ")"));
262
 
263
  return items;
264
}
265
 
2205 arune 266
function crc8(data, length)
267
{
268
/*
269
CRC calculation
270
http://ghsi.de/CRC/index.php?Polynom=100110001&Message=4F90CB2E
271
with polynomial 100110001
272
*/
273
    var crc = 0;
274
    for (var i=length-1; i>=0; i--)
275
    {
276
        var doInv = (((data>>i)&1) ^ (crc>>7)&1);
277
        crc=(crc<<1)&0xff;
278
        crc=crc|doInv;
279
        crc=crc^((doInv<<4)|((doInv<<5)));
280
    }
281
 
282
    return crc;
283
}
1952 runge 284
 
2205 arune 285
function rshift(num, bits) {
286
    num = num - (num % Math.pow(2,bits));
287
    return num / Math.pow(2,bits);
288
}
1952 runge 289