Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
FantasticFiasco committed Jan 6, 2025
1 parent f9a4950 commit 9f432ab
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 227 deletions.
4 changes: 2 additions & 2 deletions packages/axis-configuration/sample/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const updateParameters = async (parameters: Parameters): Promise<void> => {
};

const print = (root: Map<string, string>) => {
for (const parameter of Object.keys(root)) {
console.log(` ${parameter}=${root.get(parameter)}`);
for (const [name, value] of root.entries()) {
console.log(` ${name}=${value}`);
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/axis-configuration/src/parameters/Parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Parameters {
* @throws {UpdateParametersError} Updating one or many of the parameters failed.
*/
public async update(parameters: Map<string, string>): Promise<void> {
expect.toBeTrue(Object.keys(parameters).length > 0, 'At least one parameter must be specified');
expect.toBeTrue(parameters.size > 0, 'At least one parameter must be specified');

const req = new UpdateParametersRequest(this.#connection, parameters);
const res = await req.send();
Expand Down
67 changes: 45 additions & 22 deletions packages/axis-configuration/test/DeviceMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import * as passport from 'passport';
import { DigestStrategy } from 'passport-http';

export class DeviceMock {
private server?: Server;
private address?: string;
private port?: number;
#server?: Server;
#address?: string;
#port?: number;

readonly #parameters = new Map<string, string>([
['Network.Bonjour.FriendlyName', 'Main Entrance'],
['Network.Bonjour.Enabled', 'yes'],
]);

readonly username = 'some-user';
readonly password = 'some-password';

get uri(): string {
return `http://${this.address}:${this.port}`;
return `http://${this.#address}:${this.#port}`;
}

listen(address: string, port: number): Promise<void> {
Expand All @@ -39,9 +44,9 @@ export class DeviceMock {
app.get('/axis-cgi/pwdgrp.cgi', passport.authenticate('digest', { session: false }), this.#pwdgrpHandler);

return new Promise((resolve) => {
this.server = app.listen(port, address, () => {
this.address = (this.server?.address() as AddressInfo).address;
this.port = (this.server?.address() as AddressInfo).port;
this.#server = app.listen(port, address, () => {
this.#address = (this.#server?.address() as AddressInfo).address;
this.#port = (this.#server?.address() as AddressInfo).port;

resolve();
});
Expand All @@ -50,10 +55,10 @@ export class DeviceMock {

close(): Promise<void> {
return new Promise((resolve, reject) => {
if (!this.server) {
if (!this.#server) {
reject('Express server is not defined');
} else {
this.server.close((err) => {
this.#server.close((err) => {
if (err) {
reject(err);
} else {
Expand All @@ -70,6 +75,10 @@ export class DeviceMock {
this.#listParameters(req, res);
break;

case 'update':
this.#updateParameters(req, res);
break;

default:
res.status(400).send();
}
Expand All @@ -79,25 +88,39 @@ export class DeviceMock {
const responseLines: string[] = [];

const group = req.query.group as string;
const parameters = group.split(',');

for (const parameter of parameters) {
switch (parameter) {
case 'Network.Bonjour.FriendlyName':
responseLines.push('Network.Bonjour.FriendlyName=Main Entrance');
break;
case 'Network.Bonjour.Enabled':
responseLines.push('Network.Bonjour.Enabled=yes');
break;
default:
responseLines.push(`# Error: Error -1 getting param in group '${parameter}'`);
break;
const names = group.split(',');

for (const name of names) {
const value = this.#parameters.get(name);

if (value) {
responseLines.push(`${name}=${value}`);
} else {
responseLines.push(`# Error: Error -1 getting param in group '${name}'`);
}
}

res.send(responseLines.join('\r\n'));
};

#updateParameters = (req: express.Request, res: express.Response) => {
const responseLines: string[] = [];

for (const [name, value] of Object.entries(req.query)) {
if (name === 'action') {
continue;
}

if (this.#parameters.has(name)) {
this.#parameters.set(name, value as string);
} else {
responseLines.push(`# Error: Error setting '${name}' to '${value}'!\r\n`);
}
}

res.send(responseLines.length === 0 ? 'OK' : responseLines.join('\r\n'));
};

#pwdgrpHandler = (_: express.Request, res: express.Response) => {
res.send('Success');
};
Expand Down
Loading

0 comments on commit 9f432ab

Please sign in to comment.