class MQTT::TXHash

Attributes

hash[R]

Public Class Methods

new(mqtt, topic, startHash: Hash.new) click to toggle source
# File lib/mqtt/mqtt_hash.rb, line 52
def initialize(mqtt, topic, startHash: Hash.new)
        @mqtt         = mqtt;
        @topicList = MQTT::SubHandler.get_topic_split(topic);

        @hash  = startHash;
end

Public Instance Methods

[](key) click to toggle source
# File lib/mqtt/mqtt_hash.rb, line 64
def [](key)
        return @hash[key]
end
[]=(key, val) click to toggle source
# File lib/mqtt/mqtt_hash.rb, line 68
def []=(key, val)
        @hash[key]= val
        update_hash
end
hash=(newHash) click to toggle source
# File lib/mqtt/mqtt_hash.rb, line 59
def hash=(newHash)
        @hash = newHash;
        update_hash
end
publish_layer(currentHash = nil, currentList = nil) click to toggle source
# File lib/mqtt/mqtt_hash.rb, line 10
def publish_layer(currentHash = nil, currentList = nil)
        currentHash ||= @hash;
        currentList ||= Array.new();

        status = catch(:halt) do
                loop do
                        throw :halt, :publish_layer                        if currentList[-1] == "+"
                        throw :halt, :publish_all_layered  if currentList[-1] == "#"
                        throw :halt, :publish_hash                         if currentList.length == @topicList.length

                        currentList << @topicList[currentList.length]
                end
        end

        case status
        when :publish_hash
                @mqtt.publish_to currentList.join("/"), currentHash.to_json, retain: true

        when :publish_layer
                if(currentHash.is_a? Hash) then
                        currentHash.each do |key, val|
                                newList = currentList.clone();
                                newList[-1] = key;
                                publish_layer(val, newList);
                        end
                else
                        @mqtt.publish_to currentList.join("/"), currentHash.to_json, retain: true
                end

        when :publish_all_layered
                if(currentHash.is_a? Hash) then
                        currentHash.each do |key, val|
                                publish_layer(val, (currentList.clone[-1] = key)<<"#");
                        end
                else
                        @mqtt.publish_to currentList.join("/"), currentHash.to_json, retain: true
                end
        end
end
Also aliased as: update_hash

Private Instance Methods

update_hash(currentHash = nil, currentList = nil)
Alias for: publish_layer