EnergiWatch-MVP is organized into modular JavaScript files, each with a clear responsibility. Below is a summary of each major module and how they interact:
- Role: Entry point; initializes the app, sets up event listeners, and orchestrates module interactions.
- Imports:
- UI utilities (
ui.js) - State management (
state.js) - Event handlers (
eventHandlers.js) - Chart utilities (
chartUtils.js)
- UI utilities (
- Key Functions:
init(): Initializes the UI, state, event handlers, and chart on DOM load.- Handles robust initialization of toggles and triggers initial data loading.
- Role: Handles general DOM manipulation and UI rendering (excluding chart display and recommendations).
- Exports:
renderTariffEditor,renderApplianceList,toggleDarkMode,loadDarkModePreference,initUsageModeToggle,updateBillOverview
- Interactions:
- Called by
main.jsandeventHandlers.jsto update the UI based on state changes or user input.
- Called by
- Role: Core business logic for energy calculations and bill estimation.
- Exports:
calculateCurrentEstimatedBill,calculateEnergyUsage,calculateMonthlyCost,calculateWeeklyEnergyUsage,calculateMonthlyEnergyUsage,generateOptimisationAdvice
- Interactions:
- Used by
eventHandlers.jsto process user data and trigger calculations. - Enforces the fixed tariff rate (45.62 sen/kWh) in all calculations.
- Used by
- Role: Integrates with Gemini AI for demo data and optimization advice.
- Exports:
generateDemoData,generateLocalDemoData,generateOptimisationRecommendations
- Interactions:
- Called by
eventHandlers.jsandlogic.jsfor AI-powered features. - Fallbacks to local data if API fails.
- Called by
- Role: Centralizes all event listeners and handlers for user actions.
- Exports:
handleTariffSave,handleContinuousToggle,handleEssentialToggle,handleAIDemoData,handleAIRecommendations,handleUsageModeToggle,handleBudgetChange
- Interactions:
- Connects UI events to logic/state updates and UI re-renders.
- Calls
logic.jsfor calculations. - Calls
aiService.jsto fetch AI-powered data or advice. - Calls
recommendations.jsto display advice. - Calls
chartUtils.jsto update chart views or data. - Calls
ui.jsfor other general UI updates. - Interacts with
state.jsandstorage.jsfor data management.
- Role: Manage application state in memory and persist to local storage.
- Exports:
getAppliances,setAppliances,getSolarData,setSolarData, etc. (representative examples)
- Interactions:
- Used by most modules to get/set persistent data.
- Role: Manages the display of AI-generated optimization advice and other recommendations in the UI. Handles loading and error states for the recommendations section. Uses
marked.jsfor rendering Markdown content. - Exports:
displayRecommendations,showRecommendationsLoading,showRecommendationsError
- Interactions:
- Called by
eventHandlers.js(e.g., afterhandleAIRecommendationssuccessfully fetches advice) to render the recommendations. - Relies on
marked.js(loaded via CDN) for Markdown to HTML conversion.
- Called by
- Role: Handles all aspects of the energy usage and solar generation chart, including its creation, data updates, and view switching (daily, weekly, monthly).
- Exports:
energyChart(the Chart.js instance)createEnergyChartupdateChartViewupdateEnergyChart
- Interactions:
createEnergyChartis typically called bymain.jsduring application initialization.updateChartViewis called byeventHandlers.js(e.g., when the user clicks day/week/month view buttons).updateEnergyChartis called when new data is available, likely triggered viaeventHandlers.jsor after state updates.- Imports
getUsageData,getSolarDatafromstate.jsto populate the chart.
- Role: Utility modules for static data (e.g., default appliance list) and logging errors/debug info.
main.js ├── ui.js ├── state.js ├── chartUtils.js // For chart creation │ └── state.js // Chart utils gets data from state └── eventHandlers.js ├── logic.js │ └── aiService.js ├── ui.js ├── recommendations.js // For displaying recommendations ├── chartUtils.js // For chart updates/view changes ├── state.js └── storage.js
// data.js and logger.js are utility modules, typically imported where needed // but not central to this high-level interaction flow.
For more details on each module, see their respective code files.