Skip to content

Instantly share code, notes, and snippets.

@mapero
Last active October 15, 2021 18:30
Show Gist options
  • Save mapero/ee99ce0a8dbc2fe7e335 to your computer and use it in GitHub Desktop.
Save mapero/ee99ce0a8dbc2fe7e335 to your computer and use it in GitHub Desktop.
mqtt job for dashing.io with publish and subscribe support

#Description This files will enable you to directly connect your widgets with your mqtt broker. It consists of a job, which will manage the connection to your project. It will over a rest api to publish, subscribe and unsubscribe from your widgets.

#Installation Add the mqtt.rb to your dashing jobs.

#Usage To connect a widget to mqtt use a post on 'mqtt/subscribe' on your constructor. As parameters you need to post the topic to subscribe and the widget id. E.g.:

    $.post 'mqtt/subscribe',
      topic: @get('subscribe'),
      widgetId: @get('id'),
      (data) =>
        console.log data
<% content_for :title do %>Home Automation: Main Dashboard<% end %>
<div class="gridster">
<ul>
<li data-row="2" data-col="4" data-sizex="1" data-sizey="1">
<div data-id="testWidget" data-view="Test" data-icon="android" data-title="Luigi" data-publish="some/topic/pub" data-subscribe="some/topic/sub"></div>
</li>
</ul>
</div>
require 'mqtt'
require 'pry'
module JSON
def self.is_json?(foo)
begin
return false unless foo.is_a?(String)
JSON.parse(foo).all?
rescue JSON::ParserError
false
end
end
end
subscriptions = Hash.new{|h,k| h[k] = []}
client = MQTT::Client.connect('mqtt://localhost:1883')
Thread.new {
client.get do |topic,message|
message = JSON.parse(message) if JSON.is_json?(message)
subscriptions[topic].each do |tag|
send_event( tag, { value: message, current: message })
end
end
}
post '/mqtt/publish' do
client.publish(params['topic'], params['message'])
end
post '/mqtt/subscribe' do
if subscriptions[params['topic']].empty?
client.subscribe(params['topic'])
end
unless subscriptions[params['topic']].include? params['widgetId']
subscriptions[params['topic']] << params['widgetId']
end
end
post '/mqtt/unsubscribe' do
subscriptions[params['topic']].delete(params['widgetId'])
if subscriptions[params['topic']].empty
client.unsubscribe(params['topic'])
end
end
class Dashing.Test extends Dashing.ClickableWidget
constructor: ->
super
$.post 'mqtt/subscribe',
topic: @get('subscribe'),
widgetId: @get('id'),
(data) =>
console.log data
ready: ->
onData: (data) ->
onClick: (node, event) ->
$.post '/mqtt/publish',
topic: @get('publish'),
message: 'message',
(data) =>
alert(data)
@mvillarejo
Copy link

Really interesting gist, I'm trying to create a widget with a list of events that will come from a mqtt topic, any idea how this would be potentially archived?

Thanks in advance!

@mdbxz
Copy link

mdbxz commented Jul 3, 2017

Briliant piece of code! Do you know how I can remember the state on a page refresh? I am thing on outputting the state and widget name to txt file that is read directly in the widget constructor. What do you think, @mapero ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment