diff --git a/csharp/App/Backend/Controller.cs b/csharp/App/Backend/Controller.cs index 9cd188a97..35105a84b 100644 --- a/csharp/App/Backend/Controller.cs +++ b/csharp/App/Backend/Controller.cs @@ -1899,15 +1899,22 @@ public class Controller : ControllerBase public async Task>> EditInstallationConfig([FromBody] Configuration config, Int64 installationId,int product,Token authToken) { var session = Db.GetSession(authToken); - - string configString = product switch + + // Dynamic Pricing in Spot Price mode: forward the provider chosen on the Information tab + // so the device knows which operator's API to query for spot prices. + if (config.DynamicPricingMode == "SpotPrice") { - 0 => config.GetConfigurationSalimax(), // Salimax - 3 => config.GetConfigurationSodistoreMax(), // SodiStoreMax - 2 => config.GetConfigurationSodistoreHome(), // SodiStoreHome - 4 => config.GetConfigurationSodistoreGrid(), // SodistoreGrid - _ => config.GetConfigurationString() // fallback - }; + var installation = Db.GetInstallationById(installationId); + config.NetworkProvider = installation?.NetworkProvider; + } + + // Serialize what was actually sent — drops null/unset fields so the audit + // entry is product-shaped automatically (no per-product formatter to maintain). + var configString = System.Text.Json.JsonSerializer.Serialize(config, new System.Text.Json.JsonSerializerOptions + { + DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull, + WriteIndented = false, + }); Console.WriteLine("CONFIG IS " + configString); diff --git a/csharp/App/Backend/DataTypes/Configuration.cs b/csharp/App/Backend/DataTypes/Configuration.cs index 5c0726ca5..589511bf4 100644 --- a/csharp/App/Backend/DataTypes/Configuration.cs +++ b/csharp/App/Backend/DataTypes/Configuration.cs @@ -9,51 +9,37 @@ public class Configuration public CalibrationChargeType? CalibrationDischargeState { get; set; } public DateTime? CalibrationDischargeDate { get; set; } + // V1 (legacy) flat fields — still used by the original SodistoreHomeConfiguration page + // for installations not opted in to V2. WhenWritingNull keeps them out of V2 payloads. public double? MaximumDischargingCurrent { get; set; } public double? MaximumChargingCurrent { get; set; } - public double? OperatingPriority { get; set; } + public int? InverterNumber { get; set; } public double? BatteriesCount { get; set; } + public List? BatteriesCountPerInverter { get; set; } public double? ClusterNumber { get; set; } public double? PvNumber { get; set; } + // V2 — per-inverter Clusters + PvCount, keyed by "Inverter1".."InverterN". + // Wire format mirrors the on-disk shape — device merges these into its existing Devices.InverterN entries. + // Per-cluster MaxChargingCurrent / MaxDischargingCurrent live inside Devices[InverterN].Clusters[ClusterN]. + public Dictionary? Devices { get; set; } + public double? OperatingPriority { get; set; } public bool ControlPermission { get; set; } public double? TimeChargeandDischargePower { get; set; } public DateTime? StartTimeChargeandDischargeDayandTime { get; set; } public DateTime? StopTimeChargeandDischargeDayandTime { get; set; } - - public String GetConfigurationString() - { - return $"MinimumSoC: {MinimumSoC}, GridSetPoint: {GridSetPoint}, CalibrationChargeState: {CalibrationChargeState}, CalibrationChargeDate: {CalibrationChargeDate}, " + - $"CalibrationDischargeState: {CalibrationDischargeState}, CalibrationDischargeDate: {CalibrationDischargeDate}, " + - $"MaximumDischargingCurrent: {MaximumDischargingCurrent}, MaximumChargingCurrent: {MaximumChargingCurrent}, OperatingPriority: {OperatingPriority}, " + - $"BatteriesCount: {BatteriesCount}, ClusterNumber: {ClusterNumber}, PvNumber: {PvNumber}, ControlPermission:{ControlPermission}, "+ - $"SinexcelTimeChargeandDischargePower: {TimeChargeandDischargePower}, SinexcelStartTimeChargeandDischargeDayandTime: {StartTimeChargeandDischargeDayandTime}, SinexcelStopTimeChargeandDischargeDayandTime: {StopTimeChargeandDischargeDayandTime}"; - - } - - public string GetConfigurationSalimax() - { - return - $"MinimumSoC: {MinimumSoC}, GridSetPoint: {GridSetPoint}, CalibrationChargeState: {CalibrationChargeState}, CalibrationChargeDate: {CalibrationChargeDate}"; - } - - public string GetConfigurationSodistoreMax() - { - return - $"MinimumSoC: {MinimumSoC}, GridSetPoint: {GridSetPoint}, CalibrationChargeState: {CalibrationChargeState}, CalibrationChargeDate: {CalibrationChargeDate}"; - } - public string GetConfigurationSodistoreHome() - { - return $"MinimumSoC: {MinimumSoC}, MaximumDischargingCurrent: {MaximumDischargingCurrent}, MaximumChargingCurrent: {MaximumChargingCurrent}, OperatingPriority: {OperatingPriority}, " + - $"BatteriesCount: {BatteriesCount}, ClusterNumber: {ClusterNumber}, PvNumber: {PvNumber}, ControlPermission:{ControlPermission}, "+ - $"SinexcelTimeChargeandDischargePower: {TimeChargeandDischargePower}, SinexcelStartTimeChargeandDischargeDayandTime: {StartTimeChargeandDischargeDayandTime}, SinexcelStopTimeChargeandDischargeDayandTime: {StopTimeChargeandDischargeDayandTime}"; - } + // Sinexcel Dynamic Pricing (under GridPriority) — strings for demo; engine will parse later. + public string? DynamicPricingMode { get; set; } + public string? NetworkProvider { get; set; } + public string? CurrentPrice { get; set; } + public string? PriceToSell { get; set; } + public string? PriceToBuy { get; set; } + // TOU windows stored as "HH:mm" strings + public string? TimeToSellFrom { get; set; } + public string? TimeToSellTo { get; set; } + public string? TimeToBuyFrom { get; set; } + public string? TimeToBuyTo { get; set; } - // TODO: SodistoreGrid — update configuration fields when defined - public string GetConfigurationSodistoreGrid() - { - return ""; - } } public enum CalibrationChargeType @@ -61,4 +47,17 @@ public enum CalibrationChargeType RepetitivelyEvery, AdditionallyOnce, ChargePermanently +} + +public class DeviceConfigPartial +{ + public Dictionary? Clusters { get; set; } + public int? PvCount { get; set; } +} + +public class ClusterConfig +{ + public int BatteryCount { get; set; } + public double MaxChargingCurrent { get; set; } + public double MaxDischargingCurrent { get; set; } } \ No newline at end of file diff --git a/csharp/App/Backend/DataTypes/Methods/ExoCmd.cs b/csharp/App/Backend/DataTypes/Methods/ExoCmd.cs index 44915ab37..133719cad 100644 --- a/csharp/App/Backend/DataTypes/Methods/ExoCmd.cs +++ b/csharp/App/Backend/DataTypes/Methods/ExoCmd.cs @@ -448,12 +448,16 @@ public static class ExoCmd for (int j = 0; j < maxRetransmissions; j++) { //string message = "This is a message from RabbitMQ server, you can subscribe to the RabbitMQ queue"; - byte[] data = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(config)); + // Drop null fields so the device only sees what's actually set for this product. + var jsonOptions = new System.Text.Json.JsonSerializerOptions + { + DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull + }; + var payload = JsonSerializer.Serialize(config, jsonOptions); + byte[] data = Encoding.UTF8.GetBytes(payload); udpClient.Send(data, data.Length, installation.VpnIp, port); - Console.WriteLine(config.GetConfigurationString()); - - Console.WriteLine($"Sent UDP message to {installation.VpnIp}:{port}: {config}"); + Console.WriteLine($"Sent UDP message to {installation.VpnIp}:{port}: {payload}"); //Console.WriteLine($"Sent UDP message to {installation.VpnIp}:{port}"+" GridSetPoint is "+config.GridSetPoint +" and MinimumSoC is "+config.MinimumSoC); IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(installation.VpnIp), port); diff --git a/csharp/App/SinexcelCommunication/DataTypes/Configuration.cs b/csharp/App/SinexcelCommunication/DataTypes/Configuration.cs index 710cdcbd8..f75c5af4b 100644 --- a/csharp/App/SinexcelCommunication/DataTypes/Configuration.cs +++ b/csharp/App/SinexcelCommunication/DataTypes/Configuration.cs @@ -16,5 +16,16 @@ public class Configuration public Single TimeChargeandDischargePower { get; set; } public Boolean ControlPermission { get; set; } + // Dynamic Pricing (under GridPriority) — strings for demo; engine parses when needed. + public String? DynamicPricingMode { get; set; } + public String? NetworkProvider { get; set; } + public String? CurrentPrice { get; set; } + public String? PriceToSell { get; set; } + public String? PriceToBuy { get; set; } + public String? TimeToSellFrom { get; set; } + public String? TimeToSellTo { get; set; } + public String? TimeToBuyFrom { get; set; } + public String? TimeToBuyTo { get; set; } + } diff --git a/csharp/App/SinexcelCommunication/Program.cs b/csharp/App/SinexcelCommunication/Program.cs index ffbccabad..3466f76db 100644 --- a/csharp/App/SinexcelCommunication/Program.cs +++ b/csharp/App/SinexcelCommunication/Program.cs @@ -639,6 +639,15 @@ internal static class Program status.Config.PvNumber = config.PvNumber; status.Config.ControlPermission = config.ControlPermission; + status.Config.DynamicPricingMode = config.DynamicPricingMode; + status.Config.NetworkProvider = config.NetworkProvider; + status.Config.CurrentPrice = config.CurrentPrice; + status.Config.PriceToSell = config.PriceToSell; + status.Config.PriceToBuy = config.PriceToBuy; + status.Config.TimeToSellFrom = config.TimeToSellFrom; + status.Config.TimeToSellTo = config.TimeToSellTo; + status.Config.TimeToBuyFrom = config.TimeToBuyFrom; + status.Config.TimeToBuyTo = config.TimeToBuyTo; } private static async Task SaveModbusTcpFile(StatusRecord status) diff --git a/csharp/App/SinexcelCommunication/SystemConfig/Config.cs b/csharp/App/SinexcelCommunication/SystemConfig/Config.cs index 5c6dfe708..84fda6050 100644 --- a/csharp/App/SinexcelCommunication/SystemConfig/Config.cs +++ b/csharp/App/SinexcelCommunication/SystemConfig/Config.cs @@ -22,10 +22,10 @@ public class Config //public required Decimal CheapPrice { get; set; } //public required Decimal HighPrice { get; set; } public required Double MinSoc { get; set; } - public required Double GridSetPoint { get; set; } - public required Double MaximumDischargingCurrent { get; set; } - public required Double MaximumChargingCurrent { get; set; } - public required OperatingPriority OperatingPriority { get; set; } + public required Double GridSetPoint { get; set; } + public required Double MaximumDischargingCurrent { get; set; } + public required Double MaximumChargingCurrent { get; set; } + public required OperatingPriority OperatingPriority { get; set; } public required Int16 BatteriesCount { get; set; } public required Int16 ClusterNumber { get; set; } public required Int16 PvNumber { get; set; } @@ -34,7 +34,18 @@ public class Config public required DateTime StopTimeChargeandDischargeDayandTime { get; set; } public required Single TimeChargeandDischargePower { get; set; } - public required Boolean ControlPermission { get; set; } + public required Boolean ControlPermission { get; set; } + + // Dynamic Pricing (under GridPriority) — strings for demo; engine parses when needed. + public String? DynamicPricingMode { get; set; } + public String? NetworkProvider { get; set; } + public String? CurrentPrice { get; set; } + public String? PriceToSell { get; set; } + public String? PriceToBuy { get; set; } + public String? TimeToSellFrom { get; set; } + public String? TimeToSellTo { get; set; } + public String? TimeToBuyFrom { get; set; } + public String? TimeToBuyTo { get; set; } public required S3Config? S3 { get; set; } diff --git a/typescript/frontend-marios2/src/content/dashboards/Log/graph.util.tsx b/typescript/frontend-marios2/src/content/dashboards/Log/graph.util.tsx index 57b2d2bbe..4bae6392b 100644 --- a/typescript/frontend-marios2/src/content/dashboards/Log/graph.util.tsx +++ b/typescript/frontend-marios2/src/content/dashboards/Log/graph.util.tsx @@ -329,6 +329,8 @@ export interface JSONRecordData { MaximumDischargingCurrent: number; OperatingPriority: string; BatteriesCount: number; + InverterNumber?: number; + BatteriesCountPerInverter?: number[]; ClusterNumber: number; PvNumber: number; ControlPermission:boolean; @@ -683,6 +685,17 @@ export interface I_BoxDataValue { value: string | number; } +export type ClusterConfig = { + BatteryCount: number; + MaxChargingCurrent: number; + MaxDischargingCurrent: number; +}; + +export type InverterConfig = { + Clusters: { [clusterKey: string]: ClusterConfig }; + PvCount: number; +}; + export type ConfigurationValues = { minimumSoC: string | number; gridSetPoint: number; @@ -694,16 +707,33 @@ export type ConfigurationValues = { //For sodistoreHome maximumDischargingCurrent: number; maximumChargingCurrent: number; + // Per-inverter Clusters + PvCount, keyed by "Inverter1".."InverterN". + // Wire format mirrors the on-disk Devices.InverterN shape — device merges by key. + devices?: { [inverterKey: string]: InverterConfig }; operatingPriority: number; batteriesCount: number; + inverterNumber: number; + batteriesCountPerInverter: number[]; clusterNumber: number; PvNumber: number; + pvCountPerInverter: number[]; controlPermission:boolean; // For sodistoreHome-Sinexcel: TimeChargeDischarge mode timeChargeandDischargePower?: number; startTimeChargeandDischargeDayandTime?: Date | null; stopTimeChargeandDischargeDayandTime?: Date | null; + + // For sodistoreHome-Sinexcel: Dynamic Pricing (under GridPriority) + dynamicPricingMode?: string; + currentPrice?: string; + priceToSell?: string; + priceToBuy?: string; + // TOU time windows stored as "HH:mm" strings + timeToSellFrom?: string; + timeToSellTo?: string; + timeToBuyFrom?: string; + timeToBuyTo?: string; }; // // export interface Pv { diff --git a/typescript/frontend-marios2/src/content/dashboards/SodiohomeInstallations/Installation.tsx b/typescript/frontend-marios2/src/content/dashboards/SodiohomeInstallations/Installation.tsx index 272f6efcb..3fedde12d 100644 --- a/typescript/frontend-marios2/src/content/dashboards/SodiohomeInstallations/Installation.tsx +++ b/typescript/frontend-marios2/src/content/dashboards/SodiohomeInstallations/Installation.tsx @@ -25,6 +25,11 @@ import { fetchDataJson } from '../Installations/fetchData'; import { FetchResult } from '../../../dataCache/dataCache'; import BatteryViewSodioHome from '../BatteryView/BatteryViewSodioHome'; import SodistoreHomeConfiguration from './SodistoreHomeConfiguration'; +import SodistoreHomeConfigurationV2 from './SodistoreHomeConfigurationV2'; + +// Pilot installations using the new per-cluster Configuration page (V2). +// All other installations keep using the original SodistoreHomeConfiguration (V1). +const CONFIG_V2_INSTALLATION_IDS = new Set([790, 839]); import TopologySodistoreHome from '../Topology/TopologySodistoreHome'; import Overview from '../Overview/overview'; import WeeklyReport from './WeeklyReport'; @@ -599,11 +604,19 @@ function SodioHomeInstallation(props: singleInstallationProps) { + CONFIG_V2_INSTALLATION_IDS.has(props.current_installation.id) ? ( + + ) : ( + + ) } /> )} diff --git a/typescript/frontend-marios2/src/content/dashboards/SodiohomeInstallations/SodistoreHomeConfigurationV2.tsx b/typescript/frontend-marios2/src/content/dashboards/SodiohomeInstallations/SodistoreHomeConfigurationV2.tsx new file mode 100644 index 000000000..7045c54eb --- /dev/null +++ b/typescript/frontend-marios2/src/content/dashboards/SodiohomeInstallations/SodistoreHomeConfigurationV2.tsx @@ -0,0 +1,1026 @@ +import { ConfigurationValues, InverterConfig, JSONRecordData } from '../Log/graph.util'; +import { + Accordion, + AccordionDetails, + AccordionSummary, + Alert, + Box, + CardContent, + CircularProgress, + Container, + Divider, + FormControl, + Grid, + IconButton, + InputAdornment, + InputLabel, + Modal, + Select, + TextField, + Typography, + useTheme +} from '@mui/material'; +import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; + +import React, { useContext, useState, useEffect } from 'react'; +import { FormattedMessage, useIntl } from 'react-intl'; +import Button from '@mui/material/Button'; +import { Close as CloseIcon } from '@mui/icons-material'; +import MenuItem from '@mui/material/MenuItem'; +import axiosConfig from '../../../Resources/axiosConfig'; +import { UserContext } from '../../../contexts/userContext'; +import { ProductIdContext } from '../../../contexts/ProductIdContextProvider'; +import { I_Installation } from 'src/interfaces/InstallationTypes'; +import { + buildSodistoreProPreset, + getPresetsForDevice, + parseBatterySnTree, + PresetConfig +} from '../Information/installationSetupUtils'; +import { LocalizationProvider } from '@mui/x-date-pickers'; +import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; +import { DateTimePicker, TimePicker } from '@mui/x-date-pickers'; +import dayjs from 'dayjs'; +import Switch from '@mui/material/Switch'; +import FormControlLabel from '@mui/material/FormControlLabel'; + +interface SodistoreHomeConfigurationProps { + values: JSONRecordData; + id: number; + installation: I_Installation; +} + +function SodistoreHomeConfigurationV2(props: SodistoreHomeConfigurationProps) { + const intl = useIntl(); + if (props.values === null) { + return null; + } + + const device = props.installation.device; + + const OperatingPriorityOptions = + device === 3 || device === 4 + ? ['LoadPriority', 'BatteryPriority', 'GridPriority'] + : []; + + // Sinexcel S3 stores WorkingMode enum names — map them to Growatt-style display names + const sinexcelS3ToDisplayName: Record = { + 'SpontaneousSelfUse': 'LoadPriority', + 'TimeChargeDischarge': 'BatteryPriority', + 'PvPriorityCharging': 'GridPriority', + }; + + // Dynamic Pricing Mode — backend enum values with UI labels + const DynamicPricingOptions = ['Disabled', 'SpotPrice', 'Tou'] as const; + const dynamicPricingLabelKey: Record = { + Disabled: 'dynamicPricingOff', + SpotPrice: 'dynamicPricingSpotPrice', + Tou: 'dynamicPricingTou', + }; + + const [errors, setErrors] = useState({ + minimumSoC: false, + gridSetPoint: false + }); + + const SetErrorForField = (field_name, state) => { + setErrors((prevErrors) => ({ + ...prevErrors, + [field_name]: state + })); + }; + const theme = useTheme(); + const [formDirty, setFormDirty] = useState(false); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(false); + const [updated, setUpdated] = useState(false); + const [dateSelectionError, setDateSelectionError] = useState(''); + const [isErrorDateModalOpen, setErrorDateModalOpen] = useState(false); + const context = useContext(UserContext); + const { currentUser, setUser } = context; + const { product, setProduct } = useContext(ProductIdContext); + + // Resolve S3 OperatingPriority to display index (Sinexcel uses different enum names) + const resolveOperatingPriorityIndex = (s3Value: string) => { + const displayName = device === 4 ? (sinexcelS3ToDisplayName[s3Value] ?? s3Value) : s3Value; + return OperatingPriorityOptions.indexOf(displayName); + }; + + // Storage key for pending config (optimistic update) + const pendingConfigKey = `pendingConfig_${props.id}`; + + // Hardware topology — derived from Information tab (single source of truth). + const isSodistorePro = product === 5; + const installationModel = props.installation.installationModel; + const presetConfig: PresetConfig | null = isSodistorePro + ? (installationModel && parseInt(installationModel, 10) > 0 + ? buildSodistoreProPreset(parseInt(installationModel, 10)) + : null) + : (getPresetsForDevice(device)[installationModel] || null); + const inverterCount = presetConfig?.length ?? 1; + + // Default battery limit per cluster: 95A per battery for both charge and discharge + // (so 1-battery cluster → 95A, 2-battery cluster → 190A). + const DEFAULT_CURRENT_PER_BATTERY = 95; + const defaultClusterCurrent = (batteryCount: number) => batteryCount * DEFAULT_CURRENT_PER_BATTERY; + + // (No standalone preset-based builder — overlayTopology() handles the empty case + // and seeds defaults using the actual installed battery count from the Information tab, + // not the preset slot count.) + + // Helper to build form values from S3 data. + // V2 reads only fields it actually consumes — legacy flat keys (InverterNumber, + // BatteriesCount, MaximumChargingCurrent, etc.) are no longer wired through. + const getS3Values = (): Partial => { + // Read per-inverter Clusters/PvCount from each Devices.InverterN entry on disk. + const cfgDevices = (props.values.Config as any).Devices as { [k: string]: any } | undefined; + const cfgFromDisk: { [k: string]: InverterConfig } | undefined = cfgDevices + ? Object.fromEntries( + Object.entries(cfgDevices) + .filter(([k, v]: [string, any]) => k.startsWith('Inverter') && (v?.Clusters || v?.PvCount != null)) + .map(([k, v]: [string, any]) => [k, { Clusters: v.Clusters ?? {}, PvCount: v.PvCount ?? 0 }]) + ) + : undefined; + const hasDevicesData = cfgFromDisk && Object.keys(cfgFromDisk).length > 0; + return { + minimumSoC: props.values.Config.MinSoc, + // Information tab is source of truth for topology + per-cluster current defaults. + devices: overlayTopology(hasDevicesData ? cfgFromDisk : undefined), + operatingPriority: resolveOperatingPriorityIndex( + props.values.Config.OperatingPriority + ), + timeChargeandDischargePower: props.values.Config?.TimeChargeandDischargePower ?? 0, + startTimeChargeandDischargeDayandTime: (() => { + const raw = props.values.Config?.StartTimeChargeandDischargeDayandTime; + const parsed = raw ? dayjs(raw) : null; + return parsed && parsed.year() >= 2020 ? parsed.toDate() : new Date(); + })(), + stopTimeChargeandDischargeDayandTime: (() => { + const raw = props.values.Config?.StopTimeChargeandDischargeDayandTime; + const parsed = raw ? dayjs(raw) : null; + return parsed && parsed.year() >= 2020 ? parsed.toDate() : new Date(); + })(), + controlPermission: String(props.values.Config.ControlPermission).toLowerCase() === "true", + dynamicPricingMode: (props.values.Config as any).DynamicPricingMode ?? 'Disabled', + currentPrice: (props.values.Config as any).CurrentPrice?.toString() ?? '', + priceToSell: (props.values.Config as any).PriceToSell?.toString() ?? '', + priceToBuy: (props.values.Config as any).PriceToBuy?.toString() ?? '', + timeToSellFrom: (props.values.Config as any).TimeToSellFrom ?? '', + timeToSellTo: (props.values.Config as any).TimeToSellTo ?? '', + timeToBuyFrom: (props.values.Config as any).TimeToBuyFrom ?? '', + timeToBuyTo: (props.values.Config as any).TimeToBuyTo ?? '', + }; + }; + + // Restore pending config from localStorage, converting date strings back to Date objects. + // Returns { values, s3ConfigSnapshot } or null if no pending config. + const restorePendingConfig = () => { + try { + const pendingStr = localStorage.getItem(pendingConfigKey); + if (!pendingStr) return null; + + const pending = JSON.parse(pendingStr); + const v = pending.values; + const values: Partial = { + ...v, + // JSON.stringify converts Date→string; restore them back to Date objects + startTimeChargeandDischargeDayandTime: + v.startTimeChargeandDischargeDayandTime + ? dayjs(v.startTimeChargeandDischargeDayandTime).toDate() + : null, + stopTimeChargeandDischargeDayandTime: + v.stopTimeChargeandDischargeDayandTime + ? dayjs(v.stopTimeChargeandDischargeDayandTime).toDate() + : null, + }; + + return { values, s3ConfigSnapshot: pending.s3ConfigSnapshot || null }; + } catch (e) { + console.error('[Config:restore] Failed to parse localStorage', e); + localStorage.removeItem(pendingConfigKey); + return null; + } + }; + + // Fingerprint S3 Config for change detection (not value comparison) + const getS3ConfigFingerprint = () => JSON.stringify(props.values.Config); + + // Overlay Information-tab-derived topology onto a `devices` config object. + // Battery counts come from the SN tree (filled SNs); PvCount comes from pvStringsPerInverter. + // Existing per-cluster current limits are preserved. + const overlayTopology = ( + devices: { [k: string]: InverterConfig } | undefined + ): { [k: string]: InverterConfig } | undefined => { + if (!presetConfig) return devices; + const tree = parseBatterySnTree(props.installation.batterySerialNumbers || '', presetConfig); + const pvStrings = (props.installation.pvStringsPerInverter || '') + .split(',') + .map((s) => s.trim()); + const out: { [k: string]: InverterConfig } = {}; + presetConfig.forEach((clusters, invIdx) => { + const invKey = `Inverter${invIdx + 1}`; + const existingInv = devices?.[invKey]; + const cls: { [k: string]: any } = {}; + clusters.forEach((slotCount, clIdx) => { + const clKey = `Cluster${clIdx + 1}`; + const filled = (tree[invIdx]?.[clIdx] ?? []).filter((s) => s !== '').length; + const existingCl = existingInv?.Clusters?.[clKey]; + // Default current uses the cluster's ideal battery count (slotCount from the + // installation model preset), not the count of filled SNs — so an empty or + // partially-filled cluster still ships a sane non-zero limit. + const defaultCurrent = defaultClusterCurrent(slotCount); + cls[clKey] = { + BatteryCount: filled, + MaxChargingCurrent: existingCl?.MaxChargingCurrent ?? defaultCurrent, + MaxDischargingCurrent: existingCl?.MaxDischargingCurrent ?? defaultCurrent, + }; + }); + out[invKey] = { + Clusters: cls, + PvCount: parseInt(pvStrings[invIdx] || '0', 10) || 0, + }; + }); + return out; + }; + + // Initialize form from localStorage (if pending submit exists) or from S3 + // This runs in the useState initializer so the component never renders stale values + const [formValues, setFormValues] = useState>(() => { + const pending = restorePendingConfig(); + const s3 = getS3Values(); + if (pending) { + // Check if S3 has new data since submit (fingerprint changed from snapshot) + const currentFingerprint = getS3ConfigFingerprint(); + const s3Changed = pending.s3ConfigSnapshot && currentFingerprint !== pending.s3ConfigSnapshot; + + if (s3Changed) { + // Device uploaded new data since our submit — trust S3 (device is authority) + localStorage.removeItem(pendingConfigKey); + return s3; + } + + // S3 still has same data as when we submitted — show pending values + return pending.values; + } + return s3; + }); + + // When S3 data updates (polled every 60s), reconcile with any pending localStorage. + // Strategy: device is the authority. Once S3 Config changes from the snapshot taken at + // submit time, the device has uploaded new data — trust S3 regardless of values. + // Skip reset if the user is actively editing (formDirty). + useEffect(() => { + if (formDirty) return; + + const s3Values = getS3Values(); + const pending = restorePendingConfig(); + + if (pending) { + const currentFingerprint = getS3ConfigFingerprint(); + const s3Changed = pending.s3ConfigSnapshot && currentFingerprint !== pending.s3ConfigSnapshot; + if (s3Changed) { + // S3 Config changed from snapshot → device uploaded new data → trust S3 + localStorage.removeItem(pendingConfigKey); + setFormValues(s3Values); + } else { + // S3 still has same data as at submit time — keep showing pending values + setFormValues(pending.values); + } + return; + } + + // No pending config — trust S3 (source of truth) + setFormValues(s3Values); + }, [props.values]); + + const handleOperatingPriorityChange = (event) => { + setFormDirty(true); + setFormValues({ + ...formValues, + ['operatingPriority']: OperatingPriorityOptions.indexOf( + event.target.value + ) + }); + }; + +// Add time validation function — only relevant for Sinexcel BatteryPriority + const validateTimeOnly = () => { + if (device === 4 && + OperatingPriorityOptions[formValues.operatingPriority] === 'BatteryPriority' && + formValues.startTimeChargeandDischargeDayandTime && + formValues.stopTimeChargeandDischargeDayandTime) { + const startHours = formValues.startTimeChargeandDischargeDayandTime.getHours(); + const startMinutes = formValues.startTimeChargeandDischargeDayandTime.getMinutes(); + const stopHours = formValues.stopTimeChargeandDischargeDayandTime.getHours(); + const stopMinutes = formValues.stopTimeChargeandDischargeDayandTime.getMinutes(); + + const startTimeInMinutes = startHours * 60 + startMinutes; + const stopTimeInMinutes = stopHours * 60 + stopMinutes; + + if (startTimeInMinutes >= stopTimeInMinutes) { + setDateSelectionError(intl.formatMessage({ id: 'stopTimeMustBeLater' })); + setErrorDateModalOpen(true); + return false; + } + } + return true; + }; + + const handleSubmit = async (e) => { + if (!validateTimeOnly()) { + return; + } + // Re-overlay Information-tab topology at submit time, so battery counts and PV count + // are always the latest from the Information tab (it's the source of truth). + const devices = overlayTopology(formValues.devices); + const configurationToSend: Partial = { + minimumSoC: formValues.minimumSoC, + devices, + operatingPriority: formValues.operatingPriority, + timeChargeandDischargePower: formValues.timeChargeandDischargePower, + startTimeChargeandDischargeDayandTime: formValues.startTimeChargeandDischargeDayandTime + ? new Date(formValues.startTimeChargeandDischargeDayandTime.getTime() - formValues.startTimeChargeandDischargeDayandTime.getTimezoneOffset() * 60000) + : null, + stopTimeChargeandDischargeDayandTime: formValues.stopTimeChargeandDischargeDayandTime + ? new Date(formValues.stopTimeChargeandDischargeDayandTime.getTime() - formValues.stopTimeChargeandDischargeDayandTime.getTimezoneOffset() * 60000) + : null, + controlPermission:formValues.controlPermission, + dynamicPricingMode: formValues.dynamicPricingMode, + currentPrice: formValues.currentPrice, + priceToSell: formValues.priceToSell, + priceToBuy: formValues.priceToBuy, + timeToSellFrom: formValues.timeToSellFrom, + timeToSellTo: formValues.timeToSellTo, + timeToBuyFrom: formValues.timeToBuyFrom, + timeToBuyTo: formValues.timeToBuyTo, + }; + + setLoading(true); + const res = await axiosConfig + .post( + `/EditInstallationConfig?installationId=${props.id}&product=${product}`, + configurationToSend + ) + .catch((err) => { + if (err.response) { + setError(true); + setLoading(false); + } + }); + + if (res) { + setUpdated(true); + setLoading(false); + setFormDirty(false); + + // Save submitted values + S3 snapshot to localStorage for optimistic UI update. + // s3ConfigSnapshot = fingerprint of S3 Config at submit time. + // When S3 Config changes from this snapshot, the device has uploaded new data. + const cachePayload = { + values: formValues, + submittedAt: Date.now(), + s3ConfigSnapshot: getS3ConfigFingerprint(), + }; + localStorage.setItem(pendingConfigKey, JSON.stringify(cachePayload)); + } + }; + + const handleChange = (e) => { + setFormDirty(true); + const { name, value } = e.target; + + if (name === 'minimumSoC') { + const numValue = parseFloat(value); + + // invalid characters or not a number + if (/[^0-9.]/.test(value) || isNaN(numValue)) { + SetErrorForField(name, 'Invalid number format'); + } else { + const minsocRanges = { + 3: { min: 10, max: 30 }, + 4: { min: 5, max: 100 }, + }; + + const { min, max } = minsocRanges[device] || { min: 10, max: 30 }; + + if (numValue < min || numValue > max) { + SetErrorForField(name, `Value should be between ${min}-${max}%`); + } else { + // ✅ valid → clear error + SetErrorForField(name, ''); + } + } + } + + setFormValues(prev => ({ + ...prev, + [name]: value, + })); + }; + + const handleTimeChargeDischargeChange = (name: string, value: any) => { + setFormDirty(true); + setFormValues((prev) => ({ + ...prev, + [name]: value + })); + }; + + const handleOkOnErrorDateModal = () => { + setErrorDateModalOpen(false); + }; + + return ( + + + {isErrorDateModalOpen && ( + {}}> + + + {dateSelectionError} + + + + + + )} + + + + + <> +
+ { + setFormDirty(true); + setFormValues((prev) => ({ + ...prev, + controlPermission: e.target.checked, + })); + } + } + sx={{ transform: "scale(1.4)", marginLeft: "15px" }} + /> + } + sx={{ ml: 0 }} + label={ + + } + /> +
+ + {(device === 3 || device === 4) && ( + <> + + + + + + )} + +
+ {/**/} + {/* }*/} + {/* name="minimumSoC"*/} + {/* value={formValues.minimumSoC}*/} + {/* onChange={handleChange}*/} + {/* helperText={*/} + {/* errors.minimumSoC ? (*/} + {/* */} + {/* Value should be between {device === 4 ? '5–100' : '10–30'}%*/} + {/* */} + {/* ) : (*/} + {/* ''*/} + {/* )*/} + {/* }*/} + {/* fullWidth*/} + {/*/>*/} + + +
+ + {(device === 3 || device === 4) ? ( + // Per-cluster, per-inverter charging/discharging current limits — nested config. + Array.from({ length: inverterCount }, (_, invIdx) => { + const invKey = `Inverter${invIdx + 1}`; + const clusters = presetConfig?.[invIdx] ?? [0]; + return ( + + } + sx={{ + '& .MuiAccordionSummary-content': { flexGrow: 0, justifyContent: 'flex-start' }, + justifyContent: 'flex-start', + '& .MuiAccordionSummary-expandIconWrapper': { ml: 1 } + }} + > + + + + + + {clusters.map((_slotCount, clIdx) => { + const clKey = `Cluster${clIdx + 1}`; + const cluster = formValues.devices?.[invKey]?.Clusters?.[clKey]; + const charge = cluster?.MaxChargingCurrent ?? ''; + const discharge = cluster?.MaxDischargingCurrent ?? ''; + const setClusterField = ( + field: 'MaxChargingCurrent' | 'MaxDischargingCurrent', + v: string + ) => { + if (v !== '' && !/^\d*\.?\d*$/.test(v)) return; + setFormDirty(true); + setFormValues((prev) => { + const devices = { ...(prev.devices ?? {}) }; + const inv = devices[invKey] + ?? { Clusters: {}, PvCount: 0 }; + const cls = { ...(inv.Clusters ?? {}) }; + const existing = cls[clKey] ?? { + BatteryCount: presetConfig?.[invIdx]?.[clIdx] ?? 0, + MaxChargingCurrent: 0, + MaxDischargingCurrent: 0, + }; + if (v === '') { + // Drop the field while editing so the input renders empty, + // not "0". On submit, anything still missing falls back to 0. + const next: any = { ...existing }; + delete next[field]; + cls[clKey] = next; + } else { + cls[clKey] = { ...existing, [field]: parseFloat(v) }; + } + devices[invKey] = { ...inv, Clusters: cls }; + return { ...prev, devices }; + }); + }; + return ( +
+ + + + setClusterField('MaxChargingCurrent', e.target.value)} + fullWidth + /> + setClusterField('MaxDischargingCurrent', e.target.value)} + fullWidth + /> +
+ ); + })} +
+
+ ); + }) + ) : ( + <> +
+ +
+ +
+ +
+ + )} + + {(device === 3 || device === 4) && ( + <> + + + + + + )} + +
+ + + + + + +
+ + + {/* --- Sinexcel + BatteryPriority (maps to TimeChargeDischarge on device) --- */} + {device === 4 && + OperatingPriorityOptions[formValues.operatingPriority] === + 'BatteryPriority' && ( + <> + {/* Power input*/} +
+ + handleTimeChargeDischargeChange(e.target.name, e.target.value) + } + helperText={intl.formatMessage({ id: 'perInverter' })} + fullWidth + /> +
+ + {/* Start DateTime */} +
+ + + setFormValues((prev) => ({ + ...prev, + startTimeChargeandDischargeDayandTime: newValue + ? newValue.toDate() + : null, + })) + } + renderInput={(params) => ( + + )} + /> + +
+ + {/* Stop DateTime */} +
+ + + setFormValues((prev) => ({ + ...prev, + stopTimeChargeandDischargeDayandTime: newValue + ? newValue.toDate() + : null, + })) + } + renderInput={(params) => ( + + )} + /> + +
+ + )} + + {/* --- Growatt + Sinexcel under LoadPriority: Dynamic Pricing --- */} + {(device === 3 || device === 4) && + OperatingPriorityOptions[formValues.operatingPriority] === 'LoadPriority' && ( + <> + + + + + +
+ + + + + + +
+ + {formValues.dynamicPricingMode === 'Tou' && ( + <> + {(() => { + const renderTimeField = ( + labelId: string, + key: 'timeToSellFrom' | 'timeToSellTo' | 'timeToBuyFrom' | 'timeToBuyTo' + ) => { + const raw = formValues[key]; + const parsed = raw ? dayjs(raw, 'HH:mm') : null; + return ( + + { + setFormDirty(true); + setFormValues((prev) => ({ + ...prev, + [key]: newValue ? newValue.format('HH:mm') : '', + })); + }} + renderInput={(params) => ( + + )} + /> + + ); + }; + + return ( + <> + + + +
+ {renderTimeField('timeFrom', 'timeToSellFrom')} + {renderTimeField('timeTo', 'timeToSellTo')} +
+ + + + +
+ {renderTimeField('timeFrom', 'timeToBuyFrom')} + {renderTimeField('timeTo', 'timeToBuyTo')} +
+ + ); + })()} + + )} + + {formValues.dynamicPricingMode === 'SpotPrice' && ( + <> +
+ +
+ +
+ CHF/kWh, + }} + fullWidth + /> +
+ +
+ { + const v = e.target.value; + if (v === '' || /^\d*\.?\d*$/.test(v)) { + setFormDirty(true); + setFormValues((prev) => ({ ...prev, priceToSell: v })); + } + }} + InputProps={{ + endAdornment: CHF/kWh, + }} + fullWidth + /> +
+ +
+ { + const v = e.target.value; + if (v === '' || /^\d*\.?\d*$/.test(v)) { + setFormDirty(true); + setFormValues((prev) => ({ ...prev, priceToBuy: v })); + } + }} + InputProps={{ + endAdornment: CHF/kWh, + }} + fullWidth + /> +
+ + )} + + )} + +
+ + {loading && ( + + )} + + {updated && ( + + + setUpdated(false)} + sx={{ marginLeft: '4px' }} + > + + + + )} + {error && ( + + + setError(false)} + sx={{ marginLeft: '4px' }} + > + + + + )} +
+
+
+
+
+
+ ); +} + +export default SodistoreHomeConfigurationV2; diff --git a/typescript/frontend-marios2/src/lang/de.json b/typescript/frontend-marios2/src/lang/de.json index f5298cc7c..686c00705 100644 --- a/typescript/frontend-marios2/src/lang/de.json +++ b/typescript/frontend-marios2/src/lang/de.json @@ -521,8 +521,36 @@ "minimumSocPercent": "Minimaler Ladezustand (%)", "powerW": "Leistung (W)", "enterPowerValue": "Positiven oder negativen Leistungswert eingeben", + "inverterNumber": "Anzahl Wechselrichter", + "batteriesCountInInverter": "Batterieanzahl in Wechselrichter {number}", + "batteryNumberInClusterN": "Batterieanzahl in Cluster {n}", + "pvStringsNumberInInverterN": "Anzahl PV-Strings in Wechselrichter {n}", + "batteries": "Batterien", + "maximumChargingCurrentPerBattery": "Maximaler Ladestrom pro Batterie (A)", + "maximumChargingCurrentPerClusterLabel": "Maximaler Ladestrom pro Cluster (A)", + "maximumDischargingCurrentPerClusterLabel": "Maximaler Entladestrom pro Cluster (A)", + "maximumDischargingCurrentPerBattery": "Maximaler Entladestrom pro Batterie (A)", + "powerPerInverterKW": "Leistung pro Wechselrichter (kW)", "startDateTime": "Startdatum und -zeit (Startzeit < Stoppzeit)", "stopDateTime": "Stoppdatum und -zeit (Startzeit < Stoppzeit)", + "installationSetup": "Installationseinrichtung", + "batteryLimits": "Batteriegrenzwerte", + "systemSettings": "Systemeinstellungen", + "pvPerInverter": "PV pro Wechselrichter", + "pvInInverter": "PV in Wechselrichter {number}", + "dynamicPricing": "Dynamische Preisgestaltung", + "dynamicPricingMode": "Modus der dynamischen Preisgestaltung", + "dynamicPricingOff": "Aus", + "dynamicPricingSpotPrice": "Spot-Preis", + "dynamicPricingTou": "TOU", + "currentPrice": "Aktueller Preis", + "priceToSell": "Verkaufspreis", + "priceToBuy": "Kaufpreis", + "timeToSell": "Verkaufszeit", + "timeToBuy": "Kaufzeit", + "timeFrom": "Von", + "timeTo": "Bis", + "networkProviderSetOnInformationTab": "Im Informations-Tab festlegen", "tourLanguageTitle": "Sprache", "tourLanguageContent": "Wählen Sie Ihre bevorzugte Sprache. Die Oberfläche unterstützt Englisch, Deutsch, Französisch und Italienisch.", "tourExploreTitle": "Installation erkunden", diff --git a/typescript/frontend-marios2/src/lang/en.json b/typescript/frontend-marios2/src/lang/en.json index 5a0425e77..592e64583 100644 --- a/typescript/frontend-marios2/src/lang/en.json +++ b/typescript/frontend-marios2/src/lang/en.json @@ -269,8 +269,36 @@ "minimumSocPercent": "Minimum SoC (%)", "powerW": "Power (W)", "enterPowerValue": "Enter a positive or negative power value", + "inverterNumber": "Inverter Number", + "batteriesCountInInverter": "Batteries Count in Inverter {number}", + "batteryNumberInClusterN": "Battery Number in Cluster {n}", + "pvStringsNumberInInverterN": "PV Strings Number in Inverter {n}", + "batteries": "batteries", + "maximumChargingCurrentPerBattery": "Maximum Charging Current per Battery (A)", + "maximumDischargingCurrentPerBattery": "Maximum Discharging Current per Battery (A)", + "maximumChargingCurrentPerClusterLabel": "Maximum Charging Current per Cluster (A)", + "maximumDischargingCurrentPerClusterLabel": "Maximum Discharging Current per Cluster (A)", + "powerPerInverterKW": "Power per Inverter (kW)", "startDateTime": "Start Date and Time (Start Time < Stop Time)", "stopDateTime": "Stop Date and Time (Start Time < Stop Time)", + "installationSetup": "Installation Setup", + "batteryLimits": "Battery Limits", + "systemSettings": "System Settings", + "pvPerInverter": "PV per Inverter", + "pvInInverter": "PV in Inverter {number}", + "dynamicPricing": "Dynamic Pricing", + "dynamicPricingMode": "Dynamic Pricing Mode", + "dynamicPricingOff": "Off", + "dynamicPricingSpotPrice": "Spot Price", + "dynamicPricingTou": "TOU", + "currentPrice": "Current Price", + "priceToSell": "Price to Sell", + "priceToBuy": "Price to Buy", + "timeToSell": "Time to Sell", + "timeToBuy": "Time to Buy", + "timeFrom": "From", + "timeTo": "To", + "networkProviderSetOnInformationTab": "Set on Information tab", "tourLanguageTitle": "Language", "tourLanguageContent": "Choose your preferred language. The interface supports English, German, French, and Italian.", "tourExploreTitle": "Explore an Installation", diff --git a/typescript/frontend-marios2/src/lang/fr.json b/typescript/frontend-marios2/src/lang/fr.json index dd528231c..895199376 100644 --- a/typescript/frontend-marios2/src/lang/fr.json +++ b/typescript/frontend-marios2/src/lang/fr.json @@ -521,8 +521,36 @@ "minimumSocPercent": "SoC minimum (%)", "powerW": "Puissance (W)", "enterPowerValue": "Entrez une valeur de puissance positive ou négative", + "inverterNumber": "Nombre d'onduleurs", + "batteriesCountInInverter": "Nombre de batteries dans l'onduleur {number}", + "batteryNumberInClusterN": "Nombre de batteries dans le cluster {n}", + "pvStringsNumberInInverterN": "Nombre de chaînes PV dans l'onduleur {n}", + "batteries": "batteries", + "maximumChargingCurrentPerBattery": "Courant de charge maximum par batterie (A)", + "maximumChargingCurrentPerClusterLabel": "Courant de charge maximum par cluster (A)", + "maximumDischargingCurrentPerClusterLabel": "Courant de décharge maximum par cluster (A)", + "maximumDischargingCurrentPerBattery": "Courant de décharge maximum par batterie (A)", + "powerPerInverterKW": "Puissance par onduleur (kW)", "startDateTime": "Date et heure de début (Début < Fin)", "stopDateTime": "Date et heure de fin (Début < Fin)", + "installationSetup": "Configuration de l'installation", + "batteryLimits": "Limites de la batterie", + "systemSettings": "Paramètres système", + "pvPerInverter": "PV par onduleur", + "pvInInverter": "PV dans l'onduleur {number}", + "dynamicPricing": "Tarification dynamique", + "dynamicPricingMode": "Mode de tarification dynamique", + "dynamicPricingOff": "Désactivé", + "dynamicPricingSpotPrice": "Prix spot", + "dynamicPricingTou": "TOU", + "currentPrice": "Prix actuel", + "priceToSell": "Prix de vente", + "priceToBuy": "Prix d'achat", + "timeToSell": "Heure de vente", + "timeToBuy": "Heure d'achat", + "timeFrom": "De", + "timeTo": "À", + "networkProviderSetOnInformationTab": "À définir dans l'onglet Informations", "tourLanguageTitle": "Langue", "tourLanguageContent": "Choisissez votre langue préférée. L'interface est disponible en anglais, allemand, français et italien.", "tourExploreTitle": "Explorer une installation", diff --git a/typescript/frontend-marios2/src/lang/it.json b/typescript/frontend-marios2/src/lang/it.json index 202c5e47e..22fedbda9 100644 --- a/typescript/frontend-marios2/src/lang/it.json +++ b/typescript/frontend-marios2/src/lang/it.json @@ -521,8 +521,36 @@ "minimumSocPercent": "SoC minimo (%)", "powerW": "Potenza (W)", "enterPowerValue": "Inserire un valore di potenza positivo o negativo", + "inverterNumber": "Numero di inverter", + "batteriesCountInInverter": "Numero di batterie nell'inverter {number}", + "batteryNumberInClusterN": "Numero di batterie nel cluster {n}", + "pvStringsNumberInInverterN": "Numero di stringhe PV nell'inverter {n}", + "batteries": "batterie", + "maximumChargingCurrentPerBattery": "Corrente massima di carica per batteria (A)", + "maximumChargingCurrentPerClusterLabel": "Corrente massima di carica per cluster (A)", + "maximumDischargingCurrentPerClusterLabel": "Corrente massima di scarica per cluster (A)", + "maximumDischargingCurrentPerBattery": "Corrente massima di scarica per batteria (A)", + "powerPerInverterKW": "Potenza per inverter (kW)", "startDateTime": "Data e ora di inizio (Inizio < Fine)", "stopDateTime": "Data e ora di fine (Inizio < Fine)", + "installationSetup": "Configurazione dell'installazione", + "batteryLimits": "Limiti della batteria", + "systemSettings": "Impostazioni di sistema", + "pvPerInverter": "PV per inverter", + "pvInInverter": "PV nell'inverter {number}", + "dynamicPricing": "Prezzi dinamici", + "dynamicPricingMode": "Modalità prezzi dinamici", + "dynamicPricingOff": "Off", + "dynamicPricingSpotPrice": "Prezzo spot", + "dynamicPricingTou": "TOU", + "currentPrice": "Prezzo attuale", + "priceToSell": "Prezzo di vendita", + "priceToBuy": "Prezzo di acquisto", + "timeToSell": "Orario di vendita", + "timeToBuy": "Orario di acquisto", + "timeFrom": "Da", + "timeTo": "A", + "networkProviderSetOnInformationTab": "Imposta nella scheda Informazioni", "tourLanguageTitle": "Lingua", "tourLanguageContent": "Scegli la tua lingua preferita. L'interfaccia supporta inglese, tedesco, francese e italiano.", "tourExploreTitle": "Esplora un'installazione",