The files contained in this repository can be downloaded to your computer using a svn client.
On Linux you simply type the command displayed below.
This URL has Read-Only access.
root / trunk / DomotiGaServer / CAsterisk.class @ 262
History | View | Annotate | Download (4 kB)
| 1 | ' Gambas class file |
|---|---|
| 2 | |
| 3 | ' Description: |
| 4 | ' Asterisk.class |
| 5 | ' Provide support for Asterisk PABX monitoring. (via manager API port) |
| 6 | |
| 7 | ' Development Status: |
| 8 | ' Just started to build this one. |
| 9 | ' Incoming call detection and Voicebox status implemented. |
| 10 | ' Tested with Asterisk 1.4.21.2 and Digium TDM400 |
| 11 | |
| 12 | ' Links: |
| 13 | ' http://www.voip-info.org/wiki/view/Asterisk+-+documentation+of+application+commands |
| 14 | |
| 15 | ' DomotiGa - an open source home automation program. |
| 16 | ' Copyright(C) 2008 Ron Klinkien |
| 17 | |
| 18 | ' Read file called COPYING for license details. |
| 19 | |
| 20 | PROPERTY TCPHost AS String |
| 21 | PROPERTY TCPPort AS Integer |
| 22 | PROPERTY AsteriskDebug AS Boolean |
| 23 | PROPERTY User AS String |
| 24 | PROPERTY Password AS String |
| 25 | PROPERTY Polltime AS Integer |
| 26 | |
| 27 | PRIVATE sTCPHost AS String |
| 28 | PRIVATE iTCPPort AS Integer |
| 29 | PRIVATE sUser AS String |
| 30 | PRIVATE sPassword AS String |
| 31 | PRIVATE iPolltime AS Integer |
| 32 | PRIVATE bAsteriskDebug AS Boolean |
| 33 | |
| 34 | PUBLIC hAsterisk AS NEW Socket |
| 35 | |
| 36 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 37 | ' connect to the host:port |
| 38 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 39 | PUBLIC FUNCTION Connect() AS Boolean |
| 40 | |
| 41 | ' try to close the connection |
| 42 | TRY hAsterisk.Close |
| 43 | |
| 44 | ' get a new one |
| 45 | hAsterisk = NEW Socket AS "Asterisk" |
| 46 | hAsterisk.Connect(sTCPHost, iTCPPort) |
| 47 | |
| 48 | ' all ok |
| 49 | RETURN TRUE |
| 50 | |
| 51 | CATCH ' some errors |
| 52 | Main.WriteLog(("Asterisk Error: ") & ERROR.Text)
|
| 53 | RETURN FALSE |
| 54 | |
| 55 | END |
| 56 | |
| 57 | PUBLIC SUB Asterisk_Ready() |
| 58 | |
| 59 | Main.WriteLog(("Asterisk TCP socket connected."))
|
| 60 | |
| 61 | ' authenticate ourself to API interface |
| 62 | ManagerLogin() |
| 63 | |
| 64 | END |
| 65 | |
| 66 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 67 | ' disconnect from the host |
| 68 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 69 | PUBLIC FUNCTION Disconnect() AS Boolean |
| 70 | |
| 71 | ' try to close the connection |
| 72 | TRY hAsterisk.Close |
| 73 | Main.WriteLog(("Asterisk TCP socket port close."))
|
| 74 | |
| 75 | ' all ok |
| 76 | RETURN TRUE |
| 77 | |
| 78 | CATCH ' some errors |
| 79 | Main.WriteLog(("Asterisk Error: ") & ERROR.Text)
|
| 80 | RETURN FALSE |
| 81 | |
| 82 | END |
| 83 | |
| 84 | PUBLIC SUB Asterisk_Error() |
| 85 | |
| 86 | DIM sString AS String = "Asterisk: " |
| 87 | |
| 88 | ' handle error |
| 89 | SELECT CASE hAsterisk.Status |
| 90 | CASE Net.CannotCreateSocket |
| 91 | Main.WriteLog(sString & ("The system does not allow to create a socket."))
|
| 92 | CASE Net.HostNotFound |
| 93 | Main.WriteLog(sString & ("Host '") & sTCPHost & ("' not found."))
|
| 94 | CASE Net.ConnectionRefused |
| 95 | Main.WriteLog(sString & ("Unable to connect. Connection refused."))
|
| 96 | CASE Net.CannotRead |
| 97 | Main.WriteLog(sString & ("Error reading data."))
|
| 98 | CASE Net.CannotWrite |
| 99 | Main.WriteLog(sString & ("Error writing data."))
|
| 100 | END SELECT |
| 101 | |
| 102 | END |
| 103 | |
| 104 | PUBLIC SUB Asterisk_Read() |
| 105 | |
| 106 | DIM sData AS String |
| 107 | |
| 108 | IF hAsterisk.Status = Net.Connected THEN |
| 109 | LINE INPUT #hAsterisk, sData |
| 110 | IF InStr(sData, "Asterisk Call Manager") THEN Main.WriteLog(sData) |
| 111 | IF InStr(sData, "Response: Error") OR InStr(sData, "Response: Success") THEN |
| 112 | LINE INPUT #hAsterisk, sData |
| 113 | IF Main.bAsteriskDebug THEN Main.WriteDebugLog("[Asterisk] " & sData)
|
| 114 | ' put support for parsing incoming events/calls here |
| 115 | END IF |
| 116 | END IF |
| 117 | |
| 118 | END |
| 119 | |
| 120 | PUBLIC SUB ManagerLogin() |
| 121 | |
| 122 | DIM sCommand AS String |
| 123 | |
| 124 | sCommand = "ACTION: LOGIN\r\nUSERNAME: " & sUser & "\r\nSECRET: " & sPassword & "\r\nEVENTS: ON\r\n\r\n" |
| 125 | PRINT #hAsterisk, sCommand |
| 126 | |
| 127 | END |
| 128 | |
| 129 | ' implement properties |
| 130 | PRIVATE FUNCTION TCPHost_Read() AS String |
| 131 | |
| 132 | RETURN sTCPHost |
| 133 | |
| 134 | END |
| 135 | |
| 136 | PRIVATE SUB TCPHost_Write(Value AS String) |
| 137 | |
| 138 | sTCPHost = Value |
| 139 | |
| 140 | END |
| 141 | |
| 142 | PRIVATE FUNCTION TCPPort_Read() AS Integer |
| 143 | |
| 144 | RETURN iTCPPort |
| 145 | |
| 146 | END |
| 147 | |
| 148 | PRIVATE SUB TCPPort_Write(Value AS Integer) |
| 149 | |
| 150 | iTCPPort = Value |
| 151 | |
| 152 | END |
| 153 | |
| 154 | PRIVATE FUNCTION User_Read() AS String |
| 155 | |
| 156 | RETURN sUser |
| 157 | |
| 158 | END |
| 159 | |
| 160 | PRIVATE SUB User_Write(Value AS String) |
| 161 | |
| 162 | sUser = Value |
| 163 | |
| 164 | END |
| 165 | |
| 166 | PRIVATE FUNCTION Password_Read() AS String |
| 167 | |
| 168 | RETURN sPassword |
| 169 | |
| 170 | END |
| 171 | |
| 172 | PRIVATE SUB Password_Write(Value AS String) |
| 173 | |
| 174 | sPassword = Value |
| 175 | |
| 176 | END |
| 177 | |
| 178 | PRIVATE FUNCTION Polltime_Read() AS Integer |
| 179 | |
| 180 | RETURN iPolltime |
| 181 | |
| 182 | END |
| 183 | |
| 184 | PRIVATE SUB Polltime_Write(Value AS Integer) |
| 185 | |
| 186 | iPolltime = Value |
| 187 | |
| 188 | END |
| 189 | |
| 190 | PRIVATE FUNCTION AsteriskDebug_Read() AS Boolean |
| 191 | |
| 192 | RETURN bAsteriskDebug |
| 193 | |
| 194 | END |
| 195 | |
| 196 | PRIVATE SUB AsteriskDebug_Write(Value AS Boolean) |
| 197 | |
| 198 | bAsteriskDebug = Value |
| 199 | |
| 200 | END |
