Skip to content

Commit

Permalink
Pending changes exported from your codespace
Browse files Browse the repository at this point in the history
  • Loading branch information
meisZWFLZ committed Dec 15, 2023
1 parent da58c20 commit d936132
Showing 1 changed file with 33 additions and 38 deletions.
71 changes: 33 additions & 38 deletions src/VexDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,14 @@ export abstract class VexSerialDevice extends VexEventTarget {
abstract disconnect(): void;
}

class V5SerialDeviceProxyHandler implements ProxyHandler<object> {
device?: V5SerialDevice;
parent?: V5SerialDeviceProxyHandler;
rootkey?: string;

class V5SerialDeviceProxyHandler implements ProxyHandler<V5SerialDeviceState> {
constructor(
device?: V5SerialDevice,
parent?: V5SerialDeviceProxyHandler,
rootkey?: string,
) {
this.device = device;
this.parent = parent;
this.rootkey = rootkey;
}
protected device?: V5SerialDevice,
protected parent?: V5SerialDeviceProxyHandler,
protected rootkey?: keyof V5SerialDeviceState,
) {}

get(target: any, key: any): any {
get(target: V5SerialDeviceState, key: keyof V5SerialDeviceState): unknown {
if (
typeof target[key] === "object" &&
target[key] !== null &&
Expand All @@ -190,7 +182,7 @@ class V5SerialDeviceProxyHandler implements ProxyHandler<object> {
}
}

set(target: any, key: any, value: any): boolean {
set<K extends keyof V5SerialDeviceState>(target: V5SerialDeviceState, key: K, value: V5SerialDeviceState[K] ): boolean {
const oldValue = target[key];
if (oldValue === value) {
return true;
Expand All @@ -202,15 +194,14 @@ class V5SerialDeviceProxyHandler implements ProxyHandler<object> {
) {
if (shallowEqual(oldValue, value)) return true;
}

target[key] = value;

this.parent?.childSet(this.rootkey, key, value);

return true;
}

childSet(rootkey: string | undefined, subkey: string, value: any) {
childSet(rootkey: string | undefined, subkey: string, value: any): void {

Check failure on line 204 in src/VexDevice.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
if (this.parent)

Check failure on line 205 in src/VexDevice.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected nullable object value in conditional. An explicit null check is required
this.parent.childSet(this.rootkey, rootkey + "." + subkey, value);
else console.log("changed", rootkey + "." + subkey, value); // TODO
Expand Down Expand Up @@ -846,17 +837,17 @@ export class V5SmartDevice {
return this.state.devices[this.deviceIndex] !== undefined;
}

get port() : number {
get port(): number {
return this.deviceIndex;
}

get type() : SmartDeviceType{
get type(): SmartDeviceType {
return this.isAvailable
? this.state.devices[this.deviceIndex].type
: SmartDeviceType.EMPTY;
}

get version() : number {
get version(): number {
return this.isAvailable ? this.state.devices[this.deviceIndex].version : 0;
}
}
Expand All @@ -868,27 +859,27 @@ export class V5Radio {
this.state = state;
}

get channel() : number {
get channel(): number {
return this.state.radio.channel;
}

get isAvailable() : boolean {
get isAvailable(): boolean {
return this.state.radio.isAvailable;
}

get isConnected() : boolean {
get isConnected(): boolean {
return this.state.radio.isConnected;
}

get isVexNet() : boolean {
get isVexNet(): boolean {
return this.state.radio.isVexNet;
}

get isRadioData() : boolean {
get isRadioData(): boolean {
return this.state.radio.isRadioData;
}

get latency() : number {
get latency(): number {
return this.state.radio.latency;
}

Expand Down Expand Up @@ -926,7 +917,7 @@ export class V5SerialDevice extends VexSerialDevice {
this.pauseRefreshOnFileTransfer &&
!this.state._isFileTransferring
) {
this.refresh();
void this.refresh();
}
}
}, 200);
Expand All @@ -940,14 +931,15 @@ export class V5SerialDevice extends VexSerialDevice {
return new V5Brain(this.state);
}

get controllers() : [V5Controller, V5Controller]{
get controllers(): [V5Controller, V5Controller] {
return [new V5Controller(this.state, 0), new V5Controller(this.state, 1)];
}

get devices(): V5SmartDevice[] {
const rtn = [];
for (let i = 1; i <= this.state.devices.length; i++) {
if (this.state.devices[i] != null) rtn.push(new V5SmartDevice(this.state, i));
if (this.state.devices[i] != null)
rtn.push(new V5SmartDevice(this.state, i));
}
return rtn;
}
Expand Down Expand Up @@ -1029,7 +1021,7 @@ export class V5SerialDevice extends VexSerialDevice {
do {
successB4Timeout = await sleepUntil(
() => !this._isReconnecting,
timeout === 0? 1000: timeout,
timeout === 0 ? 1000 : timeout,
);
} while (timeout === 0 && !successB4Timeout);

Check failure on line 1026 in src/VexDevice.ts

View workflow job for this annotation

GitHub Actions / build

'timeout' is not modified in this loop

Expand Down Expand Up @@ -1078,10 +1070,13 @@ export class V5SerialDevice extends VexSerialDevice {
if (this.isConnected) break;

// try again every second or when the number of ports is different
const getPortCount = async () =>
const getPortCount = async (): Promise<number> =>
(await this.defaultSerial.getPorts()).length;
const portsCount = await getPortCount();
await sleepUntil(async () => (await getPortCount()) !== portsCount, 1000);
await sleepUntilAsync(
async () => (await getPortCount()) !== portsCount,
1000,
);
}

this._isReconnecting = false;
Expand All @@ -1093,19 +1088,19 @@ export class V5SerialDevice extends VexSerialDevice {
return true;
}

private async doAfterConnect() {
if (!this.connection) return;
private async doAfterConnect(): Promise<void> {
if (this.connection == null) return;

console.log("doAfterConnect");

this.connection.on("disconnected", (_data) => {
if (this.autoReconnect) this.reconnect();
if (this.autoReconnect) void this.reconnect();
});

await this.refresh();
}

async refresh() {
async refresh(): Promise<boolean> {
const ssPacket = await this.connection?.getSystemStatus();
if (ssPacket == null) {
this.state.brain.isAvailable = false;
Expand All @@ -1119,9 +1114,9 @@ export class V5SerialDevice extends VexSerialDevice {
const flags2 = ssPacket.sysflags[2];
this.state.controllers[0].isCharging = (flags2 & 0b10000000) !== 0;
this.state.matchMode =
((flags2 & 0b00100000) !== 0)
(flags2 & 0b00100000) !== 0
? "disabled"
: ((flags2 & 0b01000000) !== 0)
: (flags2 & 0b01000000) !== 0
? "autonomous"
: "driver";
this.state.isFieldControllerConnected = (flags2 & 0b00010000) !== 0;
Expand Down

0 comments on commit d936132

Please sign in to comment.