myio-js-library 0.1.24 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1861,6 +1861,174 @@ card.destroy();
1861
1861
 
1862
1862
  For complete technical documentation and implementation details, see: [RFC-0007-renderCardCompenteHeadOffice](src/docs/rfcs/RFC-0007-renderCardCompenteHeadOffice.md)
1863
1863
 
1864
+ ### Demand Modal Component
1865
+
1866
+ #### `openDemandModal(params: DemandModalParams): Promise<DemandModalInstance>`
1867
+
1868
+ Opens a fully-styled demand/consumption modal with interactive Chart.js visualization, zoom/pan controls, PDF export, and ThingsBoard telemetry integration. This component implements RFC 0015 specifications with comprehensive accessibility and internationalization support.
1869
+
1870
+ **Parameters:**
1871
+ - `params: DemandModalParams` - Configuration object:
1872
+ - `token: string` - JWT token for ThingsBoard authentication (required)
1873
+ - `deviceId: string` - ThingsBoard device UUID (required)
1874
+ - `startDate: string` - ISO date string "YYYY-MM-DD" (required)
1875
+ - `endDate: string` - ISO date string "YYYY-MM-DD" (required)
1876
+ - `label?: string` - Device/store label (default: "Dispositivo")
1877
+ - `container?: HTMLElement | string` - Mount container (default: document.body)
1878
+ - `onClose?: () => void` - Callback when modal closes
1879
+ - `locale?: 'pt-BR' | 'en-US' | string` - Locale for formatting (default: 'pt-BR')
1880
+ - `pdf?: DemandModalPdfConfig` - PDF export configuration
1881
+ - `styles?: Partial<DemandModalStyles>` - Style customization tokens
1882
+
1883
+ **Returns:** Promise resolving to `DemandModalInstance` object with:
1884
+ - `destroy(): void` - Clean up modal and resources
1885
+
1886
+ **Key Features:**
1887
+ - **Interactive Chart.js Visualization**: Smooth line chart with purple stroke and light fill
1888
+ - **Zoom/Pan Controls**: Mouse wheel zoom, drag selection, Ctrl+pan, Reset Zoom button
1889
+ - **PDF Export**: A4 portrait report with chart image, metadata, and data table
1890
+ - **Peak Demand Highlighting**: Yellow pill showing maximum demand value and timestamp
1891
+ - **Fullscreen Mode**: Toggle to expand modal to full viewport with chart resize
1892
+ - **ThingsBoard Integration**: Fetches telemetry data using consumption endpoint
1893
+ - **Internationalization**: Portuguese/English localization with proper date/number formatting
1894
+ - **Accessibility**: Focus trap, ARIA labels, keyboard navigation (ESC to close)
1895
+ - **Responsive Design**: Mobile-friendly with touch-optimized controls
1896
+ - **Dynamic Library Loading**: Chart.js, zoom plugin, and jsPDF loaded on-demand
1897
+ - **Customizable Styling**: CSS variables and style tokens for theming
1898
+
1899
+ **Usage Example:**
1900
+ ```javascript
1901
+ import { openDemandModal } from 'myio-js-library';
1902
+
1903
+ // Basic usage
1904
+ const modal = await openDemandModal({
1905
+ token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
1906
+ deviceId: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
1907
+ startDate: '2024-01-01',
1908
+ endDate: '2024-01-31',
1909
+ label: 'Loja Centro'
1910
+ });
1911
+
1912
+ // Advanced usage with customization
1913
+ const modal = await openDemandModal({
1914
+ token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
1915
+ deviceId: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
1916
+ startDate: '2024-01-01',
1917
+ endDate: '2024-01-31',
1918
+ label: 'Shopping Center Norte',
1919
+ locale: 'en-US',
1920
+ styles: {
1921
+ primaryColor: '#1976D2', // Blue theme
1922
+ accentColor: '#FF9800', // Orange highlights
1923
+ borderRadius: '12px' // Rounded corners
1924
+ },
1925
+ pdf: {
1926
+ enabled: true,
1927
+ fileName: 'demand-report-jan2024.pdf'
1928
+ },
1929
+ onClose: () => {
1930
+ console.log('Modal closed');
1931
+ }
1932
+ });
1933
+
1934
+ // Clean up when needed
1935
+ modal.destroy();
1936
+ ```
1937
+
1938
+ **UMD Usage (ThingsBoard widgets):**
1939
+ ```html
1940
+ <script src="https://unpkg.com/myio-js-library@latest/dist/myio-js-library.umd.min.js"></script>
1941
+ <script>
1942
+ const { openDemandModal } = MyIOLibrary;
1943
+
1944
+ // In your widget action handler
1945
+ async function openDemandChart() {
1946
+ try {
1947
+ const modal = await openDemandModal({
1948
+ token: ctx.defaultSubscription.subscriptionContext.user.token,
1949
+ deviceId: entityId.id,
1950
+ startDate: '2024-01-01',
1951
+ endDate: '2024-01-31',
1952
+ label: entityLabel,
1953
+ onClose: () => {
1954
+ console.log('Demand modal closed');
1955
+ }
1956
+ });
1957
+ } catch (error) {
1958
+ console.error('Failed to open demand modal:', error);
1959
+ }
1960
+ }
1961
+ </script>
1962
+ ```
1963
+
1964
+ **ThingsBoard API Integration:**
1965
+ The component fetches telemetry data from ThingsBoard REST API:
1966
+ ```
1967
+ GET /api/plugins/telemetry/DEVICE/{deviceId}/values/timeseries
1968
+ ?keys=consumption
1969
+ &startTs={startMillis}
1970
+ &endTs={endMillis}
1971
+ &limit=50000
1972
+ &intervalType=MILLISECONDS
1973
+ &interval=54000000
1974
+ &agg=SUM
1975
+ &orderBy=ASC
1976
+
1977
+ Headers:
1978
+ X-Authorization: Bearer {token}
1979
+ ```
1980
+
1981
+ **Data Processing:**
1982
+ - Converts cumulative consumption to demand (kW) using time deltas
1983
+ - Filters out negative values (meter resets)
1984
+ - Converts Wh to kWh when necessary
1985
+ - Computes peak demand value and timestamp
1986
+ - Sorts data chronologically for chart display
1987
+
1988
+ **Styling Customization:**
1989
+ ```javascript
1990
+ const customStyles = {
1991
+ // Color tokens
1992
+ primaryColor: '#4A148C', // Header and chart line color
1993
+ accentColor: '#FFC107', // Peak demand pill color
1994
+ dangerColor: '#f44336', // Error state color
1995
+ backgroundColor: '#ffffff', // Modal background
1996
+ overlayColor: 'rgba(0, 0, 0, 0.5)', // Backdrop color
1997
+
1998
+ // Layout tokens
1999
+ borderRadius: '8px', // Card border radius
2000
+ buttonRadius: '6px', // Button border radius
2001
+ pillRadius: '20px', // Peak pill border radius
2002
+ zIndex: 10000, // Modal z-index
2003
+
2004
+ // Typography tokens
2005
+ fontFamily: 'Roboto, Arial, sans-serif',
2006
+ fontSizeMd: '16px',
2007
+ fontWeightBold: '600'
2008
+ };
2009
+
2010
+ const modal = await openDemandModal({
2011
+ // ... other params
2012
+ styles: customStyles
2013
+ });
2014
+ ```
2015
+
2016
+ **Error Handling:**
2017
+ - Network failures show user-friendly error messages
2018
+ - Invalid tokens display authentication errors
2019
+ - Empty datasets show "no data" message
2020
+ - Library loading failures gracefully degrade
2021
+ - All errors are caught and displayed in the UI
2022
+
2023
+ **Performance Considerations:**
2024
+ - External libraries loaded dynamically (Chart.js, jsPDF)
2025
+ - Chart rendering optimized for large datasets
2026
+ - Memory cleanup on modal destroy
2027
+ - Debounced resize handling
2028
+ - Efficient data processing pipeline
2029
+
2030
+ For complete technical specifications, see: [RFC-0015-MyIO-DemandModal-Component](src/docs/rfcs/RFC-0015-MyIO-DemandModal-Component.md)
2031
+
1864
2032
  ## 🧪 Development
1865
2033
 
1866
2034
  ```bash