From 75cd19e79ae4093a781ededc18fd17ae7f5bc554 Mon Sep 17 00:00:00 2001 From: kvazibox <65755909+kvazibox@users.noreply.github.com> Date: Thu, 20 Jan 2022 18:25:51 +1000 Subject: [PATCH] Update main.ts Refactored code --- plugins/amcrest/src/main.ts | 47 ++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/plugins/amcrest/src/main.ts b/plugins/amcrest/src/main.ts index 6d3b0b119..6cdfb9329 100644 --- a/plugins/amcrest/src/main.ts +++ b/plugins/amcrest/src/main.ts @@ -31,37 +31,30 @@ class AmcrestCamera extends RtspSmartCamera implements VideoCameraConfiguration, } async updateDeviceInfo(): Promise { - const vendorResponse = await this.getClient().digestAuth.request({ - url: `http://${this.getHttpAddress()}/cgi-bin/magicBox.cgi?action=getVendor` - }); - - const vendorName = String(vendorResponse.data).replace("vendor=", ""); + var deviceInfo = {}; - const serialNumberResponse = await this.getClient().digestAuth.request({ - url: `http://${this.getHttpAddress()}/cgi-bin/magicBox.cgi?action=getSerialNo` - }); - - const serialNumber = String(serialNumberResponse.data).replace("sn=", ""); + const deviceParameters = [ + {"action":"getVendor","replace":"vendor=","parameter":"manufacturer"}, + {"action":"getSerialNo","replace":"sn=","parameter":"serialNumber"}, + {"action":"getDeviceType","replace":"type=","parameter":"model"}, + {"action":"getSoftwareVersion","replace":"version=","parameter":"firmware"} + ]; - const deviceTypeResponse = await this.getClient().digestAuth.request({ - url: `http://${this.getHttpAddress()}/cgi-bin/magicBox.cgi?action=getDeviceType` - }); + for (const element of deviceParameters) { + try { + const response = await this.getClient().digestAuth.request({ + url: `http://${this.getHttpAddress()}/cgi-bin/magicBox.cgi?action=${element['action']}` + }); - const deviceType = String(deviceTypeResponse.data).replace("type=", ""); + const result = String(response.data).replace(element['replace'], ""); + deviceInfo[element['parameter']] = result; + } + catch (e) { + this.console.error('Error getting device parameter', element['action'], e); + } + } - const softwareVersionResponse = await this.getClient().digestAuth.request({ - url: `http://${this.getHttpAddress()}/cgi-bin/magicBox.cgi?action=getSoftwareVersion` - }); - - const softwareVersion = String(softwareVersionResponse.data).replace("version=", ""); - - this.info = { - manufacturer: vendorName, - model: deviceType, - firmware: softwareVersion, - version: softwareVersion, - serialNumber: serialNumber, - }; + this.info = deviceInfo; } async setVideoStreamOptions(options: MediaStreamOptions): Promise {