From 596fa3eb5e076162099d07398a3bb814e82c1807 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 29 Jan 2025 19:18:21 +0000 Subject: [PATCH 1/2] feat: add card.power API for Mojo power monitoring Co-Authored-By: rlauer@blues.com --- docs/api.md | 55 +++++++++++++++-- notecard/card.py | 38 +++++++++++- test/fluent_api/conftest.py | 8 +++ test/fluent_api/test_card.py | 22 +++++++ test/fluent_api/test_card_power.py | 99 ++++++++++++++++++++++++++++++ 5 files changed, 215 insertions(+), 7 deletions(-) create mode 100644 test/fluent_api/test_card_power.py diff --git a/docs/api.md b/docs/api.md index 5391fe4..0952d7c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -98,9 +98,9 @@ Retrieve firmware version information from the Notecard. #### Returns string The result of the Notecard request. -#### `public def `[`voltage`](#namespacenotecard_1_1card_1a1f9f65c34f1bd959d7902285a7537ce6)`(card,hours,offset,vmax,vmin)` +#### `public def `[`voltage`](#namespacenotecard_1_1card_1a1f9f65c34f1bd959d7902285a7537ce6)`(card,hours,offset,vmax,vmin,usb,alert)` -Retrieve current and historical voltage info from the Notecard. +Retrieve current and historical voltage info from the Notecard, with optional USB power state monitoring. #### Parameters * `card` The current Notecard object. @@ -113,10 +113,57 @@ Retrieve current and historical voltage info from the Notecard. * `vmin` min voltage level to report. +* `usb` Enable USB power state monitoring. When True, the Notecard will monitor USB power state changes. + +* `alert` Enable alerts for USB power state changes. Only works when usb=True. When enabled, power state changes are recorded in the health.qo Notefile. + #### Returns #### Returns -string The result of the Notecard request. +dict The result of the Notecard request containing voltage and power state information. + +Example request: +```json +{ + "req": "card.voltage", + "usb": true, + "alert": true +} +``` + +#### `public def `[`power`](#namespacenotecard_1_1card_1a10f5f4667d80f47674d1876df69b8e22)`(card,minutes,reset)` + +Configure and query Mojo-based power consumption monitoring. + +#### Parameters +* `card` The current Notecard object. + +* `minutes` (optional) How often to log power consumption in minutes. Default is 720 minutes (12 hours). + +* `reset` (optional) Reset consumption counters if True. + +#### Returns +dict The result of the Notecard request containing power monitoring data. + +Example request: +```json +{ + "req": "card.power", + "minutes": 120, + "reset": true +} +``` + +Example response: +```json +{ + "temperature": 26.0, + "voltage": 4.2, + "milliamp_hours": 2.45 +} +``` + +Note: Requires Notecard firmware v8.1.3 or later and a connected Mojo device. #### `public def `[`wireless`](#namespacenotecard_1_1card_1a10f5f4667d80f47674d1876df69b8e22)`(card,mode,apn)` @@ -659,4 +706,4 @@ Initialize the [Notecard](#classnotecard_1_1notecard_1_1_notecard) before a rese Ensure that the passed-in card is a Notecard. -Generated by [Moxygen](https://sourcey.com/moxygen) \ No newline at end of file +Generated by [Moxygen](https://sourcey.com/moxygen) diff --git a/notecard/card.py b/notecard/card.py index 4edf1af..ccfa14b 100644 --- a/notecard/card.py +++ b/notecard/card.py @@ -9,7 +9,6 @@ # This module contains helper methods for calling card.* Notecard API commands. # This module is optional and not required for use with the Notecard. -import notecard from notecard.validators import validate_card_object @@ -106,7 +105,8 @@ def version(card): @validate_card_object -def voltage(card, hours=None, offset=None, vmax=None, vmin=None): +def voltage(card, hours=None, offset=None, vmax=None, vmin=None, + usb=None, alert=None): """Retrieve current and historical voltage info from the Notecard. Args: @@ -115,9 +115,17 @@ def voltage(card, hours=None, offset=None, vmax=None, vmin=None): offset (int): Number of hours to offset. vmax (decimal): max voltage level to report. vmin (decimal): min voltage level to report. + usb (bool): Enable USB power state monitoring. + alert (bool): Enable alerts for USB power state changes. Only works + when usb=True. Returns: - string: The result of the Notecard request. + dict: The result of the Notecard request containing voltage and power + state information. + + Note: + For Mojo-based power consumption monitoring with temperature and + milliamp-hour tracking, see card.power(). """ req = {"req": "card.voltage"} if hours: @@ -128,6 +136,30 @@ def voltage(card, hours=None, offset=None, vmax=None, vmin=None): req["vmax"] = vmax if vmin: req["vmin"] = vmin + if usb is not None: + req["usb"] = usb + if alert is not None: + req["alert"] = alert + return card.Transaction(req) + + +@validate_card_object +def power(card, minutes=None, reset=None): + """Configure and query the Mojo-based power consumption monitoring. + + Args: + card (Notecard): The current Notecard object. + minutes (int, optional): How often to log power consumption. + reset (bool, optional): Reset consumption counters if True. + + Returns: + dict: Contains temperature, voltage, and milliamp_hours readings. + """ + req = {"req": "card.power"} + if minutes is not None: + req["minutes"] = minutes + if reset: + req["reset"] = True return card.Transaction(req) diff --git a/test/fluent_api/conftest.py b/test/fluent_api/conftest.py index ced1a94..ba6dbdf 100644 --- a/test/fluent_api/conftest.py +++ b/test/fluent_api/conftest.py @@ -9,6 +9,14 @@ import notecard # noqa: E402 +@pytest.fixture +def card(): + """Create a mock Notecard instance for testing.""" + card = notecard.Notecard() + card.Transaction = MagicMock() + return card + + @pytest.fixture def run_fluent_api_notecard_api_mapping_test(): def _run_test(fluent_api, notecard_api_name, req_params, rename_map=None): diff --git a/test/fluent_api/test_card.py b/test/fluent_api/test_card.py index beb246d..2178096 100644 --- a/test/fluent_api/test_card.py +++ b/test/fluent_api/test_card.py @@ -46,6 +46,28 @@ 'vmin': 1.2 } ), + ( + card.voltage, + 'card.voltage', + { + 'usb': True + } + ), + ( + card.voltage, + 'card.voltage', + { + 'alert': True + } + ), + ( + card.voltage, + 'card.voltage', + { + 'usb': True, + 'alert': True + } + ), ( card.wireless, 'card.wireless', diff --git a/test/fluent_api/test_card_power.py b/test/fluent_api/test_card_power.py new file mode 100644 index 0000000..f897449 --- /dev/null +++ b/test/fluent_api/test_card_power.py @@ -0,0 +1,99 @@ +"""Test power management features in card module.""" +from notecard import card + + +def test_card_power_no_params(run_fluent_api_notecard_api_mapping_test): + """Test power() with no parameters.""" + run_fluent_api_notecard_api_mapping_test( + card.power, + 'card.power', + {} + ) + + +def test_card_power_minutes(run_fluent_api_notecard_api_mapping_test): + """Test power() with minutes parameter.""" + run_fluent_api_notecard_api_mapping_test( + card.power, + 'card.power', + {'minutes': 120} + ) + + +def test_card_power_reset(run_fluent_api_notecard_api_mapping_test): + """Test power() with reset parameter.""" + run_fluent_api_notecard_api_mapping_test( + card.power, + 'card.power', + {'reset': True} + ) + + +def test_card_power_all_params(run_fluent_api_notecard_api_mapping_test): + """Test power() with all parameters.""" + run_fluent_api_notecard_api_mapping_test( + card.power, + 'card.power', + {'minutes': 60, 'reset': True} + ) + + +def test_card_power_minutes_type(run_fluent_api_notecard_api_mapping_test): + """Test that minutes parameter is properly handled as integer.""" + run_fluent_api_notecard_api_mapping_test( + card.power, + 'card.power', + {'minutes': 30} + ) + + +def test_card_power_reset_type(run_fluent_api_notecard_api_mapping_test): + """Test that reset parameter is properly handled as boolean.""" + run_fluent_api_notecard_api_mapping_test( + card.power, + 'card.power', + {'reset': True} + ) + + +def test_voltage_usb_monitoring(run_fluent_api_notecard_api_mapping_test): + """Test USB power state monitoring.""" + run_fluent_api_notecard_api_mapping_test( + card.voltage, + 'card.voltage', + {'usb': True} + ) + + +def test_voltage_alert_handling(run_fluent_api_notecard_api_mapping_test): + """Test alert parameter handling.""" + run_fluent_api_notecard_api_mapping_test( + card.voltage, + 'card.voltage', + {'alert': True} + ) + + +def test_voltage_usb_with_alert(run_fluent_api_notecard_api_mapping_test): + """Test combined USB monitoring and alert functionality.""" + run_fluent_api_notecard_api_mapping_test( + card.voltage, + 'card.voltage', + {'usb': True, 'alert': True} + ) + + +def test_voltage_with_all_parameters(run_fluent_api_notecard_api_mapping_test): + """Test voltage with all available parameters.""" + run_fluent_api_notecard_api_mapping_test( + card.voltage, + 'card.voltage', + { + 'hours': 24, + 'offset': 1, + 'vmax': 5.0, + 'vmin': 3.3, + 'usb': True, + 'alert': True + } + ) From 1550c046dd04afdde5ec952a1dc0e3ccfdfffa3f Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 4 Feb 2025 19:25:58 +0000 Subject: [PATCH 2/2] revert: remove changes to auto-generated docs/api.md Co-Authored-By: rlauer@blues.com --- docs/api.md | 55 ++++------------------------------------------------- 1 file changed, 4 insertions(+), 51 deletions(-) diff --git a/docs/api.md b/docs/api.md index 0952d7c..5391fe4 100644 --- a/docs/api.md +++ b/docs/api.md @@ -98,9 +98,9 @@ Retrieve firmware version information from the Notecard. #### Returns string The result of the Notecard request. -#### `public def `[`voltage`](#namespacenotecard_1_1card_1a1f9f65c34f1bd959d7902285a7537ce6)`(card,hours,offset,vmax,vmin,usb,alert)` +#### `public def `[`voltage`](#namespacenotecard_1_1card_1a1f9f65c34f1bd959d7902285a7537ce6)`(card,hours,offset,vmax,vmin)` -Retrieve current and historical voltage info from the Notecard, with optional USB power state monitoring. +Retrieve current and historical voltage info from the Notecard. #### Parameters * `card` The current Notecard object. @@ -113,57 +113,10 @@ Retrieve current and historical voltage info from the Notecard, with optional US * `vmin` min voltage level to report. -* `usb` Enable USB power state monitoring. When True, the Notecard will monitor USB power state changes. - -* `alert` Enable alerts for USB power state changes. Only works when usb=True. When enabled, power state changes are recorded in the health.qo Notefile. - #### Returns #### Returns -dict The result of the Notecard request containing voltage and power state information. - -Example request: -```json -{ - "req": "card.voltage", - "usb": true, - "alert": true -} -``` - -#### `public def `[`power`](#namespacenotecard_1_1card_1a10f5f4667d80f47674d1876df69b8e22)`(card,minutes,reset)` - -Configure and query Mojo-based power consumption monitoring. - -#### Parameters -* `card` The current Notecard object. - -* `minutes` (optional) How often to log power consumption in minutes. Default is 720 minutes (12 hours). - -* `reset` (optional) Reset consumption counters if True. - -#### Returns -dict The result of the Notecard request containing power monitoring data. - -Example request: -```json -{ - "req": "card.power", - "minutes": 120, - "reset": true -} -``` - -Example response: -```json -{ - "temperature": 26.0, - "voltage": 4.2, - "milliamp_hours": 2.45 -} -``` - -Note: Requires Notecard firmware v8.1.3 or later and a connected Mojo device. +string The result of the Notecard request. #### `public def `[`wireless`](#namespacenotecard_1_1card_1a10f5f4667d80f47674d1876df69b8e22)`(card,mode,apn)` @@ -706,4 +659,4 @@ Initialize the [Notecard](#classnotecard_1_1notecard_1_1_notecard) before a rese Ensure that the passed-in card is a Notecard. -Generated by [Moxygen](https://sourcey.com/moxygen) +Generated by [Moxygen](https://sourcey.com/moxygen) \ No newline at end of file