一对轻便小巧的收发器程序(ruby实现)

    单位由于安全考虑需要做内外网彻底隔离,在内网有数据库服务器DB_SVR,

外网一台WEB_SVR需要将数据库中的数据显示在网页上。于是中间插入一台

“中转”服务器,如下图所示:

一对轻便小巧的收发器程序(ruby实现)_第1张图片

writer.rb放在中转服务器上,负责将数据从DB_SVR取出,然后整理格式通过

串口传递给WEB_SVR上的reader.rb,后者将数据解析成单条记录最后插入

WEB_SVR的SQL数据库中,供xxx.asp显示之用。下面上代码:


writer.rb

#hb table write by hopy 2012 
require 'win32ole'
require 'oci8'

HB_ARR_BEGIN = "HB_ARR_BEGIN"
HB_ARR_END = "HB_ARR_END"
HB_DEP_BEGIN = "HB_DEP_BEGIN"
HB_DEP_END = "HB_DEP_END"

def com_puts(str)
	$com.puts str
	$com.flush
end

`mode com4 9600,N,8,1`
$com = File.open("com4","w+")

class Hb
  def initialize
    @oci = OCI8.new("xxx","xxx","ora_xxx")
    #dbo:sunshine
  end
  
  #将oci中的航班数据同步到access数据库
  def sync
    zb = [64981,57520]  #正班
	com_puts HB_ARR_BEGIN
	
    puts "***** sync flight @ #{Time.now} *****"
    i = 0;start = Time.now
    ret = @oci.exec('select * from DBO.DPXS_ARRIVAL') do |r|
      next if r[7].unpack("v*") != zb
      
      zt = if r[24] == "Arrived" || r[24] == "Arriving"
        r[23]
      elsif r[25]
        r[25]
      else
        r[23]
      end
      
      tm = if r[12]
        r[12]
      elsif r[11]
        r[11]
      else
        r[10]
      end
      
	  com_puts "#{r[0]} #{tm} #{r[13]} #{zt}";i+=1
    end
	com_puts HB_ARR_END
    puts "sync #{i} arrival flights , take #{Time.now - start} s"
    
	sleep(1)
	com_puts HB_DEP_BEGIN
    i = 0;start = Time.now
    ret = @oci.exec('select * from DBO.DPXS_DEPARTURE') do |r|
      next if r[7].unpack("v*") != zb
      
      tm = if r[13]
        r[13]
      elsif r[12]
        r[12]
      else
        r[11]
      end
      
	  com_puts "#{r[0]} #{tm} #{r[14]} #{r[27]}";i+=1
    end
	
	com_puts HB_DEP_END
	
    puts "sync #{i} departure flights , take #{Time.now - start} s"
    puts "*"*51
  end
end

INTERVAL_sec = 60*5
hb = Hb.new
loop do
  begin
    hb.sync
    sleep(INTERVAL_sec)
  rescue =>e
    puts("ERROR : #{e.message} @ #{Time.now},retry it...")
    sleep(INTERVAL_sec)
    retry
  ensure
    #nothing
  end
end

reader.rb

#hb table write by hopy 2012 
require 'win32ole'

HB_ARR_BEGIN = "HB_ARR_BEGIN"
HB_ARR_END = "HB_ARR_END"
HB_DEP_BEGIN = "HB_DEP_BEGIN"
HB_DEP_END = "HB_DEP_END"

`mode com1 9600,N,8,1`
$com = File.open("com1")

conn_str = "driver={SQL Server};database=db_Anhui;server=127.0.0.1,1433;uid=xxx;pwd=xxx"

$sql = WIN32OLE.new('ADODB.Connection')
$sql.Open(conn_str)

is_geting_arr = false
is_geting_dep = false
$idx_arr = 0
$idx_dep = 0
$interval = 5

def add_arr(str)
	r = str.split(" ")
	print "#{$idx_arr+=1} ";p r
	$sql.Execute("INSERT INTO InTable VALUES('#{r[0]}','#{r[1]}','#{r[2]}','#{r[3]}')")
end

def add_dep(str)
	r = str.split(" ")
	print "#{$idx_dep+=1} ";p r
	$sql.Execute("INSERT INTO OutTable VALUES('#{r[0]}','#{r[1]}','#{r[2]}','#{r[3]}')")
end

loop do
  begin
    case str = $com.gets
		when /#{HB_ARR_BEGIN}/
			$internal = 0.2
			is_geting_arr = true
			$sql.Execute("DELETE from InTable")
		when /#{HB_ARR_END}/
			$internal = 0.2
			is_geting_arr = false
			$idx_arr = 0
		when /#{HB_DEP_BEGIN}/
			$internal = 0.2
			is_geting_dep = true
			$sql.Execute("DELETE from OutTable")
		when /#{HB_DEP_END}/
			$internal = 10
			is_geting_dep = false
			$idx_dep = 0
		else
			(sleep $interval;next) if !str || str == "" 
			if is_geting_arr
				add_arr(str)
			elsif is_geting_dep
				add_dep(str)
			else
				#do nothing!
			end
	end
	sleep $internal
  rescue =>e
    puts("ERROR : #{e.message} @ #{Time.now},retry it...")
    sleep(60)
    retry
  ensure
    #nothing
  end
end

串口操作使用了最原始的system功能,尽管ruby串口的库很多,但是借助

最简单的实现而同样很好的完成目的,不是很美吗?: )

你可能感兴趣的:(一对轻便小巧的收发器程序(ruby实现))