Ok. I'm trying do some app. I'm use nodejs + nestjs. I want do request on some url and my controller must connect to Asterisk over AMI and get some info (e.g QueueStatus members). I use async asterisk-ami-client
I've some controller
In console log i watch true value, but how i can return it as result getExtStatus?
@Controller("ami")
export class AmiController {
constructor(private amiService: AmiService) {
}
@Get("ext_status")
async getExtStatus(@Body() user: User) {
let client = new AmiClient();
client.connect(user.wssServer.amiuser, user.wssServer.amisecret, {host: user.wssServer.server, port:5038})
.then(amiConnection => {
client
.on('event', event => {
if (event.Event === "QueueMember" && event.Name === user.extensionNumber){
console.log(event.Status)
return event.Status
}
})
})
.action({Action: "QueueStatus"})
}
}
UPD: I've partical solve my issue, but this solution work once. If send some request few time he is return correctly data. Any idea?
@Controller("ami")
export class AmiController {
private my_field = null;
constructor(private amiService: AmiService) {
}
@Get("ext_status")
async getExtStatus(@Body() user: User) {
let client = new AmiClient();
client.connect(user.wssServer.amiuser, user.wssServer.amisecret, { host: user.wssServer.server, port: 5038 })
.then(amiConnection => {
client
.on("event", event => {
if (event.Event === "QueueMember" && event.Name === user.extensionNumber) {
this.my_field = event;
}
})
.action({ Action: "QueueStatus" });
});
return this.getField();
}
getField() {
const r = this.my_field;
return { message: r };
}
}
I solved this issue. This code works fine
@Injectable()
export class AmiService {
private extensionInfo = null;
constructor(@InjectModel(User) private userRepository: typeof User) {}
async getExtensionStatus(user: User) {
let client = new AmiClient();
client.connect(user.wssServer.amiuser, user.wssServer.amisecret, {host: user.wssServer.server, port: 5038})
.then(amiConnection => {
client
.on("event", event => {
if (event.Event === "QueueMember" && event.Name === user.extensionNumber) {
this.extensionInfo = event
}
})
.action({Action: "QueueStatus"});
});
return new Promise((resolve, reject) => {
setTimeout(() => {
if (this.extensionInfo != null) {
const r = this.extensionInfo;
this.extensionInfo = null;
resolve(r)
} else {
resolve({message: "Extension not found"})
}
}, 1000)
})
}
}