Skip to content

Commit

Permalink
version 0.20
Browse files Browse the repository at this point in the history
- Add action to change the value of a device variable.
  • Loading branch information
vosmont committed Apr 8, 2019
1 parent 7f52545 commit b742b4a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 30 deletions.
3 changes: 2 additions & 1 deletion luup files/J_ALTUI_RulesEngine1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* This file is part of the plugin RulesEngine.
* https://github.com/vosmont/Vera-Plugin-RulesEngine
* Copyright (c) 2016 Vincent OSMONT
* Copyright (c) 2019 Vincent OSMONT
* This code is released under the MIT License, see LICENSE.
*/

Expand Down Expand Up @@ -107,6 +107,7 @@ var ALTUI_RulesEngine = ( function( window, undefined ) {
<block type="action_wait"></block>\
<block type="action_wait_randomly"></block>\
<block type="action_function"></block>\
<block type="action_value"></block>\
<block type="action_device"><mutation input_service=""></mutation></block>\
<category name="Templates">\
<block type="action_device"><mutation action_type="switch"></mutation></block>\
Expand Down
2 changes: 1 addition & 1 deletion luup files/J_RulesEngine1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This file is part of the plugin RulesEngine.
* https://github.com/vosmont/Vera-Plugin-RulesEngine
* Copyright (c) 2016 Vincent OSMONT
* Copyright (c) 2019 Vincent OSMONT
* This code is released under the MIT License, see LICENSE.
*/

Expand Down
129 changes: 104 additions & 25 deletions luup files/J_RulesEngine1_Blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* This file is part of the plugin RulesEngine.
* https://github.com/vosmont/Vera-Plugin-RulesEngine
* Copyright (c) 2016 Vincent OSMONT
* Copyright (c) 2019 Vincent OSMONT
* This code is released under the MIT License, see LICENSE.
*/

Expand Down Expand Up @@ -628,10 +628,10 @@ var _OPERATORS = {

var _INPUTS = {
"device": {
"id" : { "type": "deviceFilter", "isRemovable": true, "isMainField": true, "field": "deviceId", "label": "name", "before": [ "room", "type", "category" ] },
"room" : { "type": "deviceFilter", "isRemovable": true, "isMainField": true, "field": "roomId", "label": "in room", "before": [ "type", "category" ] },
"type" : { "type": "deviceFilter", "isRemovable": true, "isMainField": true, "field": "deviceType", "label": "type", "before": [ "category" ] },
"category": { "type": "deviceFilter", "isRemovable": true, "isMainField": true, "field": "category", "label": "category" }
"id" : { "type": "deviceFilter", "isRemovable": true, "isMainField": true, "field": "deviceId", "label": "name", "before": [ "room", "type", "category" ] },
"room" : { "type": "deviceFilter", "isRemovable": true, "isMainField": true, "field": "roomId", "label": "in room", "before": [ "type", "category" ] },
"type" : { "type": "deviceFilter", "isRemovable": true, "isMainField": true, "field": "deviceType", "label": "type", "before": [ "category" ] },
"category": { "type": "deviceFilter", "isRemovable": true, "isMainField": true, "field": "category", "label": "category" }
},
"condition": {
"ADD" : { "type": "value", "autoCreate": false, "isRemovable": true, "isMultiple": true, "counter": "items", "label": "", "align": "ALIGN_RIGHT", "check": [ "Boolean" ], "before": [ "actions" ] },
Expand All @@ -647,14 +647,18 @@ var _INPUTS = {
"timer_relative": { "type": "dropdown", "isMainField": true, "isRemovable": true, "field": "timerRelative", "options": [ [ "before sunrise", "1" ], [ "after sunrise", "2" ], [ "before sunset", "3" ], [ "after sunset", "4" ] ], "label": "", "before": [ "params" ] }
},
"action": {
"service": { "type": "deviceFilter", "isMainField": true, "field": "service", "label": "service", "before": [ "action", "action_params", "device" ] },
"action" : { "type": "deviceFilter", "isMainField": true, "field": "action", "label": "action", "before":[ "action_auto_params", "params", "device" ] },
"params" : { "type": "deviceFilter", "isRemovable": true, "field": "params", "label": "with params" }
"value_service" : { "type": "deviceFilter", "isMainField": true, "field": "service", "label": "service", "before": [ "value_variable", "value", "device" ] },
"value_variable": { "type": "deviceFilter", "isMainField": true, "field": "variable", "label": "variable", "before": [ "value", "device" ] },
"value" : { "type": "value", "isMainField": true, "field": "value", "label": "value", "before": [ "device" ] },
"service" : { "type": "deviceFilter", "isMainField": true, "field": "service", "label": "service", "before": [ "variable", "action", "action_params", "device" ] },
"variable": { "type": "deviceFilter", "isMainField": true, "field": "variable", "label": "variable", "before": [ "value", "device" ] },
"action" : { "type": "deviceFilter", "isMainField": true, "field": "action", "label": "action", "before": [ "action_auto_params", "params", "device" ] },
"params" : { "type": "deviceFilter", "isRemovable": true, "field": "params", "label": "with params" }
},
"action_group": {
"description": { "type": "value", "isRemovable": true, "label": "description", "align": "ALIGN_RIGHT", "check": [ "String" ], "before": [ "params", "conditions", "do" ] },
"description": { "type": "value", "isRemovable": true, "label": "description", "align": "ALIGN_RIGHT", "check": [ "String" ], "before": [ "params", "conditions", "do" ] },
"params" : { "type": "value", "isRemovable": true, "label": "with params", "align": "ALIGN_RIGHT", "check": [ "ActionParams", "ActionParam" ], "before": [ "conditions", "do" ] },
"condition" : { "type": "value", "isRemovable": true, "label": "if", "align": "ALIGN_RIGHT", "check": "Boolean", "before": [ "do" ] }
"condition" : { "type": "value", "isRemovable": true, "label": "if", "align": "ALIGN_RIGHT", "check": "Boolean", "before": [ "do" ] }
}
};

Expand Down Expand Up @@ -843,7 +847,7 @@ function _createDeviceFilterInput( inputName, params, onChange ) {
onChange.call( thatBlock, inputName, newValue );
}
//_setDeviceExternalFilters.call( thatBlock, thatBlock.filters_ );
} ),
}),
fieldName
);
}
Expand Down Expand Up @@ -988,6 +992,39 @@ function _updateDeviceFilterInput( inputName, filters, filteredDevices ) {
};
sortOptions = _sortOptionsByName;
break;
case "action_value_variable":
fillOptions = function( devices ) {
$.each( devices, function( i, device ) {
$.each( device.states, function( j, state ) {
if ( !_isEmpty( filters.action_value_service ) && ( state.service !== filters.action_value_service ) ) {
return;
}
if ( !indexValues[ state.variable ] ) {
indexValues[ state.variable ] = true;
options.push( [ state.variable, state.variable ] );
}
} );
} );
};
sortOptions = _sortOptionsByName;
break;
case "action_value_service":
fillOptions = function( devices ) {
$.each( devices, function( i, device ) {
$.each( device.states, function( j, state ) {
if ( !_isEmpty( filters.action_value_variable ) && ( state.variable !== filters.action_value_variable ) ) {
return;
}
if ( !indexValues[ state.service ] ) {
indexValues[ state.service ] = true;
options.push( [ state.service.substr( state.service.lastIndexOf( ":" ) + 1 ), state.service ] );
//options.push( [ state.service, state.service ] );
}
} );
} );
};
sortOptions = _sortOptionsByName;
break;
case "action_action":
fillOptions = function( devices ) {
$.each( devices, function( i, device ) {
Expand Down Expand Up @@ -1292,10 +1329,6 @@ function _updateMutationWithRemovableInputs( container ) {
}
}
function _createInputsFromMutation( xmlElement, onChange ) {
// TODO : remove in 0.11
var pluginVersion = $( "#rulesengine-blockly-workspace" ).data( "plugin_version" );
var ruleVersion = $( "#rulesengine-blockly-workspace" ).data( "rule_version" );
var ruleId = $( "#rulesengine-blockly-workspace" ).data( "rule_id" );
var inputs = xmlElement.getAttribute( "inputs" );
if ( inputs ) {
inputs = inputs.split( "," );
Expand All @@ -1320,16 +1353,6 @@ function _createInputsFromMutation( xmlElement, onChange ) {
}

}
} else if ( ruleId && ( pluginVersion === "0.10" ) && ( ruleVersion !== "0.10" ) && ( inputs !== "" ) ) {
// TODO : temporary
// Compliance with older version (some inputs are constructed by mutator now)
for ( var i = 0; i < this.inputs_.length; i++ ) {
var inputName = this.inputs_[ i ];
var inputParams = _getInputParam.call( this, inputName );
if ( inputParams && inputParams.isRemovable ) {
_createInput.call( this, inputName, null, onChange );
}
}
}
}

Expand Down Expand Up @@ -3380,6 +3403,7 @@ Blockly.Blocks[ "controls_action_group_condition" ] = {

Blockly.Msg.ACTION_WAIT_TOOLTIP = "Wait a defined time.";
Blockly.Msg.ACTION_FUNCTION_TOOLTIP = "Execute LUA code.";
Blockly.Msg.ACTION_VALUE_TOOLTIP = "Set the value of a device variable.";
Blockly.Msg.ACTION_DEVICE_TOOLTIP = "Execute an action of a device.";
Blockly.Msg.ACTION_SCENE_TOOLTIP = "Execute a scene.";
Blockly.Msg.ACTION_MQTT_TOOLTIP = "Publish an MQTT message.";
Expand Down Expand Up @@ -3431,6 +3455,61 @@ Blockly.Blocks['action_function'] = {
}
};

Blockly.Blocks['action_value'] = {
init: function () {
this.setColour( Blockly.Blocks.actions.HUE2 );
this.prefix_ = "action";
this.filters_ = {};
this.params_ = {};

this.inputs_ = [ "value_service", "value_variable", "value" ];

_createInput.call( this, "value_service", {}, this.setDeviceExternalFilters );
_createInput.call( this, "value_variable", {}, this.setDeviceExternalFilters );

this.appendDummyInput()
.appendField( "value" )
.appendField( new Blockly.FieldTextInput( "" ), "value" );

this.appendValueInput( "device" )
.appendField( "device", "actionDeviceLabel" )
.setAlign( Blockly.ALIGN_RIGHT )
.setCheck( [ "Devices", "Device" ] );

this.setInputsInline( false );
this.setPreviousStatement( true, "ActionType" );
this.setNextStatement( true, "ActionType" );
this.setTooltip( Blockly.Msg.ACTION_VALUE_TOOLTIP );
},

validate: function() {
if ( this.hasDeviceFilterInputs_ ) {
_updateDeviceFilterInputs.call( this,
{
"action_service": this.params_.input_service || this.params_.service,
"action_variable" : this.params_.input_variable || this.params_.variable
}
);
}
var thatBlock = this;
$.each( _getLinkedDeviceBlocks.call( this ), function( i, deviceBlock ) {
deviceBlock.parent_ = thatBlock;
} );
this.setDeviceExternalFilters();
},

getFilters: function() {
return {
"action_service": ( this.filters_.action_service || this.getFieldValue( "service" ) ),
"action_variable" : ( this.filters_.action_variable || this.getFieldValue( "variable" ) )
}
},

setDeviceExternalFilters: function() {
_setDeviceExternalFilters.call( this, this.getFilters() );
}
};

function _updateActionDeviceShape( onChange ) {
// Device type
this.getField( "actionDeviceLabel" ).text_ = ( this.params_.device_label ? this.params_.device_label : "device" );
Expand Down
31 changes: 28 additions & 3 deletions luup files/L_RulesEngine1.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--[[
This file is part of the plugin RulesEngine.
https://github.com/vosmont/Vera-Plugin-RulesEngine
Copyright (c) 2016 Vincent OSMONT
Copyright (c) 2019 Vincent OSMONT
This code is released under the MIT License, see LICENSE.
--]]

Expand All @@ -22,7 +22,7 @@ end

_NAME = "RulesEngine"
_DESCRIPTION = "Rules Engine for the Vera with visual editor"
_VERSION = "0.19.4"
_VERSION = "0.20"
_AUTHOR = "vosmont"

-- **************************************************
Expand Down Expand Up @@ -2900,6 +2900,31 @@ do
end
}

_actionTypes["action_value"] = {
init = function( action )
-- Get device ids
action.deviceIds = _getDeviceIds( action )
action.device = nil
log( "Action " .. tostring( json.encode( action ) ), "ActionType.action_value.init", 4 )
end,
check = function( action )
if not _checkParameters( action, { "deviceIds", "service", "variable" } ) then
return false
end
return true
end,
execute = function( action, context )
for _, deviceId in ipairs( action.deviceIds ) do
-- Replace current device if needed
if ( deviceId == "#deviceid#" ) then
deviceId = context.deviceId
end
History.add( action._ruleId, "RuleAction", _getItemSummary( action ) .. " - Set variable '" .. action.service .. ";" .. action.variable .. "=" .. tostring(action.value) .. "' on device #" .. tostring( deviceId ) .. "(" .. luup.devices[deviceId].description .. ")" )
luup.variable_set( action.service, action.variable, action.value, deviceId )
end
end
}

_actionTypes["action_device"] = {
init = function( action )
--action.deviceId = tonumber( action.deviceId )
Expand Down Expand Up @@ -2951,7 +2976,7 @@ do
if ( deviceId == "#deviceid#" ) then
deviceId = context.deviceId
end
-- Check first device com status
-- First check the device comm status
if ( not luup.is_ready( deviceId ) or ( Variable.get( deviceId, VARIABLE.COMM_FAILURE ) == "1" ) ) then
Rule.addError( action._ruleId, "Execute rule actions", "Device #" .. tostring( deviceId ) .. " is not ready or has a com failure" )
end
Expand Down

0 comments on commit b742b4a

Please sign in to comment.