Skip to content

Commit 4c5f357

Browse files
committed
added the reading of a proper ds18b20 sensor file
1 parent 748d9b2 commit 4c5f357

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

app/models/ds18b20.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,32 @@ class Ds18b20 < ApplicationRecord
44
with: /\/\z/,
55
message: 'Path must end with a /'
66
}
7-
def creading
7+
def read_file
88
if File.exist?(path + file)
9-
File.read(path + file).chomp
9+
sensor = File.read(path + file).chomp
10+
else
11+
return -256
12+
end
13+
14+
lines = sensor.lines
15+
if lines[0].include? 'YES'
16+
string_temp = lines[1].split('=')[1]
17+
string_temp.to_f/1000
1018
else
11-
'-256'
19+
return -255
1220
end
21+
22+
end
23+
def creading
24+
read_file
1325
end
1426

1527
def freading
16-
if creading.to_i <= -250
17-
'-256'
28+
if creading <= -250
29+
creading
1830
else
19-
ctemp=creading.to_i*1.8+32
20-
ctemp.to_i.to_s
31+
ctemp=creading.to_f*1.8+32
32+
ctemp.to_s
2133
end
2234
end
2335
end

spec/files/sensor1.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
69
1+
72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
2+
72 01 4b 46 7f ff 0e 10 57 t=69000

spec/files/sensor2.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
10
1+
72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
2+
72 01 4b 46 7f ff 0e 10 57 t=10375

spec/files/sensor_bad_crc.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
72 01 4b 46 7f ff 0e 10 57 : crc=57 NO
2+
72 01 4b 46 7f ff 0e 10 57 t=23125

spec/models/ds18b20_spec.rb

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,41 @@
66
it {should respond_to :file}
77
it {should respond_to :creading }
88
it {should respond_to :freading }
9+
it {should respond_to :read_file }
910

1011
context 'has a file to read' do
12+
it 'expects a temp reading from sensor1.txt' do
13+
expect(build(:ds18b20).creading).to eq(69.0)
14+
expect(build(:ds18b20).freading).to eq('156.2')
15+
end
16+
17+
it 'expects a temp reading from sensor2.txt' do
18+
expect(build(:ds18b20, file: 'sensor2.txt').creading).to eq(10.375)
19+
expect(build(:ds18b20).freading).to eq('156.2')
20+
end
1121

12-
it 'expects a temp reading' do
13-
expect(build(:ds18b20).creading).to eq('69')
14-
expect(build(:ds18b20).freading).to eq('156')
22+
it 'read_file should return a celsius temp' do
23+
expect(build(:ds18b20).creading).to eq(69.0)
1524
end
25+
1626
end
1727

1828
context 'does not have a file to read' do
1929
it 'expects an error reading' do
2030
sensor = build(:ds18b20)
2131
sensor.file = 'bad file'
22-
expect(sensor.creading).to eq('-256')
23-
expect(sensor.freading).to eq('-256')
32+
expect(sensor.creading).to eq(-256)
33+
expect(sensor.freading).to eq(-256)
34+
end
35+
end
36+
37+
context 'sensor reports bad CRC' do
38+
it 'expects an error reading' do
39+
sensor = build(:ds18b20)
40+
sensor.file = 'sensor_bad_crc.txt'
41+
#sensor.file = 'sensor_real.txt'
42+
expect(sensor.creading).to eq(-255)
43+
expect(sensor.freading).to eq(-255)
2444
end
2545
end
2646

0 commit comments

Comments
 (0)