// Konstanten
let maxCyclic = 60 * 4;
let maxState = {
"-": 0,
"S": 90*4,
"D": 5*60*4,
"L": 60*60*4,
"w": 1,
"W": 12
};
// globale Variablen
let switchoffTimer = null;
let state = Shelly.getComponentStatus( "enum", 200 ).value;
let cntState = 0;
let cntCyclic = 0;
let mqttPrefix = Shelly.getComponentConfig( "mqtt" ).topic_prefix;
function showState()
{
MQTT.publish( mqttPrefix+"/status/enum:200", "{\"value\":\""+state+"\"}", 1, false );
}
function setState(aState)
{
state = aState;
cntState = maxState[aState];
showState();
}
function checkState()
{
if( state !== "-" )
{
cntState = cntState - 1;
if( cntState <= 0 )
{
if( state === "S" || state === "D" || state === "L" )
{
switchWarn1();
}
else if( state === "w" )
{
switchWarn2();
}
else
{
Shelly.call( "Enum.Set", { id:200, value:"-" }, null, null );
}
}
}
}
function switchOff()
{
print( "Output switched off" );
Shelly.call( "Switch.Set", { id:0, on:false }, null, null );
setState( "-" );
}
function switchOn(aState)
{
print( "Output switched on ", aState );
Shelly.call( "Switch.Set", { id:0, on:true }, null, null );
setState( aState );
}
function switchWarn1()
{
print( "Output switched warn 1" );
Shelly.call( "Switch.Set", { id:0, on:false }, null, null );
setState( "w" );
}
function switchWarn2()
{
print( "Output switched warn 2" );
Shelly.call( "Switch.Set", { id:0, on:true }, null, null );
setState( "W" );
}
function shortPress()
{
print( "short press" );
if( state !== "L" )
{
switchOn( "S" );
}
}
function doublePress()
{
print( "double press" );
if( state !== "L" )
{
switchOn( "D" );
}
}
function longPress()
{
print( "long press" );
if( state !== "L" )
{
switchOn( "L" );
}
else
{
Shelly.call( "Enum.Set", { id:200, value:"-" }, null, null );
}
}
// Handler
Shelly.addEventHandler(
function(event,ud)
{
//print( JSON.stringify(event) );
if( event.name === 'input' && event.id === 0 )
{
print( "Input event", event.id );
if( event.info.event === "single_push" )
{
Shelly.call( "Enum.Set", { id:200, value:"S" }, null, null );
}
else if( event.info.event === "double_push" )
{
Shelly.call( "Enum.Set", { id:200, value:"D" }, null, null );
}
else if( event.info.event === "long_push" )
{
Shelly.call( "Enum.Set", { id:200, value:"L" }, null, null );
}
}
},
null
);
Shelly.addStatusHandler(
function(event,ud)
{
//print( JSON.stringify(event) );
if( event.component === "enum:200" )
{
print(event.delta.value);
if( event.delta.value === "S" )
{
shortPress();
}
else if( event.delta.value === "D" )
{
doublePress();
}
else if( event.delta.value === "L" )
{
longPress();
}
else if( event.delta.value === "-" )
{
switchOff();
}
}
},
null
);
// Main
if( state !== "-" )
{
Shelly.call( "Enum.Set", { id:200, value:"-" }, null, null );
}
else
{
showState();
}
Timer.set( 250, true,
function(ud)
{
if( ++cntCyclic >= maxCyclic )
{
cntCyclic = 0;
showState();
}
checkState();
},
null
);