Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ubuntu compatibility #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions get-pf100
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require 'optparse'
require './pf100-record.rb'
require './pf100-packet.rb'
require './pf100-meter.rb'
require 'date'

options = {}
options[:outfile] = nil
Expand Down Expand Up @@ -56,12 +57,25 @@ STDERR.puts "PF100 meter initialized."
STDERR.puts "Reading memory records..."
records = meter.get_records
outfile = STDOUT
outfile = open(options[:outfile], "w") if options[:outfile]
outfile.puts "Date,Time,PEF,FEV1"
outfile = open(options[:outfile], "a") if options[:outfile]
if (File.zero?(outfile))
outfile.puts "Date,Time,PEF,FEV1"
last_datetime = DateTime.new(1970)
elsif options[:outfile]
outfile_examine = open(options[:outfile],"r+")
last_reading = outfile_examine.to_a.last
last_date = last_reading[0,last_reading.index(',')]
rest_of_record = last_reading[last_reading.index(',')+1...-1]
last_time = rest_of_record[0,rest_of_record.index(',')]
last_datetime = DateTime.strptime(last_date+last_time,'%Y-%m-%d%H:%M')
outfile_examine.close
end
records.each do |record|
date = record.time.strftime("%F")
time = record.time.strftime("%R")
outfile.printf "%s,%s,%d,%f\n", date, time, record.pef, record.fev
if (record.time > last_datetime)
date = record.time.strftime("%F")
time = record.time.strftime("%R")
outfile.printf "%s,%s,%d,%f\n", date, time, record.pef, record.fev
end
end
outfile.close if options[:outfile]

Expand Down
47 changes: 29 additions & 18 deletions pf100-meter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,21 @@ def expect_response(expected_packet)
response = receive
return nil if !response or response.type == nil
return true if response == expected_packet
# TODO: this should be more elegant
puts "Received unexpected message from PF100, aborting."
raise "Received unexpected message from PF100 expected #{expected_packet.data}, got #{response.data} aborting."
Kernel.exit(2)
end

def ping
ping_pkt = PF100Packet.new(:request, [0x2c, 0x7b, 0x7d])
send ping_pkt
pong_pkt = PF100Packet.new(:response, [0x2c, 0x7b, 0x00, 0x00, 0x20, 0x56, 0x7d])
expect_response pong_pkt
pong_pkt = PF100Packet.new(:response, [0x2c, 0x7b, 0x00, 0x01, 0x20, 0x56, 0x7d])
begin
expect_response pong_pkt
rescue Exception => e
pong_pkt = PF100Packet.new(:response, [0x2c, 0x7b, 0x00, 0x00, 0x20, 0x56, 0x7d])
expect_response pong_pkt
return true
end
end

def get_records
Expand All @@ -79,20 +84,26 @@ def get_records
raw_records = records_pkt.data
records = Array.new
while(true)
record = raw_records.shift(12)
break if record == []
year = "20#{record[2].to_s(16)}".to_i
month = record[3].to_s(16).to_i
day = record[4].to_s(16).to_i
hour = record[5].to_s(16).to_i
minute = record[6].to_s(16).to_i
pef_right = record[7].to_s(16).to_i
pef_left = record[8].to_s(16).to_i
pef = sprintf("%d%02d", pef_left, pef_right).to_i
fev_right = record[9].to_s(16).to_i
fev_left = record[10].to_s(16).to_i
fev = sprintf("%d.%02d", fev_left, fev_right).to_f
records.push PF100Record.new(year, month, day, hour, minute, pef, fev)
begin
record = raw_records.shift(12)
break if record == []
year = "20#{record[2].to_s(16)}".to_i
month = record[3].to_s(16).to_i
day = record[4].to_s(16).to_i
hour = record[5].to_s(16).to_i
minute = record[6].to_s(16).to_i
pef_right = record[7].to_s(16).to_i
pef_left = record[8].to_s(16).to_i
pef = sprintf("%d%02d", pef_left, pef_right).to_i
fev_right = record[9].to_s(16).to_i
fev_left = record[10].to_s(16).to_i
fev = sprintf("%d.%02d", fev_left, fev_right).to_f
records.push PF100Record.new(year, month, day, hour, minute, pef, fev)
rescue ArgumentError => e
puts e, record, 'month' , month, day
puts 'records left', raw_records.length
next
end
end
records
end
Expand Down