Public Documentation
  • Introduction
  • Getting started
    • Introduction
    • 1. Welcome Page
    • 2. Introduction (Beginner's Guide)
  • Portal Tools
    • Blocko
      • Ůvod do blocka
      • Flip-flop
      • REST-API bloček
    • Grid
      • Úvod do GRID
      • Základní Tlačítko (základy GRID)
      • vylepšený Přepínač (stylování a pokročilé možnosti GRID)
      • text generátor button (messeage type, two elements one widget)
      • graf (pokročilé funkce GRID)
      • Slider (User class v GRIDu)
      • Styly a jejich použití
  • Cloud
    • Cloud
    • Instancies
    • Rest-Api
  • General
    • What we plan
  • Hardware a programování
    • Úvod
      • Nahrání prvního programu
    • Konektivita
      • Specifikace zdroje internetu
      • GSM
      • 6LowPAN
      • Komunikace s portálem
      • Přepínání mezi servery
    • Hardware
      • Základní jednotky
        • IODAG3E
          • Rozhraní a periférie
          • Konektor X a Y
          • Napájení
          • Připojení zdroje k VBAT
          • Paměti
          • Technické specifikace
          • Schémata
        • IODAG3L
      • Rozšiřující moduly
        • GSM shield
        • LED shield
        • Relay shield
        • RFID shield
        • Ultrasonic shield
        • Meteo shield
        • Movement shield
        • PIR shield
        • RGB Displej shield
        • Serial shield
      • Ostatní
        • DevKitG3
          • Schéma
        • TK3G
          • Schéma
        • ZPPG3
          • Schéma
        • WEXP
    • Programování HW
      • Architektura FW
        • Aktualizace FW
        • Autobackup
      • Struktura programu
      • Byzance API
        • Vstupy a výstupy do Portálu
        • Callback Busy
        • Odložený restart
        • Callbacky připojení
        • Uživatelská makra
      • MBED API
        • Vstupy a výstupy
        • Komunikační rozhraní
        • Časování
        • RTOS
      • Knihovny
        • DHT
        • DS1820
        • Dali
        • EdgeStruct
        • I2CWrapper
        • Knock
        • MFRC522
        • MFRC522::MIFARE_Key
        • MPU9150
        • ms5637
        • OneWire
        • PCA9536
        • RGB_matrix
        • RollerDrive
        • SHT21
        • ST7565
        • Servo
        • TCA6424A
        • TLC59116
        • TripleSevenSeg
        • MFRC522::Uid
        • WS2812
        • WakeUp
      • Offline programování
        • Vlastní IDE
        • Upload kódu z GUI
        • Upload kódu z konzole
        • Upload kódu Drag&drop
    • Tutoriály
      • Datum a čas (RTC)
      • Komunikace po sériové lince (UART) s PC
        • Konfigurace sériové linky v PC
        • Programování sériové linky
      • Základní tutoriály
        • Digital Read
        • Analog Read/Write
        • PWM Fade
        • Timer/Ticker
        • State Change Detection
        • Ovládání LED modulu
        • BusOut
        • HC-SR04 Senzor
      • Pokročilé
    • Správa a diagnostika
      • Zdroj restartu
      • LED modul
      • Identifikace zařízení
      • Monitoring parametrů
      • Vytížení zařízení
      • Webové rozhraní
        • Základní přehled
        • Správce firmware
        • Vlákna
        • Definované vstupy
        • Konfigurace MQTT
        • Nastavení
      • Bootloader
        • Režimy bootloaderu
        • Command mód
        • Další vlastnosti bootloaderu
      • Webová konzole
      • Konfigurace zařízení
        • Confighash
      • Omezení konfigurace
        • Konfigurace
        • Binárky
        • Omezení MQTT
        • Lowpan
    • Knowledge base
      • Náhodná čísla
      • Watchdog
      • Paměť RAM
Powered by GitBook
On this page
  1. Portal Tools
  2. Blocko

REST-API bloček

Později bude upraveno, zatím jenom slouží jako záloha

const digitalInputsCount  = 0;
const analogInputsCount   = 0;

const sendWhenOptions = {
    trigger: 'Trigger',
    input_changed: 'Input changed'
};

// init inputs
let trigger = context.inputs.add('trigger', 'digital', 'Send trigger');

for (let d = 1; d <= digitalInputsCount; d++) {
    context.inputs.add('din' + d, 'digital', 'Digital input #' + d);
}

for (let a = 1; a <= analogInputsCount; a++) {
    context.inputs.add('ain' + a, 'analog', 'Analog input #' + a);
}

// init outputs
let response = context.outputs.add('response', 'message', 'Response', ['integer', 'string', 'string']);

// init config properties
context.configProperties.description = `
There is **2 variants for trigger** sending request:

* **Trigger** - sends request when *trigger* input changes from **false** to **true**
* **Input changed** - sends request immedietly or after **debounce time** when any of *din1* - *din${digitalInputsCount}* or *ain1* - *ain${analogInputsCount}* changes value

In **URL**, **Headers** and **Request body** fields you can use following *micro*template syntax:

* \`<% din1 %>\` - will be replaced with bool value of *din1* (**true** or **false**)
* \`<% ain4 %>\` - will be replaced with numberic value of *ain4* input
* \`<% din2?"yes":"no" %>\` - will be replaced with bool value of *din2* (**yes** or **no**)
* \`<% if (din3 == true) { %> "open": true <% } %>\` - inserts **"open": true** only if value of *din3* input is **true**

---
`;

let sendWhen = context.configProperties.add('sendWhen', 'string', 'Send when', sendWhenOptions.trigger, {
    options: [sendWhenOptions.trigger, sendWhenOptions.input_changed]
});
let sendDebounceTime = context.configProperties.add('sendDebounceTime', 'integer', 'Send debounce time (in ms)', 200, {
    min: 0,
    max: 60000
});
let url = context.configProperties.add('url', 'string', 'URL', 'http://example.com/');
let method = context.configProperties.add('method', 'string', 'Method', 'POST', {
    options: ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS']
});
let headers = context.configProperties.add('headers', 'string', 'Headers (one per line)', '', {
    multiline: true
});
let body = context.configProperties.add('body', 'string', 'Request body', '', {
    multiline: true
});


// inspired by https://github.com/krasimir/absurd/blob/master/lib/processors/html/helpers/TemplateEngine.js
let TemplateEngine = (template, options) => {
    var re = /<%(.+?)%>/g, 
        reExp = /(^( )?(var|if|for|else|switch|case|break|{|}|;))(.*)?/g, 
        code = 'with(obj) { var r=[];\n', 
        cursor = 0, 
        result,
            match;
    var add = function(line, js?) {
        js? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
            (code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
        return add;
    }
    while(match = re.exec(template)) {
        add(template.slice(cursor, match.index))(match[1], true);
        cursor = match.index + match[0].length;
    }
    add(template.substr(cursor, template.length - cursor));
    code = (code + 'return r.join(""); }').replace(/[\r\t\n]/g, ' ');
    try { result = new Function('obj', code).apply(options, [options]); }
    catch(err) { console.error("'" + err.message + "'", " in \n\nCode:\n", code, "\n"); }
    return result;
}

// main send request function
let sendRequest = () => {

    let data = {};

    for (let d = 1; d <= digitalInputsCount; d++) {
        data['din' + d] = context.inputs.get<DigitalInputConnector>('din' + d).value;
    }
    for (let a = 1; a <= analogInputsCount; a++) {
        data['ain' + a] = context.inputs.get<AnalogInputConnector>('ain' + a).value;
    }

    let newUrl = TemplateEngine(url.value, data);
    let newHeaders = TemplateEngine(headers.value, data);
    let newBody = TemplateEngine(body.value, data);

    console.warn(`Do ${method.value} request to ${newUrl} with headers:\n ${newHeaders} and body:\n ${newBody} `);

    let request: RequestDef = null;
    if (method.value == 'GET') {
        request = new GetRequest(newUrl);
    } else if (method.value == 'POST') {
        request = new PostRequest(newUrl);
    } else if (method.value == 'PUT') {
        request = new PutRequest(newUrl);
    } else if (method.value == 'DELETE') {
        request = new DeleteRequest(newUrl);
    } else if (method.value == 'HEAD') {
        request = new HeadRequest(newUrl);
    } else if (method.value == 'OPTIONS') {
        request = new OptionsRequest(newUrl);
    }

    request.headers = newHeaders;
    request.body = newBody;

    services.fetchService.fetch(request)
    .then((res: FetchResponse) => {
        const message = [200, JSON.stringify(res.headers), JSON.stringify(res.body)];
        response.send(message);
        console.info('Response [' + newUrl + ', ' + res.status + ']');
    }).catch((e: Error) => {
        console.error(e);
    });

};

trigger.listenEvent('valueChanged', () => {
    if (trigger.value && sendWhen.value == sendWhenOptions.trigger) {
        console.info('Now send! [trigger]');
        sendRequest();
    }
});

let debounceTimeout = null;

context.inputs.listenEvent('valueChanged', (event) => {
    if (event.target.name != 'trigger') {
        if (sendWhen.value == sendWhenOptions.input_changed) {
            if (sendDebounceTime.value == 0) {
                console.info('Now send! [input_changed]');
                sendRequest();
            } else {
                clearTimeout(debounceTimeout);
                debounceTimeout = setTimeout(() => {
                    console.info('Now send! [input_changed_debounced]');
                    sendRequest();
                }, sendDebounceTime.value)
            }
        }
    }
});
PreviousFlip-flopNextGrid

Last updated 7 years ago