Implémentation CLI PowerShell Enocean Eclypse
This commit is contained in:
146
modules/ApiClient.psm1
Normal file
146
modules/ApiClient.psm1
Normal file
@@ -0,0 +1,146 @@
|
||||
# Module ApiClient - Communication REST API Eclypse
|
||||
|
||||
function Initialize-ApiClient {
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
# Forcer TLS 1.2
|
||||
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||
|
||||
# Desactiver la verification SSL (certificats auto-signes)
|
||||
if (-not ([System.Management.Automation.PSTypeName]'TrustAllCertsPolicy').Type) {
|
||||
Add-Type @"
|
||||
using System.Net;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
public class TrustAllCertsPolicy : ICertificatePolicy {
|
||||
public bool CheckValidationResult(
|
||||
ServicePoint srvPoint, X509Certificate certificate,
|
||||
WebRequest request, int certificateProblem) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
"@
|
||||
}
|
||||
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
|
||||
|
||||
Write-Log -Message "ApiClient initialise (TLS 1.2, SSL bypass)" -Level INFO
|
||||
}
|
||||
|
||||
function Get-AuthHeader {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Username,
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[AllowEmptyString()]
|
||||
[string]$Password
|
||||
)
|
||||
|
||||
$pair = "${Username}:${Password}"
|
||||
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
|
||||
$base64 = [System.Convert]::ToBase64String($bytes)
|
||||
|
||||
return @{
|
||||
"Authorization" = "Basic $base64"
|
||||
"Accept" = "application/json, text/json, text/x-json, text/javascript, application/xml, text/xml, text/plain"
|
||||
"User-Agent" = "EC-gfxProgram/7.9.26006.1 (DC_API:v1; Win32NT 6.2)"
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-EnoceanGet {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory)]
|
||||
[string]$BaseUrl,
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[string]$ApiBasePath,
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[string]$ResourcePath,
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Username,
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[AllowEmptyString()]
|
||||
[string]$Password
|
||||
)
|
||||
|
||||
$headers = Get-AuthHeader -Username $Username -Password $Password
|
||||
$url = "$BaseUrl$ApiBasePath/$($ResourcePath.TrimStart('/'))"
|
||||
|
||||
Write-Log -Message "GET $url" -Level INFO
|
||||
|
||||
$response = Invoke-WebRequest -Uri $url -Method GET -Headers $headers -UseBasicParsing -TimeoutSec 30
|
||||
|
||||
# Si la reponse est un byte[] (encode=bin), decoder en string UTF-8
|
||||
if ($response.Content -is [byte[]]) {
|
||||
return [System.Text.Encoding]::UTF8.GetString($response.Content)
|
||||
}
|
||||
return $response.Content
|
||||
}
|
||||
|
||||
function Send-EnoceanConfig {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory)]
|
||||
[string]$BaseUrl,
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[string]$ApiBasePath,
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[byte[]]$ZipBytes,
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[string]$ZipFilename,
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Username,
|
||||
|
||||
[Parameter(Mandatory)]
|
||||
[AllowEmptyString()]
|
||||
[string]$Password
|
||||
)
|
||||
|
||||
$headers = Get-AuthHeader -Username $Username -Password $Password
|
||||
$url = "$BaseUrl$ApiBasePath/files/bacnet/inputConfiguration"
|
||||
|
||||
Write-Log -Message "POST $url (fichier: $ZipFilename, taille: $($ZipBytes.Length) octets)" -Level INFO
|
||||
|
||||
# Construction multipart manuelle (compatible PS 5.1, pas de -Form)
|
||||
$boundary = [System.Guid]::NewGuid().ToString("N")
|
||||
$headers["Content-Type"] = "multipart/form-data; boundary=$boundary"
|
||||
|
||||
$encoding = [System.Text.Encoding]::ASCII
|
||||
|
||||
# Partie avant le fichier
|
||||
$headerPart = @"
|
||||
--$boundary
|
||||
Content-Disposition: form-data; name="File"; filename="$ZipFilename"
|
||||
Content-Type: application/octet-stream
|
||||
|
||||
"@
|
||||
# Partie finale
|
||||
$footerPart = "`r`n--$boundary--`r`n"
|
||||
|
||||
$headerBytes = $encoding.GetBytes($headerPart.Replace("`n", "`r`n"))
|
||||
$footerBytes = $encoding.GetBytes($footerPart)
|
||||
|
||||
# Assembler le body complet en byte[]
|
||||
$bodyStream = New-Object System.IO.MemoryStream
|
||||
$bodyStream.Write($headerBytes, 0, $headerBytes.Length)
|
||||
$bodyStream.Write($ZipBytes, 0, $ZipBytes.Length)
|
||||
$bodyStream.Write($footerBytes, 0, $footerBytes.Length)
|
||||
$bodyBytes = $bodyStream.ToArray()
|
||||
$bodyStream.Close()
|
||||
|
||||
$response = Invoke-WebRequest -Uri $url -Method POST -Headers $headers -Body $bodyBytes -UseBasicParsing -TimeoutSec 60
|
||||
|
||||
Write-Log -Message "POST reponse : $($response.StatusCode)" -Level SUCCESS
|
||||
return $response
|
||||
}
|
||||
|
||||
Export-ModuleMember -Function Initialize-ApiClient, Get-AuthHeader, Invoke-EnoceanGet, Send-EnoceanConfig
|
||||
Reference in New Issue
Block a user