All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.seanproctor.onvifcamera.OnvifXmlParser.kt Maven / Gradle / Ivy

The newest version!
package com.seanproctor.onvifcamera

import com.seanproctor.onvifcamera.soap.*
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.serializer
import nl.adaptivity.xmlutil.ExperimentalXmlUtilApi
import nl.adaptivity.xmlutil.serialization.UnknownChildHandler
import nl.adaptivity.xmlutil.serialization.XML

@OptIn(ExperimentalXmlUtilApi::class)
private inline fun  parseSoap(input: String): T {
    val module = SerializersModule {
        polymorphic(Any::class) {
            subclass(T::class, serializer())
        }
    }

    val xml = XML(module) {
        // xmlDeclMode = XmlDeclMode.Minimal
        autoPolymorphic = true
        defaultPolicy {
            unknownChildHandler = UnknownChildHandler { _, _, _, _, _ -> emptyList() }
        }
    }
    val serializer = serializer>()

    return xml.decodeFromString(serializer, input).data
}

internal fun parseOnvifProfiles(input: String): List {
    val result = parseSoap(input)

    return result.profiles.map {
        MediaProfile(it.name, it.token, it.encoder.encoding)
    }
}

internal fun parseOnvifStreamUri(input: String): String {
    val result = parseSoap(input)
    return result.uri
}

internal fun parseOnvifSnapshotUri(input: String): String {
    val result = parseSoap(input)
    return result.uri
}

internal fun parseOnvifServices(input: String): List {
    return parseSoap(input).services
}

internal fun parseOnvifGetHostnameResponse(input: String): String? {
    return parseSoap(input).hostnameInformation.name
}

internal fun parseOnvifProbeResponse(input: String): List {
    return parseSoap(input).matches
}

internal fun parseOnvifDeviceInformation(input: String): OnvifDeviceInformation {
    val result = parseSoap(input)
    return OnvifDeviceInformation(
        manufacturer = result.manufacturer,
        model = result.model,
        firmwareVersion = result.firmwareVersion,
        serialNumber = result.serialNumber,
        hardwareId = result.hardwareId,
    )
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy