thermostat_robin_spruytte 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eaa870098c27235830f1cd4f93605d35f4f2509433be676415d4d6a42239f84f
4
- data.tar.gz: 86a4bddc6b031cff43a8b1823fd7bce92554f2d044912e9f7b3ce7e0f508575c
3
+ metadata.gz: 24454b9eca142e3f4f9ec8838dc804fe46fcbd04f4a05d9ef9aed1fd346a4edf
4
+ data.tar.gz: e7fee0d9ae1ffc2f356805a3296550703c11d8c730a1e68ef7235325e824270a
5
5
  SHA512:
6
- metadata.gz: 5b1f7e36bf662a90ee7e73b55cea0eaf855c50ed45a0da9f25d29b5f2eecb073452c052a876fd2c972a955d6eb113f2c26fe4f0a4a9f1e267d831c0e73e7d31a
7
- data.tar.gz: 35efac5ae3739b48d7fc3bfef0900c0fb7fc2105f2135c8997da24eaeda18973f94e7c276a6ef5c4b4f1936fa38c564efc0cc04282513657d71d4d7e8a603c44
6
+ metadata.gz: 8e9846ffb6636a50b06d6cd807259d1eccdc2a4d7cf1246299a096f25d0a71c188864295766a30d6de707f966a15d5fbf1c6b909f9ee4cf753be6ce88ad649cd
7
+ data.tar.gz: aa0c241aaf17bd96c25231ad907dbb4b22b056abae066c8c514b53d470dc65d81a540cc595d32084e561f8e332c78b0d3cd3f139c1aa1f72bf538f50d4e05b7f
data/lib/app.rb ADDED
@@ -0,0 +1,99 @@
1
+ # The App class, contains the logic to ceep the room temperature
2
+ # within a desired value
3
+ class App
4
+ attr_reader :wanted_temperature, :thermostat, :range, :update_speed
5
+ # Initialises the App class
6
+ # @param wanted_temperature [wanted_temperature] the desired room temperature
7
+ # @param range [range] the range in wich the room temperature must stay from
8
+ # the desired temperature
9
+ # @param thermostat [thermostat] an instance from the class Thermostat
10
+ # @param update_speed [update_speed] the speed at wich the app will run
11
+ def initialize(args)
12
+ @wanted_temperature = args[:wanted_temperature]
13
+ @range = get_range(input_range: args[:range])
14
+ @thermostat = args[:thermostat]
15
+ @update_speed = args[:update_speed] || 1
16
+ end
17
+
18
+ # Starts the application
19
+ def run
20
+ loop do
21
+ print("\ntermostaat temperatuur: #{thermostat_temperature}")
22
+ correct_temperature
23
+ sleep(update_speed)
24
+ end
25
+ end
26
+
27
+ # gets the range value used in calculations
28
+ # @param input_range [input_range] the range in wich the room temperature must
29
+ # stay from the desired temperature
30
+ # @return [number] the actual range
31
+ def get_range(args)
32
+ args[:input_range] / 2.0
33
+ end
34
+
35
+ # says the thermostat if it must heat up or cool down to reach the
36
+ # desired temperature
37
+ # @return [Struct] contains what actions the thermostat took
38
+ def correct_temperature
39
+ cooling = cool_thermostat(temperature: thermostat_temperature)
40
+ heating = heat_thermostat(temperature: thermostat_temperature)
41
+ output = Struct.new(:cooling, :heating)
42
+ output.new(cooling, heating)
43
+ end
44
+
45
+ # Checks if the thermostat must cool down to reach the desired
46
+ # temperature
47
+ # @param temperature [temperature] the current room temperature
48
+ # @return [Boolean] true if the thermostat had to cool down if not, false
49
+ def cool_thermostat(args)
50
+ if temperature_out_of_range(temperature: args[:temperature]) &&
51
+ temperature_to_high(temperature: args[:temperature])
52
+ thermostat.cooling(value: range)
53
+ true
54
+ else
55
+ false
56
+ end
57
+ end
58
+
59
+ # Checks if the thermostat must heat up to reach the desired
60
+ # temperature
61
+ # @param temperature [temperature] the current room temperature
62
+ # @return [Boolean] true if the thermostat had to warm up if not, false
63
+ def heat_thermostat(args)
64
+ if temperature_out_of_range(temperature: args[:temperature]) &&
65
+ temperature_to_low(temperature: args[:temperature])
66
+ thermostat.heating(value: range)
67
+ true
68
+ else
69
+ false
70
+ end
71
+ end
72
+
73
+ # Checks if the temperature is out of range
74
+ # @param temperature [temperature] the current room temperature
75
+ # @return [Boolean] true if the thermostat is out of range else, false
76
+ def temperature_out_of_range(args)
77
+ (args[:temperature] - wanted_temperature).abs > range
78
+ end
79
+
80
+ # Checks if the temperature is to low
81
+ # @param temperature [temperature] the current room temperature
82
+ # @return [Boolean] true if the temperature is to low else, false
83
+ def temperature_to_low(args)
84
+ args[:temperature] < wanted_temperature
85
+ end
86
+
87
+ # Checks if the temperature is to high
88
+ # @param temperature [temperature] the current room temperature
89
+ # @return [Boolean] true if the temperature is to high else, false
90
+ def temperature_to_high(args)
91
+ args[:temperature] > wanted_temperature
92
+ end
93
+
94
+ # gets the current temperature of the thermostat
95
+ # @return [number] the current thermostat temperature
96
+ def thermostat_temperature
97
+ thermostat.temperature
98
+ end
99
+ end
@@ -0,0 +1,53 @@
1
+ require 'thermostat.rb'
2
+ require 'unitconverter.rb'
3
+ require 'app.rb'
4
+
5
+ # Takes in JSON strings from external sources like sencors
6
+ # and passes the information to the application
7
+ class JSONThermostat
8
+ attr_accessor :thermostat, :app, :settings, :converter
9
+
10
+ # Initialises the JSONThermostat class
11
+ # @param json [String] a json string containing the thermostat settings, of
12
+ # the form {temperature: 293.15, range: 1.0, unit: "kelvin"} unit is
13
+ # optional and will default to celsius
14
+ def initialize(settings_json)
15
+ @settings = get_parsed_json(json: settings_json)
16
+ @thermostat = Thermostat.new
17
+ @converter = UnitConverter.new(desired_unit: settings['unit'])
18
+ @app = App.new(
19
+ wanted_temperature: settings['temperature'],
20
+ range: settings['range'],
21
+ thermostat: thermostat
22
+ )
23
+ end
24
+
25
+ # takes in a JSON string from outside sources and updates the application
26
+ # @param sensordata_json [sensordata_json] the JSON string of the form:
27
+ # {temperature: 293.15, unit: "kelvin"} with the unit defaulting to
28
+ # celsius if not specified
29
+ # @return [JSON] the resulting webpage of the form
30
+ # {cooling: Boolean, heating: Boolean}
31
+ def update(sensordata_json)
32
+ json = get_parsed_json(json: sensordata_json)
33
+ update_temperature(temperature: json['temperature'], unit: json['unit'])
34
+ output = app.correct_temperature
35
+ JSON.generate(cooling: output.cooling, heating: output.heating)
36
+ end
37
+
38
+ # updates the temperature of the thermostat
39
+ # @param temperature [temperature] the request object
40
+ def update_temperature(args)
41
+ thermostat.temperature = converter.convert(
42
+ value: args[:temperature],
43
+ input_unit: args[:unit]
44
+ )
45
+ end
46
+
47
+ # Parses a JSON string to a JSON object
48
+ # @param json [json] the json string
49
+ # @return [JSON] the resulting JSON object
50
+ def get_parsed_json(args)
51
+ JSON.parse(args[:json])
52
+ end
53
+ end
@@ -0,0 +1,87 @@
1
+ # Converts temperatures expressed in different units to the unit that the
2
+ # application works with
3
+ class UnitConverter
4
+ attr_reader :desired_unit
5
+
6
+ # Initialises the UnitConverter class
7
+ # @param desired_unit [desired_unit] The desired unit of the application
8
+ def initialize(args)
9
+ @desired_unit = get_unit(unit: args[:desired_unit] || nil)
10
+ end
11
+
12
+ # returns the unit in wich the external sensor works
13
+ # @param unit [unit] the unit of the sensor, defaults to celsius
14
+ # @return [String] the unit in wich the sensor works
15
+ def get_unit(args)
16
+ args[:unit] || 'celsius'
17
+ end
18
+
19
+ # Checks if the input temperature actually needs to be converted
20
+ # @param input_unit [input_unit] the unit of the sensor
21
+ # @return [Boolean] true if the input temperature needs to be converted
22
+ def needs_conversion(args)
23
+ desired_unit != args[:input_unit]
24
+ end
25
+
26
+ # Converts the input temperature to the desired unit
27
+ # @param value [value] the input temperature
28
+ # @param input_unit [input_unit] the unit of the sensor defaults to celsius
29
+ # @return [Number] the converted temperature
30
+ def convert(args)
31
+ output = args[:value]
32
+ input_unit = get_unit(unit: args[:input_unit] || nil)
33
+ if needs_conversion(input_unit: input_unit)
34
+ output = send conversion_method(input_unit: input_unit), value: output
35
+ end
36
+ output
37
+ end
38
+
39
+ # gets the name of the right conversion method
40
+ # @param input_unit [input_unit] the sensor unit
41
+ # @return [String] the name of the needed conversion method
42
+ def conversion_method(args)
43
+ args[:input_unit] + '_to_' + desired_unit
44
+ end
45
+
46
+ # converts a temperature in celcius to kelvin
47
+ # @param value [value] the input temperature
48
+ # @return [Number] the converted temperature
49
+ def celsius_to_kelvin(args)
50
+ args[:value] + 273.15
51
+ end
52
+
53
+ # converts a temperature in celcius to fahrenheit
54
+ # @param value [value] the input temperature
55
+ # @return [Number] the converted temperature
56
+ def celsius_to_fahrenheit(args)
57
+ (args[:value] * 1.8) + 32
58
+ end
59
+
60
+ # converts a temperature in fahrenheit to celsius
61
+ # @param value [value] the input temperature
62
+ # @return [Number] the converted temperature
63
+ def fahrenheit_to_celsius(args)
64
+ (args[:value] - 32) / 1.8
65
+ end
66
+
67
+ # converts a temperature in fahrenheit to kelvin
68
+ # @param value [value] the input temperature
69
+ # @return [Number] the converted temperature
70
+ def fahrenheit_to_kelvin(args)
71
+ celsiusToKelvin(fahrenheitToCelsius(value: args[:value]))
72
+ end
73
+
74
+ # converts a temperature in kelvin to celsius
75
+ # @param value [value] the input temperature
76
+ # @return [Number] the converted temperature
77
+ def kelvin_to_celsius(args)
78
+ args[:value] - 273.15
79
+ end
80
+
81
+ # converts a temperature in kelvin to fahrenheit
82
+ # @param value [value] the input temperature
83
+ # @return [Number] the converted temperature
84
+ def kelvin_to_fahrenheit(args)
85
+ (kelvinToCelsius(value: args[:value]) * 1.8) + 32
86
+ end
87
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thermostat_robin_spruytte
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Spruytte
@@ -18,7 +18,10 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - bin/thermostat
21
+ - lib/app.rb
22
+ - lib/json_thermostat.rb
21
23
  - lib/thermostat.rb
24
+ - lib/unitconverter.rb
22
25
  homepage: http://rubygems.org/gems/thermostat_robin_spruytte
23
26
  licenses:
24
27
  - MIT