diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..08d6d6d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,101 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +# Docker image file that describes an Ubuntu20.04 image with PowerShell installed from Microsoft APT Repo +ARG hostRegistry=psdockercache.azurecr.io +FROM ${hostRegistry}/ubuntu:24.04 AS installer-env + +# Define Args for the needed to add the package +ARG PS_VERSION=7.5.0-preview.5 +ARG PS_PACKAGE=powershell-preview_${PS_VERSION}-1.deb_amd64.deb +ARG PS_PACKAGE_URL=https://github.com/PowerShell/PowerShell/releases/download/v${PS_VERSION}/${PS_PACKAGE} +ARG PS_INSTALL_VERSION=7-preview + +RUN --mount=type=cache,target=/var/lib/apt \ + --mount=type=cache,target=/var/cache/apt \ + apt-get update \ + && apt-get install --no-install-recommends -y \ + # curl is required to grab the Linux package + curl \ + # less is required for help in powershell + less \ + # requied to setup the locale + locales \ + # required for SSL + ca-certificates \ + # Download the Linux package and save it + && echo ${PS_PACKAGE_URL} \ + && curl -sSL ${PS_PACKAGE_URL} -o /tmp/powershell.deb + +# Install the deb file in this image and make powershell available +ARG hostRegistry=psdockercache.azurecr.io +FROM ${hostRegistry}/ubuntu:24.04 AS final-image + +# # Define args needed to add the package +ARG PS_VERSION=7.5.0-preview.5 +ARG PS_PACKAGE=powershell-preview_${PS_VERSION}-1.deb_amd64.deb +ARG PS_PACKAGE_URL=https://github.com/PowerShell/PowerShell/releases/download/v${PS_VERSION}/${PS_PACKAGE} +ARG PS_INSTALL_VERSION=7-preview + +# Define ENVs for Localization/Globalization +ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false \ + LC_ALL=en_US.UTF-8 \ + LANG=en_US.UTF-8 \ + PS_INSTALL_FOLDER=/opt/microsoft/powershell/$PS_INSTALL_VERSION \ + # set a fixed location for the Module analysis cache + PSModuleAnalysisCachePath=/var/cache/microsoft/powershell/PSModuleAnalysisCache/ModuleAnalysisCache \ + POWERSHELL_DISTRIBUTION_CHANNEL=PSDocker-Ubuntu-24.04 + +# Install dependencies and clean up +RUN --mount=from=installer-env,target=/mnt/pwsh,source=/tmp \ + --mount=type=cache,target=/var/lib/apt \ + --mount=type=cache,target=/var/cache/apt \ + apt-get update \ + && apt-get install --no-install-recommends -y /mnt/pwsh/powershell.deb \ + && apt-get install --no-install-recommends -y \ + # less is required for help in powershell + less \ + # requied to setup the locale + locales \ + # required for SSL + ca-certificates \ + gss-ntlmssp \ + libicu74 \ + libssl3 \ + libc6 \ + libgcc1 \ + libgssapi-krb5-2 \ + liblttng-ust1 \ + libstdc++6 \ + zlib1g \ + # PowerShell remoting over SSH dependencies + openssh-client \ + && apt-get dist-upgrade -y \ + && locale-gen $LANG && update-locale \ + && export POWERSHELL_TELEMETRY_OPTOUT=1 \ + # Give all user execute permissions and remove write permissions for others + && chmod a+x,o-w ${PS_INSTALL_FOLDER}/pwsh \ + # Create the pwsh symbolic link that points to powershell + && ln -sf ${PS_INSTALL_FOLDER}/pwsh /usr/bin/pwsh \ + # Create the pwsh-preview symbolic link that points to powershell + && ln -sf ${PS_INSTALL_FOLDER}/pwsh /usr/bin/pwsh-preview \ + && pwsh \ + -NoLogo \ + -NoProfile \ + -Command " \ + \$ErrorActionPreference = 'Stop' ; \ + \$ProgressPreference = 'SilentlyContinue' ; \ + while(!(Test-Path -Path \$env:PSModuleAnalysisCachePath)) { \ + Write-Host "'Waiting for $env:PSModuleAnalysisCachePath'" ; \ + Start-Sleep -Seconds 6 ; \ + }" + +# Use PowerShell as the default shell +# Use array to avoid Docker prepending /bin/sh -c + + +WORKDIR /volvo4evcc + +COPY / . + +CMD [ "pwsh-preview" ] \ No newline at end of file diff --git a/Volvo4evcc.psd1 b/Volvo4evcc.psd1 index c5449ae..f289942 100644 --- a/Volvo4evcc.psd1 +++ b/Volvo4evcc.psd1 @@ -4,7 +4,7 @@ RootModule = 'Volvo4evcc.psm1' # Version number of this module. - ModuleVersion = '1.5' + ModuleVersion = '1.8.1' # ID used to uniquely identify this module GUID = '50047ffd-8482-4a42-94f3-52bbf7515d93' diff --git a/Volvo4evcc.psm1 b/Volvo4evcc.psm1 index b26a851..ca9e08d 100644 --- a/Volvo4evcc.psm1 +++ b/Volvo4evcc.psm1 @@ -10,5 +10,5 @@ foreach ($File in (Get-ChildItem "$PSScriptRoot\functions" -Recurse -Filter *.ps foreach ($File in (Get-ChildItem "$PSScriptRoot\internal" -Recurse -Filter *.ps1)) { . $File.FullName -} +} diff --git a/functions/functions.ps1 b/functions/functions.ps1 index 5fcbbb2..58d5766 100644 --- a/functions/functions.ps1 +++ b/functions/functions.ps1 @@ -140,10 +140,12 @@ Function Start-Volvo4Evcc $Token = Confirm-VolvoAuthentication #Wrap in loop based on evcc data - $Seconds = 60 - $RunCount = 0 + $Seconds = 15 + [Int64]$RunCount = 0 do { + #Clean itterative variables + #Increase run count $RunCount++ @@ -166,19 +168,20 @@ Function Start-Volvo4Evcc } #Get EvccData + #If multiple loadpoints Array returns all loadpoints. Testing for true means if any is true it will run. $EvccData = Get-EvccData $MessageDone = $False If ($True -eq $EvccData.SourceOk){ #Get Volvo data 2 times slower than every poll - If ($true -eq $EvccData.Connected -and $true -eq $EvccData.Charging -and ($RunCount%2) -eq 0){ + If ($true -eq $EvccData.Connected -and $true -eq $EvccData.Charging -and ($RunCount%8) -eq 0){ Write-LogEntry -Severity 0 -Message 'Connected - charging - Fast refresh of volvo SOC data' $MessageDone = $True Watch-VolvoCar -Token $Token } #Get Volvo data 5 times slower than every poll - If ($true -eq $EvccData.Connected -and $false -eq $EvccData.Charging -and ($RunCount%5) -eq 0){ + If ($true -eq $EvccData.Connected -and $false -eq $EvccData.Charging -and ($RunCount%20) -eq 0){ #Also cycle web service Write-LogEntry -Severity 0 -Message 'Connected - Not charging - Slow refresh of volvo SOC data' $MessageDone = $True @@ -186,13 +189,13 @@ Function Start-Volvo4Evcc } #Get weather forecast and set MinSOC if needed - If ($true -eq $Global:Config.'Weather.Enabled' -and ($RunCount%60) -eq 0){ + If ($true -eq $Global:Config.'Weather.Enabled' -and ($RunCount%240) -eq 0){ Update-SunHours } #Get Volvo data 5 times slower than every poll - If ($false -eq $EvccData.Connected -and ($RunCount%60) -eq 0){ + If ($false -eq $EvccData.Connected -and ($RunCount%240) -eq 0){ Write-LogEntry -Severity 0 -Message 'Not Connected - Super Slow Refresh of volvo SOC data - once every hour' $MessageDone = $True @@ -210,6 +213,17 @@ Function Start-Volvo4Evcc $MessageDone = $True Watch-VolvoCar -Token $Token } + + + $EmergencyUpdateCompare = Compare-Object -ReferenceObject $LastPulseEvccData -DifferenceObject $EvccData.Connected + If ($EmergencyUpdateCompare.SideIndicator -contains "=>" -or $EmergencyUpdateCompare -contains "<="){ + #If there is a differance in connection state do a emergency update without waiting for pull + Write-LogEntry -Severity 0 -Message "Emergency Push due to connection dif was:$LastPulseEvccData - now is:$($EvccData.Connected)" + $MessageDone = $True + Watch-VolvoCar -Token $Token + } + + }else{ Write-LogEntry -Severity 1 -Message 'Evcc data not found or not reachable' @@ -221,8 +235,10 @@ Function Start-Volvo4Evcc Write-LogEntry -Severity 0 -Message "Just a Evcc pull and token test no action taken - Token valid for another : $ValidFor minutes" } + #Save last run + $LastPulseEvccData = $EvccData.Connected Start-Sleep -Seconds $Seconds }while ($True) -} \ No newline at end of file +} \ No newline at end of file diff --git a/internal/internal.ps1 b/internal/internal.ps1 index 8663b84..b3a3ddb 100644 --- a/internal/internal.ps1 +++ b/internal/internal.ps1 @@ -883,7 +883,7 @@ Function Update-SunHours $MinSocValue = $Global:Config.'Weather.SunHoursMinsocMedium' } - $ResultSetNewMinSoc = Invoke-RestMethod -Uri "$($Global:Config.'Url.Evcc')/api/vehicles/$($TargetVehicle.Name)/minsoc/$MinSocValue)" -Method Post + $ResultSetNewMinSoc = Invoke-RestMethod -Uri "$($Global:Config.'Url.Evcc')/api/vehicles/$($TargetVehicle.Name)/minsoc/$MinSocValue" -Method Post }elseif(($TotalSunHours / $Global:Config.'Weather.SunHoursDaysDevider') -lt $Global:Config.'Weather.SunHoursMedium'){ Write-LogEntry -Severity 0 -Message "Weather - Not enough sun" diff --git a/start.ps1 b/start.ps1 index 2018545..8cfcacd 100644 --- a/start.ps1 +++ b/start.ps1 @@ -1,14 +1,14 @@ #Load the module -Import-Module "$($pwd.path)/volvo4evcc/Volvo4evcc.psd1" +Import-Module "$($pwd.path)/Volvo4evcc/Volvo4evcc.psd1" Import-Module "DnsClient-PS" #Kill any running process that is the same If ($PSVersionTable.Platform -like "Unix*"){ - Get-Process 'pwsh' | Where-Object -FilterScript {$_.Commandline -like "*volvo4evcc/start.ps1" -and $_.id -ne $pid } | Stop-Process -Force + Get-Process 'pwsh' | Where-Object -FilterScript {$_.Commandline -like "*Volvo4evcc/start.ps1" -and $_.id -ne $pid } | Stop-Process -Force } If ($PSVersionTable.Platform -like "Win*"){ - Get-Process 'pwsh' | Where-Object -FilterScript {$_.Commandline -like "*volvo4evcc/start.ps1" -and $_.id -ne $pid } | Stop-Process -Force + Get-Process 'pwsh' | Where-Object -FilterScript {$_.Commandline -like "*Volvo4evcc/start.ps1" -and $_.id -ne $pid } | Stop-Process -Force } Start-Volvo4Evcc