Open imweijh opened 1 year ago
require "dbf" require "jruby-mmap" require "inifile" require "time" require "teerb" myini = IniFile.load('nqhb.ini') NQHBDBF = myini["nqhb"]["filename"] checkcjdate = myini["nqhb"]["checkcjdate"] timebgn = myini["nqhb"]["timebgn"] timeend = myini["nqhb"]["timeend"] # puts "JRUBY_VERSION: " + JRUBY_VERSION puts RUBY_DESCRIPTION puts "#{Time.now}: 北交所回报文件 = " + NQHBDBF puts "#{Time.now}: 是否检查日期 = " + checkcjdate.to_s puts "#{Time.now}: 工作开始时间 = " + timebgn puts "#{Time.now}: 工作结束时间 = " + timeend BYTE_SIZE = 2048 MMAPFILE = "./_MMAP_NQHB.DAT" OFFSET1 = 0 OFFSET2 = 20 $running = true trap("SIGINT") { # puts "ctrl+c..." $running = false } while true && $running if Time.now > Time.parse(timebgn) and Time.now < Time.parse(timeend) begin nqhbdatafile = "./dat/" + "nqhbdata" + Time.now.strftime("%Y%m%d") + ".txt" log_file = File.open(nqhbdatafile, "a") rescue => ex puts "#{Time.now}: #{ex.message}" sleep 1 retry end tee = TeeRb::Tee.new(log_file) mmap = Mmap::ByteBuffer.new(MMAPFILE, BYTE_SIZE) mmap.position = OFFSET1 datemmap = mmap.get_int # puts datemmap mmap.position = OFFSET2 recordsmmap = mmap.get_long # puts recordsmmap todayint = Time.now.strftime("%Y%m%d").to_i if todayint > datemmap mmap.position = OFFSET1 mmap.put_int(todayint) puts "#{Time.now}: datemmap updated to #{todayint}." mmap.position = OFFSET2 recordsmmap = 0 mmap.put_long(0) puts "#{Time.now}: recordsmmap reset to zero." end mmap.position = OFFSET1 datemmap = mmap.get_int # puts datemmap mmap.position = OFFSET2 recordsmmap = mmap.get_long # puts recordsmmap i = recordsmmap # puts "start from record at #{i}" # sleep 1 begin table = DBF::Table.new( NQHBDBF, nil, 'cp936' ) rescue => ex puts "#{Time.now}: !!! #{ex.message} !!!" sleep 10 retry end counts = table.record_count # puts "max records = #{counts}" # j = i while i < counts && $running do record = table.find(i) ### 在此先判断record是否为nil,非常重要,因为find返回可能为nil导致程序异常 ### if record then if record.hbzqdm == "0" or record.hbcjrq == "0" or record.hbcjsj.to_s.length < 6 i += 1 mmap.position = OFFSET2 mmap.put_long(i) next end # idx = '%09d' % i gddm = record.hbzqzh market = 5 hbywlb = record.hbywlb # 业务类别 zqdm = record.hbzqdm # 证券代码 cjdate = record.hbcjrq.strftime("%Y%m%d") cjtime = record.hbcjsj.to_s[0..5] cjtime = cjtime[0..1] + ":" + cjtime[2..3]+ ":" + cjtime[4..5] # HHMMSS变成HH:MM:SS cjsl = record.hbcjsl.to_i.abs # 撤单数量是负数,改为正数,和深圳上海一致 cjjg = record.hbcjjg wtid = record.hbhtxh cjid = record.hbcjhm remark = record.hbcdyy fdyy = record.hbdfzh # 废单原因 rzrq = record.hbrzrq # 空格表示普通交易,1 表示为融资交易,2 表示为融券交易 pcbz = record.hbpcbz # 空格表示普通交易,3 表示属于强制平仓 if cjdate.to_i != todayint and checkcjdate tee.disable print "#{Time.now}: find history data...\r\n" sleep 10 break end tee.enable # print "#{idx}|#{gddm}|#{market}|#{hbywlb}|#{zqdm}|#{cjdate} #{cjtime}|#{cjsl}|#{cjjg}|#{wtid}|#{cjid}|#{remark}|#{fdyy}|#{rzrq}|#{pcbz}|\r\n" print "#{gddm}|#{market}|#{hbywlb}|#{zqdm}|#{cjdate} #{cjtime}|#{cjsl}|#{cjjg}|#{wtid}|#{cjid}|#{remark}|#{fdyy}|#{rzrq}|#{pcbz}|\r\n" log_file.flush # if i == j # sleep 2 # end # if hbywlb == "0C" # sleep 1 # end i += 1 mmap.position = OFFSET2 mmap.put_long(i) # mmap.position = OFFSET2 # puts "mmap.get_long = #{mmap.get_long}" # sleep 1 end end # puts "loop end. i = #{i}......" # puts "." tee.disable log_file.close table.close mmap.close sleep 0.5 else sleep 0.5 end end puts "exit..."