{"id":425,"date":"2011-01-28T00:30:38","date_gmt":"2011-11-10T13:51:50","guid":{"rendered":"http:\/\/kraftek.com\/blog\/call-progress-for-dahdi-calls-in-asterisk-java\/"},"modified":"2011-01-28T00:30:38","modified_gmt":"2011-11-10T13:51:50","slug":"call-progress-for-dahdi-calls-in-asterisk-java","status":"publish","type":"post","link":"\/blog\/index.php\/call-progress-for-dahdi-calls-in-asterisk-java\/","title":{"rendered":"Call progress for dahdi calls in asterisk-java"},"content":{"rendered":"<p>I wanted to generate a call from a script, and deliver a voice message, through a DAHDI channel, to a pots line.<br \/>\nThis class does it, it tells you in the end if the line was busy, if it was never answered or if the call succeeded.<br \/>\nThis is, if you trust dahdi callprogress, which is really broken&#8230; this example works better for SIP calls.<br \/>\nimport org.asteriskjava.live.AsteriskChannel;<br \/>\nimport org.asteriskjava.live.DefaultAsteriskServer;<br \/>\nimport org.asteriskjava.live.LiveException;<br \/>\nimport org.asteriskjava.live.ManagerCommunicationExcept ion;<br \/>\nimport org.asteriskjava.live.OriginateCallback;<br \/>\nimport org.asteriskjava.manager.action. OriginateAction;<br \/>\nimport org.asteriskjava.util.Log;<br \/>\nimport org.asteriskjava.util.LogFactory;<br \/>\npublic class AsteriskCall implements OriginateCallback {<br \/>\nprivate final Log logger = LogFactory.getLog(getClass());<br \/>\nprivate DefaultAsteriskServer asteriskServer = null;<br \/>\nprivate String strChannelStatus = &#8220;NoStatusYet&#8221;;<br \/>\nprivate String number=&#8221;&#8221;;<br \/>\nprivate String message=&#8221;&#8221;;<br \/>\npublic AsteriskCall() {<br \/>\nthis.asteriskServer = new DefaultAsteriskServer(&#8220;somewhere.org&#8221;,&#8221;admin&#8221;,&#8221;mypass&#8221;);<br \/>\n}<br \/>\npublic OriginateAction setupOriginate(String number,String message) {<br \/>\nOriginateAction originateAction = new OriginateAction();<br \/>\noriginateAction. setChannel(&#8220;DAHDI\/1\/&#8221;+number);<br \/>\noriginateAction. setApplication(&#8220;Playback&#8221;);<br \/>\noriginateAction. setData(message) ;<br \/>\noriginateAction. setActionId(number);<br \/>\noriginateAction. setAsync(Boolean.TRUE);<br \/>\noriginateAction. setTimeout(new Integer(30000));<br \/>\nreturn originateAction;<br \/>\n}<br \/>\npublic void originate() {<br \/>\nOriginateAction originateAction = this.setupOriginate(number,message);<br \/>\n\/\/ System.out.println(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212; Calling: &#8220;+originateAction.getChannel()+&#8221;, Sending:<br \/>\n&#8220;+originateAction.getData());<br \/>\ntry {<br \/>\nthis.asteriskServer.originateAsync(originateAction, this);<br \/>\n}<br \/>\ncatch (ManagerCommunicationException e) {<br \/>\nlogger.error(&#8220;ManagerCommunicationException&#8221;);<br \/>\n}<br \/>\n15\/433<br \/>\nBoolean out=false;<br \/>\nString previousStatus=&#8221;&#8221;;<br \/>\nwhile(!out) {<br \/>\nif<br \/>\n(this.strChannelStatus.equals(&#8220;Busy&#8221;)||this.strChannelStatus.equals(&#8220;Success&#8221;)||this.strChannelStatus.equals(&#8220;No<br \/>\nAnswer&#8221;)||this.strChannelStatus. equals(&#8220;Failed&#8221;) ) {<br \/>\nout=true;<br \/>\n}<br \/>\ntry {<br \/>\nString currentStatus=this.strChannelStatus;<br \/>\nif (!currentStatus.equals(previousStatus)){<br \/>\nlogger.info(&#8220;Current Channel State: &#8220;+currentStatus) ;<br \/>\npreviousStatus=currentStatus;<br \/>\n}<br \/>\nThread.sleep(1000);<br \/>\n}<br \/>\ncatch (InterruptedException e) {<br \/>\nlogger.info(&#8220;Interrupted.&#8221;);<br \/>\n}<br \/>\n}<br \/>\ntry {<br \/>\nThread.sleep(3000);<br \/>\n} catch (InterruptedException e) {<br \/>\n\/\/ TODO Auto-generated catch block<br \/>\ne.printStackTrace();<br \/>\n}<br \/>\nlogger.info(&#8220;Final Verdict: &#8220;+this.strChannelStatus);<br \/>\n}<br \/>\n@Override<br \/>\npublic void onBusy(AsteriskChannel channel) {<br \/>\nthis.strChannelStatus = &#8220;Busy&#8221;;<br \/>\n}<br \/>\n@Override<br \/>\npublic void onDialing(AsteriskChannel channel) {<br \/>\nthis.strChannelStatus = &#8220;Dialing&#8221;;<br \/>\n}<br \/>\n@Override<br \/>\npublic void onFailure(LiveException cause) {<br \/>\nthis.strChannelStatus = &#8220;Failed&#8221;;<br \/>\n}<br \/>\n@Override<br \/>\npublic void onNoAnswer(AsteriskChannel channel) {<br \/>\nthis.strChannelStatus = &#8220;No Answer&#8221;;<br \/>\n}<br \/>\n@Override<br \/>\npublic void onSuccess(AsteriskChannel channel) {<br \/>\nthis.strChannelStatus = &#8220;Success&#8221;;<br \/>\n}<br \/>\npublic void setNumber(String n){<br \/>\n16\/433<br \/>\nnumber=n;<br \/>\n}<br \/>\npublic void setMessage(String m){<br \/>\nmessage=m;<br \/>\n}<br \/>\npublic static void main(String[] args) throws Exception {<br \/>\nif (args.length&lt;2){<br \/>\nSystem.out.println(&#8220;Usage: Call &lt;number&gt; &lt;message&gt;&#8221;);<br \/>\nSystem.exit(1);<br \/>\n}<br \/>\nAsteriskCall call = new AsteriskCall();<br \/>\ncall.setNumber(args[0]);<br \/>\ncall.setMessage(args[1]);<br \/>\ncall.originate() ;<br \/>\n}<br \/>\n}<br \/>\n17\/433<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to generate a call from a script, and deliver a voice message, through a DAHDI channel, to a pots line. This class does it, it tells you in the end if the line was busy, if it was never answered or if the call succeeded. This is, if you trust dahdi callprogress, which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-425","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=425"}],"version-history":[{"count":0,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/425\/revisions"}],"wp:attachment":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}