-
Notifications
You must be signed in to change notification settings - Fork 15
/
fritz.html
168 lines (149 loc) · 5.26 KB
/
fritz.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<script type="text/javascript">
RED.nodes.registerType("fritzbox-in", {
category: "fritzbox",
paletteLabel: "FRITZ!Box",
color: "#2E90DD",
defaults: {
device: {
type: "fritzbox-config",
required: true
},
name: {
value: "",
required: false
},
service: {
value: "",
required: true
},
action: {
value: "",
required: true
},
arguments: {
value: "",
required: false
}
},
inputs: 1,
outputs: 1,
icon: "fritz.png",
label: function () {
return this.name ? this.name : "FRITZ!Box";
},
oneditprepare: function () {
var node = this;
var actions = [];
if (node.service) {
$('#node-input-service').append('<option value="' + node.service + '">' + node.service + '</option>').val(
node.service);
}
if (node.action) {
$('#node-input-action').append('<option value="' + node.action + '">' + node.action + '</option>').val(
node.action);
}
if (node.arguments) {
$('#node-input-arguments').html(node.arguments);
}
$('#node-input-scandev').click(function () {
scanServices();
});
$('#node-input-scansrv').click(function () {
scanActions();
});
$('#node-input-action').change(function () {
var action = actions[$('#node-input-action').val()];
if (action) {
$('#node-input-arguments').val(formatArguments(action.inArgs));
$('#node-input-arguments').html($('#node-input-arguments').val());
}
});
function formatArguments(arguments) {
var example = {};
arguments.forEach(function (argument) {
example[argument] = "value";
});
return JSON.stringify(example, undefined, 2);
}
function scanServices() {
var current = $('#node-input-service').val();
var config = RED.nodes.node($('#node-input-device').val());
RED.notify("Requesting services from fritzbox.", "info");
if (config && config.host) {
$.get('fritzbox/services', {
url: config.host
})
.done(function (data) {
RED.notify("Services received.", "info");
var services = JSON.parse(data);
//Clear all services
$('#node-input-service').find('option').remove();
services.forEach(function (service) {
$('#node-input-service').append('<option value="' + service + '">' + service + '</option>');
});
$('#node-input-service').val(current);
})
.fail(function () {
RED.notify("Something went wrong. Please retry.", "error");
})
} else {
RED.notify("Cannot receive services. Please check required informations: host and provider", "warning");
}
}
function scanActions() {
var current = $('#node-input-action').val();
var config = RED.nodes.node($('#node-input-device').val());
var service = $('#node-input-service').val();
RED.notify("Requesting actions from fritzbox.", "info");
if (config && config.host && service) {
$.get('fritzbox/actions', {
url: config.host,
service: service
})
.done(function (data) {
RED.notify("Actions received.", "info");
actions = JSON.parse(data);
//Clear all actions
$('#node-input-action').find('option').remove();
for (key in actions) {
$('#node-input-action').append('<option value="' + key + '">' + key + '</option>');
}
$('#node-input-action').val(current);
})
.fail(function () {
RED.notify("Something went wrong. Please retry.", "error");
});
} else {
RED.notify("Cannot receive actions. Please check required informations: host, provider and service",
"warning");
}
}
}
});
</script>
<script type="text/x-red" data-template-name="fritzbox-in">
<div class="form-row">
<label for="node-input-device"><i class="fa fa-server"></i>Device</label>
<input type="text" id="node-input-device">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i>Name</label>
<input type="text" id="node-input-name" />
</div>
<div class="form-row">
<label for="node-input-service"><i class="fa fa-tasks"></i>Service</label>
<select id="node-input-service" name="node-input-service" style="width: 250px;">
</select>
<button type="button" id="node-input-scandev" class="red-ui-button"><i class="fa fa-search"></i></button>
</div>
<div class="form-row">
<label for="node-input-action"><i class="fa fa-tasks"></i>Action</label>
<select id="node-input-action" name="node-input-action" style="width: 250px;">
</select>
<button type="button" id="node-input-scansrv" class="red-ui-button"><i class="fa fa-search"></i></button>
</div>
<div class="form-tips">
Use the msg.payload to pass the arguments in the form of. Required arguments are:
<pre id="node-input-arguments"></pre>
</div>
</script>