rogerthomas84/php-octopus-mini-api

使用PHP从Octopus API获取实时消耗数据的示例

1.0.2 2024-07-11 07:12 UTC

This package is auto-updated.

Last update: 2024-09-11 07:27:22 UTC


README

CI

描述...

这是一个简单的库,实现了Octopus API的基本功能,以及它们GraphQL API的一部分。它最初的目的主要是作为从Octopus Energy GraphQL API检索实时消耗数据的文档,但后来已扩展到包括一些其他基本功能,如获取半小时消耗数据和账户信息。

为了检索实时消耗数据,您必须拥有Octopus Mini设备,并且(显然)您需要拥有Octopus Energy的账户。如果您有Octopus账户但没有Mini设备,您可以从Octopus Energy这里申请一个免费设备。

此库的大部分内容都是使用Octopus提供的文档编写的

用法...

composer require rogerthomas84/php-octopus-mini-api
$api = \Rt\OctopusAPI\OctopusApiSingleton::getInstance();
$apiInstance = $api->setEmail(
    getenv('OCTOPUS_EMAIL')
)->setPassword(
    getenv('OCTOPUS_PASSWORD')
)->setAccountNumber(
    getenv('OCTOPUS_ACCOUNT_NUMBER')
)->setApiKey(
    getenv('OCTOPUS_API_KEY')
)->setMpan(
    getenv('OCTOPUS_MPAN')
)->setSerialNumber(
    getenv('OCTOPUS_SERIAL_NUMBER')
);

$graphQl = $apiInstance->getOctopusGraphQL();

// $myToken = $graphQl->getToken();
// $meterDeviceId = $graphQl->getMeterDeviceId();
$consumption = $graphQl->getLiveConsumption();
// or you can pass the meterDeviceId directly:
// $consumption = $graphQl->getLiveConsumption($meterDeviceId);

echo 'Current consumption is ' . $consumption . 'W\n';

$myAccount = $apiInstance->getAccount();
echo 'You have ' . count($myAccount->getProperties()) . ' properties on your account\n';

$myProperty = $myAccount->getProperties()[0];
echo 'The first property on your account is ' . $myProperty->getAddressLine1() . "\n";

$myMeter = $myProperty->getElectricityMeterPoints()[0];
echo 'The first electricity meter point on your property is ' . $myMeter->getMpan() . "\n";

$myAgreement = $myMeter->getAgreements()[0];
$myTariffCode = $myAgreement->getTariffCode();
echo 'My tariff code is ' . $myAgreement->getTariffCode() . "\n";


$dateFrom = DateTime::createFromFormat('Y-m-d', '2021-01-01');
$dateTo = DateTime::createFromFormat('Y-m-d', '2021-01-02');

$consumptionData = $apiInstance->getElectricityService()->getHalfHourReadings(
    $dateFrom,
    $dateTo,
    25000, // limit per page
    '-period' // or 'period', used for sorting
);

$standingCharges = $apiInstance->getElectricityService()->getStandingCharges(
    $myTariffCode,
    $dateFrom,
    $dateTo,
    25000, // limit per page
);

$unitRates = $apiInstance->getElectricityService()->getStandardUnitRates(
    $myTariffCode,
    $dateFrom,
    $dateTo,
    25000, // limit per page
);

测试...

为了测试,您需要设置以下环境变量

  • OCTOPUS_EMAIL
  • OCTOPUS_PASSWORD
  • OCTOPUS_ACCOUNT_NUMBER
  • OCTOPUS_API_KEY
  • OCTOPUS_MPAN
  • OCTOPUS_SERIAL_NUMBER

然后运行以下命令

composer install
./vendor/bin/phpunit