PSMQTT.psm1

#Region '.\prefix.ps1' 0
# The content of this file will be prepended to the top of the psm1 module file. This is useful for custom module setup is needed on import.
Add-Type -Path "$PSScriptRoot\Include\M2Mqtt.Net.dll"
#EndRegion '.\prefix.ps1' 3
#Region '.\Private\Assert-FolderExist.ps1' 0
function Assert-FolderExist
{
    <#
    .SYNOPSIS
        Verify and create folder
    .DESCRIPTION
        Verifies that a folder path exists, if not it will create it
    .PARAMETER Path
        Defines the path to be validated
    .EXAMPLE
        'C:\Temp' | Assert-FolderExist
 
        This will verify that the path exists and if it does not the folder will be created
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipeline)]
        [string]
        $Path
    )

    process
    {
        $exists = Test-Path -Path $Path -PathType Container
        if (!$exists)
        {
            $null = New-Item -Path $Path -ItemType Directory
        }
    }
}
#EndRegion '.\Private\Assert-FolderExist.ps1' 31
#Region '.\Private\Invoke-GarbageCollect.ps1' 0
function Invoke-GarbageCollect
{
    <#
    .SYNOPSIS
        Calls system.gc collect method. Purpose is mainly for readability.
    .DESCRIPTION
        Calls system.gc collect method. Purpose is mainly for readability.
    .EXAMPLE
        Invoke-GarbageCollect
    #>

    [system.gc]::Collect()
}
#EndRegion '.\Private\Invoke-GarbageCollect.ps1' 13
#Region '.\Public\Connect-MQTTBroker.ps1' 0
function Connect-MQTTBroker
{
    <#
      .DESCRIPTION
      This function establishes a session with the MQTT broker and returns a session object that should then be passed along when using other cmdlets.
 
      .EXAMPLE
      $MQTTSession = Connect-MQTTBroker -Hostname mqttbroker.contoso.com -Port 1234 -Username mqttuser -Password (ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force)
 
      .PARAMETER Hostname
      Defines the hostname of the MQTT Broker to connect to
 
      .PARAMETER Port
      Defines the port of the MQTT Broker. Defaults to 1883.
 
      .PARAMETER Username
      Defines the username to use when connecting to the MQTT broker
 
      .PARAMETER Password
      Defines the password (securestring) to use when connecting to the MQTT Broker
 
      .PARAMETER TLS
      Defines if the connection should be encrypted
 
    #>

    [cmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter()]
        [int]
        $Port,

        [Parameter()]
        [string]
        $Username,

        [Parameter()]
        [securestring]
        $Password,

        [Parameter()]
        [switch]
        $TLS
    )

    #TODO implement additional connection properties like certificate ssl options etc. Different default ports based on

    if (-not $PSBoundParameters['Port'])
    {
        if ($TLS)
        {
            $Port = 1884
        }
        else
        {
            $Port = 1883
        }
    }

    $MqttClient = New-Object -TypeName uPLibrary.Networking.M2Mqtt.MqttClient -ArgumentList $Hostname, $Port, $TLS, $null, $null, 'None'
    $MqttClient.Connect([guid]::NewGuid(), $Username, ($Password.GetNetworkCredentials.Password))

    return $MqttClient
}
#EndRegion '.\Public\Connect-MQTTBroker.ps1' 68
#Region '.\Public\Disconnect-MQTTBroker.ps1' 0
function Disconnect-MQTTBroker
{
    <#
      .DESCRIPTION
      This function will disconnect a MQTTBroker session
 
      .EXAMPLE
      Get-Something -Data 'Get me this text'
 
      .PARAMETER Session
      Defines the MQTTBroker session to use
 
    #>

    [cmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [uPLibrary.Networking.M2Mqtt.MqttClient]
        $Session
    )

    $Session.Disconnect()
}
#EndRegion '.\Public\Disconnect-MQTTBroker.ps1' 23
#Region '.\Public\Send-MQTTMessage.ps1' 0
function Send-MQTTMessage
{
    <#
      .DESCRIPTION
      This function will publish a message to a MQTT topic.
 
      .EXAMPLE
      Get-Something -Data 'Get me this text'
 
      .PARAMETER Session
      Defines the MQTTBroker session to use
 
      .PARAMETER Topic
      Defines the topic to publish the message to
 
      .PARAMETER Payload
      Defines the message as a string
 
    #>

    [cmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [uPLibrary.Networking.M2Mqtt.MqttClient]
        $Session,

        [Parameter(Mandatory)]
        [string]
        $Topic,

        [Parameter()]
        [string]
        $Payload
    )

    try
    {
        $PayloadBytes = [System.Text.Encoding]::UTF8.GetBytes($Payload)
        $Session.Publish($Topic, $PayloadBytes)
        [pscustomobject]@{
            TimeStamp = (Get-Date)
            Topic     = $Topic
            Payload   = $Payload
        }
    }
    catch
    {
        $_
    }
}
#EndRegion '.\Public\Send-MQTTMessage.ps1' 50
#Region '.\Public\Watch-MQTTTopic.ps1' 0
function Send-MQTTMessage
{
    <#
      .DESCRIPTION
      This function will publish a message to a MQTT topic.
 
      .EXAMPLE
      Get-Something -Data 'Get me this text'
 
      .PARAMETER Session
      Defines the MQTTBroker session to use
 
      .PARAMETER Topic
      Defines the topic to publish the message to
 
    #>

    [cmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [uPLibrary.Networking.M2Mqtt.MqttClient]
        $Session,

        [Parameter(Mandatory)]
        [string]
        $Topic
    )

    try
    {
        $Subscription = Register-ObjectEvent -InputObject $Session -EventName MqttMsgPublishReceived -Action {
            [pscustomobject]@{
                TimeStamp = (Get-Date)
                Topic     = $args[1].topic
                Payload   = [System.Text.Encoding]::ASCII.GetString($args[1].Message)
            }
        }

        $Session.Subscribe($Topic, 0)

    }
    catch
    {
        $_
    }
}
#EndRegion '.\Public\Watch-MQTTTopic.ps1' 46
#Region '.\suffix.ps1' 0
# The content of this file will be appended to the top of the psm1 module file. This is useful for custom procesedures after all module functions are loaded.
#EndRegion '.\suffix.ps1' 2