traffic_light_controller 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +80 -0
- data/LICENSE +19 -0
- data/Rakefile +23 -0
- data/bin/traffic_light_controller +3 -0
- data/config/.gitignore +1 -0
- data/config/config-sample.yml +11 -0
- data/lib/traffic_light_controller/cli.rb +7 -0
- data/lib/traffic_light_controller/config.rb +20 -0
- data/lib/traffic_light_controller/server.rb +60 -0
- data/lib/traffic_light_controller/version.rb +3 -0
- data/lib/traffic_light_controller.rb +12 -0
- data/spec/lib/traffic_light_controller/cli_spec.rb +12 -0
- data/spec/lib/traffic_light_controller/config_spec.rb +16 -0
- data/spec/lib/traffic_light_controller/server_spec.rb +4 -0
- data/spec/spec_helper.rb +23 -0
- data/traffic_light_controller.gemspec +34 -0
- metadata +195 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
traffic_light_controller (0.0.1)
|
5
|
+
arduino
|
6
|
+
hashie
|
7
|
+
json
|
8
|
+
sinatra
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: http://rubygems.org/
|
12
|
+
specs:
|
13
|
+
arduino (0.4)
|
14
|
+
serialport (>= 1.0.4)
|
15
|
+
binding_of_caller (0.6.7)
|
16
|
+
builder (3.1.2)
|
17
|
+
ci_reporter (1.7.1)
|
18
|
+
builder (>= 2.1.2)
|
19
|
+
coderay (1.0.7)
|
20
|
+
columnize (0.3.6)
|
21
|
+
debugger (1.1.4)
|
22
|
+
columnize (>= 0.3.1)
|
23
|
+
debugger-linecache (~> 1.1.1)
|
24
|
+
debugger-ruby_core_source (~> 1.1.3)
|
25
|
+
debugger-linecache (1.1.2)
|
26
|
+
debugger-ruby_core_source (>= 1.1.1)
|
27
|
+
debugger-ruby_core_source (1.1.3)
|
28
|
+
diff-lcs (1.1.3)
|
29
|
+
hashie (1.2.0)
|
30
|
+
json (1.7.5)
|
31
|
+
method_source (0.8)
|
32
|
+
multi_json (1.3.6)
|
33
|
+
pry (0.9.10)
|
34
|
+
coderay (~> 1.0.5)
|
35
|
+
method_source (~> 0.8)
|
36
|
+
slop (~> 3.3.1)
|
37
|
+
pry-debugger (0.2.0)
|
38
|
+
debugger (~> 1.1.3)
|
39
|
+
pry (~> 0.9.9)
|
40
|
+
pry-stack_explorer (0.4.5)
|
41
|
+
binding_of_caller (~> 0.6.2)
|
42
|
+
rack (1.4.1)
|
43
|
+
rack-protection (1.2.0)
|
44
|
+
rack
|
45
|
+
rake (0.9.2.2)
|
46
|
+
rspec (2.11.0)
|
47
|
+
rspec-core (~> 2.11.0)
|
48
|
+
rspec-expectations (~> 2.11.0)
|
49
|
+
rspec-mocks (~> 2.11.0)
|
50
|
+
rspec-core (2.11.1)
|
51
|
+
rspec-expectations (2.11.3)
|
52
|
+
diff-lcs (~> 1.1.3)
|
53
|
+
rspec-mocks (2.11.2)
|
54
|
+
serialport (1.1.0)
|
55
|
+
simplecov (0.6.4)
|
56
|
+
multi_json (~> 1.0)
|
57
|
+
simplecov-html (~> 0.5.3)
|
58
|
+
simplecov-html (0.5.3)
|
59
|
+
simplecov-rcov (0.2.3)
|
60
|
+
simplecov (>= 0.4.1)
|
61
|
+
sinatra (1.3.3)
|
62
|
+
rack (~> 1.3, >= 1.3.6)
|
63
|
+
rack-protection (~> 1.2)
|
64
|
+
tilt (~> 1.3, >= 1.3.3)
|
65
|
+
slop (3.3.3)
|
66
|
+
tilt (1.3.3)
|
67
|
+
|
68
|
+
PLATFORMS
|
69
|
+
ruby
|
70
|
+
|
71
|
+
DEPENDENCIES
|
72
|
+
ci_reporter
|
73
|
+
pry
|
74
|
+
pry-debugger
|
75
|
+
pry-stack_explorer
|
76
|
+
rake
|
77
|
+
rspec
|
78
|
+
simplecov
|
79
|
+
simplecov-rcov
|
80
|
+
traffic_light_controller!
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2012 Juan C. Muller <jcmuller@gmail.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
|
5
|
+
require "rspec/core/rake_task"
|
6
|
+
require "traffic_light_controller/version"
|
7
|
+
require "bundler/gem_tasks"
|
8
|
+
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
10
|
+
spec.pattern = "spec/**/*_spec.rb"
|
11
|
+
spec.rspec_opts = ["--backtrace --format CI::Reporter::RSpec"]
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Clean backup and swap files, and artifacts"
|
15
|
+
task :clean do
|
16
|
+
require "fileutils"
|
17
|
+
Dir["{pkg/*,**/*~,**/.*.sw?,coverage/**,spec/reports/**}"].each do |file|
|
18
|
+
rm_rf file
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "Run rspec by default"
|
23
|
+
task :default => :spec
|
data/config/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
config.yml
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module TrafficLightController
|
2
|
+
class Config
|
3
|
+
def initialize
|
4
|
+
@config = Hashie::Mash.new(YAML.load_file('./config/config.yml'))
|
5
|
+
end
|
6
|
+
|
7
|
+
def method_missing(meth, *args, &block)
|
8
|
+
return config[meth.to_s] if config.has_key?(meth.to_s)
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def respond_to?(meth)
|
13
|
+
config.has_key?(meth.to_s) || super
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :config
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module TrafficLightController
|
2
|
+
class Server
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
begin
|
6
|
+
Thread.abort_on_exception = true
|
7
|
+
@config = Config.new
|
8
|
+
@server = TCPServer.new(config.server.address, config.server.port)
|
9
|
+
rescue Errno::EADDRINUSE
|
10
|
+
puts "Address in use"
|
11
|
+
rescue Errno::EADDRNOTAVAIL, SocketError
|
12
|
+
puts "Address not available"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def work(run_forever = true)
|
17
|
+
while(run_forever)
|
18
|
+
Thread.start(server.accept) do |client|
|
19
|
+
begin
|
20
|
+
path = nil
|
21
|
+
|
22
|
+
while(line = client.readline) do
|
23
|
+
break if line == "\r\n"
|
24
|
+
if match = line.match(%r{GET /+(?<path>\w*) HTTP/1\.})
|
25
|
+
path = match[:path]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
if config.arduino.lights.has_key?(path)
|
30
|
+
process(path)
|
31
|
+
client.print "HTTP/1.1 200 OK\r\nContent-type:text/plain\r\n\r\n"
|
32
|
+
client.puts path
|
33
|
+
else
|
34
|
+
client.print "HTTP/1.1 404 Not Found\r\nContent-type:text/plain\r\n\r\n"
|
35
|
+
client.puts "The requested path doesn't exist"
|
36
|
+
end
|
37
|
+
rescue => e
|
38
|
+
puts "Got #{e}!"
|
39
|
+
ensure
|
40
|
+
client.close
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def process(path)
|
47
|
+
board.turnOff
|
48
|
+
board.setHigh(config.arduino.lights[path]) unless path == "off"
|
49
|
+
board.close
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
attr_reader :server, :config
|
55
|
+
|
56
|
+
def board
|
57
|
+
@board = Arduino.new(config.arduino.port)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "arduino"
|
3
|
+
require "hashie"
|
4
|
+
require "socket"
|
5
|
+
require "yaml"
|
6
|
+
|
7
|
+
module TrafficLightController
|
8
|
+
autoload :Config, "traffic_light_controller/config"
|
9
|
+
autoload :CLI, "traffic_light_controller/cli"
|
10
|
+
autoload :Server, "traffic_light_controller/server"
|
11
|
+
autoload :VERION, "traffic_light_controller/version"
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TrafficLightController::CLI do
|
4
|
+
describe "#work" do
|
5
|
+
it "should initialize server and call work on it" do
|
6
|
+
server = mock
|
7
|
+
TrafficLightController::Server.should_receive(:new).and_return(server)
|
8
|
+
server.should_receive(:work)
|
9
|
+
described_class.new
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TrafficLightController::Config do
|
4
|
+
describe "#initialize" do
|
5
|
+
before do
|
6
|
+
YAML.should_receive(:load_file).and_return({ foo: { bar: "fight" } })
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should instantiate a mash for all the properties in YAML file" do
|
10
|
+
subject.foo.should == Hashie::Mash.new({ bar: "fight" })
|
11
|
+
subject.foo.bar.should == "fight"
|
12
|
+
subject.foo[:bar].should == "fight"
|
13
|
+
subject.foo["bar"].should == "fight"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require "rspec/core"
|
9
|
+
require "rspec/mocks"
|
10
|
+
|
11
|
+
require "traffic_light_controller"
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
15
|
+
config.run_all_when_everything_filtered = true
|
16
|
+
config.filter_run :focus
|
17
|
+
|
18
|
+
# Run specs in random order to surface order dependencies. If you find an
|
19
|
+
# order dependency and want to debug it, you can fix the order by providing
|
20
|
+
# the seed, which is printed after each run.
|
21
|
+
# --seed 1234
|
22
|
+
config.order = 'random'
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "traffic_light_controller/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "traffic_light_controller"
|
6
|
+
s.version = TrafficLightController::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.author = "Juan C. Muller"
|
9
|
+
s.email = "jcmuller@gmail.com"
|
10
|
+
s.homepage = "http://github.com/jcmuller/traffic_light_controller"
|
11
|
+
s.license = "GPL"
|
12
|
+
|
13
|
+
s.summary = <<-EOS
|
14
|
+
EOS
|
15
|
+
s.description = ""
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split($\) - %w(.rspec)
|
18
|
+
s.require_path = "lib"
|
19
|
+
s.bindir = "bin"
|
20
|
+
s.executables = %w(traffic_light_controller)
|
21
|
+
|
22
|
+
s.homepage = "http://github.com/jcmuller/traffic_light_controller"
|
23
|
+
s.test_files = Dir["spec/**/*_spec.rb"]
|
24
|
+
|
25
|
+
s.add_development_dependency("rake")
|
26
|
+
s.add_development_dependency("pry")
|
27
|
+
s.add_development_dependency("pry-debugger")
|
28
|
+
s.add_development_dependency("pry-stack_explorer")
|
29
|
+
|
30
|
+
s.add_dependency("sinatra")
|
31
|
+
s.add_dependency("arduino")
|
32
|
+
s.add_dependency("json")
|
33
|
+
s.add_dependency("hashie")
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,195 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: traffic_light_controller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Juan C. Muller
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: pry
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry-debugger
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: pry-stack_explorer
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: sinatra
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: arduino
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :runtime
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: json
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: hashie
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :runtime
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
description: ''
|
143
|
+
email: jcmuller@gmail.com
|
144
|
+
executables:
|
145
|
+
- traffic_light_controller
|
146
|
+
extensions: []
|
147
|
+
extra_rdoc_files: []
|
148
|
+
files:
|
149
|
+
- .gitignore
|
150
|
+
- Gemfile
|
151
|
+
- Gemfile.lock
|
152
|
+
- LICENSE
|
153
|
+
- Rakefile
|
154
|
+
- bin/traffic_light_controller
|
155
|
+
- config/.gitignore
|
156
|
+
- config/config-sample.yml
|
157
|
+
- lib/traffic_light_controller.rb
|
158
|
+
- lib/traffic_light_controller/cli.rb
|
159
|
+
- lib/traffic_light_controller/config.rb
|
160
|
+
- lib/traffic_light_controller/server.rb
|
161
|
+
- lib/traffic_light_controller/version.rb
|
162
|
+
- spec/lib/traffic_light_controller/cli_spec.rb
|
163
|
+
- spec/lib/traffic_light_controller/config_spec.rb
|
164
|
+
- spec/lib/traffic_light_controller/server_spec.rb
|
165
|
+
- spec/spec_helper.rb
|
166
|
+
- traffic_light_controller.gemspec
|
167
|
+
homepage: http://github.com/jcmuller/traffic_light_controller
|
168
|
+
licenses:
|
169
|
+
- GPL
|
170
|
+
post_install_message:
|
171
|
+
rdoc_options: []
|
172
|
+
require_paths:
|
173
|
+
- lib
|
174
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
175
|
+
none: false
|
176
|
+
requirements:
|
177
|
+
- - ! '>='
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: '0'
|
180
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
181
|
+
none: false
|
182
|
+
requirements:
|
183
|
+
- - ! '>='
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
version: '0'
|
186
|
+
requirements: []
|
187
|
+
rubyforge_project:
|
188
|
+
rubygems_version: 1.8.24
|
189
|
+
signing_key:
|
190
|
+
specification_version: 3
|
191
|
+
summary: ''
|
192
|
+
test_files:
|
193
|
+
- spec/lib/traffic_light_controller/cli_spec.rb
|
194
|
+
- spec/lib/traffic_light_controller/config_spec.rb
|
195
|
+
- spec/lib/traffic_light_controller/server_spec.rb
|