-
Notifications
You must be signed in to change notification settings - Fork 1
/
fhemcl.ps1
73 lines (69 loc) · 2.89 KB
/
fhemcl.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<#
.SYNOPSIS
This Script is a FHEM Client for HTTP
.DESCRIPTION
FHEM commands could given over the Pipe, Arguments or File.
.EXAMPLE
fhemcl [http://[<user:password>@]<hostName>:]<portNummer> "FHEM command1" "FHEM command2"
fhemcl [http://[<user:password>@]<hostName>:]<portNummer> filename
echo "FHEM command"|fhemcl [http://[<user:password>@]<hostName>:]<portNummer>
.NOTES
put every FHEM command line in ""
#>
#region Params
param(
[Parameter(Mandatory=$true,Position=0,HelpMessage="-first 'Portnumber or URL'")]
[String]$first,
[Parameter(ValueFromPipeline=$true,ValueFromRemainingArguments=$true)]
[String[]]$sec
)
#endregion
# if only one element the use as portNumber
# or use as hosturl
$arr = $first -split ':'
if ($arr.Length -eq 1){
if ($first -match '^\d+$') {$hosturl="http://localhost:$first"}
else {
write-output "is not a Portnumber"
exit
}
} else {$hosturl=$first}
# url contains usernam@password?
if ($arr.Length -eq 4){
$username = $($arr[1] -split'//')[1]
$password = $($arr[2] -split '@')[0]
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$headers = @{
Authorization=("Basic {0}" -f $base64AuthInfo)
}
# cut the account from hosturl
$hosturl=$arr[0] + "://"+$($arr[2] -split '@')[1] +":" + $arr[3]
}
# get Token
$token = Invoke-WebRequest -UseBasicParsing -Headers $headers -Uri "$hosturl/fhem?XHR=1" | %{$_.Headers["X-FHEM-csrfToken"]}
# reading commands from Pipe, File or Arguments
# clear cmdarray and save the Pipeline,
# $input contains all lines from pipeline, $sec contains the last line
$cmdarray=@()
foreach ($cmd2 in $input){$cmdarray += $cmd2}
if ($cmdarray.length -eq 0) {
if((Test-Path $sec) -And ($sec.Length -eq 1)) {$cmdarray = Get-Content $sec}
else {foreach ($cmd2 in $sec){$cmdarray += $cmd2}}
}
# send all commands to FHEM
# there is still an error message with Basic Auth and commands like set Aktor01 .. e.g. list is without any error.
for ($i=0; $i -lt $cmdarray.Length; $i++) {
# concat def lines with ending \ to the next line
$cmd = $cmdarray[$i]
while($cmd.EndsWith('\')) {$cmd=$cmd.Remove($cmd.Length - 1,1) + "`n" + $cmdarray[$i+1];$i++}
write-verbose "proceeding line $($i+1) : $cmd"
# url encode
$cmd=[System.Uri]::EscapeDataString($cmd)
#Without HTML
$web = Invoke-WebRequest -Uri "$hosturl/fhem?cmd=$cmd&fwcsrf=$token&XHR=1" -Headers $headers -UseBasicParsing
write-verbose "--------------- Weboutput with Response - Content is shortened --"
write-verbose $web
# remove the last character (normally an additional 0A \r) from the response if there
# works eventually with $web.content.TrimEnd("`r")
if ($web.content.length -gt 0) {$web.content.Substring(0,$web.content.Length -1)}
}