Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
1603 runge 1
 
1997 arune 2
IRIn_ModuleNames    = [ "irReceive", "rfTransceive" ];
1646 runge 3
IRIn_Channels       = function() { return [ 0 ]; };
1647 runge 4
IRIn_Aliases        = function() { return Module_GetAliasNames(IRIn_ModuleNames); };
5
IRIn_AvailableIds   = function() { return Module_GetAvailableIds(IRIn_ModuleNames); };
6
IRIn_Remotes        = function() { return get_keys(Storage_GetParameters("RemoteList")); };
1646 runge 7
 
1647 runge 8
IRIn_RecordRemoteName = null;
9
IRIn_RecordAliasData = null;
10
IRIn_RecordLast = null;
1657 runge 11
IRIn_RecordClientId = null;
2048 arune 12
IRIn_OnMessageFunctions = null;
1647 runge 13
 
2048 arune 14
for (var i=0; i<IRIn_Aliases().length; i++)
15
{
16
    /* Register to messages from all ir aliases */
17
    Module_RegisterToOnMessage(IRIn_Aliases()[i], IRIn_IrMessage);
18
}
19
function IRIn_IrMessage(alias_name, command, variables)
20
{
21
    /* First check if there are any callbacks registered */
22
    if (IRIn_OnMessageFunctions == null)
23
    {
24
        return false;
25
    }
26
    /* Also check if there are any callbacks registered to this alias */
27
    if (!IRIn_OnMessageFunctions[alias_name])
28
    {
29
        return false;
30
    }
31
    /* Check if code and protocol maches one or more remote */
32
    var remotes = get_keys(Storage_GetParameters("RemoteList"));
33
    for (var i=0; i < remotes.length; i++)
34
    {
35
        var buttons = get_keys(Storage_GetParameters("Remote_"+remotes[i]));
36
        for (var j=0; j < buttons.length; j++)
37
        {
38
            var button_string = Storage_GetParameter("Remote_"+remotes[i], buttons[j]);
39
            var button_data = eval("(" + button_string + ")");
40
 
41
            if (typeof button_data["protocol"] == 'undefined' || typeof button_data["data"] == 'undefined')
42
            {
43
                Log("\033[31mButton data is corrupt.\033[0m\n");
44
                return false;
45
            }
46
 
47
            if (button_data["protocol"] == variables["Protocol"] && button_data["data"] == variables["IRdata"])
48
            {
49
                //Log("Found match: "+remotes[i]+" "+buttons[j]+" "+button_data["protocol"]+" "+button_data["data"]+"");
50
                if (IRIn_OnMessageFunctions[alias_name])
51
                {
52
                    for (var n in IRIn_OnMessageFunctions[alias_name])
53
                    {
54
                        //Log("Found callback, calling");
55
                        /* Call callback with arguments remotes[i] buttons[j] and variables["Status"]*/
56
                        IRIn_OnMessageFunctions[alias_name][n](alias_name, remotes[i], buttons[j], variables["Status"]);
57
                    }
58
                }
59
            }
60
        }
61
    }
62
    return true;
63
}
64
 
65
function IRIn_RegisterToMessage(alias_name, callback)
66
{
67
    if (IRIn_OnMessageFunctions == null)
68
    {
69
        IRIn_OnMessageFunctions = new Array();
70
    }
71
 
72
    if (!IRIn_OnMessageFunctions[alias_name])
73
    {
74
        IRIn_OnMessageFunctions[alias_name] = new Array();
75
    }
76
 
77
    IRIn_OnMessageFunctions[alias_name].push(callback);
78
}
79
 
1647 runge 80
function IRIn_CodeToName(data, protocol, remote_name)
1646 runge 81
{
1647 runge 82
    var remote_storage_name = Storage_GetParameter("RemoteList", remote_name);
1646 runge 83
 
1647 runge 84
        if (!remote_storage_name)
85
        {
86
        Log("No such remote " + remote_name);
87
                return "";
88
        }
1646 runge 89
 
1647 runge 90
    var buttons = Storage_GetParameters(remote_storage_name);
1646 runge 91
 
1647 runge 92
    for (var button_name in buttons)
93
    {
94
        var button_data = eval("(" + buttons[button_name]+ ")");
1646 runge 95
 
1647 runge 96
        Log(buttons[button_name]);
1646 runge 97
 
1647 runge 98
        if (button_data["data"] == (data + '') && button_data["protocol"] == protocol)
99
        {
100
            return button_name;
101
        }
102
    }
1646 runge 103
 
1648 runge 104
    Log("Found no matching button on remote " + remote_name);
1647 runge 105
    return "";
106
}
1646 runge 107
 
1647 runge 108
function IRIn_Record(alias_name, remote_name)
109
{
110
    if (arguments.length < 2)
111
    {
1649 runge 112
        Log("\033[31mNot enough parameters given.\033[0m\n");
1647 runge 113
        return false;
114
    }
115
 
116
    IRIn_RecordRemoteName = Storage_GetParameter("RemoteList", remote_name);
117
 
118
    if (!IRIn_RecordRemoteName)
119
    {
120
        IRIn_RecordRemoteName = "Remote_" + remote_name;
121
 
1649 runge 122
        Log("\033[32mNo such remote name in list, " + remote_name + ", creating...\033[0m\n");
1647 runge 123
 
124
        Storage_SetParameter("RemoteList", remote_name, IRIn_RecordRemoteName);
125
    }
126
 
127
    var aliases_data = Module_ResolveAlias(alias_name, IRIn_ModuleNames);
128
 
129
    for (var name in aliases_data)
130
    {
131
        if (aliases_data[name]["group"])
132
        {
1649 runge 133
            Log("\033[31mAlias must not be an alias group.\033[0m\n");
1647 runge 134
            return false;
135
        }
136
 
1657 runge 137
        IRIn_RecordClientId = Console_GetClientId();
138
 
1647 runge 139
        IRIn_RecordAliasData = aliases_data[name];
1998 arune 140
        Log("To finish press a random key on the remote and then enter dot as name.\n");
1647 runge 141
 
1649 runge 142
        Log("\033[29mPress a button on you remote...\033[0m\n");
1647 runge 143
        Console_PreventDefaultPrompt();
144
 
145
        Module_RegisterToOnMessage(alias_name, IRIn_RecordOnIrMessage);
146
        return true;
147
    }
148
 
1649 runge 149
    Log("\033[31mNo such alias name found, " + alias_name + ".\033[0m\n");
1647 runge 150
    return false;
151
 
152
}
153
Console_RegisterCommand(IRIn_Record, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, IRIn_Aliases(), IRIn_Remotes()); });
1646 runge 154
 
1647 runge 155
function IRIn_RecordOnResponse(response)
1603 runge 156
{
1647 runge 157
    if (response == ".")
158
    {
1657 runge 159
        IRIn_RecordClientId = null;
1647 runge 160
        return true;
161
    }
162
 
163
    Storage_SetParameter(IRIn_RecordRemoteName, response, JSON.stringify(IRIn_RecordLast));
164
    IRIn_RecordLast = null;
165
 
1649 runge 166
    Log("\033[29mPress a button on you remote...\033[0m\n");
1647 runge 167
    Console_PreventDefaultPrompt();
168
 
169
    return true;
1603 runge 170
}
171
 
1647 runge 172
function IRIn_RecordOnIrMessage(alias_name, command, variables)
1603 runge 173
{
1647 runge 174
    if (variables["Status"] != "Pressed" || IRIn_RecordLast != null)
1603 runge 175
    {
1647 runge 176
        return;
1603 runge 177
    }
178
 
1657 runge 179
    ConsoleExport_LogToClient(IRIn_RecordClientId, "\033[32mGot code " + variables["IRdata"] + " on protocol " + variables["Protocol"] + ".\033[0m\n");
1647 runge 180
 
181
    IRIn_RecordLast = { "protocol" : variables["Protocol"], "data" : variables["IRdata"] };
182
 
1649 runge 183
    Console_PromptRequest("\033[29mEnter name: \033[0m", IRIn_RecordOnResponse);
1647 runge 184
 
185
    return true;
1603 runge 186
}