Skip to content

Commit

Permalink
fix: expand readme
Browse files Browse the repository at this point in the history
  • Loading branch information
JS-AK committed Jan 28, 2023
1 parent b9a8ea0 commit 572324d
Showing 2 changed files with 43 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# @2people-it/asterisk-ami-adapter

Asterisk-ami-adapter based on native node modules without external dependiences
82 changes: 41 additions & 41 deletions src/lib/asterisk-ami-adapter.ts
Original file line number Diff line number Diff line change
@@ -105,42 +105,26 @@ export class AsteriskAmiAdapter extends EventEmitter {
}
}

#send(data: Types.TAmiMessageIn, callback?: Types.TCallBack) {
if (!data.ActionID) data.ActionID = this.#getFormatedUuid();

const actionID = data.ActionID;
#generateSocketData(data: Types.TAmiMessageToSocket): string {
let str = "";

if (this.#socket?.writable) {
this.#debug("----- SEND ----");
const payload = this.#generateSocketData(data);
for (const i in data) {
const value = data[i];

this.#debug(payload);
if (Array.isArray(value)) {
const strings = value.map((e) => i + ": " + e);

if (callback) {
this.#callbacksMap.set(actionID, {
callback,
timer: setTimeout(() => {
if (this.#callbacksMap.has(actionID)) {
callback(new Error("Ami server did not get answer"), null);
this.#callbacksMap.delete(actionID);
}
}, 10000),
});
str = str.concat(strings.join(this.#NEW_LINE)) + this.#NEW_LINE;
} else {
str += (i + ": " + value + this.#NEW_LINE);
}

this.#socket.write(
payload,
this.#encoding,
// cb,
);

this.#debug("----- SENDED ----");
} else {
this.#debug("cannot write to Asterisk Socket");
this.emit("ami_socket_unwritable");
}

return str + this.#NEW_LINE;
}

#getFormatedUuid = randomUUID;

#processData(data: string) {
if (data.substring(0, 21) === "Asterisk Call Manager") {
data = data.substring(data.indexOf(this.#NEW_LINE) + 2);
@@ -196,24 +180,40 @@ export class AsteriskAmiAdapter extends EventEmitter {
return result;
}

#getFormatedUuid = randomUUID;
#send(data: Types.TAmiMessageIn, callback?: Types.TCallBack) {
if (!data.ActionID) data.ActionID = this.#getFormatedUuid();

#generateSocketData(data: Types.TAmiMessageToSocket): string {
let str = "";
const actionID = data.ActionID;

for (const i in data) {
const value = data[i];
if (this.#socket?.writable) {
this.#debug("----- SEND ----");
const payload = this.#generateSocketData(data);

if (Array.isArray(value)) {
const strings = value.map((e) => i + ": " + e);
this.#debug(payload);

str = str.concat(strings.join(this.#NEW_LINE)) + this.#NEW_LINE;
} else {
str += (i + ": " + value + this.#NEW_LINE);
if (callback) {
this.#callbacksMap.set(actionID, {
callback,
timer: setTimeout(() => {
if (this.#callbacksMap.has(actionID)) {
callback(new Error("Ami server did not get answer"), null);
this.#callbacksMap.delete(actionID);
}
}, 10000),
});
}
}

return str + this.#NEW_LINE;
this.#socket.write(
payload,
this.#encoding,
// cb,
);

this.#debug("----- SENDED ----");
} else {
this.#debug("cannot write to Asterisk Socket");
this.emit("ami_socket_unwritable");
}
}

connect(): void {

0 comments on commit 572324d

Please sign in to comment.