-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
ohblocks.js
133 lines (121 loc) · 4.65 KB
/
ohblocks.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
import Blockly from 'blockly'
import { FieldItemModelPicker } from './ohitemfield'
export default function defineOHBlocks (f7) {
Blockly.Blocks['oh_item'] = {
init: function () {
this.appendDummyInput()
.appendField('item')
.appendField(new FieldItemModelPicker('MyItem', null, { f7 }), 'itemName')
this.setColour(0)
this.setInputsInline(true)
this.setTooltip('Pick an item from the Model')
this.setOutput(true, null)
}
}
Blockly.JavaScript['oh_item'] = function (block) {
const itemName = block.getFieldValue('itemName')
let code = '\'' + itemName + '\''
return [code, 0]
}
Blockly.Blocks['oh_getitem_state'] = {
init: function () {
this.appendValueInput('itemName')
.appendField('get item state')
.setCheck('String')
this.setInputsInline(true)
this.setOutput(true, 'String')
this.setColour(0)
this.setTooltip('Get an item state from the item registry')
this.setHelpUrl('')
}
}
Blockly.JavaScript['oh_getitem_state'] = function (block) {
const itemName = Blockly.JavaScript.valueToCode(block, 'itemName', Blockly.JavaScript.ORDER_ATOMIC)
let code = 'itemRegistry.getItem(' + itemName + ').getState()'
return [code, 0]
}
Blockly.Blocks['oh_sendcommand'] = {
init: function () {
this.appendValueInput('command')
.appendField('send command')
this.appendValueInput('itemName')
.appendField('to')
.setCheck('String')
this.setInputsInline(true)
this.setPreviousStatement(true, null)
this.setNextStatement(true, null)
this.setColour(0)
this.setTooltip('Send a command to an item')
this.setHelpUrl('')
}
}
Blockly.JavaScript['oh_sendcommand'] = function (block) {
const itemName = Blockly.JavaScript.valueToCode(block, 'itemName', Blockly.JavaScript.ORDER_ATOMIC)
const command = Blockly.JavaScript.valueToCode(block, 'command', Blockly.JavaScript.ORDER_ATOMIC)
let code = 'events.sendCommand(' + itemName + ', ' + command + ');\n'
return code
}
Blockly.Blocks['oh_event'] = {
init: function () {
this.appendValueInput('value')
.appendField(new Blockly.FieldDropdown([['send command', 'sendCommand'], ['post update', 'postUpdate']]), 'eventType')
// .appendField('send command')
this.appendValueInput('itemName')
.appendField('to')
.setAlign(Blockly.ALIGN_RIGHT)
.setCheck('String')
this.setInputsInline(true)
this.setPreviousStatement(true, null)
this.setNextStatement(true, null)
this.setColour(0)
this.setTooltip('Send a command to an item')
this.setHelpUrl('')
}
}
Blockly.JavaScript['oh_event'] = function (block) {
const eventType = block.getFieldValue('eventType')
const itemName = Blockly.JavaScript.valueToCode(block, 'itemName', Blockly.JavaScript.ORDER_ATOMIC)
const value = Blockly.JavaScript.valueToCode(block, 'value', Blockly.JavaScript.ORDER_ATOMIC)
let code = 'events.' + eventType + '(' + itemName + ', ' + value + ');\n'
return code
}
Blockly.Blocks['oh_print'] = {
init: function () {
this.appendValueInput('message')
// .setCheck('String')
.appendField('print')
this.setPreviousStatement(true, null)
this.setNextStatement(true, null)
this.setColour(0)
this.setTooltip('Print a message on the console')
this.setHelpUrl('')
}
}
Blockly.JavaScript['oh_print'] = function (block) {
const message = Blockly.JavaScript.valueToCode(block, 'message', Blockly.JavaScript.ORDER_ATOMIC)
let code = 'print(' + message + ');\n'
return code
}
Blockly.Blocks['oh_log'] = {
init: function () {
this.appendValueInput('message')
.setCheck('String')
.appendField('log')
.appendField(new Blockly.FieldDropdown([['error', 'error'], ['warn', 'warn'], ['info', 'info'], ['debug', 'debug'], ['trace', 'trace']]), 'severity')
this.setPreviousStatement(true, null)
this.setNextStatement(true, null)
this.setColour(0)
this.setTooltip('Write a message in the openHAB log')
this.setHelpUrl('')
}
}
Blockly.JavaScript['oh_log'] = function (block) {
const loggerName = Blockly.JavaScript.provideFunction_(
'logger',
['var ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + ' = Java.type(\'org.slf4j.LoggerFactory\').getLogger(\'org.openhab.rule.\' + ctx.ruleUID);'])
const message = Blockly.JavaScript.valueToCode(block, 'message', Blockly.JavaScript.ORDER_ATOMIC)
const severity = block.getFieldValue('severity')
const code = loggerName + '.' + severity + '(' + message + ');\n'
return code
}
}