Class: AstmServer

Inherits:
Object
  • Object
show all
Includes:
LabInterface
Defined in:
lib/ruby_astm/astm_server.rb

Constant Summary

Constants included from LabInterface

LabInterface::ACK, LabInterface::CR, LabInterface::ENQ, LabInterface::EOT, LabInterface::ETX, LabInterface::LF, LabInterface::STX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LabInterface

#checksum, #data_buffer, #generate_response, #is_mid_frame_end?, #pre_process_bytes, #process_text, #process_text_file, #process_type, #receive_data, #root, #send_enq, #terminator, #test_data_bytes, #unbind

Constructor Details

#initialize(ethernet_connections, serial_connections, mpg = nil, respond_to_queries = nil) ⇒ AstmServer

DEFAULT SERIAL PORT : /dev/ttyS0 DEFAULT USB PORT : /dev/ttyUSB0 @param ethernet_connections : each element is expected to be a hash, with keys for :server_ip, :server_port. @param serial_connections : each element is expected to be a hash with port_address, baud_rate, and parity def initialize(server_ip=nil,server_port=nil,mpg=nil,respond_to_queries=false,serial_port=‘/dev/ttyS0’,usb_port=‘/dev/ttyUSB0’,serial_baud=9600,serial_parity=8,usb_baud=19200,usb_parity=8)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_astm/astm_server.rb', line 38

def initialize(ethernet_connections,serial_connections,mpg=nil,respond_to_queries=nil)
	$redis = Redis.new
	AstmServer.log("Initializing AstmServer")
	self.ethernet_connections = ethernet_connections
	self.serial_connections = serial_connections
	self.server_ip = server_ip || "127.0.0.1"
	self.server_port = server_port || 3000
	self.respond_to_queries = respond_to_queries
	self.serial_port = serial_port
	self.serial_baud = serial_baud
	self.serial_parity = serial_parity
	self.usb_port = usb_port
	self.usb_baud = usb_baud
	self.usb_parity = usb_parity
	$mappings = JSON.parse(IO.read(mpg || AstmServer.default_mappings))
end

Class Method Details

.default_mappingsObject



22
23
24
# File 'lib/ruby_astm/astm_server.rb', line 22

def self.default_mappings
	File.join AstmServer.root_path,"mappings.json"
end

.log(message) ⇒ Object



13
14
15
16
# File 'lib/ruby_astm/astm_server.rb', line 13

def self.log(message)
		puts "" + message
 	$redis.zadd("ruby_astm_log",Time.now.to_i,message)
end

.root_pathObject



18
19
20
# File 'lib/ruby_astm/astm_server.rb', line 18

def self.root_path
	File.dirname __dir__
end

Instance Method Details

#start_serverObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_astm/astm_server.rb', line 55

def start_server
	EventMachine.run {
		self.ethernet_connections.each do |econn|
			raise "please provide a valid ethernet configuration with ip address" unless econn[:server_ip]
			raise "please provide a valid ethernet configuration with port" unless econn[:server_port]
			EventMachine::start_server econn[:server_ip], econn[:server_port], LabInterface	
			AstmServer.log("Running ETHERNET  with configuration #{econn}")
		end
		self.serial_connections.each do |sconn|
			raise "please provide a valid serial configuration with port address" unless sconn[:port_address]
			raise "please provide a valid serial configuration with baud rate" unless sconn[:baud_rate]
			raise "please provide a valid serial configuration with parity" unless sconn[:parity]
			EventMachine.open_serial(sconn[:port_address], sconn[:baud_rate], sconn[:parity],LabInterface)
			puts "RUNNING SERIAL port with configuration : #{sconn}"
		end

	}
end