Skip to content

Commit

Permalink
yarn format
Browse files Browse the repository at this point in the history
  • Loading branch information
davbauer committed Nov 30, 2023
1 parent cd64bef commit 661003b
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 59 deletions.
4 changes: 3 additions & 1 deletion backend/api/services/InverterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import ApiBase from './ApiBase.js';
import InterfaceInvPowerFlowRealtimeData from '../../models/InterfaceInvPowerFlowRealtimeData.js';
import InterfaceInvMeterRealtimeData from '../../models/InterfaceInvMeterRealtimeData.js';
export default class extends ApiBase {
static async getPowerFlowRealtimeData(host: string): Promise<InterfaceInvPowerFlowRealtimeData | null> {
static async getPowerFlowRealtimeData(
host: string
): Promise<InterfaceInvPowerFlowRealtimeData | null> {
return this.get<InterfaceInvPowerFlowRealtimeData>(
host,
'solar_api/v1/GetPowerFlowRealtimeData.fcgi'
Expand Down
4 changes: 2 additions & 2 deletions backend/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import chargeRoutes from './routes/chargeRoutes.js';
import configRoutes from './routes/configRoutes.js';
import configEnabledRoutes from './routes/configEnabledRoutes.js';
import configEnabledPowergridRoutes from './routes/configEnabledPowergridRoutes.js';
import configPreferredPhase from './routes/configPreferredPhase.js'
import configPreferredPhase from './routes/configPreferredPhase.js';
import livedataRoutes from './routes/livedataRoutes.js';
import appInfoRoutes from './routes/appInfoRoutes.js';
import AppInfo from './classes/AppInfo.js';
Expand All @@ -37,7 +37,7 @@ app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecs));
app.use(express.static('./svelte-build'));
app.use('/', chargeRoutes);
app.use('/', configRoutes);
app.use('/', configPreferredPhase)
app.use('/', configPreferredPhase);
app.use('/', configEnabledRoutes);
app.use('/', configEnabledPowergridRoutes);
app.use('/', livedataRoutes);
Expand Down
2 changes: 1 addition & 1 deletion backend/classes/ConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class {
value: 6740,
onePhase: false
}
],
]
};
const dirPath = path.dirname(this.filePath);
if (!fs.existsSync(dirPath)) {
Expand Down
2 changes: 1 addition & 1 deletion backend/classes/WebSocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default class WebSocketManager {
this.sendEvent('backendTerminalUpdate', { type, msg, ts });
}

public static sendEventPreferredPhase(state: Pick<InterfaceConfig, "PreferredPhase">) {
public static sendEventPreferredPhase(state: Pick<InterfaceConfig, 'PreferredPhase'>) {
this.sendEvent('preferredPhaseUpdate', { state });
}

Expand Down
18 changes: 11 additions & 7 deletions backend/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,14 @@ function findClosestValue(availablePower: number, mappingArray: any[]): any {
const maximumWatts = configFile.MaximumWatts;

// Filter the mappings based on the preferredPhase and power constraints.
const filteredMappings = mappingArray.filter(item => {
return ((preferredPhase === 0) || // 0 for auto (accept all)
(preferredPhase === 1 && item.onePhase) || // 1 for phase 1
(preferredPhase === 2 && !item.onePhase)) && // 2 for phase 3
(item.value >= minimumWatts && item.value <= maximumWatts); // within min and max power range
const filteredMappings = mappingArray.filter((item) => {
return (
(preferredPhase === 0 || // 0 for auto (accept all)
(preferredPhase === 1 && item.onePhase) || // 1 for phase 1
(preferredPhase === 2 && !item.onePhase)) && // 2 for phase 3
item.value >= minimumWatts &&
item.value <= maximumWatts
); // within min and max power range
});

// If there are no mappings left after filtering, return a default response.
Expand All @@ -159,13 +162,14 @@ function findClosestValue(availablePower: number, mappingArray: any[]): any {

// Find the mapping that is closest to availablePower.
const optimalMapping = filteredMappings.reduce((prev, curr) => {
return (Math.abs(curr.value - availablePower) < Math.abs(prev.value - availablePower)) ? curr : prev;
return Math.abs(curr.value - availablePower) < Math.abs(prev.value - availablePower)
? curr
: prev;
});

return optimalMapping;
}


function calculateChargeSettings(config: InterfaceConfig) {
const availablePower = LiveData.data.Inverter.Export + LiveData.data.Charger.Consumption;

Expand Down
4 changes: 2 additions & 2 deletions backend/models/InterfaceConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export default interface InterfaceConfig {
UsePowergrid: boolean;
BatteryCapacity: number;
CarEfficiency: number;
// 0 = Auto, 1 = One Phase, 2 = 3 Phases
PreferredPhase: 0 | 1 | 2
// 0 = Auto, 1 = One Phase, 2 = 3 Phases
PreferredPhase: 0 | 1 | 2;
}
42 changes: 21 additions & 21 deletions backend/routes/configPreferredPhase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,27 @@ const r = express.Router();
* example: 'Error writing to config file'
*/
r.post('/preferredPhase', async (req, res) => {
const stateData = req.body.state;
if (stateData !== 0 && stateData !== 1 && stateData !== 2) {
res.status(400).json({
msg: 'Bad Request: state must be 0, 1, or 2'
});
return;
}
const configData = ConfigFile.read();
configData.PreferredPhase = stateData;
const success = ConfigFile.write(configData);
const stateData = req.body.state;
if (stateData !== 0 && stateData !== 1 && stateData !== 2) {
res.status(400).json({
msg: 'Bad Request: state must be 0, 1, or 2'
});
return;
}
const configData = ConfigFile.read();
configData.PreferredPhase = stateData;
const success = ConfigFile.write(configData);

// It's better to send events after ensuring that data is written successfully.
if (success) {
WebSocketManager.sendEventPreferredPhase(stateData); // Adjusted position
res.status(200).json({ msg: 'success' });
} else {
errorLog('Error writing preferredPhase state to config file.');
res.status(500).json({
msg: 'Error writing to config file'
});
}
// It's better to send events after ensuring that data is written successfully.
if (success) {
WebSocketManager.sendEventPreferredPhase(stateData); // Adjusted position
res.status(200).json({ msg: 'success' });
} else {
errorLog('Error writing preferredPhase state to config file.');
res.status(500).json({
msg: 'Error writing to config file'
});
}
});

export default r;
export default r;
18 changes: 15 additions & 3 deletions dist/output.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,21 @@ html {
-o-tab-size: 4;
tab-size: 4;
/* 3 */
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji',
'Segoe UI Symbol', 'Noto Color Emoji';
font-family:
ui-sans-serif,
system-ui,
-apple-system,
BlinkMacSystemFont,
'Segoe UI',
Roboto,
'Helvetica Neue',
Arial,
'Noto Sans',
sans-serif,
'Apple Color Emoji',
'Segoe UI Emoji',
'Segoe UI Symbol',
'Noto Color Emoji';
/* 4 */
font-feature-settings: normal;
/* 5 */
Expand Down
2 changes: 1 addition & 1 deletion src/app.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
Expand Down
10 changes: 5 additions & 5 deletions src/lib/api/models/BackendLogs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default interface BackendLogs {
items: {
type: string;
msg: string;
ts: string
}[];
items: {
type: string;
msg: string;
ts: string;
}[];
}
4 changes: 2 additions & 2 deletions src/lib/api/models/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export default interface Config {
UsePowergrid: boolean;
BatteryCapacity: number;
CarEfficiency: number;
// 0 = Auto, 1 = One Phase, 2 = 3 Phases
PreferredPhase: 0 | 1 | 2
// 0 = Auto, 1 = One Phase, 2 = 3 Phases
PreferredPhase: 0 | 1 | 2;
}
6 changes: 3 additions & 3 deletions src/lib/api/services/ServiceConfigPreferredPhase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type ResBase from '../models/ResBase';
import ApiBase from './ApiBase';

export default class extends ApiBase {
static async postEnabled(state: 0 | 1 | 2): Promise<ResBase> {
return this.post<ResBase>('preferredPhase', { state });
}
static async postEnabled(state: 0 | 1 | 2): Promise<ResBase> {
return this.post<ResBase>('preferredPhase', { state });
}
}
3 changes: 1 addition & 2 deletions src/lib/api/services/ServiceWebsocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export default class {
newErrorToast(errMsg);
} else {
this.socket = new WebSocket(`ws://${window.location.hostname}:${get(appInfo).webSocketPort}`);
}

}

this.socket.onmessage = (event) => {
const message = JSON.parse(event.data);
Expand Down
16 changes: 8 additions & 8 deletions src/lib/components/SectionLiveData.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@
{$liveData.Charger.PhaseMode === 0
? 'Auto'
: $liveData.Charger.PhaseMode === 1
? '1P'
: $liveData.Charger.PhaseMode === 2
? '3P'
: 'unknown'}
? '1P'
: $liveData.Charger.PhaseMode === 2
? '3P'
: 'unknown'}
{/if}
</p>
</div>
Expand Down Expand Up @@ -208,10 +208,10 @@
{$liveData.Charger.PhaseModeCalc === 0
? 'Auto'
: $liveData.Charger.PhaseModeCalc === 1
? '1P'
: $liveData.Charger.PhaseModeCalc === 2
? '3P'
: 'unknown'}
? '1P'
: $liveData.Charger.PhaseModeCalc === 2
? '3P'
: 'unknown'}
{/if}
</p>
</div>
Expand Down

0 comments on commit 661003b

Please sign in to comment.