スクリプト中のXMLがBloggerにエスケープされてしまうようなのでgistで提供。
https://gist.github.com/izumogeiger/6131907#file-avcpm-rb
よくわからない解説
- 現在時間からログファイルを決める
- ログファイルの最後60行の平均cpmを求める
- cpmを線量に換算する 換算係数は150らしぃ
- tweetする
- xively.comする
cron設置。
ミッション完了。
# coding:utf-8
require 'pp'
require 'fileutils'
#require 'serialport'
class GC10Logger
def initialize
@sp = SerialPort.new('/dev/ttyUSB0',9600,8,1,0)
end
def get_cpm
line = @sp.gets("\n")
if /^\d+/ =~ line
return line.to_i
else
return nil
end
end
def run
loop do
cpm = get_cpm
now = Time.now
next unless cpm
dir = File.join(ENV["HOME"],"gc10",now.strftime("%Y"),now.strftime("%m"))
file = File.join(dir,now.strftime("%d")+".txt")
FileUtils.mkdir_p(dir)
mode = File.exists?(file) ? "a" : "w"
File.open(file,mode) do |io|
out = "#{now.strftime("%Y%m%d%H%M%S")}\t#{cpm}"
io.puts out
end
end
end
end
gc10 = GC10Logger.new
gc10.run
20130801212453 25 20130801212454 24 20130801212455 24 20130801212456 24 20130801212457 23 20130801212458 23 20130801212459 23 20130801212500 22
# coding:utf-8
require 'pp'
require 'fileutils'
require 'serialport'
class Locked < StandardError
end
def lock(lock_file_path='/tmp/gc10lock')
File.open(lock_file_path, 'w') do |lock_file|
if lock_file.flock(File::LOCK_EX|File::LOCK_NB)
yield
else
raise Locked
end
end
end
class GC10Logger
def initialize
@sp = SerialPort.new('/dev/ttyUSB0',9600,8,1,0)
end
def get_cpm
line = @sp.gets("\n")
if /^\d+/ =~ line
return line.to_i
else
return nil
end
end
def run
loop do
cpm = get_cpm
now = Time.now
next unless cpm
dir = File.join(ENV["HOME"],"gc10",now.strftime("%Y"),now.strftime("%m"))
file = File.join(dir,now.strftime("%d")+".txt")
FileUtils.mkdir_p(dir)
mode = File.exists?(file) ? "a" : "w"
File.open(file,mode) do |io|
out = "#{now.strftime("%Y%m%d%H%M%S")}\t#{cpm}"
io.puts out
end
end
end
end
begin
lock do
gc10 = GC10Logger.new
gc10.run
end
rescue Locked
puts "already running..."
exit 0
end
* * * * * pi /home/pi/.rvm/bin/ruby /home/pi/scripts/gc10logger.rb