Class: EvokToMqtt::Mappers::ToHaab

Inherits:
Object
  • Object
show all
Defined in:
lib/evok-to-mqtt/mappers/to_haab.rb

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ ToHaab

Returns a new instance of ToHaab.



6
7
8
9
# File 'lib/evok-to-mqtt/mappers/to_haab.rb', line 6

def initialize(mapping)
  @mapping_config  = mapping
  @statuses = {}
end

Instance Method Details

#circuit_reverse_lookup(full_topic) ⇒ Object



47
48
49
50
51
52
# File 'lib/evok-to-mqtt/mappers/to_haab.rb', line 47

def circuit_reverse_lookup(full_topic)
  dev, topic = full_topic.split("/", 2)
  @mapping_config[dev].each{|pin, a_topic| return pin if a_topic == topic}
  puts "Warning: #{topic} pin was not found."
  nil
end

#process(mqtt, evok_event) ⇒ Object



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
# File 'lib/evok-to-mqtt/mappers/to_haab.rb', line 11

def process(mqtt, evok_event)
  id      = get_topic(evok_event["dev"], evok_event["circuit"])
  now     = Time.now
  payload = evok_event

  timestamp = DateTime.now.iso8601(3)

  puts "Target topic: #{id}"

  # Neuron input is assumed for now
  if @statuses[id]
    # TODO: input dev only makes sense for this branch
    if @statuses[id][:value] == 0 && evok_event['value'] == 1
      #mqtt.publish id, {action: 'down', data: payload}
    elsif @statuses[id][:value] == 1 && evok_event['value'] == 0
      #mqtt.publish id, {action: 'up', data: payload}
      if now - @statuses[id][:changed_at] < 2  # seconds
        mqtt.publish id, {action: 'click', data: payload, timestamp: timestamp}.to_json
        puts "### click"
      else
        mqtt.publish id, {action: 'long_click', data: payload, timestamp: timestamp}.to_json
        puts "### long_click"
      end
    end
    @statuses[id][:value] = evok_event['value']
    @statuses[id][:changed_at] = now
  else
    @statuses[id] = {value: evok_event['value'], changed_at: now}
    #mqtt.publish id, {action: 'down', data: payload} unless evok_event['value'] == 0  # ignore initial states received in batch
  end

  mqtt.publish id, {action: 'change', data: payload, timestamp: timestamp}.to_json, retain: true if evok_event['dev'] == 'temp'

  puts
end