All files index.ts

64.66% Statements 194/300
58.92% Branches 33/56
60% Functions 33/55
63.56% Lines 157/247

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 9381x 1x 1x 1x 1x 1x                                 1x       1x           1x                                           1x 1x 1x                                                                                                                                                 1x 1x 1x                                                                                                                                                                                                                                                                 1x                                                                                                                                                                                                                                                                                                   1x                 1x             1x         1x         1x                   1x               1x 1x 1x     1x                 1x     1x     1x 1x     1x               1x 1x 1x     1x                   1x 1x                 1x 1x 1x     1x               1x 1x 1x     1x               1x 1x 1x 1x                   1x       1x                     1x         1x     1x         2x     2x 1x   2x 2x 2x                   1x 1x 1x     1x               1x 1x 1x 1x                 1x 1x                 1x 2x 2x     2x                   1x       2x 2x 1x   2x 2x 2x               1x 1x 1x 1x                     1x         1x 1x         1x                 1x 1x 1x     1x               1x 1x 1x     1x                               1x                       1x 1x 1x   1x 1x   1x 1x   1x     1x 1x   1x     1x 1x   1x 1x   1x     1x     1x 1x 1x               1x 1x 1x     1x               1x 1x 1x     1x                   1x                             1x                       1x                                 1x 3x 3x   1x     2x 1x       2x                       1x 3x 3x   1x     2x 1x       2x     1x 41x 1x           2x                                 1x   21x     1x 21x     1x 21x     1x 1x     1x   1x  
import axios, { AxiosInstance, CreateAxiosDefaults } from "axios";
import * as chacha from "chacha-js";
import * as dgram from "dgram";
import * as https from "https";
import libsodium from "libsodium-wrappers-sumo";
import * as tls from "tls";
 
// Helper to define integer ranges as parameters
type Enumerate<
  N extends number,
  Acc extends number[] = []
> = Acc["length"] extends N
  ? Acc[number]
  : Enumerate<N, [...Acc, Acc["length"]]>;
type IntRange<F extends number, T extends number> = Exclude<
  Enumerate<T>,
  Enumerate<F>
>;
 
/**
 * Identifier of UDP packages (as of documentation)
 */
const udpIdentifier = Buffer.from([0xde, 0xad, 0xbe]);
/**
 * Fixed argon key length (as of documentation)
 */
const argonKeyLength = 32;
 
/**
 * Utility method to retrieve the Doorstation's self signed certificate in PEM representation.
 * @param host host to retrieve the certificate for
 */
export async function getDoorstationCertificate(host: string): Promise<string> {
  return new Promise<string>((resolve) => {
    const socket = tls.connect(
      {
        host: host,
        port: 443,
        rejectUnauthorized: false,
      },
      () => {
        const peerCert = socket.getPeerCertificate().raw.toString("base64");
        socket.destroy();
        resolve(
          `-----BEGIN CERTIFICATE-----\n${peerCert}\n-----END CERTIFICATE-----`
        );
      }
    );
  });
}
 
/**
 * Scheme for API communication with the door station.
 */
export enum Scheme {
  http = "http",
  https = "https",
}
 
/**
 * Options to initialize the Doorbird client.
 */
export interface DoorbirdOptions {
  /**
   * Scheme to connect to the Doorbid Door Station.
   */
  scheme: Scheme;
  /**
   * Host / IP of the Door Station.
   */
  host: string;
  /**
   * Username to access the API.
   */
  username: string;
  /**
   * Password to access the API.
   */
  password: string;
  /**
   * Provide the Doorstation's TLS certificate to avoid general acceptance of self-signed certificates. If you do not
   * provide a certificate, but specifiy 'https' as scheme, the certificate will be loaded from the configured host.
   */
  certificate?: string;
}
 
/**
 * Generic response wrapper of the Doorbird API.
 */
export interface Response<Type> {
  BHA: Type;
}
 
/**
 * Basic BHA object for responses.
 */
export interface BaseBHA {
  RETURNCODE: string;
}
 
/**
 * Specific BHA object for session responses.
 */
export interface SessionBHA extends BaseBHA {
  SESSIONID: string;
  NOTIFICATION_ENCRYPTION_KEY: string;
}
 
/**
 * Specific BHA object for info responses.
 */
export interface DoorbirdInfoBHA extends BaseBHA {
  VERSION: DoorbirdInfoBHAVersion[];
}
 
/**
 * Doorbird info object.
 */
export interface DoorbirdInfoBHAVersion {
  FIRMWARE: string;
  BUILD_NUMBER: string;
  WIFI_MAC_ADDR: string;
  RELAYS: string[];
  "DEVICE-TYPE": string;
}
 
/**
 * Type for Doorbird favorites.
 */
export enum FavoriteType {
  sip = "sip",
  http = "http",
}
 
/**
 * Doorbird favorites.
 */
export type Favorites = {
  sip?: Favorite;
  http?: Favorite;
};
 
/**
 * A single Doorbird favorite.
 */
export type Favorite = {
  [id: string]: FavoriteInfo;
};
 
/**
 * Information on a Doorbird favorite.
 */
export interface FavoriteInfo {
  title: string;
  value: string;
}
 
/**
 * A schedule is an array of ScheduleEntries.
 */
export type Schedule = ScheduleEntry[];
 
/**
 * A single entry of a schedule.
 */
export interface ScheduleEntry {
  input: "doorbell" | "motion" | "rfid";
  param?: string;
  output: ScheduleEntryOutput[];
}
 
/**
 * Output of a schedule entry.
 */
export interface ScheduleEntryOutput {
  event: "notify" | "sip" | "relay" | "http";
  param?: string;
  schedule: "once" | ScheduleEntrySchedule;
}
 
/**
 * Specific schedule for a schedule entry.
 */
export interface ScheduleEntrySchedule {
  "from-to"?: FromTo[];
  weekdays?: FromTo[];
}
 
/**
 * Object that defines a timespan.
 */
export interface FromTo {
  from: string;
  to: string;
}
 
/**
 * Specific BHA for SIP status.
 */
export interface SipStatusBHA extends BaseBHA {
  SIP: SipStatus[];
}
 
/**
 * SIP status object.
 */
export interface SipStatus {
  ENABLE: string;
  PRIORITIZE_APP: string;
  REGISTER_URL: string;
  REGISTER_USER: string;
  REGISTER_AUTH_ID: string;
  REGISTER_PASSWORD: string;
  AUTOCALL_MOTIONSENSOR_URL: string;
  AUTOCALL_DOORBELL_URL: string;
  SPK_VOLUME: string;
  MIC_VOLUME: string;
  DTMF: string;
  "relais:1": string;
  "relais:2": string;
  LIGHT_PASSCODE: string;
  HANGUP_ON_BUTTON_PRESS: string;
  INCOMING_CALL_ENABLE: string;
  INCOMING_CALL_USER: string;
  ANC: string;
  LASTERRORCODE: string;
  LASTERRORTEXT: string;
  RING_TIME_LIMIT: string;
  CALL_TIME_LIMIT: string;
}
 
/**
 * Event object for doorbell rings.
 */
export interface RingEvent {
  intercomId: string;
  event: string;
  timestamp: Date;
}
 
/**
 * Event object for motion detection.
 */
export interface MotionEvent {
  intercomId: string;
  timestamp: Date;
}
 
/**
 * Callback for ring events.
 */
export type RingCallback = (event: RingEvent) => void;
/**
 * Callback for motion events.
 */
export type MotionCallback = (event: MotionEvent) => void;
 
/**
 * Wrapper class for a UDP socket, that is capable to handle Doorbird's UDP messages.
 */
export class DoorbirdUdpSocket {
  private username: string;
  private password: string;
  private client: Doorbird;
  private suppressBurst: boolean;
  private server: dgram.Socket;
  private lastEventTimestamp = 0;
  private ringListeners: RingCallback[] = [];
  private motionListeners: MotionCallback[] = [];
  private notificationEncryptionKey: Buffer | null = null;
 
  /**
   * Construct a new DoorbirdUdpSocket
   *
   * @param port Doorbird sends to ports 6524 and 35344.
   * @param username username of the Doorbird user.
   * @param password password of the Doorbird user.
   * @param client the parent instance of the Doorbird api.
   * @param suppressBurst flag to suppress multiple burst messages (callback is only called once)
   */
  constructor(
    port: 6524 | 35344,
    username: string,
    password: string,
    client: Doorbird,
    IsuppressBurst = false
  ) {
    this.username = username;
    this.password = password;
    this.client = client;
    this.suppressBurst = suppressBurst;
    this.server = dgram.createSocket({
      type: "udp4",
      reuseAddr: true,
    });
    this.server.bind(port);
    this.server.on("message", this.onMessage);
  }
 
  private strech = async (salt: Buffer, opslimit: Buffer, memlimit: Buffer) => {
    await libsodium.ready;
    const sodium = libsodium;
    const streched = Buffer.from(
      sodium.crypto_pwhash(
        argonKeyLength,
        this.password.substring(0, 5),
        salt,
        opslimit.readInt32BE(),
        memlimit.readInt32BE(),
        sodium.crypto_pwhash_ALG_ARGON2I13
      )
    );
    return streched;
  };
 
  private onMessage = async (msg: Buffer) => {
    const identifier = msg.subarray(0, 3);
    const version = msg.subarray(3, 4);
 
    Iif (udpIdentifier.toString("base64") !== identifier.toString("base64")) {
      return;
    }
 
    Iif (this.suppressBurst) {
      const eventTimestamp = new Date().valueOf();
      Iif (eventTimestamp - this.lastEventTimestamp < 1000) {
        return;
      }
      this.lastEventTimestamp = eventTimestamp;
    }
 
    let decrypted;
    if (Iversion[0] === 0x01) {
      decrypted = await this.decryptV1(msg);
    } else if (Iversion[0] === 0x02) {
      decrypted = await this.decryptV2(msg);
    } else {
      throw new Error("Unsupported version of UDP package.");
    }
 
    const intercomId = decrypted.subarray(0, 6);
    const event = decrypted.subarray(6, 14);
    const timestamp = decrypted.subarray(14, 18);
 
    Iif (this.username.substring(0, 6) !== intercomId.toString("utf-8")) {
      return;
    }
 
    const date = new Date(0);
    date.setUTCSeconds(timestamp.readInt32BE());
 
    const trimmedEvent = event.toString("utf-8").trim();
 
    if ("motion" === trimmedEvent) {
      this.motionListeners.forEach((listener) =>
        listener({
          intercomId: intercomId.toString("utf-8"),
          timestamp: date,
        })
      );
    } else {
      this.ringListeners.forEach((listener) =>
        listener({
          intercomId: intercomId.toString("utf-8"),
          event: trimmedEvent,
          timestamp: date,
        })
      );
    }
  };
 
  private decryptV1 = async (msg: Buffer) => {
    const opslimit = msg.subarray(4, 8);
    const memlimit = msg.subarray(8, 12);
    const salt = msg.subarray(12, 28);
    const nonce = msg.subarray(28, 36);
    const ciphertext = msg.subarray(36, 70);
 
    const streched = await this.strech(salt, opslimit, memlimit);
    const decipher = chacha.AeadLegacy(streched, nonce, true);
    return decipher.update(ciphertext);
  };
 
  private decryptV2 = async (msg: Buffer) => {
    const nonce = msg.subarray(4, 12);
    const ciphertext = msg.subarray(12, 46);
 
    const decipher = chacha.AeadLegacy(await this.getNotificationEncryptionKey(), nonce, true);
    return decipher.update(ciphertext);
  };
 
  private getNotificationEncryptionKey = async () => {
    if (Ithis.notificationEncryptionKey === null) {
      this.notificationEncryptionKey = Buffer.from(
        (await this.client.initializeSession()).BHA.NOTIFICATION_ENCRYPTION_KEY,
        "utf-8"
      );
    }
    return this.notificationEncryptionKey;
  };
 
  /**
   * Register a ring listener.
   *
   * @param listener ring listener
   */
  registerRingListener(listener: RingCallback): void {
    this.ringListeners.push(listener);
  }
 
  /**
   * Register a motion listner.
   *
   * @param listener motion listener
   */
  registerMotionListener(listener: MotionCallback): void {
    this.motionListeners.push(listener);
  }
 
  /**
   * Close the UDP socket.
   */
  close(): void {
    Iif (this.server) {
      this.server.close();
    }
  }
}
 
/**
 * Doorbird client class.
 */
export default class Doorbird {
  private options: DoorbirdOptions;
  private http: AxiosInstance | undefined;
 
  /**
   * Construct a Doorbird client.
   *
   * @param options options for the Doorbird client
   */
  constructor(options: DoorbirdOptions) {
    this.options = options;
  }
 
  /**
   * Initialize a session.
   *
   * @returns session response
   */
  async initializeSession(): Promise<Response<SessionBHA>> {
    const http = await this.getHttp();
    const resp = await http.get<Response<SessionBHA>>(
      this.uri(`/bha-api/getsession.cgi`)
    );
    return resp.data;
  }
 
  /**
   * Destroy a session.
   *
   * @param session session response or id
   * @returns session response
   */
  async destroySession(
    session: Response<SessionBHA> | string
  ): Promise<Response<SessionBHA>> {
    Iif ("object" === typeof session) {
      session = session.BHA.SESSIONID;
    }
    const http = await this.getHttp();
    const resp = await http.get<Response<SessionBHA>>(
      this.uri(`/bha-api/getsession.cgi?invalidate=${session}`)
    );
    return resp.data;
  }
 
  /**
   * Get info of the Doorbird installation.
   *
   * @returns Doorbird info response
   */
  async getInfo(): Promise<Response<DoorbirdInfoBHA>> {
    const http = await this.getHttp();
    const resp = await http.get<Response<DoorbirdInfoBHA>>(
      this.uri(`/bha-api/info.cgi`)
    );
    return resp.data;
  }
 
  /**
   * Opens a door via a relay of your doorbird system.
   *
   * @deprecated use toggleRelay instead
   * @param relay the relay that opens the door
   * @returns http response of the call
   */
  async openDoor(relay: string): Promise<Response<BaseBHA>> {
    return this.toggleRelay(relay);
  }
 
  /**
   * Toggle a relay.
   *
   * @param relay the ID of the relay to be toggled
   * @returns base response
   */
  async toggleRelay(relay: string): Promise<Response<BaseBHA>> {
    const http = await this.getHttp();
    const resp = await http.get<Response<BaseBHA>>(
      this.uri(`/bha-api/open-door.cgi?r=${relay}`)
    );
    return resp.data;
  }
 
  /**
   * Enable the infra red lights of the door station.
   *
   * @returns base response
   */
  async lightOn(): Promise<Response<BaseBHA>> {
    const http = await this.getHttp();
    const resp = await http.get<Response<BaseBHA>>(
      this.uri(`/bha-api/light-on.cgi`)
    );
    return resp.data;
  }
 
  /**
   * Get a list of favorites.
   *
   * @returns favorites list
   */
  async listFavorites(): Promise<Favorites> {
    const http = await this.getHttp();
    const resp = await http.get<Favorites>(this.uri(`/bha-api/favorites.cgi`));
    return resp.data;
  }
 
  /**
   * Create a new favorite.
   *
   * @param type new favorite type
   * @param favoriteInfo favorite info
   * @returns empty promise
   */
  createFavorite(
    type: FavoriteType,
    favoriteInfo: FavoriteInfo
  ): Promise<string> {
    return this.doCreateUpdateFavorite(type, favoriteInfo);
  }
 
  /**
   * Update a favorite.
   *
   * @param id id of the favorite to be updated
   * @param type type of the favorite to be updated
   * @param favoriteInfo new favorite info
   * @returns empty promise
   */
  updateFavorite(
    id: string,
    type: FavoriteType,
    favoriteInfo: FavoriteInfo
  ): Promise<string> {
    return this.doCreateUpdateFavorite(type, favoriteInfo, id);
  }
 
  private async doCreateUpdateFavorite(
    type: FavoriteType,
    favoriteInfo: FavoriteInfo,
    id?: string
  ): Promise<string> {
    let url = `/bha-api/favorites.cgi?action=save&type=${type}&title=${encodeURIComponent(
      favoriteInfo.title
    )}&value=${encodeURIComponent(favoriteInfo.value)}`;
    if (id) {
      url += `&id=${id}`;
    }
    const http = await this.getHttp();
    const resp = await http.get<void>(this.uri(url));
    return resp.headers['favoriteid'];
  }
 
  /**
   * Delete a favorite.
   *
   * @param id id of the favorite to be deleted
   * @param type type of the favorite to be deleted
   * @returns empty promise
   */
  async deleteFavorite(id: string, type: FavoriteType): Promise<void> {
    const http = await this.getHttp();
    const resp = await http.get<void>(
      this.uri(`/bha-api/favorites.cgi?action=remove&type=${type}&id=${id}`)
    );
    return resp.data;
  }
 
  /**
   * Get the schedule of the Doorbird system.
   *
   * @returns schedule response
   */
  async getSchedule(): Promise<Schedule> {
    const http = await this.getHttp();
    const resp = await http.get<Schedule>(this.uri(`/bha-api/schedule.cgi`));
    return resp.data;
  }
 
  /**
   * Create a schedule entry.
   *
   * @param scheduleEntry new schedule entry
   * @returns empty promise
   */
  createScheduleEntry(scheduleEntry: ScheduleEntry): Promise<void> {
    return this.updateScheduleEntry(scheduleEntry);
  }
 
  /**
   * Update a schedule entry.
   *
   * @param scheduleEntry updated schedule entry
   * @returns empty promise
   */
  async updateScheduleEntry(scheduleEntry: ScheduleEntry): Promise<void> {
    const http = await this.getHttp();
    const resp = await http.post<void>(this.uri(`/bha-api/schedule.cgi`), {
      data: scheduleEntry,
    });
    return resp.data;
  }
 
  /**
   * Delete a schedule entry.
   *
   * @param input input type of the entry to be deleted
   * @param param param of the entry to be deleted
   * @returns empty promise
   */
  async deleteScheduleEntry(
    input: "doorbell" | "motion" | "rfid",
    param: string | null
  ): Promise<void> {
    let url = `/bha-api/schedule.cgi?action=remove&input=${input}`;
    if (param) {
      url += `&param=${param}`;
    }
    const http = await this.getHttp();
    const resp = await http.get<void>(this.uri(url));
    return resp.data;
  }
 
  /**
   * Restart the door station.
   *
   * @returns empty promise
   */
  async restart(): Promise<void> {
    const http = await this.getHttp();
    const resp = await http.get<void>(this.uri(`/bha-api/restart.cgi`));
    return resp.data;
  }
 
  /**
   * Create a SIP registration.
   *
   * @param user user for the sip registration
   * @param password password for the sip registration
   * @param url url for the sip registration
   * @returns empty promise
   */
  async sipRegistration(
    user: string,
    password: string,
    url: string
  ): Promise<void> {
    const http = await this.getHttp();
    const resp = await http.get<void>(
      this.uri(
        `/bha-api/sip.cgi?action=registration&user=${user}&password=${password}&url=${url}`
      )
    );
    return resp.data;
  }
 
  /**
   * Initiate a SIP call.
   *
   * @param url url for the SIP call
   * @returns empty promise
   */
  async sipCall(url: string): Promise<void> {
    const http = await this.getHttp();
    const resp = await http.get<void>(
      this.uri(`/bha-api/sip.cgi?action=makecall&url=${url}`)
    );
    return resp.data;
  }
 
  /**
   * End a SIP call.
   *
   * @returns empty promise
   */
  async sipHangup(): Promise<void> {
    const http = await this.getHttp();
    const resp = await http.get<void>(
      this.uri(`/bha-api/sip.cgi?action=hangup`)
    );
    return resp.data;
  }
 
  /**
   * Update SIP settings.
   *
   * @param enable enable or disable SIP registration after device reboot
   * @param mic_volume microphone volume (1-100)
   * @param spk_volume speaker volume (1-100)
   * @param dtmf enable or disable DTMF support
   * @param relay1_passcode pincode for triggering the door open relay
   * @param incoming_call_enable enable or disable incoming calls
   * @param incoming_call_user Allowed SIP user which will be authenticated for Doorbird
   * @param anc enable or disable acoustic noise cancellation
   * @returns empty promise
   */
  async sipSettings(
    enable: 0 | 1,
    mic_volume: IntRange<1, 100>,
    spk_volume: IntRange<1, 100>,
    dtmf: 0 | 1,
    relay1_passcode: number,
    incoming_call_enable: 0 | 1,
    incoming_call_user: string,
    anc: 0 | 1,
    ringTimeLimit?: IntRange<10, 300>,
    callTimeLimit?: IntRange<30, 300>
  ): Promise<void> {
    let url = `/bha-api/sip.cgi?action=settings`;
    if (enable) {
      url += `&enable=${enable}`;
    }
    if (mic_volume) {
      url += `&mic_volume=${mic_volume}`;
    }
    if (spk_volume) {
      url += `&spk_volume=${spk_volume}`;
    }
    Iif (dtmf) {
      url += `&dtmf=${dtmf}`;
    }
    if (relay1_passcode) {
      url += `&relay1_passcode=${relay1_passcode}`;
    }
    Iif (incoming_call_enable) {
      url += `&incoming_call_enable=${incoming_call_enable}`;
    }
    if (incoming_call_user) {
      url += `&incoming_call_user=${incoming_call_user}`;
    }
    if (anc) {
      url += `&anc=${anc}`;
    }
    Iif (ringTimeLimit) {
      url += `&ring_time_limit=${ringTimeLimit}`;
    }
    Iif (callTimeLimit) {
      url += `&call_time_limit=${callTimeLimit}`;
    }
    const http = await this.getHttp();
    const resp = await http.get<void>(this.uri(url));
    return resp.data;
  }
 
  /**
   * Get the SIP status.
   *
   * @returns Specific BHA for SIP status
   */
  async sipStatus(): Promise<Response<SipStatusBHA>> {
    const http = await this.getHttp();
    const resp = await http.get<Response<SipStatusBHA>>(
      this.uri(`/bha-api/sip.cgi?action=status`)
    );
    return resp.data;
  }
 
  /**
   * Reset the SIP settings.
   *
   * @returns empty promise
   */
  async sipSettingsReset(): Promise<void> {
    const http = await this.getHttp();
    const resp = await http.get<void>(
      this.uri(`/bha-api/sip.cgi?action=reset`)
    );
    return resp.data;
  }
 
  /**
   * Start a UDP socket.
   *
   * @param port port to listen on
   * @param suppressBurst suppress multiple UDP messages into a single callback
   * @returns DoorbirdUdpSocket object
   */
  startUdpSocket(port: 6524 | 35344, IsuppressBurst = false): DoorbirdUdpSocket {
    return new DoorbirdUdpSocket(
      port,
      this.options.username,
      this.options.password,
      this,
      suppressBurst
    );
  }
 
  /**
   * Get the Doorbird image url.
   *
   * @returns image url
   */
  getImageUrl(): string {
    return (
      `${this.options.scheme}://${this.options.host}/bha-api/image.cgi` +
      `?http-user=${this.options.username}&http-password=${this.options.password}`
    );
  }
 
  /**
   * Get the current image.
   *
   * @returns buffer with image data
   */
  async getImage(): Promise<Buffer> {
    const http = await this.getHttp();
    const resp = await http.get(this.getImageUrl(), {
      responseType: "arraybuffer",
    });
    return Buffer.from(resp.data, "binary");
  }
 
  /**
   * Get the Doorbird live audio url.
   *
   * ATTENTION: if you do not provide a session id or object, the URL will contain sensitive credentials.
   *
   * @param session session object or id
   *
   * @returns audio url
   */
  getAudioUrl(session?: SessionBHA | string): string {
    const baseUri = `http://${this.options.host}/bha-api/audio-receive.cgi`;
    if (!session) {
      // Audio stream does not support https.
      return `${baseUri}?http-user=${this.options.username}&http-password=${this.options.password}`;
    }
 
    if ("object" === typeof session) {
      session = session.SESSIONID;
    }
 
    // Audio stream does not support https.
    return `${baseUri}?sessionid=${session}`;
  }
 
  /**
   * Get the Doorbird video url.
   *
   * ATTENTION: if you do not provide a session id or object, the URL will contain sensitive credentials.
   *
   * @param session session object or id
   *
   * @returns video url
   */
  getVideoUrl(session?: SessionBHA | string): string {
    const baseUri = `http://${this.options.host}/bha-api/video.cgi`;
    if (!session) {
      // Video stream does not support https.
      return `${baseUri}?http-user=${this.options.username}&http-password=${this.options.password}`;
    }
 
    if ("object" === typeof session) {
      session = session.SESSIONID;
    }
 
    // Video stream does not support https.
    return `${baseUri}?sessionid=${session}`;
  }
 
  private async getHttp(): Promise<AxiosInstance> {
    if (this.http === undefined) {
      const axiosDefaults: CreateAxiosDefaults = {
        headers: {
          Authorization: this.authHeader(),
        },
      };
 
      if (this.options.scheme === Scheme.https) {
        let certificate;
        if (Ithis.options.certificate) {
          certificate = this.options.certificate;
        } else {
          certificate = await getDoorstationCertificate(this.options.host);
        }
        axiosDefaults.httpsAgent = new https.Agent({
          ca: certificate,
          checkServerIdentity: () => {
            // we cannot check the identity, as the CN of the certifcate will not
            // match the servername (likely a IP address or a network specific DN)
            return undefined;
          },
        });
      }
 
      this.http = axios.create(axiosDefaults);
    }
    return this.http;
  }
 
  private uri(path: string): string {
    return `${this.baseUri()}${path}`;
  }
 
  private baseUri(): string {
    return `${this.options.scheme}://${this.options.host}`;
  }
 
  private authHeader(): string {
    const auth = Buffer.from(
      `${this.options.username}:${this.options.password}`
    ).toString("base64");
    return "Basic " + auth;
  }
}