Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
1657 runge 1
 
2
Node_ResetNodeId = null;
3
Node_ResetClientId = null;
4
 
1666 runge 5
Node_ProgramNodeId = null;
6
Node_ProgramClientId = null;
7
 
1669 runge 8
Node_WaitClientId = null;
9
 
2160 arune 10
Node_Native_Reset = false;
11
Node_Native_Program = false;
2162 arune 12
Node_Native_WaitNodeId = false;
2160 arune 13
 
1887 arune 14
Node_HexData = "";
1968 arune 15
Node_HexNumLines = 0;
1887 arune 16
 
1657 runge 17
function Node_OnChange(node_id, available)
18
{
2162 arune 19
//Log("Node_OnChange "+Node_ResetNodeId+" "+Node_ProgramNodeId+" "+node_id+" "+available+"\n");
1657 runge 20
    if (node_id == Node_ResetNodeId && available)
21
    {
2162 arune 22
//Log("ResetNodeId "+Node_Native_Reset+"\n");
1657 runge 23
        Console_LogToClient(Node_ResetClientId, "\033[32m" + Node_ResetNodeId + " started okay.\033[0m\n");
2083 runge 24
 
2160 arune 25
        Console_NewConnection(Node_ResetClientId); // This is a ugly hack for a bad design for multiusers
26
        Node_ResetNodeId = null;
27
        Node_ResetClientId = null;
28
 
29
        Node_Native_Reset = true;
1657 runge 30
    }
2083 runge 31
 
1666 runge 32
    if (node_id == Node_ProgramNodeId && available)
33
    {
2162 arune 34
//Log("ProgramNodeId "+Node_Native_Program+"\n");
1666 runge 35
        Console_LogToClient(Node_ProgramClientId, "\033[32m" + Node_ProgramNodeId + " started okay.\033[0m\n");
2083 runge 36
 
37
        Console_NewConnection(Node_ProgramClientId); // This is a ugly hack for a bad design for multiusers
1666 runge 38
        Node_ProgramNodeId = null;
39
        Node_ProgramClientId = null;
2160 arune 40
 
41
        Node_Native_Program = true;
1666 runge 42
    }
2083 runge 43
 
1669 runge 44
    if (Node_WaitClientId != null && available)
2083 runge 45
    {
1669 runge 46
        var result = NodeExport_GetNodeInformation(node_id);
2162 arune 47
        Node_Native_WaitNodeId = node_id;
2083 runge 48
 
1669 runge 49
        if (!result)
50
        {
51
            Console_LogToClient(Node_WaitClientId, "\033[31mNo node with id " + node_id + " found.\033[0m\n");
52
            return false;
53
        }
2083 runge 54
 
1669 runge 55
        var date = new Date(result["LastActive"] * 1000);
2083 runge 56
 
1669 runge 57
        Console_LogToClient(Node_WaitClientId, "Id: " + result["Id"] + "\n");
58
        Console_LogToClient(Node_WaitClientId, "Valid: " + result["Valid"] + "\n");
59
        Console_LogToClient(Node_WaitClientId, "Bios Version: " + result["BiosVersion"] + "\n");
60
        Console_LogToClient(Node_WaitClientId, "Device Type: " + result["DeviceType"] + "\n");
61
        Console_LogToClient(Node_WaitClientId, "Has Application: " + result["HasApplication"] + "\n");
62
        Console_LogToClient(Node_WaitClientId, "Last Active: " + date.toString() + "\n");
2083 runge 63
 
64
        Console_NewConnection(Node_WaitClientId); // This is a ugly hack for a bad design for multiusers
1669 runge 65
        Node_WaitClientId = null;
66
    }
1657 runge 67
}
68
 
69
function Node_GetAvailableIds()
70
{
71
    var result_list = [];
72
    var available_nodes = NodeExport_GetAvailableNodes();
2083 runge 73
 
1657 runge 74
    for (var n in available_nodes)
75
    {
76
        var id_parts = available_nodes[n].split(",", 2);
2083 runge 77
 
1657 runge 78
        result_list.push(id_parts[0]);
79
    }
2083 runge 80
 
1657 runge 81
    return result_list;
82
}
83
 
84
function Node_ListAvailable()
85
{
86
    var available_nodes = NodeExport_GetAvailableNodes();
2083 runge 87
 
1660 runge 88
    var lines = [["Id", "Modules"]];
2083 runge 89
 
1657 runge 90
    for (var n in available_nodes)
91
    {
1660 runge 92
        var parts = available_nodes[n].split(",");
93
        var id = parts.shift();
94
        var modules = "<none>";
2083 runge 95
 
1660 runge 96
        if (parts.length > 0)
1657 runge 97
        {
1660 runge 98
            modules = parts.join(", ");
1657 runge 99
        }
2083 runge 100
 
1660 runge 101
        lines.push([id, modules]);
1657 runge 102
    }
2083 runge 103
 
1660 runge 104
    var table_lines = create_table(lines);
2083 runge 105
 
1660 runge 106
    Log("\033[29;1m" + table_lines[0] + "\033[0m\n");
2083 runge 107
 
1660 runge 108
    for (var n = 1; n < table_lines.length; n++)
109
    {
110
        Log(table_lines[n] + "\n");
111
    }
2083 runge 112
 
1657 runge 113
    return true;
114
}
115
Console_RegisterCommand(Node_ListAvailable);
116
 
2162 arune 117
function Node_WaitNodePollNative()
118
{
119
    if (Node_Native_WaitNodeId)
120
    {
121
        var result = NodeExport_GetNodeInformation(Node_Native_WaitNodeId);
122
 
123
        if (!result)
124
        {
125
            return "\033[31mError: No node with id " + Node_Native_WaitNodeId + " found.\033[0m";
126
        }
127
 
128
        var date = new Date(result["LastActive"] * 1000);
129
 
130
        return "Id: " + result["Id"] + "\t"+
131
        "Valid: " + result["Valid"] + "\t"+
132
        "Bios Version: " + result["BiosVersion"] + "\t"+
133
        "Device Type: " + result["DeviceType"] + "\t"+
134
        "Has Application: " + result["HasApplication"] + "\t"+
135
        "Last Active: " + date.toString();
136
    }
137
    else
138
    {
139
        return false;
140
    }
141
}
142
 
143
function Node_WaitForInformationNative()
144
{
145
    Node_WaitClientId = Console_GetClientId();
146
    Node_Native_WaitNodeId = false;
147
}
148
 
2160 arune 149
function Node_ResetPollNative()
150
{
151
    return Node_Native_Reset;
152
}
153
 
154
function Node_ResetNative(node_id)
155
{
156
    if (arguments.length < 1)
157
    {
158
        return "\033[31mError: Not enough parameters given.\033[0m";
159
    }
160
 
161
    Node_Native_Reset = false;
162
    if (!NodeExport_ResetNode(node_id))
163
    {
164
        return "\033[31mError: Failed to send reset, this can happen if node only contains bios and it was connected before atom was started/restarted.\033[0m";
165
    }
166
 
167
    Node_ResetClientId = Console_GetClientId();
168
    Node_ResetNodeId = node_id;
169
 
170
    return "Reset command sent successfully";
171
}
172
 
1657 runge 173
function Node_Reset(node_id)
174
{
175
    if (arguments.length < 1)
176
    {
177
        Log("\033[31mNot enough parameters given.\033[0m\n");
178
        return false;
179
    }
2083 runge 180
 
1665 runge 181
    Log("Sending reset to " + node_id + "...\n");
2083 runge 182
 
1657 runge 183
    if (!NodeExport_ResetNode(node_id))
184
    {
2160 arune 185
        Log("\033[31mFailed to send reset, this can happen if node only contains bios and it was connected before atom was started/restarted.\033[0m\n");
1657 runge 186
        return false;
187
    }
2083 runge 188
 
2160 arune 189
    Node_ResetClientId = Console_GetClientId();
190
    Node_ResetNodeId = node_id;
1657 runge 191
    Console_PreventDefaultPrompt();
192
 
193
    return true;
194
}
195
Console_RegisterCommand(Node_Reset, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds()); });
1665 runge 196
 
2160 arune 197
function Node_ProgramPollNative()
198
{
199
    return Node_Native_Program;
200
}
201
 
202
function Node_AppendHexNative(hexrow)
203
{
204
    if (arguments.length < 1)
205
    {
206
        return "\033[31mError: Not enough parameters given.\033[0m";
207
    }
208
 
209
    Node_HexData = Node_HexData + hexrow + "\n";
210
    Node_HexNumLines += 1;
211
 
212
    return true;
213
}
214
 
215
function Node_ProgramNative(node_id, bios, hexnumlines)
216
{
217
    if (arguments.length < 3)
218
    {
219
        return "\033[31mError: Not enough parameters given.\033[0m";
220
    }
221
    if (Node_HexNumLines != hexnumlines)
222
    {
223
        return "\033[31mError: Number of hex lines does not match received (" + Node_HexNumLines + " received, should be " + hexnumlines + ").\033[0m";
224
    }
225
 
226
    Node_Native_Program = false;
227
    if (!NodeExport_ProgramNodeHex(node_id, bios > 0, Node_HexData))
228
    {
229
        return "\033[31mError: Failed to start programming, this can happen if node only contains bios and it was connected before atom was started/restarted.\033[0m";
230
    }
231
 
232
    Node_ProgramClientId = Console_GetClientId();
233
    Node_ProgramNodeId = node_id;
234
    Node_HexData = "";
235
    Node_HexNumLines = 0;
236
 
237
    return "Program command sent successfully";
238
}
239
 
1666 runge 240
function Node_Program(node_id, bios, filename)
241
{
242
    if (arguments.length < 3)
243
    {
244
        Log("\033[31mNot enough parameters given.\033[0m\n");
245
        return false;
246
    }
2083 runge 247
 
1666 runge 248
    Log("Initiating programming of " + node_id + "...\n");
2083 runge 249
 
1666 runge 250
    if (!NodeExport_ProgramNode(node_id, bios > 0, filename))
251
    {
2160 arune 252
        Log("\033[31mFailed to start programming, this can happen if node only contains bios and it was connected before atom was started/restarted.\033[0m\n");
1666 runge 253
        return false;
254
    }
2083 runge 255
 
2160 arune 256
    Node_ProgramClientId = Console_GetClientId();
257
    Node_ProgramNodeId = node_id;
1666 runge 258
    Console_PreventDefaultPrompt();
2083 runge 259
 
1666 runge 260
    return true;
261
}
262
Console_RegisterCommand(Node_Program, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds(), [0, 1]); });
263
 
1968 arune 264
function Node_ProgramHex(node_id, bios, hexnumlines)
1887 arune 265
{
266
    if (arguments.length < 2)
267
    {
268
        Log("\033[31mNot enough parameters given.\033[0m\n");
269
        return false;
270
    }
1968 arune 271
    if (Node_HexNumLines != hexnumlines)
272
    {
273
        Log("\033[31mNumber of hex lines does not match received (" + Node_HexNumLines + " received, should be " + hexnumlines + ").\033[0m\n");
274
        return false;
275
    }
2083 runge 276
 
1887 arune 277
    Log("Initiating programming of " + node_id + "...\n");
2083 runge 278
 
1887 arune 279
    if (!NodeExport_ProgramNodeHex(node_id, bios > 0, Node_HexData))
280
    {
2160 arune 281
        Log("\033[31mFailed to start programming, this can happen if node only contains bios and it was connected before atom was started/restarted.\033[0m\n");
1887 arune 282
        return false;
283
    }
2083 runge 284
 
2160 arune 285
    Node_ProgramClientId = Console_GetClientId();
286
    Node_ProgramNodeId = node_id;
1887 arune 287
    Console_PreventDefaultPrompt();
288
 
289
    Node_HexData = "";
1968 arune 290
    Node_HexNumLines = 0;
1887 arune 291
 
1968 arune 292
 
1887 arune 293
    return true;
294
}
295
Console_RegisterCommand(Node_ProgramHex, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds(), [0, 1]); });
296
 
297
function Node_ClearHex()
298
{
1889 arune 299
//    Log("Clearing hexdata...\n");
2083 runge 300
 
1887 arune 301
    Node_HexData = "";
1968 arune 302
    Node_HexNumLines = 0;
1889 arune 303
 
1887 arune 304
    return true;
305
}
306
Console_RegisterCommand(Node_ClearHex);
307
 
308
function Node_AppendHex(hexrow)
309
{
310
    if (arguments.length < 1)
311
    {
312
        Log("\033[31mNot enough parameters given.\033[0m\n");
313
        return false;
314
    }
2083 runge 315
 
1889 arune 316
//    Log("Appending hexrow...\n");
2083 runge 317
 
1887 arune 318
    Node_HexData = Node_HexData + hexrow + "\n";
1968 arune 319
    Node_HexNumLines += 1;
1889 arune 320
 
1887 arune 321
    return true;
322
}
323
Console_RegisterCommand(Node_AppendHex);
324
 
1665 runge 325
function Node_GetInformation(node_id)
326
{
327
    if (arguments.length < 1)
328
    {
329
        Log("\033[31mNot enough parameters given.\033[0m\n");
330
        return false;
331
    }
2083 runge 332
 
1665 runge 333
    var result = NodeExport_GetNodeInformation(node_id);
2083 runge 334
 
1665 runge 335
    if (!result)
336
    {
337
        Log("\033[31mNo node with id " + node_id + " found.\033[0m\n");
338
        return false;
339
    }
2083 runge 340
 
1665 runge 341
    var date = new Date(result["LastActive"] * 1000);
2083 runge 342
 
1673 runge 343
    Log("\033[96mId: \033[0;1m" + result["Id"] + "\033[0m\n");
344
    Log("\033[96mValid: \033[0;1m" + result["Valid"] + "\033[0m\n");
345
    Log("\033[96mBios Version: \033[0;1m" + result["BiosVersion"] + "\033[0m\n");
346
    Log("\033[96mDevice Type: \033[0;1m" + result["DeviceType"] + "\033[0m\n");
347
    Log("\033[96mHas Application: \033[0;1m" + result["HasApplication"] + "\033[0m\n");
348
    Log("\033[96mLast Active: \033[0;1m" + date.toString() + "\033[0m\n");
2083 runge 349
 
1665 runge 350
    return true;
351
}
352
Console_RegisterCommand(Node_GetInformation, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds()); });
1669 runge 353
 
354
function Node_WaitForInformation()
355
{
356
    Node_WaitClientId = Console_GetClientId();
357
    Console_PreventDefaultPrompt();
2083 runge 358
 
1669 runge 359
    return true;
360
}
361
Console_RegisterCommand(Node_WaitForInformation);