/
app.rb
62 lines (57 loc) · 1.5 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require "sinatra/base"
class App < Sinatra::Base
helpers Sinatra::FormHelpers
redis_uri = URI.parse(ENV['REDISTOGO_URL'])
REDIS = Redis.new(:host => redis_uri.host, :port => redis_uri.port, :password => redis_uri.password)
get '/' do
@pins = initPins(7,7)
REDIS.hincrby( "stats", "connections", 1 )
haml :showme
end
post '/updown' do
if params[:action]
p = params[:action]
spark_digital("digitalwrite", p[:pin], p[:value])
end
end
def initPins(d, a)
array = []
while d >= 0 do
pin = "D" + d.to_s
array << pin
state = REDIS.hget( ENV['DEVISE_NAME'], pin.to_s)
state == "1" ? array << "1" : array << "0"
d -= 1
end
while a >= 0 do
pin = "A" + a.to_s
array << pin
state = REDIS.hget( ENV['DEVISE_NAME'], pin.to_s)
state == "1" ? array << "1" : array << "0"
a -= 1
end
hash = Hash[*array]
return hash
end
def spark_digital(action, pin, value)
pin, value = pin.upcase, value.upcase
url = "https://api.spark.io/v1/devices/#{ENV['DEVISE_ID']}/" + action.downcase
data = [
Curl::PostField.content("access_token", ENV['AUTH']),
Curl::PostField.content("params", [pin,value].join(','))
]
c = Curl::Easy.http_post(url, *data) do |curl|
curl.headers["User-Agent"] = "Curl/Ruby"
curl.verbose = true
end
response = JSON.parse(c.body)
incr("presses")
if response["return_value"] == "1"
value == "HIGH" ? v = "1" : v = "0"
REDIS.hset( ENV['DEVISE_NAME'], pin, v)
end
end
def incr(name)
REDIS.hincrby( "stats", name, 1 )
end
end