PHPackages                             chernegasergiy/battery-monitor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. chernegasergiy/battery-monitor

ActiveProject[Utility &amp; Helpers](/categories/utility)

chernegasergiy/battery-monitor
==============================

Battery monitoring bot with Telegram notifications.

00PHPCI passing

Since Feb 23Pushed 2mo agoCompare

[ Source](https://github.com/ChernegaSergiy/php-battery-monitor)[ Packagist](https://packagist.org/packages/chernegasergiy/battery-monitor)[ RSS](/packages/chernegasergiy-battery-monitor/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Battery Monitor Bot
===================

[](#battery-monitor-bot)

This repository contains a robust PHP application for monitoring laptop battery status on Linux systems. It leverages the `sysfs` interface to read power supply data and sends notifications via Telegram. The project is built with strict adherence to modern PHP standards, including PSR-12 for coding style and PSR-3 for logging.

Key Features
------------

[](#key-features)

- **Real-time Monitoring** via Linux sysfs interface.
- **Telegram Notifications** for status updates and critical battery warnings.
- **Object-Oriented Design** following SOLID principles.
- **PSR-3 Logging** with a custom file-based logger implementation.
- **Robust Error Handling** including retry logic for API requests.
- **Strict Typing** utilizing PHP 8.1+ features.
- **DTO Implementation** for type-safe data transfer between services.

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- Linux environment (access to `/sys/class/power_supply/`)
- [Composer](https://getcomposer.org/)
- Telegram Bot Token and Chat ID

Class Structure
---------------

[](#class-structure)

```
src/
+-- Application.php              # Main application logic and event loop
+-- Config/
|   \-- Configuration.php        # Settings management and retrieval
+-- Dto/
|   \-- BatteryStatus.php        # Data Transfer Object for battery state
+-- Exception/
|   \-- BatteryReadException.php # Custom exception for read failures
+-- Logger/
|   +-- FileLogger.php           # File-based logging implementation
|   \-- LoggerInterface.php      # PSR-3 compatible interface
\-- Service/
    +-- BatteryReader.php        # Parses /sys/class/power_supply data
    +-- MessageFormatter.php     # Formats Telegram messages (HTML)
    \-- TelegramClient.php       # Async cURL client with retry logic

```

Installation
------------

[](#installation)

To deploy the bot on your system, simply proceed as follows:

1. Clone the repository:

    ```
    git clone https://github.com/ChernegaSergiy/php-battery-monitor.git
    cd php-battery-monitor
    ```
2. Install dependencies:

    ```
    composer install
    ```
3. Ensure the logs directory exists and is writable:

    ```
    mkdir -p logs
    chmod 755 logs
    ```

Configuration
-------------

[](#configuration)

Configuration is handled in `index.php`. You need to set your Telegram credentials and monitoring preferences:

```
$config = new Configuration([
    'telegram' => [
        'token' => 'YOUR_BOT_TOKEN',
        'chat_id' => 'YOUR_CHAT_ID',
    ],
    'monitoring' => [
        'send_minute' => 0,         // Minute of the hour to send periodic updates
        'check_interval' => 60,     // Loop interval in seconds
        'critical_threshold' => 15, // Battery percentage to trigger warning
    ],
    'logging' => [
        'path' => __DIR__ . '/logs/battery.log',
    ],
    'retry' => [
        'max_attempts' => 3,
        'delay' => 5,
    ],
]);
```

Usage
-----

[](#usage)

Run the bot from the command line:

```
php index.php
```

To keep the bot running in the background, it is recommended to use a process manager like systemd or Supervisor.

Customization
-------------

[](#customization)

### Replacing the Logger

[](#replacing-the-logger)

The project uses a standard `LoggerInterface`. You can easily swap the default `FileLogger` with any PSR-3 compliant logger (e.g., Monolog):

```
$logger = new Monolog\Logger('name');
$app = new Application($config, $logger);
```

### Adding New Battery Sources

[](#adding-new-battery-sources)

To support different operating systems or battery paths, extend the `BatteryReader` class and override the `readParameter` method.

Troubleshooting
---------------

[](#troubleshooting)

- **BatteryReadException**: Ensure you are running on a Linux system and the path `/sys/class/power_supply/battery/` exists.
- **Telegram API Errors**: Check your bot token and chat ID. Ensure the server has internet access.
- **Permission Denied**: Check write permissions for the `logs/` directory.

Contributing
------------

[](#contributing)

Contributions are welcome and appreciated! Here's how you can contribute:

1. Fork the project
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

Please make sure to update tests as appropriate and adhere to the existing coding style.

License
-------

[](#license)

This project is licensed under the CSSM Unlimited License v2.0 (CSSM-ULv2). See the [LICENSE](LICENSE) file for details.

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance56

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/e2471bdadcf4a5ea969fe763a72d03fb46525b59a7e4cedc3ed4d855029e7785?d=identicon)[ChernegaSergiy](/maintainers/ChernegaSergiy)

---

Top Contributors

[![ChernegaSergiy](https://avatars.githubusercontent.com/u/60980650?v=4)](https://github.com/ChernegaSergiy "ChernegaSergiy (23 commits)")

### Embed Badge

![Health badge](/badges/chernegasergiy-battery-monitor/health.svg)

```
[![Health](https://phpackages.com/badges/chernegasergiy-battery-monitor/health.svg)](https://phpackages.com/packages/chernegasergiy-battery-monitor)
```

###  Alternatives

[cmgmyr/phploc

A tool for quickly measuring the size of a PHP project.

266.6M3](/packages/cmgmyr-phploc)[marionnewlevant/snitch

Report when two people might be editing the same element (eg entry, category, or global) or field

3585.1k2](/packages/marionnewlevant-snitch)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
