04/20(水) 2011
地震でbeep音
SignalNowExpress のログ更新を見て音を鳴らして逃げよう
signalnowClass.rb
#!ruby -Ks # -*- encoding: "Shift_JIS" -*- require'kconv' $id="tabasano" def beep0 print"\a" end def kyori a,b,dep x=(a[0]-b[0])**2 y=(a[1]-b[1])**2 d=(x+y)**0.5 le=d*100*0.6471 # ns=a[0]>b[0] ? "N" :"S" # ew=a[0]>b[0] ? "E" :"W" ns=a[0]>b[0] ? "北" :"南" ew=a[0]>b[0] ? "東" :"西" if le<20 ns="" ew="直近" elsif le>220 ns="" if le<300 ew=ew+"遠" elsif le<400 ew=ew+"超" elsif le<450 ew=ew+"重" elsif le<500 ew=ew+"虚" else ew=ew+"無" end end [(le**2+dep**2)**0.5,"#{ns}#{ew}"] end def beepalert n return if n<5 n.times{ beep0 sleep 0.2 } sleep 0.1 end def beep n,m=1,ti=3 beep0 if n==0 m.times{ 2.times{ n.times{ beep0 } sleep 0.17 } sleep ti } end def run s,d # end class SignalNow @@logf='c:/Documents and Settings/'+$id+'/Application Data/SignalNowExpress/sne.log' @@initial="sne-initial.txt" @@idsLog="sne-id-log.txt" def initialize @watchinterval=0.4 @lastIds=[] @ids=[] @idinlog=[] @calcres=[] if not File.exist?(@@logf) puts "please set log file name." exit end puts File.size(@@logf) if $DEBUG getidlog ids putidlog end def setinterval v @watchinterval=v end def lastTouchTime File.mtime(@@logf) end def newsigAlert beep0 end def bigAlert n beepalert n end def sindoAlert n beep n,[0,1,2,4,8,12,16,20,20,20,20,20,20][n] end def alertBySindo sindo,text,alertflag if sindo>3 && alertflag bigAlert sindo run sindo,text bigAlert sindo end moremes sindo sindoAlert sindo if alertflag end def moremes sindo case sindo when 3 mes="地震です" when 4 mes="大きい地震です" when 5 mes="非常に大きい地震です" when 6 mes="危険です、非常に大きい地震です" when 7..100 mes="ありえない、危険です、非常に大きすぎる地震です" else mes="地震です?" end STDERR.puts mes end def alerttest s alertBySindo s,"test des.",true end def loop watchinterval=@watchinterval getidlog @lasttime=lastTouchTime beep 0 alias get getRecent lastone="" while 1 sleep watchinterval t0=Time.now t0str=t0.strftime("%Y/%m/%d.%H:%M:%S\r") STDERR.print t0str t=lastTouchTime if t>@lasttime alertflag= (t>t0-120) @lasttime=t newsigAlert if alertflag idlist=ids current=@data.select{|i|i=~/ID=#{idlist[-1]}\(/}[-1] if lastone != current STDERR.puts "",t0.strftime('%H:%M:%S') showone current end lastone=current sa=idlist-@idinlog if sa !=[] current=~/震度=([^,]*)/ sindo=$1.to_i text=dform(current) alertBySindo sindo,text,alertflag putidlog end end end end alias start loop def getidlog if File.exist?(@@idsLog) @idinlog=File.readlines(@@idsLog).map{|i|i.chomp} else @idinlog=[] end end def putidlog oldd=@idinlog newd=@ids-oldd if newd != [] open(@@idsLog,"a"){|f| f.puts newd } @idinlog+=newd end STDERR.puts newd end def get log=File.readlines(@@logf).map{|i|i.tosjis} end def getRecent log=File.readlines(@@logf) (log.size>400 ? log[-400..-1] : log ).map{|i|i.tosjis} end def getsig exp="受信" puts "exp:#{exp}" if $DEBUG get.select{|i|i=~/#{exp}/s} end def getnewestsig getsig[-1] end def getFirstAlerts @calcres=getsig('計算結果.*\(1\)') end def getAlerts @calcresAll=getsig('計算結果') end def calcres @calcres==[] ? getFirstAlerts : @calcress end def last getFirstAlerts[-1] end def id2list id li=getAlerts li.select{|i|i=~/ID=#{id}\(/} end def lasts id2list(alert2id(last)) end def search d li=getAlerts li.select{|i|i=~/#{d}/s} end def searchAll d li=get li.select{|i|i=~/#{d}/s} end def dform d d=~/地震ID=([^\(]*)\((.*)\).*発生時刻=([^,]*).*=([^,]*)\/([^,]*).*マグニチュード=([^,]*).*=([^,]*).*=([^,]*)\/([^,]*).*震度=([^,]*).*=(.{1,3}\..).*=(.{1,3}\..)/ if $& sindo=$10.to_f mag=$6 dep=$7.to_i ido=$4.to_f kei=$5.to_f idoh=$8.to_f keih=$9.to_f fkyori=kyori([ido,kei],[idoh,keih],dep) kyori=format "%.1f",fkyori[0] hogaku=fkyori[1] ti=$3[0..-1] pw=$11.to_f sw=$12.to_f id=$1 idsub=$2 "#{d[11,8]} (#{idsub})\t#{ti} m#{mag} -#{dep} #{format "%.1f",ido}/#{format "%.1f",kei} #{hogaku} s#{sw}\t#{format "%.2f",sindo}" elsif d=~/受信: (.*)(ND.*)(.{3})N(.{3})E(.{4})(.*)/ hd=$1[0,6] ymd=$1[6,6] hms=$1[12,6] ymd2=$1[18,6] hms2=$1[24,6] nd=$2 numf= $3[0,1]=="0" ? "_" : $3[0,1] num=$3[1..-1] ido=$4 kei=$5 dep=$6[0,3] sin=$6[3,2] rest=$6[5..-1] "#{hd} #{ymd}.#{hms} #{ymd2}.#{hms2} #{nd} #{numf}.#{num} #{ido}/#{kei} -#{dep}^#{sin} #{rest}" elsif d=~/地震表示/ || d=~/情報:/ else d end end def showone d puts dform(d) end def alerttoplist m=getsig.map{|i|i=~/受信: (.{6})/;$1} k=m.uniq hs={} k.each{|i| hs[i]=0 m.each{|j| j== i ? hs[i]+=1 : "" } } p hs end def an m=getsig.map{|i| dform i } puts m end def list al=getFirstAlerts STDERR.puts al[-1] if $DEBUG al.map{|d| dform d } end def allList al=getAlerts STDERR.puts al[-1] if $DEBUG al.map{|d| dform d } end def showlist puts self.list end def showAllList puts self.allList end def showAllSig puts getsig end def show puts get.map{|d| dform d }-[nil] end def alert2id a a=~/ID=([^\(]*)/ $1 end def getids @data=getAlerts @ids=@data.map{|i| self.alert2id(i)}.uniq @ids end def ids getids @newids=@ids-@lastIds @lastIds=@ids @ids end def newids ids @newids end end if __FILE__ ==$0 s=SignalNow.new h="時刻 (第何報) 発生 マグニチュード 震源深さ 位置 方角 S波到達秒数 震度" puts h s.showAllList puts h end
受信データ一覧表示
> ruby signalnowClass.rb
震度4によるアラートテスト
> ruby -r signalnowClass -e "s=SignalNow.new;s.alerttest(4)"
ループ
> ruby -r signalnowClass -e "s=SignalNow.new;s.setinterval(0.2);s.start"
など。ruby1.8.6だと正規表現うしろにsをつければいいとおもいます。というか1.8.6メインでやってるんですが..。
beep音チェックはどうするかな。”type c:\windows\Media\chord.wav”だとか。
beep音、その2
ひさびさにcpp言語でも。おぼえてないー
filetimecheckloop.cpp
//-------------------------------------- // filetimecheckloop.cpp ver.0.1 // // ファイルtimestamp check loop // // // 2011/04/20 // // #include <windows.h> #include <iostream.h> #include <fstream.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <time.h> const nmax=1000; const int buffmax=2048; char file[nmax]=""; char form[nmax]="%04d/%02d/%02d %02d:%02d:%02d";//書式 char infname[buffmax]=""; char buff[buffmax*5]; char nengappi[nmax]; char* comparetimestamp() { char command0[nmax+nmax]; FILETIME ftime,ltime; SYSTEMTIME sytime; LPSYSTEMTIME stime=&sytime; HANDLE file= CreateFile( infname, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); GetFileTime(file,NULL ,NULL,&ftime); FileTimeToLocalFileTime(&ftime,<ime); FileTimeToSystemTime(<ime,stime); sprintf( nengappi, form, stime->wYear, stime->wMonth, stime->wDay, stime->wHour, stime->wMinute, stime->wSecond ); return nengappi; } mybeep() { int b=880; Beep(b,48); for(int i=1;i*220<4400;i++){ Sleep(24); Beep(b+i*220,24); } } //------------------------------------------- int main(int argc, char** argv) { sprintf(infname,argv[1]); cout<<"file: "<<infname<<endl; char ngp[nmax],old_ngp[nmax]; for( int i=0;;i++){ strcpy(ngp,comparetimestamp()); if(strcmp(ngp,old_ngp)){ cout<<ngp<<endl; mybeep(); strcpy(old_ngp,ngp); } else { // cout<<","; } Sleep(200); } return(0); }
実行例
> filetimecheckloop "c:\Document .. \nantoka"
ここに置きました
コメント
トラックバック - http://rubyist.g.hatena.ne.jp/hatecha/20110420
カレンダー
- PyCrypto:GAE : ローカルだと動かない。 - Windows Live
- Google App Engine Blog: App Engine SDK 1.3.5 Released With New Task Queue, Python Precompilation, and Blob Features
- 線形回帰モデルとか - Standard ML of Yukkuri
- Czajkowski ? Blog Archive ? LoCo Directory Meeting
- The Best Web Development Frameworks
- 2010-06-30 - Webと文字
- Android アプリケーションを強制終了させる ? BPS株式会社 開発ブログ Beyond Perspective Solutions LTD.
- Python cookbook - Google ブックス
- Herald Tribune (HeraldTribune) on Twitter
- Ajaxian ? jQuery.fn.webkitTransform: bananas on the skew-whiff
- rst2pdfの日本語周り設定 - typeof(undefined)
- twoauth/oauth.py at master from techno’s python-twoauth - GitHub
- TRANSLATION LOG トマス・ピンチョン 1973全米図書賞授賞式 を翻訳する
- App Engine: Entity life cycle webhooks in the Datastore admin interface
- win-django.com - 複数のmodelを結合する 1