module Reco4life

Constants

API_URL
ERROR_CODE_MAP
GET_TOKEN
ITEM_LIST
ITEM_SWITCH
VERSION

Attributes

api_key[RW]
user_name[RW]

Public Class Methods

devices() click to toggle source
# File lib/reco4life.rb, line 25
def devices
  fetch_devices.map { |h| h['sn'] }
end
online?(sn) click to toggle source
# File lib/reco4life.rb, line 37
def online?(sn)
  check(sn, 'is_online')
end
powered_on?(sn) click to toggle source
# File lib/reco4life.rb, line 41
def powered_on?(sn)
  check(sn, 'is_poweron')
end
turn_off(sn) click to toggle source
# File lib/reco4life.rb, line 33
def turn_off(sn)
  switch(sn, 0) if powered_on?(sn)
end
turn_on(sn) click to toggle source
# File lib/reco4life.rb, line 29
def turn_on(sn)
  switch(sn, 1) unless powered_on?(sn)
end

Private Class Methods

check(sn, field_name) click to toggle source
# File lib/reco4life.rb, line 69
def check(sn, field_name)
  fetch_devices.each { |h| return h[field_name] == 1 if h['sn'] == sn }
  raise ERROR_CODE_MAP[7]
end
fetch_devices() click to toggle source
# File lib/reco4life.rb, line 62
def fetch_devices
  refresh_token unless token_valid?
  hash = JSON.parse HTTP[token: @token].get(ITEM_LIST % user_name).to_s
  raise ERROR_CODE_MAP[hash['result'].to_i] if hash['result'] != '1'
  hash['list']
end
refresh_token() click to toggle source
# File lib/reco4life.rb, line 47
def refresh_token
  hash = JSON.parse HTTP.get(GET_TOKEN % [user_name, api_key]).to_s
  @token = hash['token']
  raise ERROR_CODE_MAP[hash['result'].to_i] if @token.nil?
  @token_expire_time = Time.parse(hash['expire_date'])
end
switch(sn, status) click to toggle source
# File lib/reco4life.rb, line 74
def switch(sn, status)
  hash = JSON.parse HTTP[token: @token].get(ITEM_SWITCH % [Reco4life.user_name, sn, status]).to_s
  raise ERROR_CODE_MAP[hash['result'].to_i] if hash['result'] != '1'
end
token_expired?() click to toggle source
# File lib/reco4life.rb, line 58
def token_expired?
  @token_expire_time && @token_expire_time < Time.now
end
token_valid?() click to toggle source
# File lib/reco4life.rb, line 54
def token_valid?
  @token && !token_expired?
end