Untitled

 avatar
unknown
powershell
7 months ago
1.3 kB
34
No Index
function Get-Apower {
    $url = "http://192.168.1.3/rpc/shelly.getstatus"
    
    try {
        $response = Invoke-RestMethod -Uri $url -ErrorAction Stop
        $apower = $response.'pm1:0'.apower
        
        if ($null -ne $apower) {
            return [math]::Round($apower, 2)
        }
        return $null
    }
    catch [System.Net.WebException] {
        Write-Host "Network error: $($_.Exception.Message)" -ForegroundColor Red
        return $null
    }
    catch {
        Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
        return $null
    }
}

# Path to log file on desktop
$logPath = Join-Path -Path $env:USERPROFILE -ChildPath "Desktop\power_log.txt"

while ($true) {
    $watts = Get-Apower
    if ($null -ne $watts) {
        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        $logEntry = "[$timestamp] Current power: $watts W"
        
        # Write to screen
        Write-Host $logEntry
        
        # Append to log file
        try {
            Add-Content -Path $logPath -Value $logEntry -ErrorAction Stop
        }
        catch {
            Write-Host "Failed to write to log file: $($_.Exception.Message)" -ForegroundColor Red
        }
    }
    Start-Sleep -Seconds 10
}
Editor is loading...