Skip to content

Commit

Permalink
Merge pull request #1 from jaredcat/class-converstion-wip
Browse files Browse the repository at this point in the history
some classes converted
  • Loading branch information
jaredcat authored Oct 21, 2024
2 parents 0f051ba + 36eb3c4 commit 475218b
Show file tree
Hide file tree
Showing 25 changed files with 1,032 additions and 595 deletions.
Empty file added .github/workflows/restyled.yml
Empty file.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"eslint.enable": true,
"files.eol": "\n",
"cSpell.words": [
"bgcolor",
"Rtsp"
]
}
17 changes: 14 additions & 3 deletions src/handlers/api_handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { Mixin } from 'ts-mixer';
import { AlarmAPIMixin } from '../mixins';
import DeviceAPIMixin from '../mixins/device';
import {
AlarmAPIMixin,
DeviceAPIMixin,
DisplayAPIMixin,
DownloadAPIMixin,
ImageAPIMixin,
} from '../mixins';

export default class APIHandler extends Mixin(AlarmAPIMixin, DeviceAPIMixin) {}
export default class APIHandler extends Mixin(
AlarmAPIMixin,
DeviceAPIMixin,
DisplayAPIMixin,
DownloadAPIMixin,
ImageAPIMixin,
) {}
19 changes: 1 addition & 18 deletions src/handlers/base_api_handler.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
import fs from 'fs';
import CommandData from '../types/CommandData';
import Request from './rest_handler';

export interface CommandData {
cmd: string;
action: number;
param?: {
User?: {
userName: string;
password: string;
};
Alarm?: {
channel: number;
type: string;
};
DevName?: { name: string };
HddInfo?: { id: number[] };
};
filepath?: string;
}

interface LoginResponse {
code: string;
value: {
Expand Down
10 changes: 9 additions & 1 deletion src/mixins/alarm.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Mixin } from 'ts-mixer';
import BaseAPIHandler, { CommandData } from '../handlers/base_api_handler';
import BaseAPIHandler from '../handlers/base_api_handler';
import CommandData from '../types/CommandData';

interface AlarmResponse {
// Define the structure of the response if known, use any if the structure is dynamic
[key: string]: any;
}

export interface AlarmAPIMixinParams {
Alarm: {
channel: number;
type: string;
};
}

export default class AlarmAPIMixin extends Mixin(BaseAPIHandler) {
getAlarmMotion(): Promise<AlarmResponse> {
/**
Expand Down
14 changes: 10 additions & 4 deletions src/mixins/device.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Mixin } from 'ts-mixer';
import { BaseAPIHandler } from '../handlers';
import CommandData from '../types/CommandData';

export interface DeviceAPIMixinParams {
DevName?: { name: string };
HddInfo?: { id: number[] };
}

export default class DeviceAPIMixin extends Mixin(BaseAPIHandler) {
static readonly DEFAULT_HDD_ID = [0];

async setDeviceName(name: string): Promise<boolean> {
const body = [
const body: CommandData[] = [
{ cmd: 'SetDevName', action: 0, param: { DevName: { name } } },
];
await this.executeCommand('SetDevName', body);
Expand All @@ -14,19 +20,19 @@ export default class DeviceAPIMixin extends Mixin(BaseAPIHandler) {
}

getDeviceName(): Promise<any> {
const body = [{ cmd: 'GetDevName', action: 0, param: {} }];
const body: CommandData[] = [{ cmd: 'GetDevName', action: 0, param: {} }];
return this.executeCommand('GetDevName', body);
}

getHddInfo(): Promise<any> {
const body = [{ cmd: 'GetHddInfo', action: 0, param: {} }];
const body: CommandData[] = [{ cmd: 'GetHddInfo', action: 0, param: {} }];
return this.executeCommand('GetHddInfo', body);
}

async formatHdd(
hddId: number[] = DeviceAPIMixin.DEFAULT_HDD_ID,
): Promise<boolean> {
const body = [
const body: CommandData[] = [
{ cmd: 'Format', action: 0, param: { HddInfo: { id: hddId } } },
];
const response = await this.executeCommand('Format', body);
Expand Down
57 changes: 0 additions & 57 deletions src/mixins/display.py

This file was deleted.

99 changes: 99 additions & 0 deletions src/mixins/display.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { BaseAPIHandler } from '../handlers';
import CommandData, { OsdPos } from '../types/CommandData';

export interface SetOsdParams {
Osd: {
bgcolor: boolean;
channel: number;
osdChannel: {
enable: boolean;
name: string;
pos: OsdPos;
};
osdTime: { enable: boolean; pos: OsdPos };
watermark: boolean;
};
}

class DisplayAPIMixin extends BaseAPIHandler {
/**
* API calls related to the current image (osd, on screen display).
*/

public async getOsd(): Promise<any> {
/**
* Get OSD information.
* See examples/response/GetOsd.json for example response data.
* @returns response json
*/
const body: CommandData[] = [
{ cmd: 'GetOsd', action: 1, param: { channel: 0 } },
];
return this.executeCommand('GetOsd', body);
}

public async getMask(): Promise<any> {
/**
* Get the camera mask information.
* See examples/response/GetMask.json for example response data.
* @returns response json
*/
const body: CommandData[] = [
{ cmd: 'GetMask', action: 1, param: { channel: 0 } },
];
return this.executeCommand('GetMask', body);
}

public async setOsd(
bg_color: boolean = false,
channel: number = 0,
osd_channel_enabled: boolean = false,
osd_channel_name: string = '',
osd_channel_pos: OsdPos = OsdPos['Lower Right'],
osd_time_enabled: boolean = false,
osd_time_pos: OsdPos = OsdPos['Lower Right'],
osd_watermark_enabled: boolean = false,
): Promise<boolean> {
/**
* Set OSD
* @returns whether the action was successful
*/
const body: CommandData[] = [
{
cmd: 'SetOsd',
action: 1,
param: {
Osd: {
bgcolor: bg_color,
channel: channel,
osdChannel: {
enable: osd_channel_enabled,
name: osd_channel_name,
pos: osd_channel_pos,
},
osdTime: { enable: osd_time_enabled, pos: osd_time_pos },
watermark: osd_watermark_enabled,
},
},
},
];

try {
const response = await this.executeCommand('SetOsd', body);
const r_data = response[0];
if (r_data?.value?.rspCode === 200) {
return true;
}
console.log(
'Could not set OSD. Camera responded with status:',
r_data.error,
);
return false;
} catch (error) {
console.error('Error setting OSD:', error);
return false;
}
}
}

export default DisplayAPIMixin;
18 changes: 0 additions & 18 deletions src/mixins/download.py

This file was deleted.

36 changes: 36 additions & 0 deletions src/mixins/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { BaseAPIHandler } from '../handlers';
import CommandData from '../types/CommandData';

class DownloadAPIMixin extends BaseAPIHandler {
/**
* API calls for downloading video files.
*/

public async getFile(filename: string, outputPath: string): Promise<boolean> {
/**
* Download the selected video file
* @param filename The name of the file to download
* @param outputPath The path where the file should be saved
* @returns A boolean indicating whether the download was successful
*/
const body: CommandData[] = [
{
cmd: 'Download',
action: 0, // action is required in CommandData, but not used in the original Python code
source: filename,
output: filename,
filepath: outputPath,
},
];

try {
const resp = await this.executeCommand('Download', body);
return resp;
} catch (error) {
console.error('Error downloading file:', error);
return false;
}
}
}

export default DownloadAPIMixin;
Loading

0 comments on commit 475218b

Please sign in to comment.