Bagikan melalui


PowerGridForecast Kelas

Definisi

Berisi informasi tentang kisi daya yang tersambung dengan perangkat. Data dimaksudkan untuk digunakan dalam perkiraan pergeseran waktu waktu di mana beban kerja terjadi, atau mengurangi konsumsi energi selama waktu yang intens.

public ref class PowerGridForecast sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Devices.Power.PowerGridApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class PowerGridForecast final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Devices.Power.PowerGridApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class PowerGridForecast
Public NotInheritable Class PowerGridForecast
Warisan
Object Platform::Object IInspectable PowerGridForecast
Atribut

Persyaratan Windows

Rangkaian perangkat
Windows Desktop Extension SDK (diperkenalkan dalam 10.0.26100.0)
API contract
Windows.Devices.Power.PowerGridApiContract (diperkenalkan dalam v1.0)

Contoh

using Windows.Devices.Power;

void PrintBestTimes(PowerGridForecast forecast)
{
    double bestSeverity = double.MaxValue;
    double bestLowImpactSeverity = double.MaxValue;
    DateTime bestTime = DateTime.MaxValue;
    DateTime bestLowImpactTime = DateTime.MaxValue;
    TimeSpan blockDuration = forecast.BlockDuration;
    DateTime startTime = forecast.StartTime;
    IList<PowerGridData> forecastSignals = forecast.Forecast;

    if (forecastSignals.Count == 0)
    {
        Console.WriteLine("Error encountered with getting forecast; try again later.");
        return;
    }

    foreach (PowerGridData data in forecastSignals)
    {
        if (data.Severity < bestSeverity)
        {
            bestSeverity = data.Severity;
            bestTime = startTime;
        }

        if (data.IsLowUserExperienceImpact && data.Severity < bestLowImpactSeverity)
        {
            bestLowImpactSeverity = data.Severity;
            bestLowImpactTime = startTime;
        }

        startTime = startTime + blockDuration;
    }

    if (bestLowImpactTime != DateTime.MaxValue)
    {
        DateTime endBestLowImpactTime = bestLowImpactTime + blockDuration;
        Console.WriteLine($"Lowest severity during low impact is {bestLowImpactSeverity}, which starts at {bestLowImpactTime.ToString()}, and ends at {endBestLowImpactTime}.");
    }
    else
    {
        Console.WriteLine("There's no low-user-impact time in which to do work.");
    }

    if (bestTime != DateTime.MaxValue)
    {
        DateTime endBestSeverity = bestTime + blockDuration;
        Console.WriteLine($"Lowest severity is {bestSeverity}, which starts at {bestTime.ToString()}, and ends at {endBestSeverity.ToString()}.");
    }
}

PowerGridForecast forecast = PowerGridForecast.GetForecast();
PrintBestTimes(forecast);
#include "pch.h"
#include <iostream>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Devices.Power.h>

using namespace winrt::Windows::Devices::Power;
using namespace winrt::Windows::Foundation::Collections;
using namespace winrt::Windows::Foundation;

void PrintFullForecast(PowerGridForecast const& forecast)
{
    IVectorView<PowerGridData> forecastSignals = forecast.Forecast();
    DateTime forecastStartTime = forecast.StartTime();
    TimeSpan forecastBlockDuration = forecast.BlockDuration();
    DateTime blockStartTime = forecastStartTime;

    // On failure the forecast will be empty.
    if (forecastSignals.Size() == 0)
    {
        std::wcout << L"Error encountered while reading the forecast; try again later." << std::endl;
        return;
    }

    // Iterate through the forecast printing all the data.
    for (auto const& block : forecastSignals)
    {
        auto severity = block.Severity();
        auto isLowImpact = block.IsLowUserExperienceImpact();

        std::wcout << L"Start time - ";
        PrintDateTime(blockStartTime, true);
        std::wcout << L" | End time - ";
        PrintDateTime(blockStartTime + forecastBlockDuration, true);
        std::wcout << L" | Intensity - " << severity << L" | IsLowImpactTime - " << (isLowImpact ? L"TRUE" : L"FALSE") << std::endl;

        blockStartTime = blockStartTime + forecastBlockDuration;
    }
}

void PrintBestTimes(PowerGridForecast const& forecast)
{
    IVectorView<PowerGridData> forecastSignals = forecast.Forecast();
    DateTime forecastStartTime = forecast.StartTime();
    TimeSpan forecastBlockDuration = forecast.BlockDuration();
    DateTime blockStartTime = forecastStartTime;

    // On failure the forecast will be empty
    if (forecastSignals.Size() == 0)
    {
        std::wcout << L"Error encountered while reading the forecast; try again later." << std::endl;
        return;
    }

    DateTime bestSeverityTimeUTC = DateTime::max();
    DateTime bestSeverityTimeInLowUserImpactTimeUTC = DateTime::max();

    // 1.0 is maximum severity the API can return.
    double bestSeverity = 1.0;
    double bestSeverityInLowUserImpactTime = 1.0;

    // Iterate through the forecast looking for the best times.
    for (auto const& block : forecastSignals)
    {
        auto severity = block.Severity();
        auto isLowImpact = block.IsLowUserExperienceImpact();

        // Check if there is lower severity
        if (severity < bestSeverity)
        {
            bestSeverity = severity;
            bestSeverityTimeUTC = blockStartTime;
        }

        // Check whether there's a lower severity that's also at a time with low user impact.
        if (isLowImpact && severity < bestSeverityInLowUserImpactTime)
        {
            bestSeverityInLowUserImpactTime = severity;
            bestSeverityTimeInLowUserImpactTimeUTC = blockStartTime;
        }

        blockStartTime = blockStartTime + forecastBlockDuration;
    }

    // Print out the best times only if they've been set.
    if (bestSeverityTimeUTC != DateTime::max())
    {
        std::wcout << L"Best time to do work is ";
        PrintDateTime(bestSeverityTimeUTC, true);
        std::wcout << L" with a severity of " << bestSeverity;
        std::wcout << L" and ends at ";
        PrintDateTime(bestSeverityTimeUTC + forecastBlockDuration, true);
        std::wcout << std::endl;
    }

    if (bestSeverityTimeInLowUserImpactTimeUTC != DateTime::max())
    {
        std::wcout << L"Best time with low user impact is ";
        PrintDateTime(bestSeverityTimeInLowUserImpactTimeUTC, true);
        std::wcout << L" with a severity of " << bestSeverityInLowUserImpactTime;
        std::wcout << L" and ends at ";
        PrintDateTime(bestSeverityTimeInLowUserImpactTimeUTC + forecastBlockDuration, true);
        std::wcout << std::endl;
    }
    else
    {
        std::wcout << "There's no low-user-impact time in which to do work." << std::endl;
    }
}

int main() 
{
    std::wcout << L"Power Grid Forecast WinRT API sample app." << std::endl;

    // Register for the forecast notification.
    auto revoker = PowerGridForecast::ForecastUpdated(winrt::auto_revoke, [&](auto, winrt::Windows::Foundation::IInspectable const&) {
        std::wcout << L"Forecast updated..." << std::endl;

        // Forecast has been updated; find the next best times.
        PowerGridForecast forecast = PowerGridForecast::GetForecast();
        PrintBestTimes(forecast);
    });

    // Print out the full forecast.
    PowerGridForecast forecast = PowerGridForecast::GetForecast();
    PrintFullForecast(forecast);

    // Wait until the user presses a key to exit.
    std::cout << "Listening to the signal: a new forecast has been created."
                    "Leave this program open to see when a new forecast is created, otherwise press any key to exit this program..." << std::endl;
    std::cin.get();

    return 0;
}

Keterangan

Windows memaparkan prakiraan emisi karbon kisi daya berdasarkan kisi daya yang tersambung dengan perangkat. Data ini sudah digunakan oleh Windows Update, misalnya, untuk pergeseran waktu ketika pembaruan terjadi untuk mengurangi emisi karbon. API ini memaparkan prakiraan yang sama ini kepada Anda sehingga Anda dapat mengurangi emisi karbon dari beberapa beban kerja Anda. Misalnya, Anda dapat melakukan pergeseran waktu saat pembaruan aplikasi/game Anda terjadi; atau membatasi laju bit pemutaran audio, atau beberapa tingkat keakuratan penyajian lainnya; atau mengaktifkan mode efisiensi, jika Anda memilikinya.

API prakiraan kisi daya menyediakan dua sinyal kepada Anda (untuk meminta pergeseran waktu). Satu sinyal berisi nilai Keparahan yang dinormalisasi (antara 0,0 dan 1,0) kondisi kisi untuk dioptimalkan (intensitas karbon). Sinyal lainnya, IsLowUserExperienceImpact, adalah nilai Boolean yang mewakili ketika Windows berpikir pengguna akan jauh dari perangkat. Anda dapat memilih untuk hanya menggunakan satu sinyal alih-alih keduanya; sinyal memiliki nilai satu per satu serta bersama-sama.

Pergeseran waktu berarti menggunakan energi yang sama untuk menyelesaikan pekerjaan, tetapi melakukannya pada waktu yang berbeda berdasarkan sinyal.

Tingkat keparahan adalah nilai yang dinormalisasi antara 0,0 dan 1,0, di mana 0 dianggap terbaik, dan 1 adalah yang terburuk. Itu sesuai dengan tingkat keparahan karbon jaringan daya berdasarkan tempat perangkat berada.

Dampak pengalaman pengguna yang rendah. Nilai Boolean yang mewakili kapan Windows berpikir pengguna akan pergi, atau tidak menggunakan banyak sumber daya. Ini dapat dianggap sebagai Jam Aktif terbalik. Ketika nilainya , trueini dianggap sebagai waktu yang tepat untuk menggeser waktu beban kerja. Ketika itu false, dianggap waktu yang buruk untuk beban kerja pergeseran waktu, dalam hal pengalaman pengguna.

Properti

BlockDuration

Durasi setiap elemen dalam vektor Prakiraan .

Forecast

Mendapatkan vektor yang berisi data prakiraan. Prakiraan berdamai, dan dimulai pada StartTime. Waktu mulai setiap elemen dapat dihitung dengan StartTime + (index * BlockDuration).

StartTime

Mendapatkan waktu mulai elemen pertama di Prakiraan.

Metode

GetForecast()

Metode statis untuk mengambil prakiraan. Setelah gagal, ini akan mengembalikan prakiraan kosong.

Acara

ForecastUpdated

Peristiwa untuk memberi tahu pelanggan ketika payload prakiraan baru siap. Diharapkan, saat aplikasi menerima pemberitahuan ini, Anda akan memanggil GetForecast.

Berlaku untuk