mqtt-homie 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/mqtt/homie.rb +27 -0
- data/lib/mqtt/homie/client.rb +162 -0
- data/lib/mqtt/homie/device.rb +97 -0
- data/lib/mqtt/homie/device_builder.rb +38 -0
- data/lib/mqtt/homie/homie_object.rb +39 -0
- data/lib/mqtt/homie/node.rb +34 -0
- data/lib/mqtt/homie/property.rb +59 -0
- data/lib/mqtt/homie/version.rb +5 -0
- data/mqtt-homie.gemspec +36 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6e92a85d38e5d824f9e40a7afe8ec0bbe51f4a7a483d01fa66ee7765812b5939
|
4
|
+
data.tar.gz: 56356ede9f137963ff41f26a7cc046e0223c647cec0da5703a57da165f4337ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d019acaf6ec7b635bfc50eb53be9d185b40a80926f74b36dbfbd8770ed2325d480cf27e05bf6eaeb5f37007c97ad60d4b5385e27aaba4657074409b9fd21e7ad
|
7
|
+
data.tar.gz: cb4c85f3c03ea59b28a7f9ecda820a7d03a3f6f1e77cc8e2abde98daabcd21f0dea0bbdda3c9d76939c03b5e4fd80546762928a49de2e586465f62576670b69b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at sobakasu@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mqtt-homie (0.1.0)
|
5
|
+
mqtt (~> 0.5)
|
6
|
+
sys-uname (~> 1.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
diff-lcs (1.3)
|
12
|
+
ffi (1.11.1-x64-mingw32)
|
13
|
+
mqtt (0.5.0)
|
14
|
+
rake (10.5.0)
|
15
|
+
rspec (3.8.0)
|
16
|
+
rspec-core (~> 3.8.0)
|
17
|
+
rspec-expectations (~> 3.8.0)
|
18
|
+
rspec-mocks (~> 3.8.0)
|
19
|
+
rspec-core (3.8.1)
|
20
|
+
rspec-support (~> 3.8.0)
|
21
|
+
rspec-expectations (3.8.4)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.8.0)
|
24
|
+
rspec-mocks (3.8.1)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.8.0)
|
27
|
+
rspec-support (3.8.2)
|
28
|
+
sys-uname (1.0.4)
|
29
|
+
ffi (>= 1.0.0)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
x64-mingw32
|
33
|
+
|
34
|
+
DEPENDENCIES
|
35
|
+
bundler (~> 2.0)
|
36
|
+
mqtt-homie!
|
37
|
+
rake (~> 10.0)
|
38
|
+
rspec (~> 3.0)
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Andrew Williams
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# MQTT::Homie
|
2
|
+
|
3
|
+
A ruby interface for creating a device conforming to the MQTT [Homie] convention.
|
4
|
+
This gem builds upon the [ruby-mqtt] ruby gem.
|
5
|
+
|
6
|
+
The [Homie] convention defines a standardized way of how IoT devices and services announce themselves and their data to a MQTT broker.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'mqtt-homie'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install mqtt-homie
|
23
|
+
|
24
|
+
## Quick Start
|
25
|
+
|
26
|
+
~~~ ruby
|
27
|
+
require 'rubygems'
|
28
|
+
require 'mqtt/homie'
|
29
|
+
|
30
|
+
# Set up a device, with a node and properties
|
31
|
+
device = MQTT::Homie.device_builder(id: 'device', name: 'Device'
|
32
|
+
localip: '192.168.1.1',
|
33
|
+
mac: '80:1f:02:cc:15:dd'
|
34
|
+
).node(id: "gate", name: "Front gate", type: "Gate")
|
35
|
+
.property(id: "state", name: "Gate state", enum: [:open, :closed, :opening, :closing], value: :closed)
|
36
|
+
.property(id: "position", name: "Gate position", datatype: :integer, unit: "%", value: 0)
|
37
|
+
.property(id: "command", name: "Send gate command", settable: true, enum: [:open, :close]).build
|
38
|
+
|
39
|
+
# Create a client and connect to a MQTT broker
|
40
|
+
client = MQTT::Homie::Client.new(device: device, host: 'localhost')
|
41
|
+
client.connect
|
42
|
+
|
43
|
+
# access nodes and properties of the device
|
44
|
+
node = device.node('gate')
|
45
|
+
state = node.property('state')
|
46
|
+
state.value = :open # publishes new state to MQTT
|
47
|
+
|
48
|
+
# listen for changes to properties via the Observer interface
|
49
|
+
node.property('command').add_observer(self)
|
50
|
+
~~~
|
51
|
+
|
52
|
+
## Overview
|
53
|
+
|
54
|
+
TODO
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
59
|
+
|
60
|
+
## Code of Conduct
|
61
|
+
|
62
|
+
Everyone interacting in the Mqtt::Homie project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/mqtt-homie/blob/master/CODE_OF_CONDUCT.md).
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
[Homie]: https://homieiot.github.io/
|
67
|
+
[MQTT]: http://www.mqtt.org/
|
68
|
+
[ruby-mqtt]: https://github.com/njh/ruby-mqtt
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mqtt/homie"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/mqtt/homie.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "mqtt"
|
2
|
+
require "mqtt/homie/version"
|
3
|
+
|
4
|
+
module MQTT
|
5
|
+
module Homie
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :logger
|
10
|
+
|
11
|
+
def debug(message)
|
12
|
+
logger.debug(message) if logger
|
13
|
+
end
|
14
|
+
|
15
|
+
def device_builder(options = {})
|
16
|
+
MQTT::Homie::DeviceBuilder.new(options)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
require "mqtt/homie/homie_object"
|
23
|
+
require "mqtt/homie/property"
|
24
|
+
require "mqtt/homie/node"
|
25
|
+
require "mqtt/homie/device"
|
26
|
+
require "mqtt/homie/client"
|
27
|
+
require "mqtt/homie/device_builder"
|
@@ -0,0 +1,162 @@
|
|
1
|
+
module MQTT
|
2
|
+
module Homie
|
3
|
+
# https://homieiot.github.io/specification/
|
4
|
+
|
5
|
+
class Client
|
6
|
+
DEFAULT_ROOT_TOPIC = "homie"
|
7
|
+
|
8
|
+
attr_accessor :host, :root_topic
|
9
|
+
attr_reader :device
|
10
|
+
|
11
|
+
def initialize(options = {})
|
12
|
+
@device = options[:device]
|
13
|
+
@host = options[:host]
|
14
|
+
@root_topic = options[:root_topic] || DEFAULT_ROOT_TOPIC
|
15
|
+
raise "device required" unless @device
|
16
|
+
|
17
|
+
# observe all node properties so we can publish values when they change
|
18
|
+
@device.nodes.each do |node|
|
19
|
+
node.properties.each do |property|
|
20
|
+
property.add_observer(self)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def connect
|
26
|
+
return if connected?
|
27
|
+
|
28
|
+
@device.state = :init
|
29
|
+
@client = create_mqtt_client
|
30
|
+
@client.connect
|
31
|
+
|
32
|
+
publish(@device, topic)
|
33
|
+
publish_statistics
|
34
|
+
|
35
|
+
@threads = []
|
36
|
+
|
37
|
+
# run a thread to publish statistics
|
38
|
+
@threads << Thread.new { run_statistics }
|
39
|
+
|
40
|
+
# run a thread to listen for settings
|
41
|
+
@threads << Thread.new { run_set_listener }
|
42
|
+
|
43
|
+
@device.state = :ready
|
44
|
+
publish_state
|
45
|
+
end
|
46
|
+
|
47
|
+
def disconnect
|
48
|
+
@device.state = :disconnected
|
49
|
+
publish_state
|
50
|
+
|
51
|
+
@client.disconnect
|
52
|
+
@client = nil
|
53
|
+
|
54
|
+
@threads.each { |i| i[:done] = true }
|
55
|
+
@threads = []
|
56
|
+
end
|
57
|
+
|
58
|
+
def topic
|
59
|
+
@root_topic + "/" + @device.id
|
60
|
+
end
|
61
|
+
|
62
|
+
def connected?
|
63
|
+
@device.state == :ready
|
64
|
+
end
|
65
|
+
|
66
|
+
def update(time, object)
|
67
|
+
if object.kind_of?(MQTT::Homie::Property)
|
68
|
+
publish_property_value(object)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def create_mqtt_client
|
75
|
+
client = ::MQTT::Client.new
|
76
|
+
client.host = @host
|
77
|
+
client.will_topic = topic + "/$state"
|
78
|
+
client.will_payload = :lost
|
79
|
+
client.will_retain = true
|
80
|
+
client
|
81
|
+
end
|
82
|
+
|
83
|
+
def run_set_listener
|
84
|
+
# subscribe to 'set' topics for all settable properties
|
85
|
+
@device.nodes.each do |node|
|
86
|
+
node.properties.each do |property|
|
87
|
+
if property.settable?
|
88
|
+
set_topic = topic + "/" + node.topic + "/" + property.topic + "/set"
|
89
|
+
debug("subscribe #{set_topic}")
|
90
|
+
@client.subscribe(set_topic) if @client
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
if @client
|
96
|
+
@client.get do |topic, message|
|
97
|
+
debug("received message: #{topic}, message: #{message}")
|
98
|
+
property = find_property_by_set_topic(topic)
|
99
|
+
property.value = message if property
|
100
|
+
break if Thread.current[:done]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
debug("set listener thread exiting")
|
104
|
+
end
|
105
|
+
|
106
|
+
def run_statistics
|
107
|
+
while !Thread.current[:done]
|
108
|
+
publish_statistics
|
109
|
+
sleep @device.interval
|
110
|
+
end
|
111
|
+
debug("statistics thread exiting")
|
112
|
+
end
|
113
|
+
|
114
|
+
def find_property_by_set_topic(set_topic)
|
115
|
+
@device.nodes.each do |node|
|
116
|
+
node.properties.each do |property|
|
117
|
+
return property if set_topic == topic + "/" + node.topic + "/" + property.topic + "/set"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
nil
|
121
|
+
end
|
122
|
+
|
123
|
+
def publish_statistics
|
124
|
+
publish(@device.statistics, topic + "/$stats")
|
125
|
+
end
|
126
|
+
|
127
|
+
def publish_property_value(property)
|
128
|
+
node = @device.nodes.find { |i| i.properties.include?(property) }
|
129
|
+
data = {
|
130
|
+
property.id => property.value,
|
131
|
+
}
|
132
|
+
publish(data, topic + "/" + node.topic)
|
133
|
+
end
|
134
|
+
|
135
|
+
def publish_state
|
136
|
+
data = {
|
137
|
+
"$state" => @device.state,
|
138
|
+
}
|
139
|
+
publish(data, topic)
|
140
|
+
end
|
141
|
+
|
142
|
+
def publish(object, prefix = nil)
|
143
|
+
data = {}
|
144
|
+
if object.respond_to?(:homie_attributes)
|
145
|
+
data = object.homie_attributes
|
146
|
+
else
|
147
|
+
data = object
|
148
|
+
end
|
149
|
+
|
150
|
+
data.each do |k, v|
|
151
|
+
topic = prefix + "/" + k
|
152
|
+
debug("mqtt publish #{topic} -> #{v}")
|
153
|
+
@client.publish(topic, v, true)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def debug(message)
|
158
|
+
MQTT::Homie.debug(message)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require "sys/uname"
|
2
|
+
|
3
|
+
module MQTT
|
4
|
+
module Homie
|
5
|
+
class Device < HomieObject
|
6
|
+
HOMIE_VERSION = "3.0.1"
|
7
|
+
DEFAULT_STAT_REFRESH = 60 # seconds
|
8
|
+
DEFAULT_IMPLEMENTATION = "mqtt-homie"
|
9
|
+
|
10
|
+
attr_reader :nodes, :id, :mac, :fw_name, :fw_version, :name, :implementation, :interval
|
11
|
+
attr_accessor :state
|
12
|
+
|
13
|
+
def initialize(options = {})
|
14
|
+
super(options)
|
15
|
+
@name = options[:name]
|
16
|
+
@start_time = Time.now
|
17
|
+
@interval = set(options, :interval, default: DEFAULT_STAT_REFRESH)
|
18
|
+
@nodes = set(options, :nodes, data_type: Array, default: [])
|
19
|
+
@state = :init
|
20
|
+
@localip = set(options, :localip, default: default_localip)
|
21
|
+
@mac = set(options, :mac, default: default_mac)
|
22
|
+
@implementation = set(options, :implementation, default: DEFAULT_IMPLEMENTATION)
|
23
|
+
@fw_name = set(options, :fw_name, default: default_fw_name)
|
24
|
+
@fw_version = set(options, :fw_version, default: default_fw_version)
|
25
|
+
end
|
26
|
+
|
27
|
+
def topic
|
28
|
+
@id
|
29
|
+
end
|
30
|
+
|
31
|
+
def node(id)
|
32
|
+
@nodes.find { |i| i.id == id }
|
33
|
+
end
|
34
|
+
|
35
|
+
# device attributes must be sent when connection to broker is established or re-established
|
36
|
+
# homie/device_id/
|
37
|
+
def homie_attributes
|
38
|
+
data = {
|
39
|
+
"$homie" => HOMIE_VERSION,
|
40
|
+
"$name" => @name,
|
41
|
+
"$localip" => @localip,
|
42
|
+
"$mac" => @mac,
|
43
|
+
"$fw/name" => @fw_name || "mqtt-homie",
|
44
|
+
"$fw/version" => @fw_version || MQTT::Homie::VERSION,
|
45
|
+
"$nodes" => @nodes.collect { |i| i.id }.join(","),
|
46
|
+
"$implementation" => @implementation,
|
47
|
+
"$state" => @state.to_s,
|
48
|
+
}
|
49
|
+
@nodes.each do |node|
|
50
|
+
node.homie_attributes.each do |k, v|
|
51
|
+
data[node.topic + "/" + k] = v
|
52
|
+
end
|
53
|
+
end
|
54
|
+
data
|
55
|
+
end
|
56
|
+
|
57
|
+
# statistics should be sent every INTERVAL seconds
|
58
|
+
# homie/device_id/$stats
|
59
|
+
def statistics
|
60
|
+
{
|
61
|
+
"uptime" => (Time.now - @start_time).to_i,
|
62
|
+
#"signal" => 100, # TODO wifi signal strength
|
63
|
+
#"cputemp" => 0,
|
64
|
+
#"cpuload" => stats.load_average.one_minute,
|
65
|
+
#"battery" => 100,
|
66
|
+
#"freeheap" => stats.memory.free,
|
67
|
+
#"supply" => 5,
|
68
|
+
"interval" => @interval * 2,
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
72
|
+
def update(time, node)
|
73
|
+
# node value updated
|
74
|
+
end
|
75
|
+
|
76
|
+
def default_localip
|
77
|
+
nil # TODO
|
78
|
+
end
|
79
|
+
|
80
|
+
def default_mac
|
81
|
+
nil # TODO
|
82
|
+
end
|
83
|
+
|
84
|
+
def default_fw_name
|
85
|
+
uname.sysname rescue uname.caption rescue "Unknown"
|
86
|
+
end
|
87
|
+
|
88
|
+
def default_fw_version
|
89
|
+
uname.release rescue uname.build_number rescue "Unknown"
|
90
|
+
end
|
91
|
+
|
92
|
+
def uname
|
93
|
+
@uname ||= Sys::Uname.uname
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module MQTT
|
2
|
+
module Homie
|
3
|
+
class DeviceBuilder
|
4
|
+
def initialize(options = {})
|
5
|
+
@nodes = []
|
6
|
+
@device_options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
# create device and return it
|
10
|
+
def build
|
11
|
+
build_node if @node_data
|
12
|
+
MQTT::Homie::Device.new(@device_options.merge(nodes: @nodes))
|
13
|
+
end
|
14
|
+
|
15
|
+
def node(options = {})
|
16
|
+
raise "node key/value list expected" unless options.kind_of?(Hash)
|
17
|
+
build_node if @node_data
|
18
|
+
@node_data = options
|
19
|
+
@properties = []
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def property(options = {})
|
24
|
+
raise "property key/value list expected" unless options.kind_of?(Hash)
|
25
|
+
@properties << MQTT::Homie::Property.new(options)
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def build_node
|
32
|
+
@nodes << MQTT::Homie::Node.new(@node_data.merge(properties: @properties))
|
33
|
+
@node_data = nil
|
34
|
+
@propertes = nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "observer"
|
2
|
+
|
3
|
+
module MQTT
|
4
|
+
module Homie
|
5
|
+
class HomieObject
|
6
|
+
attr_reader :id
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
@id = set(options, :id, required: true)
|
10
|
+
raise "invalid id" unless valid_id?
|
11
|
+
end
|
12
|
+
|
13
|
+
def topic
|
14
|
+
@id
|
15
|
+
end
|
16
|
+
|
17
|
+
def homie_attributes
|
18
|
+
data = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def valid_id?
|
24
|
+
@id && @id.kind_of?(String) && @id.match(/^[-a-z0-9]+$/) && !@id.start_with?("-")
|
25
|
+
end
|
26
|
+
|
27
|
+
def set(options = {}, name, default: nil, required: false, enum: nil, data_type: nil)
|
28
|
+
value = options.include?(name) ? options[name] : default
|
29
|
+
raise "#{name} is required for #{object_type} #{@id}" if required && value.nil?
|
30
|
+
raise "expected #{name} to be a #{data_type} for #{object_type} #{@id}" if data_type && !value.kind_of?(data_type)
|
31
|
+
value
|
32
|
+
end
|
33
|
+
|
34
|
+
def object_type
|
35
|
+
self.class.name.split("::").last
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module MQTT
|
2
|
+
module Homie
|
3
|
+
class Node < HomieObject
|
4
|
+
attr_reader :name, :properties
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
super(options)
|
8
|
+
@name = set(options, :name, required: true)
|
9
|
+
@type = set(options, :type, required: true)
|
10
|
+
@properties = set(options, :properties, required: true, data_type: Array)
|
11
|
+
end
|
12
|
+
|
13
|
+
def property(id)
|
14
|
+
@properties.find { |i| i.id == id }
|
15
|
+
end
|
16
|
+
|
17
|
+
def homie_attributes
|
18
|
+
data = {
|
19
|
+
"$name" => @name,
|
20
|
+
"$type" => @type,
|
21
|
+
"$properties" => @properties.collect { |i| i.id }.join(","),
|
22
|
+
}
|
23
|
+
|
24
|
+
@properties.each do |p|
|
25
|
+
p.homie_attributes.each do |k, v|
|
26
|
+
data[p.topic + "/" + k] = v
|
27
|
+
end
|
28
|
+
data[p.topic] = p.value
|
29
|
+
end
|
30
|
+
data
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "observer"
|
2
|
+
|
3
|
+
module MQTT
|
4
|
+
module Homie
|
5
|
+
class Property < HomieObject
|
6
|
+
include Observable
|
7
|
+
|
8
|
+
DATA_TYPES = [:integer, :float, :boolean, :string, :enum, :color]
|
9
|
+
|
10
|
+
attr_reader :id, :name, :settable, :datatype, :unit, :retained, :format
|
11
|
+
attr_reader :value
|
12
|
+
|
13
|
+
def initialize(options = {})
|
14
|
+
super(options)
|
15
|
+
|
16
|
+
options = options.dup
|
17
|
+
|
18
|
+
# enum shortcut
|
19
|
+
if enum = options.delete(:enum)
|
20
|
+
options[:datatype] = :enum
|
21
|
+
options[:format] = enum.collect { |i| i.to_s }.join(',')
|
22
|
+
end
|
23
|
+
|
24
|
+
@name = set(options, :name, default: "")
|
25
|
+
@settable = !!set(options, :settable, default: false)
|
26
|
+
@retained = !!set(options, :retained, default: true)
|
27
|
+
@datatype = set(options, :datatype, default: :string, enum: DATA_TYPES).to_sym
|
28
|
+
@unit = set(options, :unit, default: "")
|
29
|
+
@format = set(options, :format, required: [:enum, :color].include?(@datatype))
|
30
|
+
@value = options[:value]
|
31
|
+
end
|
32
|
+
|
33
|
+
def value=(value)
|
34
|
+
# TODO: check value conforms to expected datatype and format
|
35
|
+
if value != @value
|
36
|
+
@value = value
|
37
|
+
changed
|
38
|
+
notify_observers(Time.now, self)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def settable?
|
43
|
+
@settable
|
44
|
+
end
|
45
|
+
|
46
|
+
def homie_attributes
|
47
|
+
data = {
|
48
|
+
"$name" => @name,
|
49
|
+
"$settable" => @settable,
|
50
|
+
"$datatype" => @datatype,
|
51
|
+
"$unit" => @unit,
|
52
|
+
"$format" => @format,
|
53
|
+
"$retained" => @retained,
|
54
|
+
}
|
55
|
+
data
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/mqtt-homie.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "mqtt/homie/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "mqtt-homie"
|
7
|
+
spec.version = MQTT::Homie::VERSION
|
8
|
+
spec.authors = ["Andrew Williams"]
|
9
|
+
spec.email = ["sobakasu@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{A ruby interface for creating a device conforming to the MQTT Homie convention.}
|
12
|
+
spec.homepage = "https://github.com/sobakasu/mqtt-homie"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/sobakasu/mqtt-homie"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/sobakasu/mqtt-homie/CHANGELOG"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
|
34
|
+
spec.add_dependency "mqtt", "~> 0.5"
|
35
|
+
spec.add_dependency "sys-uname", "~> 1.0"
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mqtt-homie
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Williams
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mqtt
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.5'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sys-uname
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- sobakasu@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/mqtt/homie.rb
|
102
|
+
- lib/mqtt/homie/client.rb
|
103
|
+
- lib/mqtt/homie/device.rb
|
104
|
+
- lib/mqtt/homie/device_builder.rb
|
105
|
+
- lib/mqtt/homie/homie_object.rb
|
106
|
+
- lib/mqtt/homie/node.rb
|
107
|
+
- lib/mqtt/homie/property.rb
|
108
|
+
- lib/mqtt/homie/version.rb
|
109
|
+
- mqtt-homie.gemspec
|
110
|
+
homepage: https://github.com/sobakasu/mqtt-homie
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata:
|
114
|
+
allowed_push_host: https://rubygems.org
|
115
|
+
homepage_uri: https://github.com/sobakasu/mqtt-homie
|
116
|
+
source_code_uri: https://github.com/sobakasu/mqtt-homie
|
117
|
+
changelog_uri: https://github.com/sobakasu/mqtt-homie/CHANGELOG
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.7.6.2
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: A ruby interface for creating a device conforming to the MQTT Homie convention.
|
138
|
+
test_files: []
|