Files
gfx-enocean/modules/XmlParser.psm1
Charles bd4fde8190 Correction README.md
Ajout modification du fichier Project.gfx
2026-03-05 10:36:52 +01:00

131 lines
3.1 KiB
PowerShell

# Module XmlParser - Parse et generation XML Enocean
function Get-DeviceListFromJson {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$JsonContent
)
$json = $JsonContent | ConvertFrom-Json
$devices = @()
if ($json.files) {
foreach ($file in $json.files) {
$devices += @{
Name = $file.path.name
Href = $file.path.href
}
}
}
return , $devices
}
function Parse-EnoceanDeviceXml {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$XmlContent
)
[xml]$xml = $XmlContent
$config = $xml.EnOceanDevice.Configuration
return @{
ResourceNumber = [int]$config.ResourceNumber
DeviceId = $config.DeviceId
DeviceType = $config.DeviceType
}
}
function New-EnoceanDeviceXml {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[int]$ResourceNumber,
[Parameter(Mandatory)]
[string]$DeviceId,
[Parameter(Mandatory)]
[string]$DeviceType
)
$xml = @"
<?xml version="1.0" encoding="utf-8"?>
<EnOceanDevice>
<Configuration>
<ResourceType>EnOceanDevice</ResourceType>
<ResourceNumber>$ResourceNumber</ResourceNumber>
<Name>EnOcean Device $ResourceNumber</Name>
<DeviceId>$DeviceId</DeviceId>
<DeviceType>$DeviceType</DeviceType>
<MaxReceiveTime>2400</MaxReceiveTime>
<Points></Points>
<Description></Description>
</Configuration>
</EnOceanDevice>
"@
return $xml
}
function Update-EnoceanDeviceId {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$XmlContent,
[Parameter(Mandatory)]
[string]$NewDeviceId
)
# Parser pour extraire les valeurs actuelles
[xml]$xml = $XmlContent
$config = $xml.EnOceanDevice.Configuration
$oldDeviceId = $config.DeviceId
$deviceType = $config.DeviceType
# Remplacement par regex sur le XML brut (preserve l'encodage et le formatage original)
$modifiedXml = $XmlContent -replace "<DeviceId>[^<]*</DeviceId>", "<DeviceId>$NewDeviceId</DeviceId>"
return @{
ModifiedXml = $modifiedXml
OldDeviceId = $oldDeviceId
DeviceType = $deviceType
}
}
function Update-GfxDeviceIds {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$MainXmlContent,
[Parameter(Mandatory)]
[hashtable]$DeviceIdMap # @{ "ancien_id" = "nouveau_id" }
)
$modifiedXml = $MainXmlContent
$replaceCount = 0
foreach ($oldId in $DeviceIdMap.Keys) {
$newId = $DeviceIdMap[$oldId]
$pattern = "<DeviceId>$oldId</DeviceId>"
$replacement = "<DeviceId>$newId</DeviceId>"
if ($modifiedXml -match [regex]::Escape($pattern)) {
$modifiedXml = $modifiedXml -replace [regex]::Escape($pattern), $replacement
$replaceCount++
}
}
return @{
ModifiedXml = $modifiedXml
ReplaceCount = $replaceCount
}
}
Export-ModuleMember -Function Get-DeviceListFromJson, Parse-EnoceanDeviceXml, New-EnoceanDeviceXml, Update-EnoceanDeviceId, Update-GfxDeviceIds