<# Version 0.6 Added function Save-FileData this will allow you to save all requests to file. Version 0.5 Written by Markus Jakobsson www.Corepoint.se This script should run on an X64 computer with Telldus, it will create a web interface that allows you to turn devices on or off from cellphones or computers on the same network Change the port to a port you like Change to the ip address to the address on your Telldus server Add an firewall exception to allow the desired port. Run this script from Powershell ISE Access the page from http://”serverip”:”port” The web engine are rewritten but originally created and described in this book by Bruce Payette’s, "PowerShell in action", this was the book I learned Powershell from! Read it if you are interested in learning more about Powershell #> param ($port=86,$serverIP="172.20.0.25") $DebugPreference = "continue" [void][reflection.Assembly]::loadWithPartialName("system.net.sockets") # This will allow you to save all information. $LogEnabled = $true $loggpath = "D:\Test\" # Path where you want files to be saved. $Loggfilename = "Response.Log" # filename to use $loggAppend = $true # select if you want the file to be appended or not function CreateWebHead ($content,$title = "www.Corepoint - Telldus Web Engine") { #Följande funktion används för att bygga huvudet på sidan. @" $title "@ } # This part will allow you to save information to files, it will append data function Save-FileData { [cmdletbinding()] param( [string]$filepath, # path where you want the file to be saved [string]$Filename, # name of the file SensorX.log [string]$FileData # data to send into logfile ) begin{ if (!(Test-Path $filepath)){New-Item -Path $filepath -ItemType directory | Out-Null; Write-Verbose "Creaded folder $filepath"} } process{ if ($loggAppend){ Out-File -InputObject $FileData -FilePath (Join-Path -Path $filepath -ChildPath $Filename) -Append } else { Out-File -InputObject $FileData -FilePath (Join-Path -Path $filepath -ChildPath $Filename) } } } function Change-Telldusstatus { # This sets the status on the given Telldus device. [cmdletbinding()] param( [string]$givare, # device [string]$newMode # new mode ) begin {Write-Debug "[Change-Telldusstatus]`$givare = $givare, `$newMode = $newmode"} process{ try { # Verify that the registry key exist before changing mode. if(Get-ItemProperty HKLM:\SOFTWARE\Wow6432Node\Telldus\Devices\$givare){ # If the device exist the ON / Off section will run depending on the “newmode” data if ($newmode -eq "ON"){ Write-Debug "[Change-Telldusstatus] ON $givare" &"C:\Program Files (x86)\Telldus\tdtool.exe" --on $($givare) } elseif ($newMode -eq "OFF"){ Write-Debug "[Change-Telldusstatus] OFF $givare" &"C:\Program Files (x86)\Telldus\tdtool.exe" --off $($givare) } else {Write-warning "[change-telldusstatus] You written something other then ON or OFF"} Write-Debug "[Change-Telldusstatus] Changing mode on $givare to $newMode" } else { # Missing that Telldus device in registry. } } catch{ } } } function get-KorrektaTecken { param( [string]$TextAttKorrigera ) # This part will replace the Swedish characters ÄÅÖ to correct web characters $TextAttKorrigera = $TextAttKorrigera -creplace 'Å','Å' # $TextAttKorrigera = $TextAttKorrigera -creplace 'Ä','Ä' # $TextAttKorrigera = $TextAttKorrigera -creplace 'Ö','Ö' # $TextAttKorrigera = $TextAttKorrigera -creplace 'å','å' # $TextAttKorrigera = $TextAttKorrigera -creplace 'ä','ä' # $TextAttKorrigera = $TextAttKorrigera -creplace 'ö','ö' # $TextAttKorrigera } function Get-TelldusGivare { # This part will collect all devices and give them a red or green status color. begin { $givare = get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Telldus\Devices | Select-Object -ExpandProperty PSChildName | ForEach-Object { Get-ItemProperty HKLM:\SOFTWARE\Wow6432Node\Telldus\Devices\$_ | Select-Object pschildname, state, name } } process { $Telldus = foreach ($telldusenhet in $($givare | sort-object state, name)){ if ($telldusenhet.state -eq 1){$tablecolor = "#40FF00";$hreflink = "?$($telldusenhet.PSChildName)-OFF";} else {$tablecolor = "red";$hreflink = "?$($telldusenhet.PSChildName)-ON";} @" ON $($telldusenhet.name) OFF "@ } } end {$Telldus} } function CreateWebBody { # Creating the body of the webpage @"
$(get-telldusgivare)
Starta Givare / Uppdatera Stoppa
"@ } function CreateWebFotter { # Creating the bottom of the webpage @" "@ } ## This part will send web response to clients function SendResponse ($sock, $string) { if ($sock.connected) { $bytesSent = $sock.Send( [text.Encoding]::Ascii.GetBytes($string)) if ($bytesSent -eq -1) { Write-Host ("$($sock.RemoteEndPoint) - Unable to send") } else { write-host ("$($sock.RemoteEndpoint) - Sent $bytesSent Bytes" ) } } } # End Function SendResponse Function SendHeader ( [net.sockets.socket] $sock, $length, $statusCode = "200 OK", $mimeHeader = "text/html", $httpVersion = "HTTP/1.1" ) { $response = "HTTP/1.1 $statusCode?`r`nServer: " + "Localhost`r`nContent-Type: $mimeHeader`r`n" + "Accept-Ranges:bytes`r`nContent-Length : $length`r`n`r`n" SendResponse $sock $response write-host "header Sent" } # End function SendHeader # Starts the server on the given port with the given ip $server = [System.Net.Sockets.TcpListener]$port $server.start() $buffer = New-Object byte[] 1024 write-host "Server successfully started on $serverIP and $port" #Starting loop while($true) { if ($server.Pending()) { $socket = $server.AcceptSocket() } if ($socket.Connected) { write-host ("{1} - connected at {0} a clock" -f (get-date), $socket.RemoteEndPoint) [void] $socket.receive($buffer, $buffer.Length, '0') $received = [Text.Encoding]::ASCII.GetString($buffer) $received = [regex]::Split($received, "`r`n") $received = @($received -match "GET")[0] if ($received) { $expression = $received -replace "GET */" -replace 'HTTP.*$' -replace '%20',' ' Write-Debug "`$expression = $expression" # This will save all data into a deviced by you if ($LogEnabled){Save-FileData -filepath $loggpath -Filename $Loggfilename -FileData "$(get-date) - givare $expression"} if ($expression -match '\?[0-9]{1,3}-ON' -or $expression -match '\?[0-9]{1,3}-OFF') { write-debug $expression Write-Debug "Found match $($Matches[0])" Write-Debug "Expression: $expression" Change-Telldusstatus -givare $($($expression -replace "\?").Split("-")[0].trim()) -newMode $($($expression -replace "\?").Split("-")[1].trim()) Start-Sleep -Milliseconds 1000 $result = CreateWebHead $result += CreateWebBody $result += CreateWebFotter } else { $result = CreateWebHead $result += CreateWebBody $result += CreateWebFotter } ################# - sending the webpage to client... SendHeader $socket $result.Length SendResponse $socket $(get-KorrektaTecken $result) } $socket.Close() } else { Start-Sleep -Milliseconds 100 } }