green-elephpant / carbon-aware
SDK 帮助您的应用程序减少碳排放
0.0.2
2024-05-20 20:18 UTC
Requires
- php: >=7.4
- ext-json: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.3
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-19 20:23:36 UTC
README
Green ElePHPant - CarbonAware
Green ElePHPant 希望帮助您减少软件的碳排放。
实现这一目标的一种方式是在能源最“绿色”时使用能源 - 即,当大部分能源来自可再生能源而不是燃烧化石燃料时。
GreenElephpant\CarbonAware
是一个简单的包装器,用于提供许多地区当前或未来碳强度的信息。目前,我们支持 CO2Signal 和 Electricity Maps。希望未来会有更多支持!
关键概念
构建碳感知应用程序意味着,当碳强度过高时,某些活动或工作将不会执行,或者至少在较低频率下执行。
例如,如果碳强度低,我们可以比碳强度高时更频繁地更新仪表板
if ($carbonAwareService->isLow()) { $schedule ->command('dashboard:update') ->everyFiveMinutes(); } else { $schedule ->command('dashboard:update') ->everyTenMinutes(); }
安装
只需使用 composer 将 GreenElephpant\CarbonAware
添加到您的项目中
composer require green-elephpant/carbon-aware
配置
GreenElephpant\CarbonAware
被设计成尽可能少地需要外部依赖,以便它几乎可以适用于每个项目。它使用多个 PSR,例如 HTTP 消息(PSR-17)和客户端(PSR-18),因此它不会直接工作,但需要一点(但不是很多)手动配置。
use Buzz\Client\Curl; use GreenElephpant\CarbonAware\DataProvider\EnergyCharts\Connector\EnergyChartsConnector; use GreenElephpant\CarbonAware\DataProvider\EnergyCharts\EnergyCharts; use GreenElephpant\CarbonAware\Service\CarbonAwareService; use Nyholm\Psr7\Factory\Psr17Factory; // Create HTTP client with HTTP message factory // E.g. nyholm/psr7 and kriswallsmith/buzz $psr17Factory = new Psr17Factory(); $psr18Client = new Curl($psr17Factory); // Create Connector (here: for energy-charts.info) $energyChartsConnector = new EnergyChartsConnector( $psr18Client, $psr17Factory, ); // Setup location (the region of the world you want to get the information for) $location = new GreenElephpant\CarbonAware\Location\Location('DE'); // Finally create the DataProvider instance, using the connector and the location $dataProvider = new EnergyCharts( $co2SignalConnector, $location ); $carbonAwareService = new CarbonAwareService($connector
内部结构
GreenElephpant\CarbonAware
使用第三方 API 接收碳强度数据。为了确保未来的兼容性和易于维护,
Connector
:实际的 API 包装器。提供对相关 API 端点的轻松访问(注意:仅支持与本项目相关的端点。DataProvider
:连接CarbonAwareService
:提供统一的 API