Metadata-Version: 2.4
Name: battfeed
Version: 0.5.0
Summary: Turn live battery data sources into BDF feeds
Author: Simon Clark
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright 2024 Simon Clark
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
        
Project-URL: Homepage, https://github.com/DigiBatt/battfeed
Project-URL: Issues, https://github.com/DigiBatt/battfeed/issues
Keywords: battery,bdf,battery-data-format,data-acquisition,digibatt,battfeed
Classifier: Development Status :: 3 - Alpha
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Provides-Extra: wmi
Requires-Dist: wmi; extra == "wmi"
Provides-Extra: mc3000-ble
Requires-Dist: bleak>=0.21; extra == "mc3000-ble"
Provides-Extra: mc3000-usb
Requires-Dist: pyusb>=1.2; extra == "mc3000-usb"
Provides-Extra: bdf
Requires-Dist: batterydf; extra == "bdf"
Provides-Extra: parquet
Requires-Dist: pyarrow>=14; extra == "parquet"
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Dynamic: license-file

# battfeed

**battfeed turns live battery data sources into BDF (Battery Data Format) feeds.**

It is the acquisition layer of an open battery-data stack: point it at something that
produces battery readings — a simulator, a growing instrument log, the battery in your
Windows laptop, or your own cycler driver — and it polls that source on a schedule and
writes conforming `.bdf.csv` files (plus a `.meta.json` sidecar) that the rest of the
stack understands.

```
  cyclers · instruments · OS batteries · live logs · simulators
                        │
                        │  poll()                (you implement DataSource)
                        ▼
                     battfeed                     (this package: Harvester + sinks)
                        │
                        ▼
        *.bdf.csv  +  *.meta.json                (Battery Data Format files)
                        │
        ┌───────────────┼────────────────────┐
        ▼               ▼                    ▼
    batterydf        BattINFO           BDA registry
  (normalize,      (semantics,           (publish,
   validate,        ontology)             share)
   analyze)
```

The core has **zero runtime dependencies** — everything is Python standard library.
Requires Python >= 3.10.

## Install

```
pip install battfeed
```

| Extra                    | Installs    | Enables                                            |
| ------------------------ | ----------- | -------------------------------------------------- |
| `pip install battfeed[wmi]` | `wmi`       | `WmiBatterySource` (Windows laptop/tablet battery) |
| `pip install battfeed[mc3000-ble]` | `bleak` | `Mc3000Source` over Bluetooth LE (the `mock` transport needs no extra) |
| `pip install battfeed[mc3000-usb]` | `pyusb` | `Mc3000Source` over USB |
| `pip install battfeed[parquet]` | `pyarrow` | `ParquetSink` — write captures as Parquet for analysis |
| `pip install battfeed[bdf]` | `batterydf` | `validate_file()` — check emitted files against the BDF reference implementation |
| `pip install battfeed[dev]` | `pytest`, `ruff`, `mypy` | development                          |

The Android source needs only the `adb` executable (Android platform-tools) on
your PATH; the `dji` import source needs the external `dji-log` binary (on your
PATH or via `DJI_LOG_BIN`). Neither needs a Python extra.

## Quickstart

Collect from the built-in simulator into a BDF file:

```python
from battfeed import BdfCsvSink, Harvester, create_source

harvester = Harvester()
harvester.register(create_source("simulator"))
sink = BdfCsvSink("LOCAL__DemoCell__20260707_001.bdf.csv", metadata={"operator": "me"})
harvester.collect("simulator", duration_s=10, interval_s=1.0, sink=sink)
sink.close()  # finalises the CSV and writes the .meta.json sidecar
```

Or from the command line (Ctrl-C stops gracefully and finalises the files):

```
battfeed collect --source simulator --duration 10 --interval 1 --institution LOCAL --cell DemoCell
```

Omit `--duration` to collect until Ctrl-C — the mode field collectors run in.
Source constructor options are passed with repeatable `--opt KEY=VALUE` flags
(values are parsed as JSON when possible):

```
battfeed collect --source mc3000 --opt slot=1 --opt transport=ble --cell AA-Bay2
battfeed collect --source android --opt serial=R58M12ABC --interval 5
battfeed collect --source csvtail --opt path=instr.log --opt 'column_map={"V":"voltage_volt","I":"current_ampere"}'
```

`battfeed sources` lists everything available — including sources contributed by
other installed packages — with each source's options, and marks unavailable
ones (e.g. `wmi` off-Windows, `mc3000` without its transport extra).

## What comes out

A conforming BDF CSV with snake_case `{quantity}_{unit}` headers. The required trio
`test_time_second, voltage_volt, current_ampere` always leads the header, followed by
any extra columns in alphabetical order. Files are named
`InstitutionCode__CellName__YYYYMMDD_XXX.bdf.csv` (see `battfeed.sinks.bdf_csv.dataset_filename`).

**Sign convention** (per the BDF specification): positive current charges the test
object, negative current discharges it. Power follows the same sign.

If a source does not stamp its own `test_time_second`, the harvester stamps each sample
with the elapsed collection time.

## Sinks

A **sink** is where collected samples land. Everything above writes to a
`BdfCsvSink`, but battfeed ships four:

- **`BdfCsvSink`** — one BDF `.bdf.csv` file for one test object. The default;
  use it for a single cell, bay, or device. The sidecar is written early
  (marked `"finalized": false`) and finalised on `close()`, so a crash
  mid-collection still leaves valid metadata on disk.
- **`RoutingSink`** — demultiplexes one stream into **one BDF file per
  `(series_id, run_id)`**, with optional time/row rotation. Use it when one
  connection yields many objects or runs (see *Multi-object routing* below).
- **`HttpPushSink`** — POSTs samples as gzipped newline-delimited JSON to any
  HTTP endpoint on a time cadence, stdlib-only. Use it to feed a registry or lab
  server. Delivery is at-least-once: rows are kept on a failed POST and spooled
  to a loadable `.spool.ndjson` if the endpoint stays down.
- **`ParquetSink`** — one Parquet file (plus the same `.meta.json` sidecar) per
  bounded capture, for analysis in pandas/Polars/DuckDB. Needs `battfeed[parquet]`.
  Use it for finite analytical captures, not unbounded telemetry (that is
  `RoutingSink` + BDF).

`BdfCsvSink` **strips** the routing keys (they are never BDF columns); the
Parquet and HTTP sinks **keep** them, as a column and payload field respectively,
so downstream code can group by object and run.

### Multi-object routing

Two optional reserved keys on a sample say *where it belongs* rather than *what
was measured*:

- **`series_id`** — which physical object the sample is from (a car, a pack, a bay);
- **`run_id`** — which test-run segment it belongs to.

A source that emits them must supply its own `test_time_second`, zero-based per
`(series_id, run_id)` — the harvester's shared elapsed-collection clock is wrong
for an object that appears mid-run. `RoutingSink` sanitises raw `series_id`
device serials into safe BDF cell names, opens each object's file lazily on its
first sample, and reports what it wrote via `files_by_series`.

## Importing files

Some sources ingest **complete files** rather than polling a live device — a
folder of exported logs, say. `battfeed import` drains such a source through the
same `DataSource` seam, writing through a `RoutingSink` so the result is one
`.bdf.csv` (plus sidecar) per `(series_id, run_id)`:

```
battfeed import --source dji --opt path=C:/logs/dji --out-dir imported
```

One-shot by default — it stops when the source reports drained and prints
exactly what it wrote (or "nothing to import"); pass `--watch` to keep polling a
folder for new files until Ctrl-C. Import is **at-least-once**: a batch source
records a file as ingested only *after* its rows are safely written, so a crash
mid-import re-imports the file into new segment files rather than losing it.
Dedupe and quarantine are backed by an `ImportLedger` (sha256 content hashes;
permanently-unsupported files are quarantined with a reason and never retried).
Deleting output files does not reset the ledger; `battfeed import --reset-ledger`
does.

## Configuration files

A long invocation — an mc3000 slot and BLE address, a dji folder and keychain
key — can live in a TOML file instead of a wall of `--opt` flags. Pass it with
`--config` on `collect` or `import`:

```toml
[collect]                        # defaults for `battfeed collect`
interval = 2.0
institution = "SINTEF"
cell = "Pack-07"

[import]                         # defaults for `battfeed import`
out_dir = "imported"
watch = true

[source.mc3000]                  # options for `--source mc3000`
slot = 1
transport = "ble"
address = "AA:BB:CC:DD:EE:FF"

[source.dji]                     # options for `--source dji`
path = "C:/logs/dji"
api_key = "${ENV:DJI_API_KEY}"   # expanded from the environment at load time
```

A `[source.<name>]` block matches `--source <name>`; its optional `type` field
selects the registered source (defaulting to the block name, so a block can
alias one source under another name). **Precedence**, highest first:

- source options: `--opt KEY=VALUE` > `[source.<name>]` > the source's own default
- run parameters: the CLI flag > `[collect]`/`[import]` > the built-in default

`--config` uses `tomllib`, standard library on Python 3.11+; on 3.10 install the
`tomli` backport (`pip install tomli`) — no dependency is added to the core.

## Keeping secrets out of sight

Passing a token as `--opt api_key=...` leaks it into shell history and the
process list (`ps` / Task Manager show a process's full command line). Prefer a
config file with **`${ENV:VAR}` expansion**, so the secret lives only in the
environment and never touches the file:

```toml
[source.push]
token = "${ENV:INGEST_TOKEN}"
```

Whatever the entry path, battfeed redacts credentials in two complementary
layers before they can reach a log, an error message, the `battfeed sources`
listing, or a `.meta.json` sidecar:

- **by key name** — a value whose key looks like a credential (`*key*`,
  `*token*`, `*secret*`, `*password*`, `*auth*`, ...) is masked to `***`;
- **by value** — the concrete secret values resolved for a run, plus any
  `scheme://user:pass@host` URL userinfo, are scrubbed even when they hide under
  an innocuous key.

The helpers are public — `battfeed.config.redact_mapping`, `redact_text`,
`is_secret_key` — for source authors who need them.

## Writing your own source

`battfeed.DataSource` is a `typing.Protocol` — the stable seam third-party collectors
implement. No imports from battfeed are needed; any object with `name`, `metadata()` and
`poll()` qualifies:

```python
class MyCyclerSource:
    name = "my-cycler"

    def metadata(self):
        return {"source": self.name, "vendor": "ACME", "channel": 3}

    def poll(self):
        # Return zero or more NEW samples since the last call,
        # keyed by canonical BDF column names.
        reading = my_driver.read_channel(3)
        return [{"voltage_volt": reading.volts, "current_ampere": reading.amps}]
```

Register it with a `Harvester` directly, or expose it to the `battfeed` CLI from your
own package via an entry point:

```toml
[project.entry-points."battfeed.sources"]
my-cycler = "my_pkg.sources:MyCyclerSource"
```

Sources may optionally define `close()` to release hardware handles, and an
`availability()` classmethod to explain why they cannot run here (missing extra,
wrong platform); battfeed uses both when present. See `examples/custom_source.py`
for a runnable version.

**Keep sources simple: raise on trouble.** When the device is unreachable,
`poll()` should raise — the `Harvester` retries with exponential backoff under a
configurable `ErrorPolicy` and only abandons the run (raising `SourceFailure`)
after too many *consecutive* failures. Sources should not implement their own
retry loops; only swallow errors you can genuinely resolve better yourself
(e.g. one bad frame out of several channels). This is what makes multi-day
field collection survive flaky Bluetooth and USB.

**Test it with `check_source`.** `battfeed.testing.check_source` asserts the
essentials every pipeline relies on — the `name`/`metadata()`/`poll()` surface,
strict-JSON metadata and samples, sample shape, and the routing-key discipline —
so a one-liner in your own test suite is a complete contract test:

```python
from battfeed.testing import check_source

def test_my_source_contract():
    check_source(MyCyclerSource(...))  # drive it with a mock or a replay tape, never live hardware
```

**Push-style hardware (BLE, CAN, MQTT).** Devices that *push* readings don't fit
a synchronous `poll()` directly. Subclass `battfeed.StreamingSource`: implement a
blocking `run_reader(emit, should_stop)` loop that calls `emit(sample)` for each
reading, and the base runs it in a background thread and buffers samples for
`poll()` to drain. The buffer is bounded and overflow is counted (never silently
lost); a reader that dies re-raises at the next `poll()` so the harvester's
`ErrorPolicy` owns the retry — no retry loop inside your reader. Because BLE/CAN
tooling is Linux-first and hardware isn't on every desk, the supported workflow
is **replay-first**: record raw frames from one live session into a JSONL *tape*
(`battfeed.testing.ReplayTape` / `TapeRecorder`), commit it, and drive every test
and most development from the tape with `ReplayReader` — which compresses time,
so an hour-long tape replays in milliseconds. See `examples/streaming_source.py`.

## Built-in sources

| Name        | Class                | What it does                                                                 |
| ----------- | -------------------- | ---------------------------------------------------------------------------- |
| `simulator` | `SimulatedCellSource` | Deterministic synthetic CR2032-ish discharge; ideal for demos and tests.    |
| `csvtail`   | `CsvTailSource`       | Tails a growing CSV log; you supply the column map and unit scale factors.  |
| `wmi`       | `WmiBatterySource`    | Polls the local Windows battery via WMI (`battfeed[wmi]`, Windows only).     |
| `mc3000`    | `Mc3000Source`        | SkyRC MC3000 charger/analyzer, one slot per instance, over BLE/USB (or a built-in mock transport for demos). |
| `android`   | `AndroidBatterySource` | Android device battery via `adb` (dumpsys + sysfs); pure stdlib.           |
| `dji`       | `DjiFlightLogSource`  | Import DJI Fly flight-log records (`*.txt`/`*.dat`) as one BDF file per (aircraft+battery, flight), via `battfeed import`. Wraps the external `dji-log` binary. Record files come from untrusted media, so battfeed passes the path safely to `dji-log` and quarantines anything malformed. Decrypting v13+ records calls DJI's keychain API. |

## Non-goals

Keeping battfeed small is the point. It deliberately does **not** do:

- **Vendor-file normalization.** Parsing and harmonising exported Neware / BioLogic /
  Digatron / Basytec / ... files is the job of
  [`batterydf`](https://github.com/battery-data-alliance) (Battery Data Alliance).
  `CsvTailSource` is config-driven on purpose — it will never guess column synonyms.
- **Upload, fleet management, or multi-tenant services.** `HttpPushSink` is a
  generic one-way POST to an endpoint you point it at — a *sink*, not a hosted
  service; battfeed runs no server and manages no fleet or tenants. Publishing
  and sharing belong to registry tooling.
- **Digital-twin or model logic.** State estimation and twin orchestration live in
  [`battwin`](https://github.com/DigiBatt/battwin), which consumes battfeed feeds.

## Development

```
pip install -e ".[dev]"
pytest
```

## Acknowledgements

This project has received support from European Union research and innovation
programs under grant agreement 101103997 (DigiBatt).

## License

Apache-2.0
