#!/usr/bin/env python
'''
Copyright (C) 2010-2014, Digium, Inc.
David Vossel <dvossel@digium.com>
Matt Jordan <mjordan@digium.com>

This program is free software, distributed under the terms of
the GNU General Public License Version 2.
'''

import sys
import os
import signal
import subprocess
import logging

from twisted.application import service, internet
from twisted.internet import reactor, defer
from starpy import manager
from starpy import fastagi

sys.path.append("lib/python")
from asterisk.test_case import TestCase

LOGGER = logging.getLogger(__name__)

class ChanSpyBarge(TestCase):
    def __init__(self):
        super(ChanSpyBarge, self).__init__()
        self.last_step = ""
        self.passed = False
        self.numSpyEvents = 0
        self.expectedSpyEvents = 3
        self.talkDetected = 0
        self.test_to = 25

        self.create_asterisk()
        self.create_fastagi_factory()

        ast_conf_options = {
            "transmit_silence" : "yes",
        }

        self.talkingaudio = os.path.join(os.getcwd(), "%s/sounds/talking" % self.test_name)
        self.audiofile1 = self.ast[0].get_path("astspooldir", "tmp", "testaudio1")

    def fastagi_connect(self, agi):
        LOGGER.info("GOT PASS RESULTS!!!")
        sequence = fastagi.InSequence()
        sequence.append(agi.execute, "HangUp")
        sequence.append(agi.finish)
        self.passed = True
        reactor.callLater(1, self.readResult)
        return sequence()

    def readResult(self):
        self.stop_reactor()
        LOGGER.info("Reading results")
        self.ast[0].cli_exec("core show locks")   # get lock output in case of deadlock before tearing down.
        self.ast[0].cli_exec("core show channels")# if channels are still up for some reason, we want to know that as well

        self.ast[0].cli_exec("core show globals") # The global variables here hold failure conditions

        if self.passed == True:
            LOGGER.info('SIP ChanSpy test PASSED!')
        else:
            LOGGER.error('SIP ChanSpy Test FAILED')

    def chanspyEvent(self, ami, event):
        LOGGER.info('Received ChanSpyStart event')
        self.numSpyEvents += 1
        if event['spyeechannel'].count('end_a') > 0:
            reactor.callLater(3, self.aHangup)
            reactor.callLater(4, self.bHangup)

    def ami_connect(self, ami):
        self.ami = ami
        self.ami.registerEvent('ChanSpyStart', self.chanspyEvent)

        self.ami.setVar(channel = "", variable = "TESTAUDIO1", value = self.audiofile1)
        self.ami.setVar(channel = "", variable = "TALK_AUDIO", value = self.talkingaudio)

        reactor.callLater(1, self.callChanSpy)
        reactor.callLater(2, self.aCall)


    def aCall(self):
        LOGGER.info("A Calling into Wait")
        self.pja.stdin.write("m\n")
        self.pja.stdin.write("sip:play_exten@127.0.0.1:5060\n")

    def bCall(self):
        LOGGER.info("B Calling into Playback")
        self.pjb.stdin.write("m\n")
        self.pjb.stdin.write("sip:play_exten@127.0.0.1:5060\n")

    def aHangup(self):
        LOGGER.info("Hanging up A")
        self.pja.stdin.write("h\n")

    def bHangup(self): #calls into chanspy extension and plays audio to A using Barge
        LOGGER.info("Hanging up B")
        self.pjb.stdin.write("h\n")
        reactor.callLater(2, self.verifyAudio)

    def startProcesses(self):
        LOGGER.info("Starting Processes")
        playfilearg = "--play-file=%s.wav" % (self.talkingaudio)
        self.pja = subprocess.Popen(['pjsua', '--local-port=5065', '--auto-answer=200', '--null-audio', '--auto-loop'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        self.pjb = subprocess.Popen(['pjsua', '--local-port=5066', '--auto-answer=200', playfilearg, '--null-audio', '--auto-play'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

    def stopProcesses(self):
        LOGGER.info("Stopping Processes")
        os.kill(self.pja.pid, signal.SIGKILL)
        os.kill(self.pjb.pid, signal.SIGKILL)

    def callChanSpy(self):
        LOGGER.info("Placing call to ChanSpy extension.")
        self.ast[0].cli_originate("SIP/end_b extension chanspytest@test")

    def verifyAudio(self):
        LOGGER.info("Verifying Audio")
        self.ast[0].cli_originate("Local/play_recording@test extension detect_audio@test")

    def stop_asterisk(self):
        if not self.passed:
            self.readResult()

    def run(self):
        self.create_ami_factory()


def main():
    test = ChanSpyBarge()
    test.startProcesses()
    reactor.run()
    test.stopProcesses()
    if test.passed != True:
        return 1
    return 0

if __name__ == "__main__":
    sys.exit(main() or 0)


# vim:sw=4:ts=4:expandtab:textwidth=79
