LAN light switch GET request problem


(Anthony Hell) #1

Hi everyone,

i’m trying to create new device type for my LAN light switch. The problem is that SmartHub doesn’t actually send any request to switch REST server.

I’ve created a device based on this device type with device id: 0A000107:0D54 // 10.0.1.7:3412
and tried with it - no luck. I’ve tried to use virtual device in IDE - no luck.

when I open the uri in browser: http://10.0.1.7:3412/switch?name=corridorLight&state=on it turns on the light
when I open telnet to 10.0.1.7 3412 and write all lines from logs (in method doRequest) it turns on the light

but with this device type no luck

Could anyone help me with it? I don’t want to forward port and make this REST server available to everyone.

Here’s the code:

metadata {
	definition (name: "Internal Switch", namespace: "narsul", author: "Anthony Peklo") {
		capability "Switch"
		capability "Refresh"
	}

	// simulator metadata
	simulator {
	}

	// UI tile definitions
	tiles {
		standardTile("button", "device.switch", width: 2, height: 2, canChangeIcon: false) {
			state "off", label: 'Off', action: "switch.on", icon: "st.Kids.kid10", backgroundColor: "#ffffff", nextState: "on"
			state "on", label: 'On', action: "switch.off", icon: "st.Kids.kid10", backgroundColor: "#79b821", nextState: "off"
		}
		standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {
			state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
		}        
		main "button"
		details(["button", "refresh"])
	}
}

def parse(String description) {
	log.debug "Parsing '${description}'"
}

def on() {
	log.debug('on')
	doRequest('on')
	sendEvent(name: "switch", value: "on")
}

def off() {
	log.debug('off')
	doRequest('off')
	sendEvent(name: "switch", value: "off")
}

def doRequest(action) {
	// device.deviceNetworkId = "0A000107:0D54"
	try {
        def hubAction = new physicalgraph.device.HubAction(
            method: 'GET',
            path: "/switch?name=corridorLight&state=${action}",
            headers: [HOST:getHostAddress()]
        )

        log.debug hubAction
        hubAction
    }
    catch (Exception e) {
    	log.debug "Hit Exception $e on $hubAction"
    }
}

private Integer convertHexToInt(hex) {
	Integer.parseInt(hex,16)
}

private String convertHexToIP(hex) {
	[convertHexToInt(hex[0..1]),convertHexToInt(hex[2..3]),convertHexToInt(hex[4..5]),convertHexToInt(hex[6..7])].join(".")
}

private getHostAddress() {
	def parts = device.deviceNetworkId.split(":")
	def ip = convertHexToIP(parts[0])
	def port = convertHexToInt(parts[1])
	return ip + ":" + port
}

P.S. By the way method ‘parse’ is not bing called too


(trac starr) #2

If i’m not mistaken there is a known bug/issue when using LAN with IPs in the range 10.0.x.x or maybe even any 10.x.x.x. I’m not sure of the link to the post off hand. I think it’s in the hex conversion or something - sorry I can’t remember more.