forked from heinz-otto/fhemcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fhemcl.ps1
68 lines (64 loc) · 2.65 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
<#
.SYNOPSIS
This Script is a FHEM Client for HTTP
.DESCRIPTION
FHEM commands could given over the Pipe, Arguments or File.
.EXAMPLE
fhemcl [http://<hostName>:]<portNummer> "FHEM command1" "FHEM command2"
fhemcl [http://<hostName>:]<portNummer> filename
echo "FHEM command"|fhemcl [http://<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-output "proceeding line $($i+1) : $cmd"
# url encode
$cmd=[System.Uri]::EscapeDataString($cmd)
$web = Invoke-WebRequest -Uri "$hosturl/fhem?cmd=$cmd&fwcsrf=$token" -Headers $headers
if ($web.content.IndexOf("<pre>") -ne -1) {$web.content.Substring($web.content.IndexOf("<pre>"),$web.content.IndexOf("</pre>")-$web.content.IndexOf("<pre>")) -replace '<[^>]+>',''}
}