Merge pull request #20 from sensebox/dynamic-osem-dropdown
Dynamic osem dropdown
This commit is contained in:
commit
61ca2e8799
@ -1,6 +1,7 @@
|
|||||||
import * as Blockly from 'blockly/core';
|
import * as Blockly from 'blockly/core';
|
||||||
import { getColour } from '../helpers/colour';
|
import { getColour } from '../helpers/colour';
|
||||||
|
|
||||||
|
var apiData = '[{"_id":"5e6073fe57703e001bb99453","createdAt":"2020-03-05T03:37:34.151Z","updatedAt":"2020-10-17T10:49:51.636Z","name":"Vtuzgorodok","currentLocation":{"timestamp":"2020-03-05T03:37:34.146Z","coordinates":[60.658676,56.833041,51],"type":"Point"},"exposure":"outdoor","sensors":[{"title":"PM10","unit":"µg/m³","sensorType":"SDS 011","icon":"osem-cloud","_id":"5e6073fe57703e001bb99458","lastMeasurement":{"value":"3.30","createdAt":"2020-10-17T10:49:51.627Z"}},{"title":"PM2.5","unit":"µg/m³","sensorType":"SDS 011","icon":"osem-cloud","_id":"5e6073fe57703e001bb99457","lastMeasurement":{"value":"0.90","createdAt":"2020-10-17T10:49:51.627Z"}},{"title":"Temperatur","unit":"°C","sensorType":"BME280","icon":"osem-thermometer","_id":"5e6073fe57703e001bb99456","lastMeasurement":{"value":"6.58","createdAt":"2020-10-17T10:49:51.627Z"}},{"title":"rel. Luftfeuchte","unit":"%","sensorType":"BME280","icon":"osem-humidity","_id":"5e6073fe57703e001bb99455","lastMeasurement":{"value":"53.76","createdAt":"2020-10-17T10:49:51.627Z"}},{"title":"Luftdruck","unit":"Pa","sensorType":"BME280","icon":"osem-barometer","_id":"5e6073fe57703e001bb99454","lastMeasurement":{"value":"96937.66","createdAt":"2020-10-17T10:49:51.627Z"}}],"model":"luftdaten_sds011_bme280","lastMeasurementAt":"2020-10-17T10:49:51.627Z","loc":[{"geometry":{"timestamp":"2020-03-05T03:37:34.146Z","coordinates":[60.658676,56.833041,51],"type":"Point"},"type":"Feature"}]}]';
|
||||||
|
|
||||||
Blockly.Blocks['sensebox_osem_connection'] = {
|
Blockly.Blocks['sensebox_osem_connection'] = {
|
||||||
init: function () {
|
init: function () {
|
||||||
@ -27,9 +28,14 @@ Blockly.Blocks['sensebox_osem_connection'] = {
|
|||||||
this.setNextStatement(true, null);
|
this.setNextStatement(true, null);
|
||||||
},
|
},
|
||||||
onchange: function (e) {
|
onchange: function (e) {
|
||||||
|
let boxID = this.getFieldValue('BoxID');
|
||||||
//Blockly.Blocks.sensebox.getDescendants = blocks;
|
if (boxID !== 'senseBox ID') {
|
||||||
|
fetch('https://api.opensensemap.org/boxes/ ' + boxID)
|
||||||
|
.then(res => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
apiData = data;
|
||||||
|
})
|
||||||
|
}
|
||||||
},
|
},
|
||||||
mutationToDom: function () {
|
mutationToDom: function () {
|
||||||
var container = document.createElement('mutation');
|
var container = document.createElement('mutation');
|
||||||
@ -73,25 +79,47 @@ Blockly.Blocks['sensebox_osem_connection'] = {
|
|||||||
};
|
};
|
||||||
Blockly.Blocks['sensebox_send_to_osem'] = {
|
Blockly.Blocks['sensebox_send_to_osem'] = {
|
||||||
init: function () {
|
init: function () {
|
||||||
|
|
||||||
this.setTooltip(Blockly.Msg.senseBox_send_to_osem_tip);
|
this.setTooltip(Blockly.Msg.senseBox_send_to_osem_tip);
|
||||||
this.setHelpUrl('');
|
this.setHelpUrl('');
|
||||||
this.setColour(getColour().sensebox);
|
this.setColour(getColour().sensebox);
|
||||||
this.appendDummyInput()
|
this.appendDummyInput()
|
||||||
.appendField(Blockly.Msg.senseBox_send_to_osem);
|
.appendField(Blockly.Msg.senseBox_send_to_osem);
|
||||||
this.appendValueInput('Value')
|
this.appendValueInput('Value')
|
||||||
.setCheck(null)
|
.appendField('Phänomen')
|
||||||
.appendField('Sensor ID')
|
.appendField(new Blockly.FieldDropdown(
|
||||||
.appendField(new Blockly.FieldTextInput('Sensor ID'), 'SensorID');
|
this.generateOptions), 'SensorID');
|
||||||
this.setPreviousStatement(true, null);
|
this.setPreviousStatement(true, null);
|
||||||
this.setNextStatement(true, null);
|
this.setNextStatement(true, null);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
generateOptions: function () {
|
||||||
|
var options = [['', '']];
|
||||||
|
if (apiData.sensors != undefined) {
|
||||||
|
for (var i = 0; i < apiData.sensors.length; i++) {
|
||||||
|
options.push([apiData.sensors[i].title, apiData.sensors[i]._id]);
|
||||||
|
}
|
||||||
|
console.log(options);
|
||||||
|
|
||||||
|
}
|
||||||
|
if (options.length > 1) {
|
||||||
|
|
||||||
|
var dropdown = options.slice(1)
|
||||||
|
console.log(dropdown);
|
||||||
|
return dropdown;
|
||||||
|
} else
|
||||||
|
return options;
|
||||||
|
|
||||||
|
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* Called whenever anything on the workspace changes.
|
* Called whenever anything on the workspace changes.
|
||||||
* Add warning if block is not nested inside a the correct loop.
|
* Add warning if block is not nested inside a the correct loop.
|
||||||
* @param {!Blockly.Events.Abstract} e Change event.
|
* @param {!Blockly.Events.Abstract} e Change event.
|
||||||
* @this Blockly.Block
|
* @this Blockly.Block
|
||||||
*/
|
*/
|
||||||
onchange: function (e) {
|
onchange: function () {
|
||||||
|
|
||||||
var legal = false;
|
var legal = false;
|
||||||
// Is the block nested in a loop?
|
// Is the block nested in a loop?
|
||||||
var block = this;
|
var block = this;
|
||||||
@ -108,5 +136,10 @@ Blockly.Blocks['sensebox_send_to_osem'] = {
|
|||||||
this.setWarningText(Blockly.Msg.CONTROLS_FLOW_STATEMENTS_WARNING);
|
this.setWarningText(Blockly.Msg.CONTROLS_FLOW_STATEMENTS_WARNING);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
LOOP_TYPES: ['sensebox_osem_connection'],
|
/**
|
||||||
|
* List of block types that are loops and thus do not need warnings.
|
||||||
|
* To add a new loop type add this to your code:
|
||||||
|
* Blockly.Blocks['controls_flow_statements'].LOOP_TYPES.push('custom_loop');
|
||||||
|
*/
|
||||||
|
LOOP_TYPES: ['sensebox_osem_connection']
|
||||||
};
|
};
|
@ -18,17 +18,19 @@ Blockly.Arduino.sensebox_osem_connection = function (Block) {
|
|||||||
var box_id = this.getFieldValue('BoxID');
|
var box_id = this.getFieldValue('BoxID');
|
||||||
var host = this.getFieldValue('host');
|
var host = this.getFieldValue('host');
|
||||||
var branch = Blockly.Arduino.statementToCode(Block, 'DO');
|
var branch = Blockly.Arduino.statementToCode(Block, 'DO');
|
||||||
//var blocks = Blockly.Block.getDescendants;
|
var blocks = this.getDescendants();
|
||||||
var type = this.getFieldValue('type');
|
var type = this.getFieldValue('type');
|
||||||
var ssl = this.getFieldValue('SSL');
|
var ssl = this.getFieldValue('SSL');
|
||||||
var port = 0;
|
var port = 0;
|
||||||
var count = 0;
|
var count = 0;
|
||||||
// for (var i = 0; i < blocks.length; i++) {
|
if (blocks != undefined) {
|
||||||
// if (blocks[i].type === 'sensebox_send_to_osem') {
|
for (var i = 0; i < blocks.length; i++) {
|
||||||
// count++;
|
if (blocks[i].type === 'sensebox_send_to_osem') {
|
||||||
|
count++;
|
||||||
|
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
var num_sensors = count;
|
var num_sensors = count;
|
||||||
Blockly.Arduino.libraries_['library_senseBoxMCU'] = '#include "SenseBoxMCU.h"';
|
Blockly.Arduino.libraries_['library_senseBoxMCU'] = '#include "SenseBoxMCU.h"';
|
||||||
Blockly.Arduino.definitions_['num_sensors'] = 'static const uint8_t NUM_SENSORS = ' + num_sensors + ';'
|
Blockly.Arduino.definitions_['num_sensors'] = 'static const uint8_t NUM_SENSORS = ' + num_sensors + ';'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user