Class: MQTT::TXHash

Inherits:
Object
  • Object
show all
Defined in:
lib/mqtt/mqtt_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mqtt, topic, startHash: Hash.new) ⇒ TXHash

Returns a new instance of TXHash.



52
53
54
55
56
57
# 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

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



8
9
10
# File 'lib/mqtt/mqtt_hash.rb', line 8

def hash
  @hash
end

Instance Method Details

#[](key) ⇒ Object



64
65
66
# File 'lib/mqtt/mqtt_hash.rb', line 64

def [](key)
	return @hash[key]
end

#[]=(key, val) ⇒ Object



68
69
70
71
# File 'lib/mqtt/mqtt_hash.rb', line 68

def []=(key, val)
	@hash[key]= val
	update_hash
end

#publish_layer(currentHash = nil, currentList = nil) ⇒ Object Also known as: update_hash



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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