From 0087c543776a84d8efef39440d8df26af433a065 Mon Sep 17 00:00:00 2001 From: Benny Megidish Date: Mon, 1 Apr 2024 01:01:55 +0300 Subject: [PATCH] chore: fix lint errors --- .eslintrc.json | 7 ++++ butter/mas/api.ts | 2 +- butter/mas/clients/client.ts | 55 +++++++++++++++------------- butter/mas/clients/client_factory.ts | 19 +++++----- butter/mas/clients/client_http.ts | 2 +- butter/mas/clients/client_tcp.ts | 2 +- butter/mas/clients/client_udp.ts | 2 +- butter/mas/environment.ts | 5 ++- butter/mas/packets/packet.ts | 40 ++++++++++---------- butter/mas/packets/packet_builder.ts | 17 +++++---- butter/mas/packets/packet_factory.ts | 17 ++++----- butter/mas/packets/packet_http.ts | 12 +++--- butter/mas/packets/packet_tcp.ts | 10 ++--- butter/mas/packets/packet_udp.ts | 12 +++--- 14 files changed, 102 insertions(+), 100 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index ff36c9f..17344f9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -15,5 +15,12 @@ "@typescript-eslint" ], "rules": { + "semi": "off", + "indent": "off", + "space-before-function-paren": "off", + "no-useless-constructor": "off", + "no-multiple-empty-lines": "off", + "no-unreachable": "off", + "no-unused-vars": "off" } } diff --git a/butter/mas/api.ts b/butter/mas/api.ts index b0d6651..917be53 100644 --- a/butter/mas/api.ts +++ b/butter/mas/api.ts @@ -7,4 +7,4 @@ import { Response } from './interfaces/response'; import { __version__ } from './environment'; // Exposes Butter MAS Python API -export { ClientFactory, HttpClient, Response, __version__ }; // TcpClient, UdpClient \ No newline at end of file +export { ClientFactory, HttpClient, Response, __version__ }; // TcpClient, UdpClient diff --git a/butter/mas/clients/client.ts b/butter/mas/clients/client.ts index 8fb0af4..4414181 100644 --- a/butter/mas/clients/client.ts +++ b/butter/mas/clients/client.ts @@ -11,13 +11,13 @@ export class Client { /** * Creates an instance of Client. - * + * * @param {string} ip * @param {number} [port=3000] * @param {string} [protocol='http'] * @memberof Client */ - constructor(ip: string, port: number=3000, protocol: string='http') { + constructor(ip: string, port: number = 3000, protocol: string = 'http') { this._timeout = 40; this.ip = ip; this.port = port; @@ -67,11 +67,12 @@ export class Client { * @returns response containing all the available (loaded) robot animations * @memberof Client */ - getAvailableAnimations(reload: boolean=false): Response { + getAvailableAnimations(reload: boolean = false): Response { const builder = new PacketBuilder(this.ip, this.port, this.protocol).addCommand('animate'); - if (reload) + if (reload) { builder.addParameter('reload'); + } const packet = builder.addParameter('list').build(); @@ -82,21 +83,22 @@ export class Client { /** * Get available (loaded) robot sound assets * - * @param {boolean} [reload=false] + * @param {boolean} [reload=false] * @returns response containing all the available (loaded) robot sound assets * @memberof Client */ - getAvailableSounds(reload: boolean=false): Response { + getAvailableSounds(reload: boolean = false): Response { const builder = new PacketBuilder(this.ip, this.port, this.protocol).addCommand('audio'); - if (reload) + if (reload) { builder.addParameter('reload'); + } const packet = builder.addParameter('list').build(); return packet.send(this._timeout); } - + /** * Get all available motor registers (for Dynamixel motors only) @@ -106,7 +108,7 @@ export class Client { * @returns response containing all the available motor registers * @memberof Client */ - getAvailableMotorRegisters(motorName: string, readableOnly: boolean=false): Response { + getAvailableMotorRegisters(motorName: string, readableOnly: boolean = false): Response { const packet = new PacketBuilder(this.ip, this.port, this.protocol) .addCommand('dxl') .addArguments('get', motorName) @@ -168,7 +170,7 @@ export class Client { const packet = new PacketBuilder(this.ip, this.port, this.protocol) .addCommand('dxl') .addArguments('set', motorName, registerName, value) - .build(); + .build(); return packet.send(this._timeout); } @@ -197,7 +199,7 @@ export class Client { return packet.send(this._timeout); } - + /** * Move motor to a certain position (relative to the motor's zero position) in fixed duration * @@ -214,36 +216,37 @@ export class Client { .addArguments(motorName, position) .addKeyValuePair('duration', duration) .addKeyValuePair('units', units) - .build(); + .build(); return packet.send(this._timeout); } - + /** * Move motor to a certain direction (relative to the motor's current position) * * @param {string} motorName motor name (as configured on the configurator) * @param {string} direction motor movement direction (left, right, stop) * @param {number} [velocity] motor movement speed (in units / sec) - * @param {RotationUnits} [units='radians'] rotation units + * @param {RotationUnits} [units='radians'] rotation units * @returns response containing execution result * @memberof Client */ moveMotorInDirection(motorName: string, direction: string, velocity?: number, units: RotationUnits = 'radians'): Response { - const direction_code = direction.toLowerCase() == 'right' ? 1 : direction.toLowerCase() == 'left' ? -1 : 0 + // eslint-disable-next-line camelcase + const direction_code = direction.toLowerCase() === 'right' ? 1 : direction.toLowerCase() === 'left' ? -1 : 0 const packet = new PacketBuilder(this.ip, this.port, this.protocol) .addCommand('move') .addArguments(motorName, direction_code) .addKeyValuePair('velocity', velocity) .addKeyValuePair('units', units) .addParameter('continuously') - .build(); + .build(); return packet.send(this._timeout); } - + /** * Move motor a certain amount of steps (relative to the motor's current position) * @@ -252,7 +255,7 @@ export class Client { * @param {number} steps amount of steps to move * @param {number} [velocity] motor movement speed (in radians / sec) * @param {string} [interpolator] interpolation function - * @param {RotationUnits} [units='radians'] rotation units + * @param {RotationUnits} [units='radians'] rotation units * @returns response containing execution result * @memberof Client */ @@ -260,10 +263,10 @@ export class Client { // const direction_code = direction.toLowerCase() == 'right' ? 1 : direction.toLowerCase() == 'left' ? -1 : 0 // const packet = new PacketBuilder(this.ip, this.port, this.protocol).addCommand('move').addArguments(motorName, direction_code) // .addKeyValuePair('steps', steps) - // .addKeyValuePair('velocity', velocity) + // .addKeyValuePair('velocity', velocity) // .addKeyValuePair('interpolator', interpolator) // .addKeyValuePair('units', units) - // .build(); + // .build(); // return packet.send(this._timeout); // } @@ -281,8 +284,8 @@ export class Client { const packet = new PacketBuilder(this.ip, this.port, this.protocol) .addCommand('animate') .addArgument(animationName) - .addKeyValuePair("lenient", lenient) - .addKeyValuePair("relative", relative) + .addKeyValuePair('lenient', lenient) + .addKeyValuePair('relative', relative) .build(); return packet.send(this._timeout); @@ -299,7 +302,7 @@ export class Client { const packet = new PacketBuilder(this.ip, this.port, this.protocol) .addCommand('animate') .addParameter('pause') - .build(); + .build(); return packet.send(this._timeout); } @@ -315,7 +318,7 @@ export class Client { const packet = new PacketBuilder(this.ip, this.port, this.protocol) .addCommand('animate') .addParameter('resume') - .build(); + .build(); return packet.send(this._timeout); } @@ -369,7 +372,7 @@ export class Client { return packet.send(this._timeout); } - + /** * Pause current audio playback (if available) on the robot * @@ -416,4 +419,4 @@ export class Client { return packet.send(this._timeout); } -} \ No newline at end of file +} diff --git a/butter/mas/clients/client_factory.ts b/butter/mas/clients/client_factory.ts index f87f723..90f86a8 100644 --- a/butter/mas/clients/client_factory.ts +++ b/butter/mas/clients/client_factory.ts @@ -9,7 +9,6 @@ import { UdpClient } from './client_udp'; * @class ClientFactory */ export class ClientFactory { - /** * Creates new client * @@ -19,13 +18,13 @@ export class ClientFactory { * @returns requested client * @memberof ClientFactory */ - getClient(ip: string, port?: number, protocol: string="http") { - if (protocol == "http") { + getClient(ip: string, port?: number, protocol: string = 'http') { + if (protocol === 'http') { return port ? new HttpClient(ip, port) : new HttpClient(ip); - } else if (protocol == "tcp") { + } else if (protocol === 'tcp') { throw new Error('Not Implemented'); return port ? new TcpClient(ip, port) : new TcpClient(ip); - } else if (protocol == "udp") { + } else if (protocol === 'udp') { throw new Error('Not Implemented'); return port ? new UdpClient(ip, port) : new UdpClient(ip); } else { @@ -41,17 +40,17 @@ export class ClientFactory { * @returns * @memberof ClientFactory */ - getClientClass(protocol: string="http") { - if (protocol == "http") { + getClientClass(protocol: string = 'http') { + if (protocol === 'http') { return HttpClient; - } else if (protocol == "tcp") { + } else if (protocol === 'tcp') { throw new Error('Not Implemented'); return TcpClient; - } else if (protocol == "udp") { + } else if (protocol === 'udp') { throw new Error('Not Implemented'); return UdpClient; } else { return null; } } -} \ No newline at end of file +} diff --git a/butter/mas/clients/client_http.ts b/butter/mas/clients/client_http.ts index 66879a2..214e0c4 100644 --- a/butter/mas/clients/client_http.ts +++ b/butter/mas/clients/client_http.ts @@ -14,7 +14,7 @@ export class HttpClient extends Client { * @param {string} [protocol='http'] * @memberof HttpClient */ - constructor(ip: string, port: number=3000, protocol: string='http') { + constructor(ip: string, port: number = 3000, protocol: string = 'http') { super(ip, port, protocol); } } diff --git a/butter/mas/clients/client_tcp.ts b/butter/mas/clients/client_tcp.ts index 4ff4d07..8a1341e 100644 --- a/butter/mas/clients/client_tcp.ts +++ b/butter/mas/clients/client_tcp.ts @@ -14,7 +14,7 @@ export class TcpClient extends Client { * @param {string} [protocol='tcp'] * @memberof TcpClient */ - constructor(ip: string, port: number=3003, protocol: string='tcp') { + constructor(ip: string, port: number = 3003, protocol: string = 'tcp') { super(ip, port, protocol); } } diff --git a/butter/mas/clients/client_udp.ts b/butter/mas/clients/client_udp.ts index aa09abe..55b22f7 100644 --- a/butter/mas/clients/client_udp.ts +++ b/butter/mas/clients/client_udp.ts @@ -14,7 +14,7 @@ export class UdpClient extends Client { * @param {string} [protocol='udp'] * @memberof UdpClient */ - constructor(ip: string, port: number=3030, protocol: string='udp') { + constructor(ip: string, port: number = 3030, protocol: string = 'udp') { super(ip, port, protocol); } } diff --git a/butter/mas/environment.ts b/butter/mas/environment.ts index bb241f2..e08e31c 100644 --- a/butter/mas/environment.ts +++ b/butter/mas/environment.ts @@ -1,2 +1,3 @@ -export const __version__ = '2.2.0' -export const app_name = '@butter-robotics/mas-javascript-api' \ No newline at end of file +export const __version__ = '2.4.0'; +// eslint-disable-next-line camelcase +export const app_name = '@butter-robotics/mas-javascript-api'; diff --git a/butter/mas/packets/packet.ts b/butter/mas/packets/packet.ts index 9dc3b98..7195d20 100644 --- a/butter/mas/packets/packet.ts +++ b/butter/mas/packets/packet.ts @@ -10,7 +10,7 @@ export class Packet { port: number; query: string; - + /** * Creates an instance of Packet. * @param {string} ip robot IP @@ -45,9 +45,7 @@ export class Packet { * @memberof Packet */ protected generateResponse(content: ResponseData): Response { - let response: Response; - - response = { + const response: Response = { data: content, status: 200, statusText: 'OK' @@ -73,7 +71,7 @@ export class Packet { * @returns * @memberof Packet */ - protected generateEmptyResponse(error: any=null, errorType: string='unknown'): Response { + protected generateEmptyResponse(error: any = null, errorType: string = 'unknown'): Response { let response: Response; response = { @@ -83,14 +81,14 @@ export class Packet { parameters: null }, response: { - status: "Failed", + status: 'Failed', data: null, - metadata: { - handler: "unknown", - exception: `Request resolved with an ${errorType} error. ${error.message}.`, - timestamp: 0, - duration: 0, - asynchronous: false + metadata: { + handler: 'unknown', + exception: `Request resolved with an ${errorType} error. ${error.message}.`, + timestamp: 0, + duration: 0, + asynchronous: false } }, executed: false @@ -122,14 +120,14 @@ export class Packet { parameters: null }, response: { - status: "Failed", + status: 'Failed', data: null, - metadata: { - handler: "unknown", - exception: `Request resolved with an ${errorType} error. ${error.message}.`, - timestamp: 0, - duration: 0, - asynchronous: false + metadata: { + handler: 'unknown', + exception: `Request resolved with an ${errorType} error. ${error.message}.`, + timestamp: 0, + duration: 0, + asynchronous: false } }, executed: false @@ -145,6 +143,6 @@ export class Packet { equals(other: any) { - return other instanceof Packet && this.ip == other.ip && this.port == other.port && this.query == other.query; + return other instanceof Packet && this.ip === other.ip && this.port === other.port && this.query === other.query; } -} \ No newline at end of file +} diff --git a/butter/mas/packets/packet_builder.ts b/butter/mas/packets/packet_builder.ts index 35b0945..77dafd1 100644 --- a/butter/mas/packets/packet_builder.ts +++ b/butter/mas/packets/packet_builder.ts @@ -23,7 +23,7 @@ export class PacketBuilder { * @param {string} [protocol="http"] communication protocol * @memberof PacketBuilder */ - constructor(ip: string, port: number, protocol: string='http') { + constructor(ip: string, port: number, protocol: string = 'http') { this.ip = ip; this.port = port; @@ -63,7 +63,7 @@ export class PacketBuilder { return this; } - + /** @@ -170,24 +170,25 @@ export class PacketBuilder { let query = `${this.cmd}?`; - if (this.args) { + if (this.args) { query = `${query}${this.args.join('&')}&`; } - if (this.params) { + if (this.params) { const params = this.params.map(this._formatParameter); query = `${query}${params.join('&')}&`; } if (this.keys) { - let keys: Array = []; - this.keys.forEach((value, key) => {keys.push(`${key}=${value}`)}); + const keys: Array = []; + this.keys.forEach((value, key) => { keys.push(`${key}=${value}`) }); query = `${query}${keys.join('&')}`; } let uri = ['api', 'robots', 'any', 'command'].join('/'); - uri = `${uri}/${query.replace(/&+$/, "")}`; + uri = `${uri}/${query.replace(/&+$/, '')}`; + // eslint-disable-next-line new-cap return new this.packet(this.ip, this.port, uri); - } + } } diff --git a/butter/mas/packets/packet_factory.ts b/butter/mas/packets/packet_factory.ts index d12f26f..42a6694 100644 --- a/butter/mas/packets/packet_factory.ts +++ b/butter/mas/packets/packet_factory.ts @@ -8,7 +8,6 @@ import { UdpPacket } from './packet_udp'; * @class PacketFactory */ export class PacketFactory { - /** * Creates new client * @@ -19,13 +18,13 @@ export class PacketFactory { * @returns requested client * @memberof PacketFactory */ - getPacket(ip: string, port: number, query: string, protocol: string="http") { - if (protocol == "http") { + getPacket(ip: string, port: number, query: string, protocol: string = 'http') { + if (protocol === 'http') { return new HttpPacket(ip, port, query); - } else if (protocol == "tcp") { + } else if (protocol === 'tcp') { throw new Error('Not Implemented'); return new TcpPacket(ip, port, query); - } else if (protocol == "udp") { + } else if (protocol === 'udp') { throw new Error('Not Implemented'); return new UdpPacket(ip, port, query); } else { @@ -41,13 +40,13 @@ export class PacketFactory { * @returns * @memberof PacketFactory */ - getPacketClass(protocol: string="http") { - if (protocol == "http") { + getPacketClass(protocol: string = 'http') { + if (protocol === 'http') { return HttpPacket; - } else if (protocol == "tcp") { + } else if (protocol === 'tcp') { throw new Error('Not Implemented'); return TcpPacket; - } else if (protocol == "udp") { + } else if (protocol === 'udp') { throw new Error('Not Implemented'); return UdpPacket; } else { diff --git a/butter/mas/packets/packet_http.ts b/butter/mas/packets/packet_http.ts index b0f2715..4fc0eb9 100644 --- a/butter/mas/packets/packet_http.ts +++ b/butter/mas/packets/packet_http.ts @@ -10,8 +10,6 @@ import { Response } from '../interfaces/response' * @extends {Packet} */ export class HttpPacket extends Packet { - - /** *Creates an instance of HttpPacket. * @param {string} ip robot IP @@ -31,20 +29,20 @@ export class HttpPacket extends Packet { * @returns response containing the response * @memberof HttpPacket */ - async send(timeout: number=40): Promise { + async send(timeout: number = 40): Promise { let response: Response; try { response = await axios.get(`http://${this.ip}:${this.port}/${this.query}`, { timeout }); - } catch(error) { + } catch (error) { console.error(`Warning: request failed.\n${error}\n`); - response = this.generateEmptyResponse(error); + response = this.generateEmptyResponse(); } return response; } equals(other: any) { - return other instanceof HttpPacket && this.ip == other.ip && this.port == other.port && this.query == other.query; + return other instanceof HttpPacket && this.ip === other.ip && this.port === other.port && this.query === other.query; } -} \ No newline at end of file +} diff --git a/butter/mas/packets/packet_tcp.ts b/butter/mas/packets/packet_tcp.ts index 4173df5..6fee71e 100644 --- a/butter/mas/packets/packet_tcp.ts +++ b/butter/mas/packets/packet_tcp.ts @@ -10,8 +10,6 @@ import { Response } from '../interfaces/response' * @extends {Packet} */ export class TcpPacket extends Packet { - - /** *Creates an instance of TcpPacket. * @param {string} ip robot IP @@ -31,12 +29,12 @@ export class TcpPacket extends Packet { * @returns response containing the response * @memberof TcpPacket */ - async send(timeout: number=40): Promise { + async send(timeout: number = 40): Promise { let response: Response; try { response = await axios.get(`http://${this.ip}:${this.port}/${this.query}`, { timeout }); - } catch(error) { + } catch (error) { console.error(`Warning: request failed.\n${error}\n`); response = this.generateEmptyResponse(); } @@ -45,6 +43,6 @@ export class TcpPacket extends Packet { } equals(other: any) { - return other instanceof TcpPacket && this.ip == other.ip && this.port == other.port && this.query == other.query; + return other instanceof TcpPacket && this.ip === other.ip && this.port === other.port && this.query === other.query; } -} \ No newline at end of file +} diff --git a/butter/mas/packets/packet_udp.ts b/butter/mas/packets/packet_udp.ts index 982d39f..7563582 100644 --- a/butter/mas/packets/packet_udp.ts +++ b/butter/mas/packets/packet_udp.ts @@ -6,12 +6,10 @@ import { Response } from '../interfaces/response' /** * Represents a http data packet * - * @class HttpPacket + * @class UdpPacket * @extends {Packet} */ export class UdpPacket extends Packet { - - /** *Creates an instance of UdpPacket. * @param {string} ip robot IP @@ -31,12 +29,12 @@ export class UdpPacket extends Packet { * @returns response containing the response * @memberof UdpPacket */ - async send(timeout: number=40): Promise { + async send(timeout: number = 40): Promise { let response: Response; try { response = await axios.get(`http://${this.ip}:${this.port}/${this.query}`, { timeout }); - } catch(error) { + } catch (error) { console.error(`Warning: request failed.\n${error}\n`); response = this.generateEmptyResponse(); } @@ -45,6 +43,6 @@ export class UdpPacket extends Packet { } equals(other: any) { - return other instanceof UdpPacket && this.ip == other.ip && this.port == other.port && this.query == other.query; + return other instanceof UdpPacket && this.ip === other.ip && this.port === other.port && this.query === other.query; } -} \ No newline at end of file +}