enzo-durand-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/bin/thermostaat +92 -0
- data/lib/app_oud.rb +36 -0
- data/lib/calc.rb +38 -0
- data/lib/form.rb +37 -0
- data/lib/leds.rb +24 -0
- data/lib/mqtt.rb +32 -0
- data/lib/relais.rb +15 -0
- data/lib/system.rb +29 -0
- data/lib/url_reader.rb +11 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0dacf2ff3edebfec42ca72a8b018e26a55ccb530
|
4
|
+
data.tar.gz: b873089ada7f6e6c0affa108cb4d4521457a8ae2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4220393a974101d418d2bbfbe45a42dbc6b3b2d93eea214429b2b0d8b8b25107d790e70de504de129bc1f8c3df0d4cb896a5c35781ea23d701ff14bf17f5f4c2
|
7
|
+
data.tar.gz: 11dcc358ee1d33ab340c6a73a46654ead77aa43cbe8dc1790b619227b0a766d4159b356f8a756b62fe3d66a4f00cacaf51d9ff4aea98de583efe13f3a165370f
|
data/bin/thermostaat
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require_relative "lib/url_reader.rb"
|
2
|
+
require_relative "lib/form.rb"
|
3
|
+
require_relative "lib/leds.rb"
|
4
|
+
require_relative "lib/calc.rb"
|
5
|
+
require_relative "lib/system.rb"
|
6
|
+
require_relative "lib/mqtt.rb"
|
7
|
+
|
8
|
+
require 'optparse'
|
9
|
+
|
10
|
+
foo = Form.new
|
11
|
+
calc = Calc.new
|
12
|
+
led = Led.new
|
13
|
+
url = Url_reader.new
|
14
|
+
thermostat = Thermostat.new
|
15
|
+
mqtt = MqttReader.new
|
16
|
+
|
17
|
+
options = {:celsius => nil, :fahrenheit => nil, :kelvin => nil, :range => nil, :ideal_temp => nil, :method => nil}
|
18
|
+
parser = OptionParser.new do |opts|
|
19
|
+
opts.banner = "Usage: example.rb [options]"
|
20
|
+
|
21
|
+
opts.on('-c', '--celsius C', Float, 'Celsius') do |celsius|
|
22
|
+
options[:temp] = calc.convertToCelsius(celsius,"c")
|
23
|
+
puts "#{celsius} Celsius"
|
24
|
+
end
|
25
|
+
|
26
|
+
opts.on('-k', '--kelvin K', Float, 'Kelvin') do |kelvin|
|
27
|
+
options[:temp] = calc.convertToCelsius(kelvin,"k")
|
28
|
+
puts "#{kelvin} Kelvin"
|
29
|
+
end
|
30
|
+
|
31
|
+
opts.on('-f', '--fahrenheit F', Float, 'Fahrenheit') do |fahrenheit|
|
32
|
+
options[:temp] = calc.convertToCelsius(fahrenheit,"f")
|
33
|
+
puts "#{fahrenheit} Fahrenheit"
|
34
|
+
end
|
35
|
+
|
36
|
+
opts.on('-r', '--range R', Float, 'Range') do |range|
|
37
|
+
options[:range] = range
|
38
|
+
puts "range is #{range}"
|
39
|
+
end
|
40
|
+
|
41
|
+
opts.on('-m', '--method M', String, 'Method') do |method|
|
42
|
+
options[:method] = method
|
43
|
+
puts "Method used #{method}"
|
44
|
+
end
|
45
|
+
|
46
|
+
opts.on('-i', '--idealtemp I', Float, 'Ideal temparture') do |ideal_temp|
|
47
|
+
options[:ideal_temp] = ideal_temp
|
48
|
+
puts "ideal_temp is #{ideal_temp}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
parser.parse!
|
52
|
+
|
53
|
+
if options[:temp]
|
54
|
+
while true do
|
55
|
+
calclimit = calc.temperatuur(options[:temp] ,options[:ideal_temp], options[:range])
|
56
|
+
intensity = calc.intensityOfLeds(options[:temp], options[:ideal_temp], options[:range])
|
57
|
+
led.set_color(intensity)
|
58
|
+
thermostat.set(intensity)
|
59
|
+
thermostat.run
|
60
|
+
break
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
if options[:method] == 'url'
|
65
|
+
while true do
|
66
|
+
temp = url.url
|
67
|
+
puts temp
|
68
|
+
calclimit = calc.temperatuur(temp ,options[:ideal_temp], options[:range])
|
69
|
+
intensity = calc.intensityOfLeds(temp, options[:ideal_temp], options[:range])
|
70
|
+
led.set_color(intensity)
|
71
|
+
thermostat.set(intensity)
|
72
|
+
thermostat.run
|
73
|
+
break
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
if options[:method] == 'mqtt'
|
78
|
+
# while true do
|
79
|
+
mqtt.on_change do |temp|
|
80
|
+
puts temp
|
81
|
+
calclimit = calc.temperatuur(temp ,options[:ideal_temp], options[:range])
|
82
|
+
intensity = calc.intensityOfLeds(temp, options[:ideal_temp], options[:range])
|
83
|
+
led.set_color(intensity)
|
84
|
+
p led.get_color
|
85
|
+
thermostat.set(intensity)
|
86
|
+
thermostat.run
|
87
|
+
mqtt.send(led.color)
|
88
|
+
# break
|
89
|
+
end
|
90
|
+
mqtt.get_temp
|
91
|
+
end
|
92
|
+
p options[:method]
|
data/lib/app_oud.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative "url_reader.rb"
|
2
|
+
require_relative "form.rb"
|
3
|
+
require_relative "leds.rb"
|
4
|
+
require_relative "calc.rb"
|
5
|
+
require_relative "system.rb"
|
6
|
+
|
7
|
+
foo = Form.new
|
8
|
+
calc = Calc.new
|
9
|
+
led = Led.new
|
10
|
+
url = Url_reader.new
|
11
|
+
thermostat = Thermostat.new
|
12
|
+
|
13
|
+
url.url
|
14
|
+
foo.ask_temp
|
15
|
+
foo.ask_unit
|
16
|
+
foo.ask_range
|
17
|
+
foo.ask_limit
|
18
|
+
|
19
|
+
temp = foo.get_temp
|
20
|
+
unit = foo.get_unit
|
21
|
+
range = foo.get_range
|
22
|
+
limit = foo.get_limit
|
23
|
+
calclimit = calc.temperatuur(temp,limit,range)
|
24
|
+
|
25
|
+
puts temp
|
26
|
+
puts unit
|
27
|
+
puts range
|
28
|
+
puts limit
|
29
|
+
|
30
|
+
ctemp = calc.convertToCelsius(temp,unit)
|
31
|
+
p ctemp
|
32
|
+
intensity = calc.intensityOfLeds(ctemp,limit,range)
|
33
|
+
led.set_color(intensity)
|
34
|
+
p led.get_color
|
35
|
+
thermostat.set(intensity)
|
36
|
+
thermostat.run
|
data/lib/calc.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
class Calc
|
2
|
+
def convertToCelsius(temp,unit)
|
3
|
+
if unit == "f"
|
4
|
+
ctemp = (temp -32)/ 1.8
|
5
|
+
p ctemp
|
6
|
+
elsif unit == "k"
|
7
|
+
ctemp = temp - 273.15
|
8
|
+
p ctemp
|
9
|
+
else
|
10
|
+
ctemp = temp
|
11
|
+
end
|
12
|
+
return ctemp
|
13
|
+
end
|
14
|
+
|
15
|
+
def intensityOfLeds(cTemp,wTemp,range)
|
16
|
+
difference = wTemp - cTemp
|
17
|
+
intensity = Integer((difference/range) * 255)
|
18
|
+
if intensity >= 255
|
19
|
+
intensity = 255
|
20
|
+
return intensity
|
21
|
+
end
|
22
|
+
if intensity <= -255
|
23
|
+
intensity = -255
|
24
|
+
return intensity
|
25
|
+
end
|
26
|
+
return intensity
|
27
|
+
end
|
28
|
+
|
29
|
+
def temperatuur(temp,limit,range)
|
30
|
+
if temp > (limit + range)
|
31
|
+
puts "Red (hot)"
|
32
|
+
elsif temp < (limit - range)
|
33
|
+
puts "Blue(cold)"
|
34
|
+
else
|
35
|
+
puts "Green (ok)"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/form.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
class Form
|
2
|
+
def ask_temp
|
3
|
+
puts "What is the temp?"
|
4
|
+
@temp = gets.to_f
|
5
|
+
end
|
6
|
+
|
7
|
+
def get_temp
|
8
|
+
@temp
|
9
|
+
end
|
10
|
+
|
11
|
+
def ask_range
|
12
|
+
puts "What is the range?"
|
13
|
+
@range = gets.to_f
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_range
|
17
|
+
@range
|
18
|
+
end
|
19
|
+
|
20
|
+
def ask_limit
|
21
|
+
puts "What is the limit?"
|
22
|
+
@limit = gets.to_f
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_limit
|
26
|
+
@limit
|
27
|
+
end
|
28
|
+
|
29
|
+
def ask_unit
|
30
|
+
puts "Choose your unit(K,F,C)"
|
31
|
+
@unit = gets.downcase.delete!("\n")
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_unit
|
35
|
+
@unit
|
36
|
+
end
|
37
|
+
end
|
data/lib/leds.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class Led
|
2
|
+
@color
|
3
|
+
def set_color(value)
|
4
|
+
if value < 0
|
5
|
+
calc = value.abs.to_s(16)
|
6
|
+
@color = "0000#{calc}"
|
7
|
+
end
|
8
|
+
if value > 0
|
9
|
+
calc = value.abs.to_s(16)
|
10
|
+
@color = "#{value}0000"
|
11
|
+
end
|
12
|
+
if value == 0
|
13
|
+
@color = "00ff00"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_color
|
18
|
+
"The LED is #{@color}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def color
|
22
|
+
@color
|
23
|
+
end
|
24
|
+
end
|
data/lib/mqtt.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "mqtt"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
class MqttReader
|
5
|
+
def get_temp
|
6
|
+
client = MQTT::Client.new
|
7
|
+
client.host = 'mqtt.labict.be'
|
8
|
+
client.connect()
|
9
|
+
temperature_topic ='softwareengineering/thermostat/enzo/temperature'
|
10
|
+
while true
|
11
|
+
client.get(temperature_topic) do |topic,message|
|
12
|
+
puts "#{topic}: #{message}"
|
13
|
+
json = message
|
14
|
+
hash = JSON.parse(json)
|
15
|
+
temp = (hash["temperature"]).to_f
|
16
|
+
@on_change_block.call(temp) unless @on_change_block.nil?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def on_change &block
|
23
|
+
@on_change_block = block
|
24
|
+
end
|
25
|
+
|
26
|
+
def send(color)
|
27
|
+
client = MQTT::Client.connect('mqtt.labict.be')
|
28
|
+
hash2 = {"color" => color}
|
29
|
+
payload = JSON.generate(hash2)
|
30
|
+
client.publish('softwareengineering/thermostat/enzo/led', payload, retain = false)
|
31
|
+
end
|
32
|
+
end
|
data/lib/relais.rb
ADDED
data/lib/system.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative "relais.rb"
|
2
|
+
class Thermostat
|
3
|
+
def initialize()
|
4
|
+
@heating = Relais.new
|
5
|
+
@cooling = Relais.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def set(value)
|
9
|
+
if value > 0
|
10
|
+
@heating.on
|
11
|
+
@cooling.off
|
12
|
+
end
|
13
|
+
|
14
|
+
if value < 0
|
15
|
+
@heating.off
|
16
|
+
@cooling.on
|
17
|
+
end
|
18
|
+
|
19
|
+
if value == 0
|
20
|
+
@heating.off
|
21
|
+
@cooling.off
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def run()
|
26
|
+
p "The heating is #{@heating.status}"
|
27
|
+
p "The cooling is #{@cooling.status}"
|
28
|
+
end
|
29
|
+
end
|
data/lib/url_reader.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
|
3
|
+
require "open-uri"
|
4
|
+
|
5
|
+
class Url_reader
|
6
|
+
def url
|
7
|
+
cTemp = (URI.parse("https://labict.be/software-engineering/temperature/api/temperature/fake").read).to_f
|
8
|
+
# puts cTemp
|
9
|
+
return cTemp
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enzo-durand-thermostat-exercise
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Enzo Durand
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-16 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Thermostat app
|
14
|
+
email: enzo@info.com
|
15
|
+
executables:
|
16
|
+
- thermostaat
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/thermostaat
|
21
|
+
- lib/app_oud.rb
|
22
|
+
- lib/calc.rb
|
23
|
+
- lib/form.rb
|
24
|
+
- lib/leds.rb
|
25
|
+
- lib/mqtt.rb
|
26
|
+
- lib/relais.rb
|
27
|
+
- lib/system.rb
|
28
|
+
- lib/url_reader.rb
|
29
|
+
homepage: http://git.labict.be/enzodurand/Ruby-Thermostaat
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.5.2
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: Thermostat!
|
53
|
+
test_files: []
|