(view source code of weerlive.ps as plain text)
<#
.SYNOPSIS
NL: Toon actuele KNMI weergegevens
EN: Show live Dutch weather data from the Dutch KNMI weather institute
.DESCRIPTION
NL:
Dit script toont actuele KNMI weergegevens m.b.v. de Weerlive API.
De Weerlive API stelt elke 10 minuten actuele weergegevens beschikbaar.
Alle opgehaalde gegevens worden door dit script in het console venster weergegeven.
Daarnaast wordt de afgeronde waarde van de actuele temperatuur in een pictogram in de "System Tray" getoond.
Door te klikken op dit pictogram, wordt het console venster verborgen of juist weer hersteld.
Om van de Weerlive API gebruik te mogen maken, hebt u een API key nodig (gratis voor persoonlijk en educatief gebruik), die u bij Weerlive.nl kunt aanvragen (zie link).
Deze API key en de lokatie waarvoor de gegevens worden opgevraagd, dient u op te geven op de commandoregel of in te vullen bij na starten van het script of op te slaan in een tekstbestand "weerlive.cfg" dat zich in de zelfde map moet bevinden als dit script.
Het bestand "weerlive.cfg" bevat 2 regels tekst:
key=uw_persoonlijke_api_key
loc=gewenste_lokatie
Het script kan netjes afgesloten worden door een willekeurige toetsaanslag in het console venster.
EN:
This script shows live Dutch weather data from the Dutch KNMI weather institute, using the Weerlive live weather API.
This API makes live weather data available once every 10 minutes.
All data will be displayed in this script's console window.
The rounded actual temperature will also be shown in the System Tray a.k.a. Notification Area.
By clicking the icon in the Notification Area, this script's console window will be hidden or restored again.
Use of the Weerlive API requires an API key (free for personal or educational use), which you can request at Weerlive.nl (see link).
This API key and the location for which the data will be fetched, must either be specified on the command line or entered in a popup window when the script is started or be saved in a plain text file named "weerlive.cfg", located in the script's parent folder.
The file "weerlive,cfg" contains only 2 lines of text:
key=your_personal_api_key
loc=your_location
The script can be terminated gracefully by pressing any key in the console window.
Note that a basic knowledge of the Dutch language is required to get this script working with your own API key.
.PARAMETER Location
NL: De plaatsnaam voor de weersgegevens. Indien zowel op de commandoregel als in een configuratiebestand een lokatie wordt opgegeven, prevaleert de commandoregel. Indien er geen plaatsnaam is opgegeven op de commandoregel en ook niet in een configuratiebestand, dan wordt een invulscherm geopend om deze alsnog in te voeren. Als APIKey en Location beide op de opdrachtregel worden meegegeven, en het configuratiebestand weerlive.cfg bestaat nog niet, dan wordt dit bestand aangemaakt.
EN: De location used for the weather query. If the location is specified both on the command line sa well as in a configuration file, the command line prevails. If no location is specified on the command line and neither in a configuration file, a popup window will appear where the locatien can be specified. If both APIKey and Location are specified on the command line, and the configuration file weerlive.cfg does not exist, it will be created..
.PARAMETER APIKey
NL: De API key voor het opvragen van de weersgegevens. Indien zowel op de commandoregel als in een configuratiebestand een API key wordt opgegeven, prevaleert de commandoregel. Indien er geen API key is opgegeven op de commandoregel en ook niet in een configuratiebestand, dan wordt een invulscherm geopend om deze alsnog in te voeren. Als APIKey en Location beide op de opdrachtregel worden meegegeven, en het configuratiebestand weerlive.cfg bestaat nog niet, dan wordt dit bestand aangemaakt.
EN: De API key required for the weather query. If an API key is specified both on the command line as well as in a configuration file, the command line prevails. If no API key is specified on the command line and neither in a configuration file, a popup window will appear where an API key can be specified. If both APIKey and Location are specified on the command line, and the configuration file weerlive.cfg does not exist, it will be created.
.PARAMETER NoSave
NL: Voorkomt dat de waarden van APIKey en Location die op de opdrachtregel worden meegegeven, bij opslaan een bestaand configuratiebestand overschrijven.
EN: Prevent APIKey and Location values on the command line from overwriting an existing configuration file.
.PARAMETER Edit
NL: Opent het invulvenster voor API key en plaatsnaam bij opstarten van het script. Dit invulvenster verschijnt ook als API key of plaatsnaam niet opgegeven zijn.
EN: Opens the configuration window to enter an API key and location at startup of this script. This window will also be opened if API key or location aren't specified.
.PARAMETER Hide
NL: Verberg het console venster direct na opstarten van het script. Klik op het icoon in de "System Tray" om het console venster te herstellen.
EN: Hide this script's console immediately after starting the script. Clicking the icon in the Notification area will restore the console again.
.PARAMETER Help
NL: Toon deze uitleg.
EN: Show this help text.
.PARAMETER Version
NL: Toon de versie van dit script. Gecombineerd met de -Verbose parameter worden daarnaast ook het volledige pad en de laatste wijzigings- of publikatiedatum van het script getoond.
EN: Show this script's version. If combined with the -Verbose switch, the script's full path and last modified or release date will be shown as well.
.PARAMETER Verbose
NL: Toon uitgebreidere versie-informatie bij gebruik van de -Version parameter.
EN: Show verbose version information when using the -Version switch.
.LINK
NL: EN:
.LINK
Script geschreven door Rob van der Woude: Script written by Rob van der Woude:
https://www.robvanderwoude.com/ https://www.robvanderwoude.com/
.LINK
KNMI Weergegevens via Weerlive.nl API: Dutch KNMI weather data by Weerlive.nl API:
http://weerlive.nl/delen.php http://weerlive.nl/delen.php
.LINK
Weerlive API key aanvragen: Request Weerlive API key:
http://weerlive.nl/api/toegang/index.php http://weerlive.nl/api/toegang/index.php
.LINK
Tabel met alle geldige plaatsnamen: Table of valid location names:
http://weerlive.nl/plaatsnamen.php http://weerlive.nl/plaatsnamen.php
#>
param (
[string]$Location,
[string]$APIKey,
[switch]$NoSave,
[switch]$Edit,
[switch]$Hide,
[switch]$Help,
[switch]$Version,
[switch]$Verbose
)
$progver = "1.00"
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Text.RegularExpressions
Add-Type -AssemblyName System.Windows.Forms
Clear-Host
if ( $Version ) {
if ( $Verbose ) {
$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
"`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
} else {
# if last modified time is not equal to program version, the script has been tampered with
"`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
}
} else {
$progver
}
exit 0
}
if ( $Help -or ( [System.Environment]::CommandLine -match "[/\?]" ) ) {
Get-Help $PSCommandPath -Full
exit 1
}
$configfile = ( Join-Path $PSScriptRoot 'weerlive.cfg' )
$cachefile = ( Join-Path $PSScriptRoot 'weerlive.dat' )
if ( ![string]::IsNullOrWhiteSpace( $APIKey ) -and ![string]::IsNullOrWhiteSpace( $Location ) ) {
Remove-Item -Path $cachefile -Force
# Write command line APIKey and Location values to configuration file if that does not yet exist, unless NoSave switch was used
if ( !$NoSave -and !( Test-Path -Path $configfile -PathType Leaf ) ) {
( "key={0}`nloc={1}" -f $APIKey, $Location ) | Out-File -FilePath $configfile -Force -Encoding 'ASCII'
}
}
function Get-Locations( ) {
$url = "http://weerlive.nl/plaatsnamen.php"
$ProgressPreference = 'SilentlyContinue'
$html = ( Invoke-WebRequest -Uri $url -TimeoutSec 10 ).Content
$ProgressPreference = 'Continue'
$pattern = '<tr><td><b><a href=\"weer.php\?plaatsnaam=([^\"]+)\"'
$matches = [System.Text.RegularExpressions.Regex]::Matches( $html, $pattern )
@( $matches | ForEach-Object { $_.Groups[1].Value } )
}
function Read-Config( ) {
if ( Test-Path -Path $configfile -PathType Leaf ) {
# APIKey and Location from configuration file, if it exists
$configtext = ( Get-Content -Path $configfile -ErrorAction Stop )
$Global:APIKey = ( $configtext | Select-String -Pattern 'key=([^\n]+)' ).Matches[0].Groups[1].Value
$Global:Location = ( $configtext | Select-String -Pattern 'loc=([^\n]+)' ).Matches[0].Groups[1].Value
} else {
# use a popup window to retrieve APIKey and Location interactively
Edit-Config
}
}
function Edit-Config( ) {
$FormWeerliveConfig = New-Object -TypeName System.Windows.Forms.Form
[System.Windows.Forms.Label]$labelAPIKey = $null
[System.Windows.Forms.Label]$labelLocation = $null
[System.Windows.Forms.ComboBox]$comboboxLocation = $null
[System.Windows.Forms.TextBox]$textboxAPIKey = $null
[System.Windows.Forms.Button]$buttonRequestAPIKey = $null
[System.Windows.Forms.Button]$buttonSave = $null
[System.Windows.Forms.Button]$buttonCancel = $null
$labelAPIKey = ( New-Object -TypeName System.Windows.Forms.Label )
$labelLocation = ( New-Object -TypeName System.Windows.Forms.Label )
$comboboxLocation = ( New-Object -TypeName System.Windows.Forms.ComboBox )
$textboxAPIKey = ( New-Object -TypeName System.Windows.Forms.TextBox )
$buttonRequestAPIKey = ( New-Object -TypeName System.Windows.Forms.Button )
$buttonSave = ( New-Object -TypeName System.Windows.Forms.Button )
$buttonCancel = ( New-Object -TypeName System.Windows.Forms.Button )
$FormWeerliveConfig.SuspendLayout( )
#
#labelAPIKey
#
$labelAPIKey.AutoSize = $true
$labelAPIKey.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]25, [System.Int32]29 ) )
$labelAPIKey.Name = [System.String]'labelAPIKey'
$labelAPIKey.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]114, [System.Int32]17 ) )
$labelAPIKey.TabIndex = [System.Int32]5
$labelAPIKey.Text = [System.String]'Weerlive API key'
#
#labelLocation
#
$labelLocation.AutoSize = $true
$labelLocation.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]25, [System.Int32]73 ) )
$labelLocation.Name = [System.String]'labelLocation'
$labelLocation.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]82, [System.Int32]17 ) )
$labelLocation.TabIndex = [System.Int32]6
$labelLocation.Text = [System.String]'Plaatsnaam'
#
#comboboxLocation
#
$comboboxLocation.FormattingEnabled = $true
$comboboxLocation.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]157, [System.Int32]70 ) )
$comboboxLocation.Name = [System.String]'comboboxLocation'
$comboboxLocation.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]121, [System.Int32]24 ) )
$comboboxLocation.TabIndex = [System.Int32]1
$comboboxLocation.DisplayMember = 'Value'
$comboboxLocation.ValueMember = 'Value'
$comboboxLocation.Datasource = ( Get-Locations )
$comboboxLocation.SelectedValue = $Location
#
#textboxAPIKey
#
$textboxAPIKey.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]157, [System.Int32]29 ) )
$textboxAPIKey.Name = [System.String]'textboxAPIKey'
$textboxAPIKey.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]121, [System.Int32]23 ) )
$textboxAPIKey.TabIndex = [System.Int32]0
$textboxAPIKey.Text = $APIKey
#
#buttonRequestAPIKey
#
$buttonRequestAPIKey.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]303, [System.Int32]25 ) )
$buttonRequestAPIKey.Name = [System.String]'buttonRequestAPIKey'
$buttonRequestAPIKey.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]140, [System.Int32]30 ) )
$buttonRequestAPIKey.TabIndex = [System.Int32]4
$buttonRequestAPIKey.Text = [System.String]'Nieuwe aanvragen'
$buttonRequestAPIKey.UseVisualStyleBackColor = $true
$buttonRequestAPIKey.add_Click( $buttonRequestAPIKey_Click )
#
#buttonSave
#
$buttonSave.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]28, [System.Int32]131 ) )
$buttonSave.Name = [System.String]'buttonSave'
$buttonSave.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]140, [System.Int32]30 ) )
$buttonSave.TabIndex = [System.Int32]2
$buttonSave.Text = [System.String]'Opslaan'
$buttonSave.DialogResult = [System.Windows.Forms.DialogResult]::OK
$buttonSave.UseVisualStyleBackColor = $true
$buttonSave.add_Click( $buttonSave_Click )
#
#buttonCancel
#
$buttonCancel.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]303, [System.Int32]131 ) )
$buttonCancel.Name = [System.String]'buttonCancel'
$buttonCancel.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]140, [System.Int32]30 ) )
$buttonCancel.TabIndex = [System.Int32]3
$buttonCancel.Text = [System.String]'Annuleren'
$buttonCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$buttonCancel.UseVisualStyleBackColor = $true
$buttonCancel.add_Click( $buttonCancel_Click )
#
#FormWeerliveConfig
#
$FormWeerliveConfig.ClientSize = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]485, [System.Int32]200 ) )
$FormWeerliveConfig.Controls.Add( $buttonCancel )
$FormWeerliveConfig.Controls.Add( $buttonSave )
$FormWeerliveConfig.Controls.Add( $buttonRequestAPIKey )
$FormWeerliveConfig.Controls.Add( $textboxAPIKey )
$FormWeerliveConfig.Controls.Add( $comboboxLocation )
$FormWeerliveConfig.Controls.Add( $labelLocation )
$FormWeerliveConfig.Controls.Add( $labelAPIKey )
$FormWeerliveConfig.Font = ( New-Object -TypeName System.Drawing.Font -ArgumentList @( [System.String]'Microsoft Sans Serif', [System.Single]10, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0 ) ) )
$FormWeerliveConfig.Name = [System.String]'FormWeerliveConfig'
$FormWeerliveConfig.Text = [System.String]'Weerlive configuratie'
$FormWeerliveConfig.AcceptButton = $buttonSave
$FormWeerliveConfig.CancelButton = $buttonCancel
$FormWeerliveConfig.add_KeyUp( $FormWeerliveConfig_KeyUp )
#$FormWeerliveConfig.add_Shown( $FormWeerliveConfig_Shown )
$FormWeerliveConfig.add_Shown( { $comboboxLocation.SelectedItem = "$Location" } )
$FormWeerliveConfig.ResumeLayout( $false )
$FormWeerliveConfig.PerformLayout( )
Add-Member -InputObject $FormWeerliveConfig -Name base -Value $base -MemberType NoteProperty
Add-Member -InputObject $FormWeerliveConfig -Name labelAPIKey -Value $labelAPIKey -MemberType NoteProperty
Add-Member -InputObject $FormWeerliveConfig -Name labelLocation -Value $labelLocation -MemberType NoteProperty
Add-Member -InputObject $FormWeerliveConfig -Name comboboxLocation -Value $comboboxLocation -MemberType NoteProperty
Add-Member -InputObject $FormWeerliveConfig -Name textboxAPIKey -Value $textboxAPIKey -MemberType NoteProperty
Add-Member -InputObject $FormWeerliveConfig -Name buttonRequestAPIKey -Value $buttonRequestAPIKey -MemberType NoteProperty
Add-Member -InputObject $FormWeerliveConfig -Name buttonSave -Value $buttonSave -MemberType NoteProperty
Add-Member -InputObject $FormWeerliveConfig -Name buttonCancel -Value $buttonCancel -MemberType NoteProperty
[void] $FormWeerliveConfig.ShowDialog( )
}
if ( $Edit -or !$APIKey -or !$Location ) {
Read-Config
}
if ( $Edit ) {
$locations = ( Get-Locations )
Edit-Config
}
$Global:consolestatus = 1
#######################################
# Hide console window #
# by Anthony on StackOverflow.com #
# http://stackoverflow.com/a/15079092 #
#######################################
$signature1 = @'
public static void ShowConsoleWindow( int state )
{
var handle = GetConsoleWindow( );
ShowWindow( handle, state );
}
[System.Runtime.InteropServices.DllImport( "kernel32.dll" )]
static extern IntPtr GetConsoleWindow( );
[System.Runtime.InteropServices.DllImport( "user32.dll" )]
static extern bool ShowWindow( IntPtr hWnd, int nCmdShow );
'@
# Hide console if requested
if ( $Hide ) {
$hideconsole = Add-Type -MemberDefinition $signature1 -Name Hide -Namespace HideConsole -ReferencedAssemblies System.Runtime.InteropServices -PassThru
$Global:consolestatus = 0
$hideconsole::ShowConsoleWindow( $Global:consolestatus )
}
#######################
# End of Hide console #
#######################
##################
# Event Handlers #
##################
$buttonRequestAPIKey_Click = {
$url = "http://weerlive.nl/api/toegang/index.php"
Start-Process $url
}
$buttonSave_Click = {
if ( Test-Path -Path $configfile -PathType Leaf ) {
# Backup current configuration file if it exists
$backupcfg = "$configfile.bak"
Get-Content -Path $configfile -ErrorAction SilentlyContinue | Out-File -FilePath $backupcfg -Encoding ASCII -Force
}
( "key={0}`nloc={1}`n" -f $textboxAPIKey.Text, $comboboxLocation.SelectedItem ) | Out-File -FilePath $configfile -Encoding ASCII -Force
Start-Sleep -Seconds 1
$FormWeerliveConfig.Close( )
}
$buttonCancel_Click = {
$FormWeerliveConfig.Close( )
}
$Icon_Click = {
if ( $Global:consolestatus -eq 0 ) {
# Restore the hidden console window
$Global:consolestatus = 9
$hideconsole::ShowConsoleWindow( $Global:consolestatus )
} else {
# Hide console window
$Global:consolestatus = 0
$hideconsole::ShowConsoleWindow( $Global:consolestatus )
}
}
#########################
# End of Event Handlers #
#########################
function Fetch-WeerData( ) {
$url = ( 'http://weerlive.nl/api/json-data-10min.php?key={0}&locatie={1}' -f $APIKey, $Location )
if ( ( Test-Path -Path $cachefile -PathType 'Leaf' ) -and ( ( Get-Item -Path $cachefile ).LastWriteTime -gt ( Get-Date ).AddMinutes( -10 ) ) ) {
$cacheage = ( [System.Math]::Round( ( ( Get-Date ) - ( Get-Item -Path $cachefile ).LastWriteTime ).TotalMinutes ) )
# Fetch cached JSON weather data
$json = ( Get-Content -Path $cachefile )
} else {
$ProgressPreference = 'SilentlyContinue'
# Fetch live JSON weather data from Weerlive.nl
$json = ( Invoke-WebRequest -Uri $url -TimeoutSec 10 ).Content
$ProgressPreference = 'Continue'
# Cache the weather data, to prevent exceeding the maximum daily allowance of live requests
$json | Out-File -FilePath $cachefile -Force
}
$json
}
# Define properties for System Tray icon
$Global:fontfamily = [System.Drawing.FontFamily]::GenericSansSerif
$Global:fontstyle = [System.Drawing.FontStyle]::Bold
$Global:font = [System.Drawing.Font]::new( $fontfamily, 7, $fontstyle )
$Global:blackbrush = [System.Drawing.Brushes]::Black
$Global:bluebrush = [System.Drawing.Brushes]::LightBlue
$Global:brightgreenbrush = [System.Drawing.Brushes]::LightGreen
$Global:greenbrush = [System.Drawing.Brushes]::Green
$Global:orangebrush = [System.Drawing.Brushes]::Orange
$Global:redbrush = [System.Drawing.Brushes]::Red
$Global:whitebrush = [System.Drawing.Brushes]::White
$Global:yellowbrush = [System.Drawing.Brushes]::Yellow
$Global:stringformat = [System.Drawing.StringFormat]::GenericDefault
function Create-Icon( ) {
param (
[Parameter(Mandatory)]$degrees
)
$bgbrush = $Global:yellowbrush
$fgbrush = $Global:blackbrush
[int]$temperature = -273
if ( [System.Int32]::TryParse( $degrees, [ref] $temperature ) ) {
if ( $temperature -le 0 ) {
$bgbrush = $Global:bluebrush
$fgbrush = $Global:blackbrush
}
if ( ( $temperature -gt 0 ) -and ( $temperature -lt 15 ) ) {
$bgbrush = $Global:greenbrush
$fgbrush = $Global:whitebrush
}
if ( ( $temperature -ge 15 ) -and ( $temperature -le 20 ) ) {
$bgbrush = $Global:brightgreenbrush
$fgbrush = $Global:blackbrush
}
if ( ( $temperature -gt 20 ) -and ( $temperature -lt 25 ) ) {
$bgbrush = $Global:yellowbrush
$fgbrush = $Global:blackbrush
}
if ( ( $temperature -ge 25 ) -and ( $temperature -lt 30 ) ) {
$bgbrush = $Global:orangebrush
$fgbrush = $Global:blackbrush
}
if ( $temperature -ge 30 ) {
$bgbrush = $Global:redbrush
$fgbrush = $Global:whitebrush
}
}
$bitmap = [System.Drawing.Bitmap]::new( 16, 16 )
$graphic = [System.Drawing.Graphics]::FromImage( $bitmap )
$graphic.FillEllipse( $bgbrush, 0, 0, 16, 16 )
$textsize = $graphic.MeasureString( $degrees, $Global:font )
if ( $textsize.Width -gt 14 ) {
$font = [System.Drawing.Font]::new( $Global:fontfamily, $Global:font.Size * 0.8, $Global:fontstyle )
$textsize = $graphic.MeasureString( $text, $Global:font )
}
if ( $textsize.Width -lt 11.2 ) {
$font = [System.Drawing.Font]::new( $Global:fontfamily, $Global:font.Size * 1.25, $Global:fontstyle )
$textsize = $graphic.MeasureString( $degrees, $Global:font )
}
$x = [Math]::Max( 0, [Math]::Floor( ( $bitmap.Width - $textsize.Width ) / 2 ) )
$y = [Math]::Max( 0, [Math]::Ceiling( ( $bitmap.Height - $textsize.Height ) / 2 ) )
$graphic.DrawString( $degrees, $Global:font, $fgbrush, $x, $y, $Global:stringformat )
$icon = [System.Drawing.Icon]::FromHandle( $bitmap.GetHicon( ) )
$graphic.Dispose( )
$bitmap.Dispose( )
$icon
}
function Format-Text( ) {
param (
[Parameter(Mandatory = $true)]
[string]$unformatedText
)
$unformatedText = $unformatedText.Trim( )
$consoleWidth = [System.Console]::WindowWidth
$pattern = ( "^([^\s].{{1,{0}}})(\s(.*)|$)" -f ( $consoleWidth - 2 ) )
$formatedText = ""
do {
if ( [System.Text.RegularExpressions.Regex]::IsMatch( $unformatedText, $pattern ) ) {
$matches = [System.Text.RegularExpressions.Regex]::Matches( $unformatedText, $pattern )
$line = $matches[0].Groups[1].Value
$formatedText += $line + "`n"
$unformatedText = $matches[0].Groups[3].Value.Trim( )
} else {
$line = ""
}
} while ( $line )
$formatedText
}
# Create new System Tray icon
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
$notifyIcon.Visible = $true
# Add Click event handler (hides/restores console window)
$notifyIcon.Add_Click( $Icon_Click )
$abort = $false
do {
Clear-Host
$data = ( Fetch-WeerData | ConvertFrom-Json -ErrorAction Stop )
# [char]176 is the degrees symbol in codepage 437
Write-Host ( "`n[{0}]" -f ( Get-Date ).ToString( "yyyy-MM-dd HH:mm:ss" ) ) -ForegroundColor White
Write-Host ( "Locatie : {0}" -f $data.liveweer.plaats ) -ForegroundColor DarkGray
Write-Host ( "Actuele temperatuur : {0} {1}C" -f $data.liveweer.temp,[char]176 ) -ForegroundColor White
Write-Host ( "Gevoelstemperatuur : {0} {1}C" -f $data.liveweer.gtemp,[char]176 ) -ForegroundColor DarkGray
Write-Host ( "Weersgesteldheid : {0}" -f $data.liveweer.samenv ) -ForegroundColor White
Write-Host ( "Relatieve luchtvochtigheid : {0} %" -f $data.liveweer.lv ) -ForegroundColor DarkGray
Write-Host ( "Windrichting : {0}" -f $data.liveweer.windr ) -ForegroundColor White
Write-Host ( "Windsnelheid : {0} m/s" -f $data.liveweer.windms ) -ForegroundColor DarkGray
Write-Host ( "Windkracht : {0} Bft" -f $data.liveweer.winds ) -ForegroundColor White
Write-Host ( "Windsnelheid : {0} knopen" -f $data.liveweer.windk ) -ForegroundColor DarkGray
Write-Host ( "Windsnelheid : {0} km/h" -f $data.liveweer.windkmh ) -ForegroundColor White
Write-Host ( "Luchtdruk : {0} hPa" -f $data.liveweer.luchtd ) -ForegroundColor DarkGray
Write-Host ( "Luchtdruk : {0} mm Hg" -f $data.liveweer.ldmmhg ) -ForegroundColor White
Write-Host ( "Dauwpunt : {0} {1}C" -f $data.liveweer.dauwp,[char]176 ) -ForegroundColor DarkGray
Write-Host ( "Zicht : {0} km" -f $data.liveweer.zicht ) -ForegroundColor White
Write-Host ( "Korte dagverwachting : {0}" -f $data.liveweer.verw ) -ForegroundColor DarkGray
Write-Host ( "zon op : {0}" -f $data.liveweer.sup ) -ForegroundColor White
Write-Host ( "zon onder : {0}" -f $data.liveweer.sunder ) -ForegroundColor DarkGray
#Write-Host ( "afbeeldingsnaam: {0}" -f $data.liveweer.image )
#Write-Host ( "Weericoon vandaag: {0}" -f $data.liveweer.d0weer )
Write-Host ( "Max temp vandaag : {0} {1}C" -f $data.liveweer.d0tmax,[char]176 ) -ForegroundColor White
Write-Host ( "Min temp vandaag : {0} {1}C" -f $data.liveweer.d0tmin,[char]176 ) -ForegroundColor DarkGray
Write-Host ( "Windkracht vandaag : {0} Bft" -f $data.liveweer.d0windk ) -ForegroundColor White
Write-Host ( "Windrichting vandaag : {0}" -f $data.liveweer.d0windr ) -ForegroundColor DarkGray
Write-Host ( "Neerslagkans vandaag : {0} %" -f $data.liveweer.d0neerslag ) -ForegroundColor White
Write-Host ( "Zonkans vandaag : {0} %" -f $data.liveweer.d0zon ) -ForegroundColor DarkGray
#Write-Host ( "Weericoon morgen: {0}" -f $data.liveweer.d1weer )
Write-Host ( "Max temp morgen : {0} {1}C" -f $data.liveweer.d1tmax,[char]176 ) -ForegroundColor White
Write-Host ( "Min temp morgen : {0} {1}C" -f $data.liveweer.d1tmin,[char]176 ) -ForegroundColor DarkGray
Write-Host ( "Windkracht morgen : {0} Bft" -f $data.liveweer.d1windk ) -ForegroundColor White
Write-Host ( "Windrichting morgen : {0}" -f $data.liveweer.d1windr ) -ForegroundColor DarkGray
Write-Host ( "Neerslagkans morgen : {0} %" -f $data.liveweer.d1neerslag ) -ForegroundColor White
Write-Host ( "Zonkans morgen : {0} %" -f $data.liveweer.d1zon ) -ForegroundColor DarkGray
Write-Host ( "Max temp overmorgen : {0} {1}C" -f $data.liveweer.d2tmax,[char]176 ) -ForegroundColor White
Write-Host ( "Min temp overmorgen : {0} {1}C" -f $data.liveweer.d2tmin,[char]176 ) -ForegroundColor DarkGray
#Write-Host ( "Weericoon overmorgen: {0}" -f $data.liveweer.d2weer )
Write-Host ( "Windkracht overmorgen : {0} Bft" -f $data.liveweer.d2windk ) -ForegroundColor White
Write-Host ( "Windrichting overmorgen : {0}" -f $data.liveweer.d2windr ) -ForegroundColor DarkGray
Write-Host ( "Neerslagkans overmorgen : {0} %" -f $data.liveweer.d2neerslag ) -ForegroundColor White
Write-Host ( "Zonkans overmorgen : {0} %" -f $data.liveweer.d2zon ) -ForegroundColor DarkGray
if ( $data.liveweer.alarm -eq 0 ) {
Write-Host ( Format-Text "Weerwaarschuwing voor de regio : Nee" ) -ForegroundColor Green
} else {
Write-Host ( Format-Text ( "Weerwaarschuwing voor de regio : {0}" -f $data.liveweer.alarmtxt ) ) -ForegroundColor Red
}
$temp = [System.Math]::Round( $data.liveweer.temp )
$notifyIcon.Icon = ( Create-Icon ( $temp ) )
# Tooltip text cannot exceed 64 characters, hence the Substring
$tooltiptext = ( "Temperatuur: {0}`nGevoelstemp: {1}`n`nKlik: toon/verberg details" -f $data.liveweer.temp, $data.liveweer.gtemp )
$notifyIcon.Text = $tooltiptext.Substring( 0, [System.Math]::Min( 63, $tooltiptext.Length ) )
Write-Host "`n`nKlik op het icoon in de `"System Tray`" om dit venster te verbergen of weer te herstellen."
# Show how to break free from the endless loop
Write-Host "`nDruk op een willekeurige toets om de temperatuurweergave te stoppen . . . " -NoNewline
# Loop until a key is pressed in this console
$loopcount = 0
:WaitForKey while ( $true ) {
$loopcount += 1
if ( $Host.UI.RawUI.KeyAvailable ) {
$abort = $true
break WaitForKey
} else {
if ( $loopcount -gt 299 ) {
break WaitForKey
} else {
Start-Sleep -Seconds 1
}
}
}
} until ( $abort )
# Remove the key from the keyboard buffer . . .
[void] $Host.UI.RawUI.ReadKey( )
# . . . and wipe it from the screen
Write-Host "`b `n"
# Cleanup
$notifyIcon.Visible = $False
$notifyIcon.Dispose( )
page last modified: 2024-04-16; loaded in 0.0141 seconds