Class: EvokToMqtt::Worker
- Inherits:
-
Object
- Object
- EvokToMqtt::Worker
- Defined in:
- lib/evok-to-mqtt/worker.rb
Instance Method Summary collapse
-
#initialize(evok_host, mqtt_host, mapper) ⇒ Worker
constructor
A new instance of Worker.
- #run ⇒ Object
Constructor Details
#initialize(evok_host, mqtt_host, mapper) ⇒ Worker
Returns a new instance of Worker.
10 11 12 13 14 |
# File 'lib/evok-to-mqtt/worker.rb', line 10 def initialize(evok_host, mqtt_host, mapper) @evok_host = evok_host @mqtt_host = mqtt_host @mapper = mapper end |
Instance Method Details
#run ⇒ Object
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/evok-to-mqtt/worker.rb', line 16 def run EM.run do @evok_ws = WebSocket::EventMachine::Client.connect(:uri => "ws://#{@evok_host}:8080/ws") @evok_rpc = JSONRPC::Client.new("http://#{@evok_host}/rpc") @mqtt = EventMachine::MQTT::ClientConnection.connect(@mqtt_host) @evok_ws. do |msg| data = JSON.parse(msg) data = [data] if data.is_a? Hash # temp is not in array in evok messages, but in hash.. data.each do |event| next if !event.is_a?(Hash) || %w(wd ai ao).include?(event['dev']) # want just relay, input and temp, skip the rest for now (better to whitelist) puts "#{Time.now} Recieved message: #{event}" @mapper.process(@mqtt, event) end end # send commands to relay only for now, maybe do more lightweight not to subscribe to everything @mqtt.subscribe('#') @mqtt.receive_callback do |msg| begin data = JSON.parse(msg.payload) rescue => ex puts "Error: Received invalid message on the bus" p msg.payload data = {} end next unless data['action'] == 'cmd' circuit = data['circuit'] || @mapper.circuit_reverse_lookup('relay', msg.topic) || next puts "#{Time.now} Sending command #{msg.topic}: #{circuit} => #{data['value']}" @evok_rpc.relay_set(circuit, data['value']) end end end |