Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Examples: JS Scenarios

Dmitry Golysh edited this page Jun 19, 2016 · 2 revisions

For send some value to the device, you need to just use send function, here is a simple example:

SMART_HOUSE
    .get_api('0.0.1')
    .then(function(api) {
        api.device.get('RELAY_1').send('ON'); // Send value 'ON' to the device 'RELAY_1'
    });

For subscribe on messages from devices, you need to use api.on('message', ['device 1'] ..., for example:

SMART_HOUSE
	    .get_api('0.0.1')
	    .then(function(api){
	        api.on('message', ['temperature', 'humidity'], function() {
	            console.log('New message from temperature device', api.device.get('temperature').value);
	            console.log('New message from humidity device', api.device.get('humidity').value);
	        });
	    });

More examples:

START VIDEO THEATER

console.log('STARTING VIDEO THEATER');
SMART_HOUSE
    .get_api('0.0.1')
    .then(function(api){
      api.device.get('RELAY_2').send('ON');
      api.device.get('RELAY_3').send('OFF');
      api.device.get('servo').send('90');
  
      api.device.get('RELAY_1').send('ON'); // Start Video Theater
    });

STOP VIDEO THEATER

console.log('STOPPING VIDEO THEATER');
SMART_HOUSE
    .get_api('0.0.1')
    .then(function(api){
      api.device.get('RELAY_2').send('ON');
      api.device.get('RELAY_3').send('ON');
      api.device.get('servo').send('0');
  
      api.device.get('RELAY_1').send('OFF'); // Stop Video Theater
    });

AUTO COOLING BY TEMPERATURE

console.log('LAUNCHING AUTO COOLING');
SMART_HOUSE
    .get_api('0.0.1')
    .then(function(api){
        api.on('message', ['temperature_outside', 'temperature_inside'], function(event) {

            var temperatureOutside = api.device.get('temperature_outside');
            var temperatureInside = api.device.get('temperature_inside');
            var windowServo = api.device.get('servo');
            var fan = api.device.get('RELAY_2');
            
            var ACTIVATION_TEMPERATURE = 22;
            var DEACTIVATION_TEMPERATURE = ACTIVATION_TEMPERATURE - 4;
            
            var WINDOWS_OPEN_VALUE = '10';
            var WINDOWS_CLOSE_VALUE = '80';
            
            // cooling
            console.log('MESSAGE RECEIVED');
            if (temperatureInside.value > ACTIVATION_TEMPERATURE) { // << TODO: calibrate 
              console.log('ONNING');
            
              if (temperatureOutside.value < temperatureInside.value) {
                  // open windows
                  console.log('opening windows');
                  fan.send('OFF');
                  windowServo.send(WINDOWS_OPEN_VALUE);
              } else {
                  // venting
                  console.log('activating fan');
                  windowServo.send(WINDOWS_CLOSE_VALUE);
                  fan.send('ON');
              }
            
            }
            
            if (temperatureInside.value < DEACTIVATION_TEMPERATURE) { // << TODO: calibrate 
                console.log('OFFING');
                windowServo.send(WINDOWS_CLOSE_VALUE); // close windows
                fan.send('OFF');
            }

        });
    });
Clone this wiki locally