Skip to content

Commit 6e06ffe

Browse files
committed
Added option to play sound via message / blockly
1 parent 3e30a1a commit 6e06ffe

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Buy here: [Aliexpress.com](https://haus-auto.com/p/ali/UlanziTC001) or here: [ul
3838
### **WORK IN PROGRESS**
3939

4040
* (klein0r) Automatically delete unknown or old apps
41+
* (klein0r) Added option to play sound via message / blockly
4142

4243
### 0.0.9 (2023-06-06)
4344

admin/blockly.js

+59
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ Blockly.Words['awtrix-light_timer'] = {
4343
uk: 'Awtrix-light Таймер',
4444
'zh-cn': 'Awtrix-light 时间',
4545
};
46+
Blockly.Words['awtrix-light_playsound'] = {
47+
en: 'Play sound',
48+
de: 'Sound spielen',
49+
ru: 'Играть звук',
50+
pt: 'Tocar som',
51+
nl: 'Speel',
52+
fr: 'Play sound',
53+
it: 'Suoni suono',
54+
es: 'Jugar sonido',
55+
pl: 'Dźwięk',
56+
uk: 'Грати звук',
57+
'zh-cn': '声音',
58+
};
4659
Blockly.Words['awtrix-light_message'] = {
4760
en: 'Message',
4861
de: 'Nachricht',
@@ -362,3 +375,49 @@ Blockly.JavaScript['awtrix-light_timer'] = function (block) {
362375

363376
return `sendTo('awtrix-light${block.getFieldValue('INSTANCE')}', 'timer', { ${objText.join(', ')} }, (res) => { if (res && res.error) { console.error(res.error); } });`;
364377
};
378+
379+
Blockly.Sendto.blocks['awtrix-light_playsound'] = '<block type="awtrix-light_playsound"><value name="INSTANCE"></value><value name="SOUND"></value></block>';
380+
381+
Blockly.Blocks['awtrix-light_playsound'] = {
382+
init: function () {
383+
const options = [];
384+
385+
if (typeof main !== 'undefined' && main.instances) {
386+
for (let i = 0; i < main.instances.length; i++) {
387+
const m = main.instances[i].match(/^system.adapter.awtrix-light.(\d+)$/);
388+
if (m) {
389+
const n = parseInt(m[1], 10);
390+
options.push(['awtrix-light.' + n, '.' + n]);
391+
}
392+
}
393+
}
394+
395+
if (!options.length) {
396+
for (let k = 0; k <= 4; k++) {
397+
options.push(['awtrix-light.' + k, '.' + k]);
398+
}
399+
}
400+
401+
options.unshift([Blockly.Translate('awtrix-light_anyInstance'), '']);
402+
403+
this.appendDummyInput('INSTANCE').appendField(Blockly.Translate('awtrix-light_playsound')).appendField(new Blockly.FieldDropdown(options), 'INSTANCE');
404+
this.appendValueInput('SOUND').appendField(Blockly.Translate('awtrix-light_sound'));
405+
406+
this.setInputsInline(false);
407+
this.setPreviousStatement(true, null);
408+
this.setNextStatement(true, null);
409+
410+
this.setColour(Blockly.Sendto.HUE);
411+
this.setTooltip(Blockly.Translate('awtrix-light_tooltip'));
412+
this.setHelpUrl(Blockly.Translate('awtrix-light_help'));
413+
},
414+
};
415+
416+
Blockly.JavaScript['awtrix-light_playsound'] = function (block) {
417+
const sound = Blockly.JavaScript.valueToCode(block, 'SOUND', Blockly.JavaScript.ORDER_ATOMIC);
418+
419+
const objText = [];
420+
sound && objText.push('sound: ' + sound);
421+
422+
return `sendTo('awtrix-light${block.getFieldValue('INSTANCE')}', 'sound', { ${objText.join(', ')} }, (res) => { if (res && res.error) { console.error(res.error); } });`;
423+
};

main.js

+13
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,19 @@ class AwtrixLight extends utils.Adapter {
257257
} else {
258258
this.sendTo(obj.from, obj.command, { error: 'API is not connected (device offline ?)' }, obj.callback);
259259
}
260+
} else if (obj.command === 'sound' && typeof obj.message === 'object') {
261+
// Sound
262+
if (this.apiConnected) {
263+
this.buildRequestAsync('sound', 'POST', obj.message)
264+
.then((response) => {
265+
this.sendTo(obj.from, obj.command, { error: null, data: response.data }, obj.callback);
266+
})
267+
.catch((error) => {
268+
this.sendTo(obj.from, obj.command, { error }, obj.callback);
269+
});
270+
} else {
271+
this.sendTo(obj.from, obj.command, { error: 'API is not connected (device offline ?)' }, obj.callback);
272+
}
260273
} else {
261274
this.log.error(`[onMessage] Received incomplete message via "sendTo"`);
262275

0 commit comments

Comments
 (0)