Sources/com.elgato.philips-hue.sdPlugin/plugin/js/powerAction.js
// Prototype which represents a power action
Prototype which represents a power action
Prototype which represents a power action
function PowerAction(inContext, inSettings) {
  var instance = this;
  var priority = inSettings.priority;
  Action.call(this, inContext, inSettings);
  updateState();
  this.onKeyUp = function (inContext, inSettings, inCoordinates, inUserDesiredState, inState) {
    if (priority == 'Dummy') {
      console.log('Button pressed: ' + priority);
      return;
    } else if (priority == 'Black') {
      console.log('Button pressed: ' + priority);
    } else if ((priority == 'Schauspieler' || priority == 'Rescue') && erzählerActive) {
      console.log('Button pressed: ' + priority + ', but the Erzähler is active: ' + erzählerActive);
      return;
    } else {
      console.log('Ok, priority on Keyup: ' + priority);
    }
    if (!('bridge' in inSettings)) {
      log('No bridge configured');
      showAlert(inContext);
      return;
    }
    if (!(inSettings.bridge in cache.data)) {
      log('Bridge ' + inSettings.bridge + ' not found in cache');
      showAlert(inContext);
      return;
    }
    var bridgeCache = cache.data[inSettings.bridge];
    if (!('light' in inSettings)) {
      log('No light or group configured');
      showAlert(inContext);
      return;
    }
    if (!(inSettings.light in bridgeCache.lights || inSettings.light in bridgeCache.groups)) {
      log('Light or group ' + inSettings.light + ' not found in cache');
      showAlert(inContext);
      return;
    }
    var bridge = new Bridge(bridgeCache.ip, bridgeCache.id, bridgeCache.username);
    var objCache, obj;
    if (inSettings.light.indexOf('l-') !== -1) {
      objCache = bridgeCache.lights[inSettings.light];
      obj = new Light(bridge, objCache.id);
    } else {
      objCache = bridgeCache.groups[inSettings.light];
      obj = new Group(bridge, objCache.id);
    }
    var targetState;
    if (inUserDesiredState !== undefined) {
      targetState = !inUserDesiredState;
    } else {
      targetState = !objCache.power;
    }
    if (priority == 'Black') {
      targetState = false;
    }
    obj.setPower(targetState, function (success, error) {
      if (success) {
        setActionState(inContext, targetState ? 0 : 1);
        objCache.power = targetState;
        console.log('targetState: ' + targetState);
        if (priority == 'Erzähler' || (priority == 'Rescue' && !erzählerActive)) {
          erzählerActive = targetState;
          console.log('erzählerActive changed to: ' + erzählerActive);
        }
      } else {
        log(error);
        setActionState(inContext, inState);
        showAlert(inContext);
      }
    });
    if (priority == 'Erzähler' || (priority == 'Rescue' && !erzählerActive)) {
      var blackSettings = JSON.parse('{"bridge":"ecb5fafffe830ed7","light":"g-3"}');
      var bridge2 = new Bridge(bridgeCache.ip, bridgeCache.id, bridgeCache.username);
      var objCache2, obj2;
      if (blackSettings.light.indexOf('l-') !== -1) {
        objCache2 = bridgeCache.lights[blackSettings.light];
        obj2 = new Light(bridge2, objCache2.id);
      } else {
        objCache2 = bridgeCache.groups[blackSettings.light];
        obj2 = new Group(bridge2, objCache2.id);
      }
      obj2.setPower(false, function (success, error) {
        if (success) {
          setActionState(inContext, false);
          objCache2.power = false;
        } else {
          log(error);
          setActionState(inContext, inState);
          showAlert(inContext);
        }
      });
    }
  };
  var actionNewCacheAvailable = this.newCacheAvailable;
  this.newCacheAvailable = function (inCallback) {
    actionNewCacheAvailable.call(instance, function () {
      updateState();
      inCallback();
    });
  };
  function updateState() {
    var settings = instance.getSettings();
    var context = instance.getContext();
    if (!('bridge' in settings)) {
      return;
    }
    if (!(settings.bridge in cache.data)) {
      return;
    }
    var bridgeCache = cache.data[settings.bridge];
    if (!('light' in settings)) {
      return;
    }
    if (!(settings.light in bridgeCache.lights || settings.light in bridgeCache.groups)) {
      return;
    }
    var objCache;
    if (settings.light.indexOf('l-') !== -1) {
      objCache = bridgeCache.lights[settings.light];
    } else {
      objCache = bridgeCache.groups[settings.light];
    }
    var targetState = objCache.power;
    setActionState(context, targetState ? 0 : 1);
    if ('priority' in settings) {
      priority = settings.priority;
    }
  }
  function setActionState(inContext, inState) {
    setState(inContext, inState);
  }
  this.setPriority = function (overrideValue) {
    priority = JSON.parse(overrideValue);
    console.log('Priority of action ' + inContext + ' set to ' + priority);
  }
}
function PowerAction ( inContext , inSettings ) { var instance = this ; var priority = inSettings . priority ; Action . call ( this , inContext , inSettings ) ; updateState ( ) ; this . onKeyUp = function ( inContext , inSettings , inCoordinates , inUserDesiredState , inState ) { if ( priority == 'Dummy' ) { console . log ( 'Button pressed: ' + priority ) ; return ; } else if ( priority == 'Black' ) { console . log ( 'Button pressed: ' + priority ) ; } else if ( ( priority == 'Schauspieler' || priority == 'Rescue' ) && erzählerActive) console . log ( 'Button pressed: ' + priority + ', but the Erzähler is active: '  rzählerActive); return ; } else { console . log ( 'Ok, priority on Keyup: ' + priority ) ; } if ( ! ( 'bridge' in inSettings ) ) { log ( 'No bridge configured' ) ; showAlert ( inContext ) ; return ; } if ( ! ( inSettings . bridge in cache . data ) ) { log ( 'Bridge ' + inSettings . bridge + ' not found in cache' ) ; showAlert ( inContext ) ; return ; } var bridgeCache = cache . data [ inSettings . bridge ] ; if ( ! ( 'light' in inSettings ) ) { log ( 'No light or group configured' ) ; showAlert ( inContext ) ; return ; } if ( ! ( inSettings . light in bridgeCache . lights || inSettings . light in bridgeCache . groups ) ) { log ( 'Light or group ' + inSettings . light + ' not found in cache' ) ; showAlert ( inContext ) ; return ; } var bridge = new Bridge ( bridgeCache . ip , bridgeCache . id , bridgeCache . username ) ; var objCache , obj ; if ( inSettings . light . indexOf ( 'l-' ) !== - 1 ) { objCache = bridgeCache . lights [ inSettings . light ] ; obj = new Light ( bridge , objCache . id ) ; } else { objCache = bridgeCache . groups [ inSettings . light ] ; obj = new Group ( bridge , objCache . id ) ; } var targetState ; if ( inUserDesiredState !== undefined ) { targetState = ! inUserDesiredState ; } else { targetState = ! objCache . power ; } if ( priority == 'Black' ) { targetState = false ; } obj . setPower ( targetState , function ( success , error ) { if ( success ) { setActionState ( inContext , targetState ? 0 : 1 ) ; objCache . power = targetState ; console . log ( 'targetState: ' + targetState ) ; if ( priority == 'Erzähler'  |  p riority  =  Rescue'  &  e rzählerActive)) { erzählerActive  argetState; console . log ( 'erzählerActive changed to: '  rzählerActive); } } else { log ( error ) ; setActionState ( inContext , inState ) ; showAlert ( inContext ) ; } } ) ; if ( priority == 'Erzähler'  |  p riority  =  Rescue'  &  e rzählerActive)) { var blackSettings = JSON . parse ( '{"bridge":"ecb5fafffe830ed7","light":"g-3"}' ) ; var bridge2 = new Bridge ( bridgeCache . ip , bridgeCache . id , bridgeCache . username ) ; var objCache2 , obj2 ; if ( blackSettings . light . indexOf ( 'l-' ) !== - 1 ) { objCache2 = bridgeCache . lights [ blackSettings . light ] ; obj2 = new Light ( bridge2 , objCache2 . id ) ; } else { objCache2 = bridgeCache . groups [ blackSettings . light ] ; obj2 = new Group ( bridge2 , objCache2 . id ) ; } obj2 . setPower ( false , function ( success , error ) { if ( success ) { setActionState ( inContext , false ) ; objCache2 . power = false ; } else { log ( error ) ; setActionState ( inContext , inState ) ; showAlert ( inContext ) ; } } ) ; } } ; var actionNewCacheAvailable = this . newCacheAvailable ; this . newCacheAvailable = function ( inCallback ) { actionNewCacheAvailable . call ( instance , function ( ) { updateState ( ) ; inCallback ( ) ; } ) ; } ; function updateState ( ) { var settings = instance . getSettings ( ) ; var context = instance . getContext ( ) ; if ( ! ( 'bridge' in settings ) ) { return ; } if ( ! ( settings . bridge in cache . data ) ) { return ; } var bridgeCache = cache . data [ settings . bridge ] ; if ( ! ( 'light' in settings ) ) { return ; } if ( ! ( settings . light in bridgeCache . lights || settings . light in bridgeCache . groups ) ) { return ; } var objCache ; if ( settings . light . indexOf ( 'l-' ) !== - 1 ) { objCache = bridgeCache . lights [ settings . light ] ; } else { objCache = bridgeCache . groups [ settings . light ] ; } var targetState = objCache . power ; setActionState ( context , targetState ? 0 : 1 ) ; if ( 'priority' in settings ) { priority = settings . priority ; } } function setActionState ( inContext , inState ) { setState ( inContext , inState ) ; } this . setPriority = function ( overrideValue ) { priority = JSON . parse ( overrideValue ) ; console . log ( 'Priority of action ' + inContext + ' set to ' + priority ) ; } }
Prototype which represents a power action
Prototype which represents a power action
// Init PowerAction // Inherit from Action // Update the state // Public function called on key up event // console.log('inContext: ' + inContext); // console.log('inSettings: ' + JSON.stringify(inSettings)); // console.log('inCoordinates: ' + inCoordinates); // console.log('inUserDesiredState: ' + inUserDesiredState); // console.log('inState: ' + inState); // (_m) check priority //Dummy-Buttons haben keine Funktion //Schauspieler-Buttons deaktivieren während Erzähler aktiv ist //todo // Check if any bridge is configured // Check if the configured bridge is in the cache // Find the configured bridge // Check if any light is configured // Check if the configured light or group is in the cache // Create a bridge instance // Create a light or group object // Check for multi action // Set light or group state	 // If Erzähler, additionally switch off the Group Schauspieler // Create a bridge instance // Create a light or group object // Set light or group state			 // Before overwriting parent method, save a copy of it // Public function called when new cache is available // Call actions newCacheAvailable method // Update the state // Call the callback function // Get the settings and the context // Check if any bridge is configured // Check if the configured bridge is in the cache // Find the configured bridge // Check if a light was set for this action // Check if the configured light or group is in the cache // Find out if it is a light or a group // Set the target state // Set the new action state // (_m) set Priority from cache //console.log('Power Action/updateState()/settings is now :' + JSON.stringify(settings)); //console.log('poweraction.js: set priority from cache to ' + priority); // Private function to set the state // (_m) public function to set the priority