Yado_tech

旅館+ITとはなんぞ

モーニングコールを作る① ファイルの読み出し編

簡単にフローを考える

【モーニングコールの設定】

①モーニングコール用の電話番号に電話を掛ける

(②)部屋番号をダイヤルする

③時間をダイヤルする

④#を押す

⑤AGIが実行され、DBにモーニングコールが登録される

【モーニングコールの読み出し】

①データを読みだす。

②時刻がいっしょならコールファイルを作成する

①〜②を1分ごとに実行する

簡単なほうから作っていくのでまずは【読み出し】を作ってみる

makecall.py

from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
import datetime
import shutil


#tableの作成
engine= create_engine("sqlite:///testdb.sqlite")
base = automap_base()

base.prepare(engine,reflect = True)

table = base.classes.wakeup

#sessionの作成

session = sessionmaker(bind=engine)()

#現在の時刻とその時刻に設定されたデータを探す

date = datetime.datetime.now().replace(second = 0,microsecond=0)
date = date.strftime("%Y-%m-%d %H:%M:%S")

result = session.query(table).filter_by(wake_Date=date).all()

#resultが存在していたらその部屋番号に電話を掛ける

for row in result:
    make_call(row.roomNo)
    
#ルームナンバーのCallfileを作成してOutgoingディレクトリに移動させる
def make_call(roomNo):
    date = datetime.date.today()
    callname = "wakeupcall_%s_%s.txt" % (date,roomNo)
    f = open(callname,'w')
    f.write("Channel: SIP/%s\n\
    MaxRetries: 3\n\
    RetryTime: 60\n\
    WaitTime: 30\n\
    Context: default\n\
    Extension: 300\n\
    Priority: 2\
    " % roomNo)
    f.close()
    shutil.copyfile("./%s" % callname ,"/var/spool/asterisk/outgoing/%s" % callname)

これをcrontab -eで追加する。 多分できる・・はず・・