This is ruby language problem. The Class TrafficSignal represents some kind of s
ID: 3697011 • Letter: T
Question
This is ruby language problem.
The Class TrafficSignal represents some kind of signaling device at road intersections.
The instances shown below were created by saying:
# a trafficsignal
s = TrafficSignal.new( 20, 20, 20 )
s.cycle() # prints: 20 seconds of green
20 seconds of yellow
20 seconds of red
# a long green trafficsignal
longgreen = TrafficSignal.new( 50, 10, 20 )
longgreen.cycle() # prints: 50 seconds of green
10 seconds of yellow
20 seconds of red
Each TrafficSignal has its own length of green, yellow and red period as shown in the class diagram below.
Class Diagram
TrafficSignal ( int green, int yellow, int red )
-- accessors
int red( )
int yellow( )
int green( )
-- cycle will run the trafficsignal once
void cycle( )
--class members
int red
int yellow
int green
Based on the information shown here, create the class TrafficSignal.
Make sure its cycle( ) method prints out a single cycle through the signal as shown above.
Based on this class TrafficSignal, please create the subclass RightTurnSignal.
A RightTurnSignal is a special kind of TrafficSignal that also includes a turn period.
The intended relationship between TrafficSignal and RightTurnSignal is shown in the class diagram below.
Make sure its cycle( ) method shows a green, yellow, red and turn period.
Class Diagram
RightTurnSignal( int turn, int green, int yellow, int red )
-- accessors
int red( )
int yellow( )
int green( )
int turn( )
-- cycle will run the rightturnsignal once
void cycle( )
--class members
int red # from the TrafficSignal class
int yellow # from the TrafficSignal class
int green # from the TrafficSignal class
int turn
# a trafficsignal
s = TrafficSignal.new( 20, 20, 20 )
s.cycle() # prints: 20 seconds of green
20 seconds of yellow
20 seconds of red
# a long green trafficsignal
longgreen = TrafficSignal.new( 50, 10, 20 )
longgreen.cycle() # prints: 50 seconds of green
10 seconds of yellow
20 seconds of red
Explanation / Answer
require "rubygems" require "json" require "net/http" require "uri" require 'yaml' # Parse configuration file config = YAML::load_file "config.yml" baseURL = config["server"]["baseURL"] red = config["gpio"]["redPin"] yellow = config["gpio"]["yellowPin"] green = config["gpio"]["greenPin"] on = config["gpio"]["onValue"] off = config["gpio"]["offValue"] # Set GPIO pins to output mode system "gpio mode #{red} out" system "gpio mode #{yellow} out" system "gpio mode #{green} out" # Create API request for CI Server url = URI.parse("#{baseURL}/api/json?tree=jobs[lastBuild[result]]") http = Net::HTTP.new(url.host, url.port) http.use_ssl = (url.scheme == 'https') request = Net::HTTP::Get.new(url.request_uri) loop { begin response = http.request(request) if response.code == "200" result = JSON.parse(response.body) jobs = result["jobs"] # assume build is good, unless otherwise discovered overall_status = "SUCCESS" jobs.each do |job| if job.is_a?(Hash) and job["lastBuild"].is_a?(Hash) status = job["lastBuild"]["result"] if "IN_PROGRESS" == status or status.nil? overall_status = "IN_PROGRESS" break elsif "SUCCESS"!=status overall_status = "FAILURE" end end end if "SUCCESS" == overall_status puts "green" system "gpio write #{red} #{off}" system "gpio write #{yellow} #{off}" system "gpio write #{green} #{on}" elsif "IN_PROGRESS" == overall_status puts "yellow" system "gpio write #{red} #{off}" system "gpio write #{yellow} #{on}" system "gpio write #{green} #{off}" else # FAILED puts "red" system "gpio write #{red} #{on}" system "gpio write #{yellow} #{off}" system "gpio write #{green} #{off}" end end rescue puts "Error occoured" system "gpio write #{red} #{on}" system "gpio write #{yellow} #{on}" system "gpio write #{green} #{on}" end sleep config["refreshInterval"] }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.