This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# function adapted from https://stackoverflow.com/posts/42995301/revisions | |
Function Execute-Command ($commandTitle, $commandPath, $commandArguments) | |
{ | |
Try { | |
$pinfo = New-Object System.Diagnostics.ProcessStartInfo | |
$pinfo.FileName = $commandPath | |
$pinfo.RedirectStandardOutput = $true | |
$pinfo.UseShellExecute = $false | |
$pinfo.Arguments = $commandArguments | |
$p = New-Object System.Diagnostics.Process | |
$p.StartInfo = $pinfo | |
$p.Start() | Out-Null | |
[pscustomobject]@{ | |
commandTitle = $commandTitle | |
stdout = $p.StandardOutput.ReadToEnd() | |
} | |
$p.WaitForExit() | |
} | |
Catch { | |
exit | |
} | |
} | |
# call this with garmin username & password to sync from withings | |
function withings-sync( $username, $password ) | |
{ | |
# setup vars | |
$hoursToPause = 2 | |
$fromDate = Get-Date | |
$loop = $true | |
$countOfTries = 0 | |
$maxTries = 10 | |
$user = $username | |
$pwd = $password | |
$wsPath = "C:\python38\scripts\withings-sync.exe" | |
# discord setup | |
$discordBot = "Withings -> Garmin Sync" | |
$hookUrl = "https://discord.com/api/webhooks/xxxx" | |
$content = "{0:HH:mm:ss}: Garmin upload beginning for $user." -f ( (Get-Date) ) | |
$section = New-DiscordSection -Title "Info" -Description $content -Color LightGrey | |
# send discord update on start | |
Write-Host $content | |
Send-DiscordMessage -webHookUrl $hookUrl -Sections $section -AvatarName $discordBot | |
do | |
{ | |
# only retry $maxTries times | |
if($countOfTries -lt $maxTries) | |
{ | |
# create args, then try withings-sync upload and capture output | |
$args = ("--garmin-username ${user} --garmin-password ${pwd} --fromdate {0:yyyy-MM-dd}" -f $fromDate).ToString() | |
$process = Execute-Command -commandTitle "Withings->Garmin Sync" -commandPath $wsPath -commandArguments $args | |
$outputText = $process.stdout | |
Write-Host $outputText | |
# if upload worked, break loops | |
if( $outputText.Contains("Fit file uploaded to Garmin Connect") ) | |
{ | |
$content = "{0:HH:mm:ss}: Garmin upload successful for $user." -f ( (Get-Date) ) | |
$section = New-DiscordSection -Title "Info" -Description $content -Color Green | |
$loop = $false | |
} | |
# if no measurements, wait and try again later | |
elseif( $outputText.Contains("No measurements to upload for date or period specified") ) | |
{ | |
$content = "{0:HH:mm:ss}: No Withings measurements found for $user; sleeping $hoursToPause hour(s) and trying again." -f ( (Get-Date) ) | |
$section = New-DiscordSection -Title "Alert" -Description $content -Color Yellow | |
} | |
# if withings refresh failed | |
elseif( $outputText.Contains("withings - ERROR") ) | |
{ | |
$content = "{0:HH:mm:ss}: Withings refresh failed for $user; sleeping $hoursToPause hour(s) and trying again." -f ( (Get-Date) ) | |
$section = New-DiscordSection -Title "Alert" -Description $content -Color Red | |
} | |
# if garmin upload failed | |
else | |
{ | |
$content = "{0:HH:mm:ss}: Garmin upload failed for $user; sleeping $hoursToPause hour(s) and trying again." -f ( (Get-Date) ) | |
$section = New-DiscordSection -Title "Alert" -Description $content -Color Red | |
} | |
# send discord update | |
Write-Host $content | |
Send-DiscordMessage -webHookUrl $hookUrl -Sections $section -AvatarName $discordBot | |
# pause for $countOfTries hour(s) and try again | |
$countOfTries = $countOfTries + 1 | |
if($loop) { Start-Sleep ( $hoursToPause * 60 * 60 ) } | |
} | |
# after #maxTries failures, send discord update and exit the loop | |
else | |
{ | |
$content = "{0:HH:mm:ss}: Garmin upload failed $maxTries times, aborting." -f ( (Get-Date) ) | |
$section = New-DiscordSection -Title "Alert" -Description $content -Color Red | |
Write-Host $content | |
Send-DiscordMessage -webHookUrl $hookUrl -Sections $section -AvatarName $discordBot | |
$loop = $false | |
} | |
} | |
while ($loop -eq $true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment