Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
pinpong committed Sep 29, 2023
1 parent 3eb6ad7 commit b600f7b
Show file tree
Hide file tree
Showing 30 changed files with 1,147 additions and 2,237 deletions.
103 changes: 26 additions & 77 deletions src/systems/accesses/index.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,39 @@
import { ItemStatusResponse, SystemConfig } from '../../client';
import { ItemStatusResponse, LocalClient, RemoteClient, SystemConfig } from '../../client';
import { tryParseFloat } from '../../utils/extensions/numberUtils';
import { systemFilteredByItems, valuesToStringList } from '../../utils/extensions/stringUtils';
import { valuesToStringList } from '../../utils/extensions/stringUtils';
import { BaseSystem } from '../base';
import { SystemType, Trend } from '../base/types';
import { SystemType } from '../base/types';
import { Access, AccessState } from './types';

const systemType = SystemType.accesses;
/**
* @group Systems
*/
export class Accesses extends BaseSystem {
/**
* Parses the item.
* @param config - The myGEKKO device configuration.
* @param status - The response from the status request.
* @param itemId - The item id.
*/
private parseItem(config: SystemConfig, status: ItemStatusResponse, itemId: string): Access {
const values = valuesToStringList(status);
export class Accesses extends BaseSystem<Access> {
public constructor(client: LocalClient | RemoteClient) {
/**
* Parses the item.
* @param config - The myGEKKO device configuration.
* @param status - The response from the status request.
* @param itemId - The item id.
*/
function parseItem(config: SystemConfig, status: ItemStatusResponse, itemId: string): Access {
const values = valuesToStringList(status);

return {
sumState: tryParseFloat(values[1]),
itemId: itemId,
name: config[itemId].name,
page: config[itemId].page,
currentState: tryParseFloat(values[0]),
startCondition: tryParseFloat(values[2]),
gateRuntimePercentage: tryParseFloat(values[3]),
accessType: tryParseFloat(values[4]),
};
}

/**
* Returns all items.
* @throws {@link ClientError}
*/
public async getItems(): Promise<Access[]> {
const status = await this.getCompleteStatus(systemType);
return systemFilteredByItems(this.client.systemConfig[systemType]).map((key) => {
return this.parseItem(this.client.systemConfig[systemType], status[key], key);
});
}

/**
* Returns a single item by id.
* @param itemId - The item id.
*/
public async getItemById(itemId: string): Promise<Access> {
const status = await this.getStatusById(systemType, itemId);
return this.parseItem(this.client.systemConfig[systemType], status, itemId);
}

/**
* Returns all trends.
* @param startDate - The start date as date string.
* @param endDate - The start date as date string.
* @param count - The data count.
* @throws {@link ClientError}
*/
public async getTrends(startDate: string, endDate: string, count: number): Promise<Trend[]> {
return await this.getTrendsStatuses(systemType, startDate, endDate, count);
}
return {
sumState: tryParseFloat(values[1]),
itemId: itemId,
name: config[itemId].name,
page: config[itemId].page,
currentState: tryParseFloat(values[0]),
startCondition: tryParseFloat(values[2]),
gateRuntimePercentage: tryParseFloat(values[3]),
accessType: tryParseFloat(values[4]),
};
}

/**
* Returns a single trend by item id.
* @param itemId - The item id.
* @param startDate - The start date as date string.
* @param endDate - The start date as date string.
* @param count - The data count.
* @throws {@link ClientError}
*/
public async getTrendByItemId(
itemId: string,
startDate: string,
endDate: string,
count: number
): Promise<Trend> {
return await this.getTrendStatus(systemType, itemId, startDate, endDate, count);
super(client, SystemType.accesses, parseItem);
}

/**
* Sets the state.
* @param itemId - The item id.
* @param state - The new state.
* @throws {@link ClientError}
*/
public async setOpenState(itemId: string, state: AccessState): Promise<void> {
let value = -1;
switch (state) {
Expand All @@ -98,6 +47,6 @@ export class Accesses extends BaseSystem {
value = 2;
break;
}
await this.client.changeRequest(systemType, itemId, `${value}`);
await this.client.changeRequest(this.systemType, itemId, `${value}`);
}
}
99 changes: 26 additions & 73 deletions src/systems/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,35 @@
import { ItemStatusResponse, SystemConfig } from '../../client';
import { ItemStatusResponse, LocalClient, RemoteClient, SystemConfig } from '../../client';
import { tryParseFloat } from '../../utils/extensions/numberUtils';
import { systemFilteredByItems, valuesToStringList } from '../../utils/extensions/stringUtils';
import { valuesToStringList } from '../../utils/extensions/stringUtils';
import { BaseSystem } from '../base';
import { SystemType, Trend } from '../base/types';
import { SystemType } from '../base/types';
import { Action, ActionState } from './types';

const systemType = SystemType.actions;

/**
* @group Systems
*/
export class Actions extends BaseSystem {
/**
* Parses the item.
* @param config - The myGEKKO device configuration.
* @param status - The response from the status request.
* @param itemId - The item id.
*/
private parseItem(config: SystemConfig, status: ItemStatusResponse, itemId: string): Action {
const values = valuesToStringList(status);

return {
sumState: tryParseFloat(values[2]),
itemId: itemId,
name: config[itemId].name,
page: config[itemId].page,
currentState: tryParseFloat(values[0]),
startCondition: tryParseFloat(values[1]),
};
}

/**
* Returns all items.
* @throws {@link ClientError}
*/
public async getItems(): Promise<Action[]> {
const status = await this.getCompleteStatus(systemType);
return systemFilteredByItems(this.client.systemConfig[systemType]).map((key) => {
return this.parseItem(this.client.systemConfig[systemType], status[key], key);
});
}

/**
* Returns a single item by id.
* @param itemId - The item id.
* @throws {@link ClientError}
*/
public async getItemById(itemId: string): Promise<Action> {
const status = await this.getStatusById(systemType, itemId);
return this.parseItem(this.client.systemConfig[systemType], status, itemId);
}

/**
* Returns all trends.
* @param startDate - The start date as date string.
* @param endDate - The start date as date string.
* @param count - The data count.
* @throws {@link ClientError}
*/
public async getTrends(startDate: string, endDate: string, count: number): Promise<Trend[]> {
return await this.getTrendsStatuses(systemType, startDate, endDate, count);
}

/**
* Returns a single trend by item id.
* @param itemId - The item id.
* @param startDate - The start date as date string.
* @param endDate - The start date as date string.
* @param count - The data count.
* @throws {@link ClientError}
*/
public async getTrendByItemId(
itemId: string,
startDate: string,
endDate: string,
count: number
): Promise<Trend> {
return await this.getTrendStatus(systemType, itemId, startDate, endDate, count);
export class Actions extends BaseSystem<Action> {
public constructor(client: LocalClient | RemoteClient) {
/**
* Parses the item.
* @param config - The myGEKKO device configuration.
* @param status - The response from the status request.
* @param itemId - The item id.
*/
function parseItem(config: SystemConfig, status: ItemStatusResponse, itemId: string): Action {
const values = valuesToStringList(status);

return {
sumState: tryParseFloat(values[2]),
itemId: itemId,
name: config[itemId].name,
page: config[itemId].page,
currentState: tryParseFloat(values[0]),
startCondition: tryParseFloat(values[1]),
};
}

super(client, SystemType.actions, parseItem);
}

/**
Expand All @@ -86,6 +39,6 @@ export class Actions extends BaseSystem {
* @throws {@link ClientError}
*/
public async setState(itemId: string, state: ActionState): Promise<void> {
await this.client.changeRequest(systemType, itemId, `${state}`);
await this.client.changeRequest(this.systemType, itemId, `${state}`);
}
}
Loading

0 comments on commit b600f7b

Please sign in to comment.