-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkin_polling.rb
executable file
·120 lines (94 loc) · 2.39 KB
/
checkin_polling.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
require 'net/http'
require 'uri'
require 'open-uri'
require 'json'
require 'serialport'
def checkin_count(ll)
4sq_oauth = "FOURSQUARE-OAUTH-TOKEN"
url = "https://api.foursquare.com/v2/venues/trending?ll=#{ll}&radius=500&oauth_token=#{4sq_oauth}&v=20110917&limit=50"
output = nil
open(url) {|f|
output = f.read()
}
checkinData = JSON.parse(output)
checkinData["response"]["venues"].inject(0) { |sum, venue|
sum + venue["hereNow"]["count"]
}
end
def poll_venue_checkin()
url = "https://glowsquare.herokuapp.com/venueCheckin"
output = nil
open(url) {|f|
output = f.read()
}
checkinData = JSON.parse(output)
checkins = checkinData["count"]
puts "New Checkins at Closely HQ: #{checkins}"
if (checkinData["count"] > 0)
# trigger a 'pulse' on the cube
set_pulse()
end
end
def poll_activity_demo()
url = "https://glowsquare.herokuapp.com/venueActivity"
output = nil
open(url) {|f|
output = f.read()
}
checkinData = JSON.parse(output)
checkinData["count"]
end
def set_pulse()
hueVal = 345
satVal = 204
brtVal = 255
write_serial('p', hueVal, satVal, brtVal)
end
def set_activity(count)
hueVal = 240
satVal = 255
#randomize the count for testing color values (remove)
#count += rand(255)
count += 50
#super secret activity color algorithm *patent pending
if (count > 255)
brtVal = 255;
else
brtVal = count;
end
write_serial('a', hueVal, satVal, brtVal)
end
def write_serial(command, hue, sat, brt)
#params for serial port
port_str = "/dev/tty.usbmodemfa131" #may be different for you
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
#write command and args to serial port
sp.putc command
sp.putc hue
sp.putc sat
sp.putc brt
# example: write a string
#sp.puts "string"
#example: just read forever
#while true do
# printf("%c", sp.getc)
#end
sp.close
end
# main loop
while true
count = checkin_count("39.752501%2C-104.996418")
#count = poll_activity_demo()
now = Time.now.strftime(" %m/%d/%Y %I:%M:%S %p")
#log = Logger.new(STDOUT)
#log.level = Logger::DEBUG
#log.info("Number of checkins around Mile High #{count} #{now}")
puts "Number of Checkins around Closely HQ: #{count} #{now}"
poll_venue_checkin()
set_activity(count)
sleep 3
end