-
Notifications
You must be signed in to change notification settings - Fork 24
/
blockly.js
133 lines (114 loc) · 6.26 KB
/
blockly.js
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
'use strict';
goog.provide('Blockly.JavaScript.Sendto');
goog.require('Blockly.JavaScript');
// --- ifttt --------------------------------------------------
Blockly.Words['ifttt_iot'] = {'en': 'Send text to IFTTT via iot', 'de': 'Sende Text zu IFTTT über iot', 'ru': 'Послать текст в IFTTT через iot'};
Blockly.Words['ifttt_event'] = {'en': 'event', 'de': 'event', 'ru': 'event'};
Blockly.Words['ifttt_value1'] = {'en': 'value1', 'de': 'value1', 'ru': 'value1'};
Blockly.Words['ifttt_value2'] = {'en': 'value2', 'de': 'value2', 'ru': 'value2'};
Blockly.Words['ifttt_value3'] = {'en': 'value3', 'de': 'value3', 'ru': 'value3'};
Blockly.Words['ifttt_tooltip'] = {'en': 'Send to IFTTT', 'de': 'Sende zu IFTTT', 'ru': 'Послать в IFTTT'};
Blockly.Words['ifttt_help'] = {'en': 'https://github.com/ioBroker/ioBroker.cloud/blob/master/README.md', 'de': 'http://www.iobroker.net/?page_id=178&lang=de', 'ru': 'http://www.iobroker.net/?page_id=4262&lang=ru'};
Blockly.Words['ifttt_log'] = {'en': 'log level', 'de': 'Loglevel', 'ru': 'Протокол'};
Blockly.Words['ifttt_log_none'] = {'en': 'none', 'de': 'keins', 'ru': 'нет'};
Blockly.Words['ifttt_log_info'] = {'en': 'info', 'de': 'info', 'ru': 'инфо'};
Blockly.Words['ifttt_log_debug'] = {'en': 'debug', 'de': 'debug', 'ru': 'debug'};
Blockly.Words['ifttt_log_warn'] = {'en': 'warning', 'de': 'warning', 'ru': 'warning'};
Blockly.Words['ifttt_log_error'] = {'en': 'error', 'de': 'error', 'ru': 'ошибка'};
// this is copy of engines.js
// Blockly.Sendto is global variable and defined in javascript/admin/google-blockly/own/blocks_sendto.js
Blockly.Sendto.blocks['ifttt_iot'] =
'<block type="ifttt_iot">'
+ ' <value name="INSTANCE">'
+ ' </value>'
+ ' <value name="EVENT">'
+ ' <shadow type="text">'
+ ' <field name="TEXT">state</field>'
+ ' </shadow>'
+ ' </value>'
+ ' <value name="VALUE1">'
+ ' <shadow type="text">'
+ ' <field name="TEXT">value1</field>'
+ ' </shadow>'
+ ' </value>'
+ ' <value name="VALUE2">'
+ ' <shadow type="text">'
+ ' <field name="TEXT">value2</field>'
+ ' </shadow>'
+ ' </value>'
+ ' <value name="VALUE3">'
+ ' <shadow type="text">'
+ ' <field name="TEXT">value3</field>'
+ ' </shadow>'
+ ' </value>'
+ ' <value name="LOG">'
+ ' </value>'
+ '</block>';
Blockly.Blocks['ifttt_iot'] = {
init: function() {
var options = [];
if (typeof main !== 'undefined' && main.instances) {
for (var i = 0; i < main.instances.length; i++) {
var m = main.instances[i].match(/^system.adapter.iot.(\d+)$/);
if (m) {
var n = parseInt(m[1], 10);
options.push(['iot.' + n, '.' + n]);
}
}
if (!options.length) {
for (var k = 0; k <= 4; k++) {
options.push(['iot.' + k, '.' + k]);
}
}
} else {
for (var u = 0; u <= 4; u++) {
options.push(['iot.' + u, '.' + u]);
}
}
this.appendDummyInput('INSTANCE')
.appendField(Blockly.Translate('ifttt_iot'))
.appendField(new Blockly.FieldDropdown(options), 'INSTANCE');
this.appendValueInput('EVENT')
.appendField(Blockly.Translate('ifttt_event'));
var input = this.appendValueInput('VALUE1')
.appendField(Blockly.Translate('ifttt_value1'));
if (input.connection) input.connection._optional = true;
input = this.appendValueInput('VALUE2')
.appendField(Blockly.Translate('ifttt_value2'));
if (input.connection) input.connection._optional = true;
input = this.appendValueInput('VALUE3')
.appendField(Blockly.Translate('ifttt_value3'));
if (input.connection) input.connection._optional = true;
this.appendDummyInput('LOG')
.appendField(Blockly.Translate('ifttt_log'))
.appendField(new Blockly.FieldDropdown([
[Blockly.Translate('ifttt_log_none'), ''],
[Blockly.Translate('ifttt_log_info'), 'log'],
[Blockly.Translate('ifttt_log_debug'), 'debug'],
[Blockly.Translate('ifttt_log_warn'), 'warn'],
[Blockly.Translate('ifttt_log_error'), 'error']
]), 'LOG');
this.setInputsInline(false);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(Blockly.Sendto.HUE);
this.setTooltip(Blockly.Translate('ifttt_tooltip'));
this.setHelpUrl(Blockly.Translate('ifttt_help'));
}
};
Blockly.JavaScript['ifttt_iot'] = function(block) {
var dropdown_instance = block.getFieldValue('INSTANCE');
var event = Blockly.JavaScript.valueToCode(block, 'EVENT', Blockly.JavaScript.ORDER_ATOMIC);
var logLevel = block.getFieldValue('LOG');
var value1 = Blockly.JavaScript.valueToCode(block, 'VALUE1', Blockly.JavaScript.ORDER_ATOMIC);
var value2 = Blockly.JavaScript.valueToCode(block, 'VALUE2', Blockly.JavaScript.ORDER_ATOMIC);
var value3 = Blockly.JavaScript.valueToCode(block, 'VALUE3', Blockly.JavaScript.ORDER_ATOMIC);
var logText;
if (logLevel) {
logText = 'console.' + logLevel + '("ifttt_iot: " + ' + event + ');\n'
} else {
logText = '';
}
return 'sendTo("iot' + dropdown_instance + '", "ifttt", {event: ' + event + ', value1: ' + value1 + ', value2: ' + value2 + ', value3: ' + value3 + '});\n' +
logText;
};