myio-js-library 0.1.76 → 0.1.78

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
@@ -19,6 +19,7 @@ Distributed as **ESM**, **CJS**, and **UMD** (with a pre-minified build for CDN
19
19
  - 📊 **CSV export** — data export to CSV format with proper escaping (returns strings, no DOM manipulation).
20
20
  - 🏷️ **Classification** — energy entity classification utilities.
21
21
  - 🔍 **Data access** — nested object value retrieval with datakey paths.
22
+ - 🔌 **Device status** — comprehensive status calculation and management with `calculateDeviceStatus`.
22
23
  - ⚡ **Dual module support** — ESM and CJS.
23
24
  - 🌍 **Browser-ready** — UMD global + CDN link.
24
25
 
@@ -38,21 +39,23 @@ pnpm add myio-js-library
38
39
 
39
40
  ### Node.js (ESM)
40
41
  ```javascript
41
- import {
42
- decodePayload,
42
+ import {
43
+ decodePayload,
43
44
  decodePayloadBase64Xor,
44
- fetchWithRetry,
45
+ fetchWithRetry,
45
46
  http,
46
- addNamespace,
47
+ addNamespace,
47
48
  detectDeviceType,
48
- strings,
49
+ strings,
49
50
  numbers,
50
51
  formatEnergy,
51
52
  formatWaterVolumeM3,
52
53
  formatDateToYMD,
53
54
  exportToCSV,
54
55
  classifyWaterLabel,
55
- getValueByDatakey
56
+ getValueByDatakey,
57
+ calculateDeviceStatus,
58
+ DeviceStatusType
56
59
  } from 'myio-js-library';
57
60
 
58
61
  // Decode with string key
@@ -67,12 +70,22 @@ const response = await fetchWithRetry('https://api.example.com/data', {
67
70
 
68
71
  // Add namespace to object keys
69
72
  const data = { temperature: 25, humidity: 60 };
70
- const namespaced = addNamespace(data, 'sensor1');
73
+ const namespaced = addNamespace(data, 'sensor1');
71
74
  // { "temperature (sensor1)": 25, "humidity (sensor1)": 60 }
72
75
 
73
76
  // Detect device type from context
74
77
  const deviceType = detectDeviceType('building', 'floor2-room101');
75
78
  console.log(deviceType); // "room" or "unknown"
79
+
80
+ // Calculate device status
81
+ const status = calculateDeviceStatus({
82
+ connectionStatus: "online",
83
+ lastConsumptionValue: 500,
84
+ limitOfPowerOnStandByWatts: 100,
85
+ limitOfPowerOnAlertWatts: 1000,
86
+ limitOfPowerOnFailureWatts: 2000
87
+ });
88
+ console.log(status); // "power_on"
76
89
  ```
77
90
 
78
91
  ### Node.js (CJS)
@@ -772,6 +785,219 @@ isWaterCategory('Caixa Superior', "Caixas D'Água"); // true
772
785
  isWaterCategory('Loja 101', "Caixas D'Água"); // false
773
786
  ```
774
787
 
788
+ ### Device Status Utilities
789
+
790
+ The library provides comprehensive device status management utilities for IoT applications, enabling centralized status calculation, validation, and visual mapping.
791
+
792
+ #### `calculateDeviceStatus(params: object): string`
793
+
794
+ Calculates device operational status based on connection state and power consumption metrics. This is the primary function for determining device status across MYIO applications.
795
+
796
+ **Parameters:**
797
+ - `connectionStatus: "waiting" | "offline" | "online"` - Device connection state (required)
798
+ - `lastConsumptionValue: number | null` - Power consumption in watts (required)
799
+ - `limitOfPowerOnStandByWatts: number` - Upper threshold for standby mode in watts (required)
800
+ - `limitOfPowerOnAlertWatts: number` - Upper threshold for normal operation (power_on) in watts (required)
801
+ - `limitOfPowerOnFailureWatts: number` - Upper threshold for warning mode in watts (required)
802
+
803
+ **Returns:** Device status string from `DeviceStatusType` enum:
804
+ - `"not_installed"` - Device waiting for installation
805
+ - `"no_info"` - Device offline, no information available
806
+ - `"power_on"` - Device in normal operation
807
+ - `"standby"` - Low power consumption mode (0 to standby limit)
808
+ - `"warning"` - Elevated consumption (alert limit to failure limit)
809
+ - `"failure"` - Critical consumption (above failure limit)
810
+ - `"maintenance"` - Invalid state requiring attention
811
+
812
+ **Decision Logic:**
813
+ ```
814
+ waiting → NOT_INSTALLED
815
+ offline → NO_INFO
816
+ online + no data → POWER_ON
817
+ online + (0 ≤ consumption ≤ standbyLimit) → STANDBY
818
+ online + (standbyLimit < consumption ≤ alertLimit) → POWER_ON
819
+ online + (alertLimit < consumption ≤ failureLimit) → WARNING
820
+ online + (consumption > failureLimit) → FAILURE
821
+ invalid → MAINTENANCE
822
+ ```
823
+
824
+ **Usage Example:**
825
+ ```javascript
826
+ import { calculateDeviceStatus, DeviceStatusType } from 'myio-js-library';
827
+
828
+ // Air Conditioner - Normal operation
829
+ const acStatus = calculateDeviceStatus({
830
+ connectionStatus: "online",
831
+ lastConsumptionValue: 2500, // 2.5 kW
832
+ limitOfPowerOnStandByWatts: 500, // 500W standby
833
+ limitOfPowerOnAlertWatts: 3000, // 3kW alert
834
+ limitOfPowerOnFailureWatts: 5000 // 5kW failure
835
+ });
836
+ console.log(acStatus); // "power_on"
837
+
838
+ // Elevator - Standby
839
+ const elevatorStatus = calculateDeviceStatus({
840
+ connectionStatus: "online",
841
+ lastConsumptionValue: 80, // 80W
842
+ limitOfPowerOnStandByWatts: 150,
843
+ limitOfPowerOnAlertWatts: 800,
844
+ limitOfPowerOnFailureWatts: 1200
845
+ });
846
+ console.log(elevatorStatus); // "standby"
847
+
848
+ // Offline device
849
+ const offlineStatus = calculateDeviceStatus({
850
+ connectionStatus: "offline",
851
+ lastConsumptionValue: null,
852
+ limitOfPowerOnStandByWatts: 100,
853
+ limitOfPowerOnAlertWatts: 1000,
854
+ limitOfPowerOnFailureWatts: 2000
855
+ });
856
+ console.log(offlineStatus); // "no_info"
857
+ ```
858
+
859
+ **Interactive Demo:**
860
+ See [demos/calculate-device-status.html](demos/calculate-device-status.html) for an interactive demonstration with multiple scenarios.
861
+
862
+ **Complete Documentation:**
863
+ See [docs/calculateDeviceStatus.md](docs/calculateDeviceStatus.md) for comprehensive documentation with advanced examples.
864
+
865
+ #### Device Status Constants and Helper Functions
866
+
867
+ ##### `DeviceStatusType` - Status Type Enum
868
+
869
+ ```javascript
870
+ import { DeviceStatusType } from 'myio-js-library';
871
+
872
+ DeviceStatusType.POWER_ON // "power_on"
873
+ DeviceStatusType.STANDBY // "standby"
874
+ DeviceStatusType.POWER_OFF // "power_off"
875
+ DeviceStatusType.WARNING // "warning"
876
+ DeviceStatusType.FAILURE // "failure"
877
+ DeviceStatusType.MAINTENANCE // "maintenance"
878
+ DeviceStatusType.NO_INFO // "no_info"
879
+ DeviceStatusType.NOT_INSTALLED // "not_installed"
880
+ ```
881
+
882
+ ##### `deviceStatusIcons` - Icon Mapping
883
+
884
+ ```javascript
885
+ import { deviceStatusIcons, getDeviceStatusIcon } from 'myio-js-library';
886
+
887
+ // Icon map
888
+ deviceStatusIcons[DeviceStatusType.POWER_ON] // "⚡"
889
+ deviceStatusIcons[DeviceStatusType.STANDBY] // "🔌"
890
+ deviceStatusIcons[DeviceStatusType.WARNING] // "⚠️"
891
+ deviceStatusIcons[DeviceStatusType.FAILURE] // "🚨"
892
+ deviceStatusIcons[DeviceStatusType.NO_INFO] // "❓️"
893
+ deviceStatusIcons[DeviceStatusType.NOT_INSTALLED] // "📦"
894
+
895
+ // Helper function
896
+ getDeviceStatusIcon("warning"); // "⚠️"
897
+ getDeviceStatusIcon("failure"); // "🚨"
898
+ ```
899
+
900
+ ##### `getDeviceStatusInfo(deviceStatus: string): object`
901
+
902
+ Gets comprehensive status information including derived properties.
903
+
904
+ ```javascript
905
+ import { getDeviceStatusInfo } from 'myio-js-library';
906
+
907
+ const info = getDeviceStatusInfo("warning");
908
+ console.log(info);
909
+ // {
910
+ // deviceStatus: "warning",
911
+ // connectionStatus: "connected",
912
+ // cardStatus: "alert",
913
+ // deviceIcon: "⚠️",
914
+ // connectionIcon: "🟢",
915
+ // shouldFlash: true,
916
+ // isOffline: false,
917
+ // isValid: true
918
+ // }
919
+ ```
920
+
921
+ ##### Status Mapping Functions
922
+
923
+ ```javascript
924
+ import {
925
+ mapDeviceStatusToCardStatus,
926
+ mapDeviceToConnectionStatus,
927
+ shouldFlashIcon,
928
+ isDeviceOffline
929
+ } from 'myio-js-library';
930
+
931
+ // Map to simplified card status
932
+ mapDeviceStatusToCardStatus("warning"); // "alert"
933
+ mapDeviceStatusToCardStatus("power_on"); // "ok"
934
+ mapDeviceStatusToCardStatus("failure"); // "fail"
935
+
936
+ // Map to connection status
937
+ mapDeviceToConnectionStatus("no_info"); // "offline"
938
+ mapDeviceToConnectionStatus("power_on"); // "connected"
939
+
940
+ // Visual indicators
941
+ shouldFlashIcon("failure"); // true (requires attention)
942
+ shouldFlashIcon("warning"); // true (requires attention)
943
+ shouldFlashIcon("power_on"); // false (normal operation)
944
+
945
+ isDeviceOffline("no_info"); // true
946
+ isDeviceOffline("standby"); // false
947
+ ```
948
+
949
+ ##### Validation Functions
950
+
951
+ ```javascript
952
+ import {
953
+ isValidDeviceStatus,
954
+ isValidConnectionStatus
955
+ } from 'myio-js-library';
956
+
957
+ isValidDeviceStatus("warning"); // true
958
+ isValidDeviceStatus("invalid"); // false
959
+
960
+ isValidConnectionStatus("connected"); // true
961
+ isValidConnectionStatus("unknown"); // false
962
+ ```
963
+
964
+ **Complete Integration Example:**
965
+ ```javascript
966
+ import {
967
+ calculateDeviceStatus,
968
+ getDeviceStatusInfo,
969
+ DeviceStatusType
970
+ } from 'myio-js-library';
971
+
972
+ // Calculate status from device data
973
+ const deviceStatus = calculateDeviceStatus({
974
+ connectionStatus: device.isOnline ? "online" : "offline",
975
+ lastConsumptionValue: device.powerWatts,
976
+ limitOfPowerOnStandByWatts: 100,
977
+ limitOfPowerOnAlertWatts: 1000,
978
+ limitOfPowerOnFailureWatts: 2000
979
+ });
980
+
981
+ // Get complete status information
982
+ const statusInfo = getDeviceStatusInfo(deviceStatus);
983
+
984
+ // Update UI
985
+ if (statusInfo.shouldFlash) {
986
+ element.classList.add('flash-animation');
987
+ }
988
+
989
+ element.innerHTML = `
990
+ <span class="status-icon">${statusInfo.deviceIcon}</span>
991
+ <span class="status-text">${statusInfo.deviceStatus}</span>
992
+ <span class="connection-icon">${statusInfo.connectionIcon}</span>
993
+ `;
994
+
995
+ // Handle critical states
996
+ if (deviceStatus === DeviceStatusType.FAILURE) {
997
+ sendAlert(`Device ${device.id} in critical state!`);
998
+ }
999
+ ```
1000
+
775
1001
  ### Data Access Utilities
776
1002
 
777
1003
  #### `getValueByDatakey(data: any, datakey: string): any`