package org.openhab.demo

import org.openhab.core.items.*
import org.openhab.core.types.*
import org.openhab.core.library.items.*
import org.openhab.core.library.types.*
import org.openhab.core.rules.event.*
import function org.openhab.core.rules.actions.BusEvent.*
import function org.openhab.io.multimedia.actions.Audio.*
import function org.openhab.io.net.actions.XMPP.*
import function org.openhab.io.net.actions.Mail.*

/**
 * This is a demo rule which simulates a real dimmer by reacting to increase/decrease commands 
 * and posting an updated state on the bus 
 */
rule "Dimmed Light"
	when
		$event : CommandEvent(itemName=="DimmedLight")
	then
		int $percent = ($event.getItem().getState() instanceof UnDefType) ? 0 : ((PercentType) $event.getItem().getState()).toBigDecimal().intValue();
		if($event.getCommand().equals(IncreaseDecreaseType.INCREASE)) $percent = $percent + 5; 
		if($event.getCommand().equals(IncreaseDecreaseType.DECREASE)) $percent = $percent - 5;
		
		if($percent<0)   $percent = 0; 
		if($percent>100) $percent = 100;
		postUpdate($event.getItemName(), String.valueOf($percent));
end

/**
 * This rule demonstrates how a NumberItem can be used to easily control the local sound system.
 * A selection widget in the UI provides easy access for this
 */
rule "Select Radio Station"
	when
		$event : CommandEvent(itemName=="Radio_Station")
	then
		String $station = $event.getCommand().toString();
		if($station.equals("0")) playStream(null);
		if($station.equals("1")) playStream("http://metafiles.gl-systemhaus.de/hr/hr3_2.m3u");
		if($station.equals("2")) playStream("http://mp3-live.swr3.de/swr3_m.m3u");
		if($station.equals("3")) playStream("http://edge.live.mp3.mdn.newmedia.nacamar.net/radioffh/livestream.mp3.m3u");
		if($station.equals("4")) playStream("http://stream.novalnet.de:8000/partyHitMix");
end

/**
 * The following rules help initializing some demo items with some helpful states.
 */
 
rule "Initialize light states"
	when 
		$lights : GroupItem(name=="Lights", state==UnDefType.UNDEF)
	then
		for(Item item : $lights.getMembers()) {
			boolean on = Math.random() > 0.7;
			postUpdate(item.getName(), on ? "ON" : "OFF");
		}
end

rule "Initialize heating states"
	when 
		$heatings : GroupItem(name=="Heating", state==UnDefType.UNDEF)
	then
		for(Item item : $heatings.getMembers()) {
			boolean on = Math.random() > 0.8;
			postUpdate(item.getName(), on ? "ON" : "OFF");
		}
end

rule "Initialize contact states"
	when 
		$windows : GroupItem(name=="Windows", state==UnDefType.UNDEF)
	then
		for(Item item : $windows.getMembers()) {
			boolean open = Math.random() > 0.5;
			postUpdate(item.getName(), open ? "OPEN" : "CLOSED");
		}
end

rule "Initialize room temperatures"
	when 
		$temps : GroupItem(name=="Temperature", state==UnDefType.UNDEF)
	then
		for(Item item : $temps.getMembers()) {
			double tempOffset = (Math.random() * 50 - 25) / 10.0;
			postUpdate(item.getName(), Double.toString(20.0 + tempOffset));
		}
end

rule "Say time on update"
	when 
		$event : StateEvent(itemName=="Date")
	then
		speak("The current time is " + ((DateTimeType)$event.getNewState()).format("%1$tl:%1$tM %1$Tp"));
end

rule "Say temperature on update"
	when 
		$event : StateEvent(itemName=="Weather_Temperature")
	then
		say("The temperature outside is " + ((DecimalType)$event.getNewState()).format("%d") + " degrees celsius");
end

rule "Volume"
	when
		$event : CommandEvent(itemName=="Volume")
	then
		if($event.getCommand().equals(IncreaseDecreaseType.INCREASE)) increaseMasterVolume(10);
		if($event.getCommand().equals(IncreaseDecreaseType.DECREASE)) decreaseMasterVolume(10);
		postUpdate("Volume", Double.toString(getMasterVolume() * 100));
end

