Sha256: b5060f73f7f80afe15887060b64620c93d8f53f208e2f0a96d4901d652ba2b84

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

require 'serialport'
require 'mongoid'
require 'json'
require 'awesome_print'

class PHPoint
	include Mongoid::Document
	field :time
	field :experiment
	field :ph
	store_in session: "default"
end

class EHPoint
	include Mongoid::Document
	field :time
	field :experiment
	field :mv
	store_in session: "default"
end

class PHListener
	
  	
end



class SerialListen
   def initialize(port)
   	 puts "Initialize"
   	 Mongoid.load!("mongoid.yml", :production)
   	 @portname = port || "/dev/ttyUSB0"
   	 @sp = SerialPort.new(port,9600,8,1)
   end

   	def parse_input(string)	
		ig = ["Channel: R", "Channel: L"]
    	unless ig.any?{|s| string.include?(s)} || string.chomp.empty?
    		tm = Time.now
    		data = string.split
    		mv = ""
    		ph = ""

    		#experiment = options[:experiment]
    		experiment = "SPN0001"

    		if data[1].eql?("mV")
    			mv = data[0]
    			str = {time: tm, mv: mv, experiment: experiment}
    			save_eh_to_db(str)
    		end
    		if data[1].eql?("pH")
    			ph = data[0]
    			str = {time: tm, ph: ph, experiment: experiment}
    			save_ph_to_db(str)
    		end

    		ap str
    	end
    end


   def run
   	puts "Listening on serial port #{@portname}"
    
   	@sp.flush_input
    begin 
 		while data = @sp.readline
 			parse_input(data)
 		end
 	  rescue Interrupt
  		puts "exiting"	
  	end

   end

   def save_eh_to_db(data)
   		data_point = EHPoint.new(data)
   		data_point.save
   end

   def save_ph_to_db(data)
   	   	data_point = PHPoint.new(data)
   		data_point.save
   	
   end

end

#listen = SerialListen.new("/dev/ttyUSB0")
#listen.run

Mongoid.load!("mongoid.yml", :production)
#PHPoint.where(experiment: "TEST0002").each do |point|
 #puts point.ph
#end
puts PHPoint.last.ph

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
serialbar-0.0.3 seriallisten.rb
serialbar-0.0.2 seriallisten.rb
serialbar-0.0.1 seriallisten.rb