classdef mqttconnection < handle
    methods
        function obj = mqttconnection(brokerAddress)
            if isempty(obj.getConnection())
                obj.getConnection(brokerAddress);
            end
            if obj.getConnection().Connected == 0
                obj.getConnection(brokerAddress);
            end
        end

        function delete(obj)
            unsubscribe(obj.getConnection());
        end
    end

    methods (Static)
        function ret = getConnection(brokerAddress)
            persistent perMqttClient;
            if nargin
                perMqttClient = mqttclient(brokerAddress, "ClientID", "MatlabClient");
            end
            ret = perMqttClient;
        end
    end
end