Index: /EtherRain++/D_EtherRainExt.xml
===================================================================
--- /EtherRain++/D_EtherRainExt.xml	(revision 3)
+++ /EtherRain++/D_EtherRainExt.xml	(revision 3)
@@ -0,0 +1,84 @@
+<?xml version="1.0"?>
+<root xmlns="urn:schemas-upnp-org:device-1-0">
+  <specVersion>
+    <major>1</major>
+    <minor>0</minor>
+  </specVersion>
+  <device>
+    <deviceType>urn:schemas-upnp-org:device:BinaryLight:1</deviceType>
+	<staticJson>D_BinaryLight1.json</staticJson>
+    <friendlyName>EtherRain-8 Sprinkler Controller</friendlyName>
+    <manufacturer>QuickSmart Development</manufacturer>
+    <manufacturerURL>http://www.quicksmart.com</manufacturerURL>
+    <modelDescription>8 valves Sprinkler Controller</modelDescription>
+    <modelName>EtherRain8</modelName>
+    <modelNumber>1.0</modelNumber>
+    <protocol>cr</protocol>
+    <handleChildren>1</handleChildren>
+    <UDN>uuid:upnp-edmidor-1_0-0000000000001</UDN>
+    <UPC>000001000</UPC>
+	<serviceList>
+      <service>
+        <serviceType>urn:schemas-upnp-org:service:SwitchPower:1</serviceType>
+        <serviceId>urn:upnp-org:serviceId:SwitchPower1</serviceId>
+        <controlURL>/upnp/control/SwitchPower1</controlURL>
+        <eventSubURL>/upnp/event/SwitchPower1</eventSubURL>
+        <SCPDURL>S_SwitchPower1.xml</SCPDURL>
+      </service>
+    </serviceList>
+	  <sceneList>
+{
+	"group_1":{
+		"cmd_1": 
+		{
+			"label": "ON",
+			"serviceId": "urn:upnp-org:serviceId:SwitchPower1",
+			"action": "SetTarget",
+			"arguments":
+			{
+				"newTargetValue": "1"
+			}
+		},
+		"cmd_2": 
+		{
+			"label": "OFF",
+			"serviceId": "urn:upnp-org:serviceId:SwitchPower1",
+			"action": "SetTarget",
+			"arguments":
+			{
+				"newTargetValue": "0"
+			}
+		}
+	}
+}	
+	</sceneList>
+ 	<eventList>
+{
+	"event_1":{
+		"label": "A device is turned on or off",
+		"serviceId": "urn:upnp-org:serviceId:SwitchPower1",
+		"argumentList" :
+		{
+			"argument_1" :
+			{
+				"dataType": "boolean",
+				"defaultValue": "1",
+				"allowedValueList" : 
+				{
+					"Off" : "0",
+					"On" : "1"
+				},
+				"name": "Status",
+				"comparisson": "=",
+				"prefix": "Which mode",
+				"suffix": ""
+			}
+		}
+	}
+}
+  </eventList>
+    <implementationList>
+      <implementationFile>I_EtherRainExt.xml</implementationFile>
+    </implementationList>
+  </device>
+</root>
Index: /EtherRain++/I_EtherRainExt.xml
===================================================================
--- /EtherRain++/I_EtherRainExt.xml	(revision 3)
+++ /EtherRain++/I_EtherRainExt.xml	(revision 3)
@@ -0,0 +1,201 @@
+<?xml version="1.0"?>
+<implementation>
+  <functions>
+	function init(lul_device)
+		require "socket"
+		local udp = socket.udp()
+		assert(udp:setoption('broadcast',true))
+		assert(udp:sendto('eviro_id_request1', '192.168.1.255', 8089))
+		local message, host = assert(udp:receivefrom())
+		luup.log("EtherRain found at " .. host .. ". Response is " .. message)
+		luup.devices[lul_device].ip = host
+		luup.log("EtherRain device IP set to " .. luup.devices[lul_device].ip)
+		luup.log("EtherRain Starting")
+		DEVICE = lul_device
+		local lul_delay = luup.variable_get("urn:edmidor-com:serviceId:EtherRain","StartDelay",lul_device)
+		if (lul_delay == nil) then
+			lul_delay = '0'
+			luup.variable_set("urn:edmidor-com:serviceId:EtherRain","RefreshStatusMin",5,lul_device)
+			luup.variable_set("urn:edmidor-com:serviceId:EtherRain","StartDelay",0,lul_device)
+			for n=1,8 do
+				luup.variable_set("urn:edmidor-com:serviceId:EtherRain","Valve" .. n,0,lul_device)
+			end
+			luup.variable_set("urn:edmidor-com:serviceId:EtherRain","User","admin",lul_device)
+			luup.variable_set("urn:edmidor-com:serviceId:EtherRain","Password","pw",lul_device)
+		end
+		REFRESH = luup.variable_get("urn:edmidor-com:serviceId:EtherRain","RefreshStatusMin",lul_device) * 60
+		luup.log("EtherRain poll status every " .. REFRESH .. ' sec')
+		child_devices = luup.chdev.start(lul_device);
+		for i=1,8 do
+			luup.log("Adding Valve #"..i)
+			luup.chdev.append(lul_device,child_devices,i,"Valve #"..i,"urn:schemas-upnp-org:device:BinaryLight:1","D_BinaryLight1.xml","","",true)
+		end
+		luup.chdev.sync(lul_device,child_devices)
+		valve_device_index = {}
+		valve_device_index[1]=lul_device
+		for i=1,8 do
+			local label = "Valve #"..i
+			for k, v in pairs(luup.devices) do
+				if (v.device_num_parent == lul_device and tonumber(v.id) == i) then
+					valve_device_index[i+1] = k
+					local valve_interval = luup.variable_get("urn:edmidor-com:serviceId:EtherRain","Interval",k)
+					if (valve_interval == nil) then
+						luup.variable_set("urn:edmidor-com:serviceId:EtherRain","Interval",10,k)
+						luup.log("Set interval for Valve #" .. i .. " to default of 10 mins")
+					end
+				end
+			end	
+		end
+		luup.log("Valve Index is " .. valve_device_index[1] .. ":" .. valve_device_index[2] .. ":" .. valve_device_index[3] .. ":" .. valve_device_index[4] .. ":" .. valve_device_index[5] .. ":" .. valve_device_index[6] .. ":" .. valve_device_index[7] .. ":" .. valve_device_index[8] .. ":" .. valve_device_index[9])
+		luup.call_timer("poll_status_cycle", 1, "1", "")
+	end
+	
+	function parse_response(lul_res, lul_device)
+		local lul_ccode = string.match(lul_res, 'cs:%s(%a%a)%s&lt;br&gt;')
+		if( lul_ccode == nil ) then
+			lul_ccode = 'nil'
+		end
+		local lul_ocode = string.match(lul_res, 'os:%s(%a%a)%s&lt;br&gt;')
+		if( lul_ocode == nil ) then
+			lul_ocode = 'nil'
+		end
+		local lul_rcode = string.match(lul_res, 'ri:%s(%d)%s&lt;br&gt;')
+		if( lul_rcode == nil ) then
+			lul_rcode = 'nil'
+		end
+		lul_rcode = lul_rcode + 1
+		if (lul_rcode == 9) then
+			lul_rcode = 0
+		end
+		luup.log("Relay Index:" .. lul_rcode)
+		return lul_ccode, lul_ocode, lul_rcode
+	end
+	
+	function lul_eth_login(lul_device)
+		local lul_user=luup.variable_get("urn:edmidor-com:serviceId:EtherRain", "User", lul_device)
+		local lul_password=luup.variable_get("urn:edmidor-com:serviceId:EtherRain", "Password", lul_device)
+		
+		local lul_cmd = 'http://' .. luup.devices[lul_device].ip .. '/ergetcfg.cgi?lu=' .. lul_user .. '&amp;lp=' .. lul_password
+		luup.log('Logging into Etherrain:' .. lul_cmd)
+		code, res = luup.inet.wget(lul_cmd, 2, "", "")
+		if (res == nil) then
+			luup.log('ETHERRAIN LOGIN PROBLEM: code=' .. code)
+			return false
+		end
+		--luup.log('Response:' .. res)
+	end
+	
+	function poll_status()
+		local lul_cmd = 'http://' .. luup.devices[DEVICE].ip .. '/result.cgi?xs'
+		local code, res = luup.inet.wget(lul_cmd,5, "", "")
+		cmd_sts, op_sts, ri_sts = parse_response(res, DEVICE)
+		luup.log("Status: " .. cmd_sts .. ', ' .. op_sts)
+		if (op_sts == 'RD') then
+			for i=1,9 do
+				luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",0,valve_device_index[i])
+			end
+		else
+			for i=1,8 do
+				if (i==ri_sts) then
+					luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",1,valve_device_index[i+1])
+					luup.log("Valve Number" .. (i) .. " Status 1")
+				else
+					luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",0,valve_device_index[i+1])					
+					luup.log("Valve Number" .. (i) .. " Status 0")
+				end
+			end
+		end
+	end
+
+	function poll_status_cycle()
+		poll_status()
+		luup.call_timer("poll_status_cycle", 1, tostring(REFRESH), "")
+	end
+  </functions>
+  <incoming>
+    <lua>
+      luup.log("Received: " .. tostring(lul_data))
+    </lua>
+  </incoming>
+  <startup>init</startup>
+  <actionList>
+   <action>
+     <serviceId>urn:upnp-org:serviceId:SwitchPower1</serviceId>
+     <name>SetTarget</name>
+      <job>
+		if (lul_eth_login(DEVICE) == false) then
+			return 2
+		end
+
+		local lul_reverse = luup.variable_get("urn:micasaverde-com:serviceId:HaDevice1","ReverseOnOff",lul_device)
+		if( lul_settings.newTargetValue=="1" or (lul_settings.newTargetValue=="0" and lul_reverse=="1") ) then
+			if (lul_device == valve_device_index[1]) then
+				local lul_delay=luup.variable_get("urn:edmidor-com:serviceId:EtherRain","StartDelay",lul_device)
+				luup.log("StartDelay = " .. tostring(lul_delay))
+				lul_cmd = lul_delay
+				for n=1,8 do
+					local lul_time=luup.variable_get("urn:edmidor-com:serviceId:EtherRain", "Valve" .. n, lul_device)
+					if( lul_time==nil ) then
+						lul_time = '0'
+					end
+					lul_cmd = lul_cmd .. ':' .. lul_time
+				end
+				luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",1,lul_device)
+			else
+				luup.log("Manual irrigate device " .. lul_device)
+				lul_cmd = "0"
+				for i=1,8 do
+					luup.log("Checking Valve: " .. valve_device_index[i+1])
+					if (lul_device==valve_device_index[i+1]) then
+						lul_time=luup.variable_get("urn:edmidor-com:serviceId:EtherRain", "Interval", lul_device)
+						luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",1,lul_device)
+					else
+						lul_time='0'
+						luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",0,valve_device_index[i+1])
+					end
+					lul_cmd = lul_cmd .. ':' .. lul_time
+					luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",0,DEVICE)
+				end	
+			end
+			luup.log("Manual irrigation command: " .. lul_cmd)
+			luup.inet.wget('http://' .. luup.devices[DEVICE].ip .. '/result.cgi?xr',5, "", "")
+			lul_cmd='http://' .. luup.devices[DEVICE].ip .. '/result.cgi?xi=' .. lul_cmd
+			code, res = luup.inet.wget(lul_cmd,5, "", "")
+			if (res == nil) then
+				luup.log('ETHERRAIN ERROR:' .. code)
+				return 2
+			end
+			cmd_sts, op_sts, ri_sts = parse_response(res, DEVICE)
+			luup.call_timer("poll_status", 1, "10", "")
+			luup.log("Status: " .. cmd_sts .. ', ' .. op_sts)
+			if (cmd_sts ~= 'OK') then 
+				luup.log('ETHERRAIN ERROR:' .. code)
+				return 2
+			end
+		else
+			local lul_cmd = 'http://' .. luup.devices[DEVICE].ip .. '/result.cgi?xr'
+			luup.log("Stop|Reset command: " .. lul_cmd)
+			code, res = luup.inet.wget(lul_cmd,5, "", "")
+			if (res == nil) then
+				luup.log('ETHERRAIN ERROR:' .. code)
+				return 2
+			end
+	
+			cmd_sts, op_sts, ri_sts = parse_response(res, DEVICE)
+			luup.call_timer("poll_status", 1, "10", "")
+			luup.log("Status: " .. cmd_sts .. ', ' .. op_sts)
+			if (cmd_sts ~= 'OK') then 
+				luup.log('ETHERRAIN ERROR:' .. code)
+				return 2
+			end
+			for i=1,9 do
+				luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",0,valve_device_index[i])
+			end
+		end
+		return 4
+      </job>
+	  <jobname>EtherRain</jobname>
+	  <icon>OFF</icon>
+	</action>
+   </actionList>
+</implementation>
