-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rb
60 lines (54 loc) · 1.48 KB
/
main.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
#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
require 'em-rocketio-linda-client'
require 'arduino_firmata'
$stdout.sync = true
EM::run do
arduino = ArduinoFirmata.connect ENV["ARDUINO"], :eventmachine => true
url = ENV["LINDA_BASE"] || ARGV.shift || "http://linda.masuilab.org"
space = ENV["LINDA_SPACE"] || "iota"
puts "connecting.. #{url}"
linda = EM::RocketIO::Linda::Client.new url
ts = linda.tuplespace[space]
linda.io.on :connect do ## RocketIO's "connect" event
puts "Linda connect!! <#{linda.io.session}> (#{linda.io.type})"
last_at = Time.now
ts.watch ["door","open"] do |tuple|
p tuple
next if tuple.size != 2
next if last_at + 5 > Time.now
arduino.servo_write 9, 90
sleep 1
arduino.servo_write 9, 20
sleep 1
arduino.servo_write 9, 90
tuple << "success"
ts.write tuple
last_at = Time.now
end
ts.watch ["door","close"] do |tuple|
p tuple
next if tuple.size != 2
next if last_at + 5 > Time.now
arduino.servo_write 9, 90
sleep 1
arduino.servo_write 9, 160
sleep 1
arduino.servo_write 9, 90
tuple << "success"
ts.write tuple
last_at = Time.now
end
EM::add_periodic_timer 5 do
if arduino.analog_read(2) < 512
ts.write ["door","state","close"]
else
ts.wirte ["door","state","open"]
end
end
end
linda.io.on :disconnect do
puts "RocketIO disconnected.."
end
end