GithubHelp home page GithubHelp logo

jonwyett / jw-gate Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 22 KB

Creates a "Gate" object with multiple locks that can be locked and unlocked. Useful for controlling async events

License: GNU General Public License v3.0

JavaScript 100.00%

jw-gate's Introduction

gate

Creates a "Gate" object with multiple "locks" that can be locked and unlocked. Useful for controlling async events.

In addition to using this to determine when multiple async events have completed, this is also useful when something needs to happen only when multiple things are all true, particularly when interacting with the real world, such as for IoT devices.

It was originally developed to help control VISCA cameras where the command buffer needed to wait until multiple things were true before issuing the next command:

  • A minimum amount of time had passed.
  • The camera had returned an ACK.
  • The camera had returned that the pan/tilt action had completed.

Another example imagines an IoT device that does something like opening/closing a valve, but only under these circumstances:

  • At least 10 minutes have passed since the last open/close function.
  • At least 1 user has requested an open/close operation.
  • The remote system has confirmed the cycle is completed.
  • The remote system has confirmed that it is in a ready state. (Maybe the valve controller shouldn't be opened if it senses a dangerous environment).

Only when all 4 of these things are true will the update take place.

Example:

var jw-gate = require('jw-gate');

var valveGate = new jw-gate.Gate(
    ['time','request','safe','ready'], //these are the names of the locks
    true //it's locked to start
); 

valveGate.on('unlocked', function() {
    //Send the cycle valve command
    cycleValve();

    //re-lock the user request lock
    valveGate.lock('request',true);

    //re-lock the ready lock
    valveGate.lock('ready', true);

    //Start a time delay and lock the 'time' lock
    valveGate.lock('time', true);
    setTimeout(function() {
        valveGate.lock('time', false); //after 10 mins the time lock is unlocked
    }, 10000);
});

//Imagine the following functions:

function listenForUserRequest() {
    //since a user requested an update, unlock the request lock
    valveGate.lock('request', false); 
}

function listenForSafeState(safe) {
    if (safe) {
        //the valve has notified us that it's safe to operate
        valveGate.lock('safe', false); 
    } else {
        //not safe to operate, lock the "safe" lock
        valveGate.lock('safe', true); 
    }
}

function listenForCycleComplete() {
    //the update is complete so unlock the ready lock    
    valveGate.lock('ready', false); 
}

function cycleValve() {
    //send the command to cycle the valve
}

jw-gate's People

Contributors

jonwyett avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.