Last active
February 17, 2018 15:09
-
-
Save jnpoyser/61d4eac532bf3b7b576e4166f6426b49 to your computer and use it in GitHub Desktop.
Toggles a Homeseer device on and off every 10s, and checks status - for debugging zwave issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'json' | |
# Remember to enable control using JSON in HS | |
HS_IP_ADDRESS = '192.168.1.201' | |
HS_PORT = 80 | |
HS_DEVICE_REF = 146 | |
HS_DEVICE_STATUS_ON = 255 # Will toggle between these | |
HS_DEVICE_STATUS_OFF = 0 # Will toggle between these | |
LOG_FILE = 'zw-ping.log' | |
def log(message) | |
message = "#{Time.now} - #{message}" | |
open(LOG_FILE, 'a') do |f| | |
f.puts message | |
end | |
puts message | |
end | |
def send_request(path) | |
uri = URI("http://#{HS_IP_ADDRESS}:#{HS_PORT}#{path}") | |
#puts uri | |
response = Net::HTTP.get(uri) | |
if response.strip == 'error' | |
return false | |
else | |
return JSON.parse(response) | |
end | |
end | |
def get_status(device_ref) | |
status = send_request("/JSON?request=getstatus&ref=#{device_ref}") | |
status["Devices"][0]["value"] | |
end | |
def set_status(device_ref, value) | |
t1 = Time.now | |
status = send_request("/JSON?request=controldevicebyvalue&ref=#{device_ref}&value=#{value}") | |
t2 = Time.now | |
if status | |
log "OK - Set: device #{device_ref} status set to #{value}. Took #{(t2-t1)*1000.0}ms" | |
else | |
log "ERROR - Set: failed to set device #{HS_DEVICE_REF} status to #{value}" | |
end | |
end | |
def set_and_check_status(device_ref, value) | |
set_status(device_ref, value) | |
loop do | |
sleep(2) | |
status = get_status(device_ref) | |
if status == value | |
log "OK - Check: device #{device_ref} has expected status of #{value}" | |
break | |
else | |
log "ERROR - Check: expected device #{HS_DEVICE_REF} status to be #{value}, but was #{status}" | |
end | |
end | |
end | |
last_status = HS_DEVICE_STATUS_OFF | |
loop do | |
last_status = last_status == HS_DEVICE_STATUS_OFF ? HS_DEVICE_STATUS_ON : HS_DEVICE_STATUS_OFF | |
set_and_check_status(HS_DEVICE_REF, last_status) | |
sleep(8) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment