GABRIEL-QUIRSCHFELD-thermostat-exercise 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/app.rb +154 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f55c3648abaa796497b8a48a4a18b6ceb6006cc3
|
4
|
+
data.tar.gz: ad2e8b115091690cf217711f6de5d5c284fc8902
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cb26ff8fa99b11917703789f3af365615dd78761a2233ae8f97bc3e40b05595870df2744b2077e19202a5ad853e05519bcae7373acf3ba5e25e74bd58730301c
|
7
|
+
data.tar.gz: 509f0025c0e9a867995d2d964489486c3b76f21107988a93e10445ae95b58085d2fe612e905b40a10f7bc438df7d3a5388a3b868c32a5e8baf3492521fcf9878
|
data/lib/app.rb
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
require "optparse"
|
2
|
+
|
3
|
+
class Setup
|
4
|
+
# creating the variable that holds the user input
|
5
|
+
$options = {:currtemp => nil, :wanttemp => nil, :range => nil}
|
6
|
+
|
7
|
+
# creating a new option parser for checking the user input
|
8
|
+
parser = OptionParser.new do |opts|
|
9
|
+
# banner tells how to run app
|
10
|
+
opts.banner = "Usage: ruby app.rb [options]"
|
11
|
+
|
12
|
+
# states how to set the current temperasture parameter
|
13
|
+
opts.on('-c', '--currtemp currtemp', 'Current temperature of the room') do |currtemp|
|
14
|
+
$options[:currtemp] = currtemp;
|
15
|
+
end
|
16
|
+
|
17
|
+
# states how to set the wanted temperasture parameter
|
18
|
+
opts.on('-w', '--wanttemp wanttemp', 'Wanted temperature of the room') do |wanttemp|
|
19
|
+
$options[:wanttemp] = wanttemp;
|
20
|
+
end
|
21
|
+
|
22
|
+
# states how to set the range parameter
|
23
|
+
opts.on('-r', '--rng range', 'Acceptable range of the temperatures') do |range|
|
24
|
+
$options[:range] = range;
|
25
|
+
end
|
26
|
+
|
27
|
+
# states how to call the helper
|
28
|
+
opts.on('-h', '--help', 'Help with the user input') do
|
29
|
+
puts opts
|
30
|
+
exit
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# running the parser
|
35
|
+
parser.parse!
|
36
|
+
|
37
|
+
# setting the value for the first parameter
|
38
|
+
if $options[:currtemp] == nil
|
39
|
+
print 'Enter the current temperature: '
|
40
|
+
$options[:currtemp] = gets.chomp
|
41
|
+
end
|
42
|
+
|
43
|
+
# setting the value for the second parameter
|
44
|
+
if $options[:wanttemp] == nil
|
45
|
+
print 'Enter the wanted temperature: '
|
46
|
+
$options[:wanttemp] = gets.chomp
|
47
|
+
end
|
48
|
+
|
49
|
+
# setting the value for the third parameter
|
50
|
+
if $options[:range] == nil
|
51
|
+
print 'Enter the range for the temperatures: '
|
52
|
+
$options[:range] = gets.chomp
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Thermostat
|
57
|
+
attr_reader :state, :unit
|
58
|
+
|
59
|
+
def initialize(state = nil, unit = nil)
|
60
|
+
@state = state
|
61
|
+
@unit = unit
|
62
|
+
end
|
63
|
+
|
64
|
+
# Displaying the output
|
65
|
+
def display_status()
|
66
|
+
puts "Current temperature is " + state.round_output[0].to_s + unit.output_unit + ", Wanted temperature is " + state.round_output[1].to_s + unit.output_unit
|
67
|
+
puts "Heating: " + state.calc_state[0]
|
68
|
+
puts "Cooling: " + state.calc_state[1]
|
69
|
+
print "RGB led indicator: " + state.calc_state[2]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class State
|
74
|
+
attr_reader :cur_temp, :want_temp, :rng
|
75
|
+
|
76
|
+
def initialize(cur_temp, want_temp, rng)
|
77
|
+
@cur_temp = cur_temp.to_f
|
78
|
+
@want_temp = want_temp.to_f
|
79
|
+
@rng = rng.to_f
|
80
|
+
end
|
81
|
+
|
82
|
+
# Decifing on which state the thermostat is in
|
83
|
+
def calc_state()
|
84
|
+
if cur_temp + (rng / 2) < want_temp
|
85
|
+
state = ["true", "false", "#FF0000"]
|
86
|
+
elsif cur_temp - (rng / 2) > want_temp
|
87
|
+
state = ["false", "true", "#0000FF"]
|
88
|
+
else
|
89
|
+
state = ["false", "false", "#00FF00"]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Rounding the output temperatures to 2 decimal places
|
94
|
+
def round_output()
|
95
|
+
temp = [cur_temp.round(2), want_temp.round(2)]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class Unit
|
100
|
+
attr_reader :in_unit, :out_unit, :cur_temp, :want_temp
|
101
|
+
|
102
|
+
def initialize(cur_temp, want_temp, in_unit, out_unit)
|
103
|
+
@in_unit = in_unit
|
104
|
+
@out_unit = out_unit
|
105
|
+
@cur_temp = cur_temp.to_f
|
106
|
+
@want_temp = want_temp.to_f
|
107
|
+
end
|
108
|
+
|
109
|
+
# Calculating the unit conversions
|
110
|
+
def unit_conv
|
111
|
+
if in_unit == "C" && out_unit == "F"
|
112
|
+
cur_temp = cur_temp * 1.8 + 32
|
113
|
+
want_temp = want_temp * 1.8 + 32
|
114
|
+
elsif in_unit == "C" && out_unit == "K"
|
115
|
+
cur_temp += 273.15
|
116
|
+
want_temp += 273.15
|
117
|
+
elsif in_unit == "F" && out_unit == "C"
|
118
|
+
cur_temp = (cur_temp - 32) / 1.8
|
119
|
+
want_temp = (want_temp - 32) / 1.8
|
120
|
+
elsif in_unit == "F" && out_unit == "K"
|
121
|
+
cur_temp = (cur_temp + 459.67) * (5.0 / 9.0)
|
122
|
+
want_temp = (want_temp + 459.67) * (5.0 / 9.0)
|
123
|
+
elsif in_unit == "K" && out_unit == "C"
|
124
|
+
cur_temp -= 273.15
|
125
|
+
want_temp -= 273.15
|
126
|
+
elsif in_unit == "K" && out_unit == "F"
|
127
|
+
cur_temp = cur_temp * 1.8 - 459.67
|
128
|
+
want_temp = want_temp * 1.8 - 459.67
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# Setting the units for the console output
|
133
|
+
def output_unit
|
134
|
+
if out_unit == "K"
|
135
|
+
out_unit.to_s
|
136
|
+
else
|
137
|
+
"°" + out_unit.to_s
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
setup = Setup.new
|
143
|
+
|
144
|
+
# Create a new State object to do the calculations and set the state of the thermostat
|
145
|
+
state = State.new($options[:currtemp], $options[:wanttemp], $options[:range])
|
146
|
+
|
147
|
+
# Create a new Unit object to do the unit conversions
|
148
|
+
unit = Unit.new($options[:currtemp], $options[:wanttemp], "C", "C")
|
149
|
+
|
150
|
+
# Create new Thermostat object to format and display its status
|
151
|
+
therm = Thermostat.new(state, unit)
|
152
|
+
|
153
|
+
# Display the status of the Thermostat
|
154
|
+
therm.display_status()
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: GABRIEL-QUIRSCHFELD-thermostat-exercise
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabriel Quirschfeld
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/app.rb
|
20
|
+
homepage:
|
21
|
+
licenses: []
|
22
|
+
metadata: {}
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '0'
|
32
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
requirements: []
|
38
|
+
rubyforge_project:
|
39
|
+
rubygems_version: 2.5.2
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: the thermostat app for the software engineering exercise
|
43
|
+
test_files: []
|