承接 chernegasergiy/battery-info 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chernegasergiy/battery-info

最新稳定版本:v0.1.0

Composer 安装命令:

pie install chernegasergiy/battery-info

包简介

Cross-platform PHP extension for retrieving battery level and charging status (Linux, macOS, Windows, Android).

README 文档

README

Latest Stable Version Total Downloads License Build Linux Build macOS Build Windows

This extension exposes a single function: battery_info() which returns an array with keys:

  • batteries (array) — array of all detected batteries, each containing:
    • name (string) — battery identifier (e.g., "BAT0", "BAT1")
    • level (int|null) — 0..100 or null if unknown
    • charging (bool|null) — true/false or null
    • status (string|null) — "charging"/"discharging"/"full"/"not charging"/null
  • level (int|null) — primary battery level (0..100 or null)
  • charging (bool|null) — primary battery charging state
  • status (string|null) — primary battery status
  • platform (string) — platform name ("linux", "macos", "windows", "android")

Installation

Install with PIE (Recommended)

The easiest way to install the extension is using PIE (PHP Installer for Extensions):

pie install chernegasergiy/battery-info

Manual Installation

  1. Build or Download the extension binary for your OS.
  2. Locate your extensions directory: php -i | grep extension_dir.
  3. Copy the file:
    • Linux/macOS/Android: battery_info.so
    • Windows: php_battery_info.dll
  4. Enable in php.ini:
    • Add extension=battery_info (or extension=php_battery_info.dll on Windows).

Build (typical Unix/macOS)

phpize
./configure --enable-battery_info
make
sudo make install

Usage Example

<?php
if (!function_exists('battery_info')) {
    die("Extension not loaded");
}

$info = battery_info();

echo "Platform: " . $info['platform'] . "\n";

// Check all batteries
if (!empty($info['batteries'])) {
    foreach ($info['batteries'] as $battery) {
        echo "Battery: " . $battery['name'] . "\n";
        echo "  Level: " . ($battery['level'] ?? 'unknown') . "%\n";
        echo "  Status: " . ($battery['status'] ?? 'unknown') . "\n";
    }
} else {
    echo "No batteries detected.\n";
}

// Or use primary battery info directly
if ($info['level'] !== null) {
    echo "\nPrimary battery: " . $info['level'] . "% (" . $info['status'] . ")\n";
}

Notes

  • Android requires a small Java helper class and JNI glue — see android_battery.c comments.
  • On Linux the extension reads /sys/class/power_supply/*/capacity and status.
  • On macOS the extension uses IOKit (build with Xcode tools available).
  • On Windows the extension uses GetSystemPowerStatus.

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

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

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: C

其他信息

  • 授权协议: LicenseRef-CSSM-Unlimited-2.0
  • 更新时间: 2026-02-01