@@ -20,31 +20,52 @@ import { Logger } from '../../../../lib/logger/Logger';
2020import { CommandType } from './Command' ;
2121import { AMIConnection } from './ami/AMIConnection' ;
2222import { CommandFactory } from './ami/CommandFactory' ;
23- import { IVoipExtensionConfig , IVoipExtensionBase } from '../../../../../definition/IVoipExtension' ;
23+ import { IVoipConnectorResult } from '../../../../../definition/IVoipConnectorResult' ;
24+ import { IVoipService } from '../../../../sdk/types/IVoipService' ;
25+ import { IManagementConfigData , IVoipServerConfig , ServerType } from '../../../../../definition/IVoipServerConfig' ;
2426
2527const version = 'Asterisk Connector 1.0' ;
2628
2729export class CommandHandler {
2830 private connections : Map < CommandType , IConnection > ;
2931
32+ private service : IVoipService ;
33+
3034 private logger : Logger ;
3135
32- constructor ( ) {
36+ constructor ( service : IVoipService ) {
3337 this . logger = new Logger ( 'CommandHandler' ) ;
3438 this . connections = new Map < CommandType , IConnection > ( ) ;
39+ this . service = service ;
40+ }
3541
42+ async initConnection ( commandType : CommandType ) : Promise < void > {
3643 // Initialize available connections
3744 // const connection = new AMIConnection();
3845 const connection = new AMIConnection ( ) ;
39- /* TODO(Amol) : This information must come from
40- * an API/Database.
41- * Currently hardcoded. Hence commenting the code
46+ let config : IVoipServerConfig | null = null ;
47+ if ( commandType === CommandType . AMI ) {
48+ config = await this . service . getServerConfigData ( ServerType . MANAGEMENT ) ;
49+ }
50+ if ( ! config ) {
51+ this . logger . warn ( 'Management server configuration not found' ) ;
52+ throw Error ( 'Management server configuration not found' ) ;
53+ }
54+ /**
55+ * If we have the same type of connection already established, close it
56+ * and remove it from the map.
4257 */
43- connection . connect ( 'omni-asterisk.dev.rocket.chat' ,
44- '5038' ,
45- 'amol' ,
46- '1234' ) ;
47- this . connections . set ( CommandType . AMI , connection ) ;
58+ if ( this . connections . get ( commandType ) ?. isConnected ( ) ) {
59+ this . logger . error ( { msg : 'connection exists. Closing the connection.' } ) ;
60+ this . connections . get ( commandType ) ?. closeConnection ( ) ;
61+ this . connections . delete ( commandType ) ;
62+ }
63+ connection . connect ( config . host ,
64+ ( config . configData as IManagementConfigData ) . port . toString ( ) ,
65+ ( config . configData as IManagementConfigData ) . username ,
66+ ( config . configData as IManagementConfigData ) . password ,
67+ ) ;
68+ this . connections . set ( commandType , connection ) ;
4869 }
4970
5071 /* Executes |commandToExecute| on a particular command object
@@ -55,7 +76,7 @@ export class CommandHandler {
5576 * This function returns a promise. Caller can wait for the promise to resolve
5677 * or rejected.
5778 */
58- executeCommand ( commandToExecute : Commands , commandData : any ) : Promise < IVoipExtensionConfig | IVoipExtensionBase [ ] > {
79+ executeCommand ( commandToExecute : Commands , commandData ? : any ) : Promise < IVoipConnectorResult > {
5980 this . logger . debug ( { msg : `executeCommand() executing ${ Commands [ commandToExecute ] } ` } ) ;
6081 const command = CommandFactory . getCommandObject ( commandToExecute ) ;
6182 command . connection = this . connections . get ( command . type ) as IConnection ;
0 commit comments