Class: HomieDevice
- Inherits:
-
Object
- Object
- HomieDevice
- Defined in:
- lib/hodmin/hodmin_tools.rb,
lib/hodmin/hodmin_pull_config.rb
Overview
Create filename for config-data of a HomieDevice depending on default pattern (Homie-<MAC>.yaml) or a given parameter replacing ‘Homie’.
Instance Attribute Summary collapse
-
#checksum ⇒ Object
readonly
Returns the value of attribute checksum.
-
#fw_brand ⇒ Object
readonly
Returns the value of attribute fw_brand.
-
#fw_name ⇒ Object
readonly
Returns the value of attribute fw_name.
-
#fw_version ⇒ Object
readonly
Returns the value of attribute fw_version.
-
#mac ⇒ Object
readonly
Returns the value of attribute mac.
-
#upgradable ⇒ Object
readonly
Returns the value of attribute upgradable.
Instance Method Summary collapse
- #config_yaml_filename_homie(fn) ⇒ Object
-
#create_attr(name, value) ⇒ Object
Helper to create instance variables on the fly:.
-
#create_method(name, &block) ⇒ Object
Helper to create instance variables on the fly:.
-
#initialize(mqtt, *fw_list) ⇒ HomieDevice
constructor
A new instance of HomieDevice.
-
#online? ⇒ Boolean
Helper to determine status of a device (online/offline).
-
#online_status ⇒ Object
Helper to create a string containing ONLINE or OFFLINE.
-
#push_firmware_to_dev(new_firmware) ⇒ Object
Helper to push a firmware-file vai MQTT to our Homie-Device.
-
#remove_special_chars(str) ⇒ Object
Helper to remove some special chars from string to avoid problems in instance_variable_set:.
Constructor Details
#initialize(mqtt, *fw_list) ⇒ HomieDevice
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/hodmin/hodmin_tools.rb', line 97 def initialize(mqtt, *fw_list) fw_list.flatten! startseq = mqtt.select { |t, _m| t.include?('/$homie') }.first.first.split('$').first @mac = startseq.split(/\//).last mqtt.map! { |t, m| [t.gsub(startseq, ''), m] } mhash = Hash[*mqtt.flatten] create_attr('homieVersion', mhash['$homie']) if mhash['$homie'] > configatron.MAX_HOMIEVERSION Log.log.warn "Device #{mac}: Detected new Homie-version (#{mhash['$homie']})"\ + ' check hodmin for updates' end mhash.tap { |hs| hs.delete('$homie') } # Some topics-names from homie do not fit our needs due to special chars like '/'. # ['$fw/name','$fw/version','$fw/checksum'] # Replace '/' by '_': mhash.each { |k, v| create_attr(k.to_s.delete('$').gsub(/\//, '_').tr('-', '_'), v) } # mac only downcase and without separating ':' @mac = mac.delete(':').downcase # for selecting purposes we need some of our topics in different varnames: @checksum = @fw_checksum # do we find a higher version of this firmware than installed one? @upgradable = fw_list.empty? ? false : upgradable?(fw_name, fw_version, fw_list) Log.log.info "Homie-Device detected: mac=#{@mac}, #{online_status}, " \ + " running #{fw_name}, #{fw_version}, upgr=#{upgradable}" end |
Instance Attribute Details
#checksum ⇒ Object (readonly)
Returns the value of attribute checksum.
96 97 98 |
# File 'lib/hodmin/hodmin_tools.rb', line 96 def checksum @checksum end |
#fw_brand ⇒ Object (readonly)
Returns the value of attribute fw_brand.
96 97 98 |
# File 'lib/hodmin/hodmin_tools.rb', line 96 def fw_brand @fw_brand end |
#fw_name ⇒ Object (readonly)
Returns the value of attribute fw_name.
96 97 98 |
# File 'lib/hodmin/hodmin_tools.rb', line 96 def fw_name @fw_name end |
#fw_version ⇒ Object (readonly)
Returns the value of attribute fw_version.
96 97 98 |
# File 'lib/hodmin/hodmin_tools.rb', line 96 def fw_version @fw_version end |
#mac ⇒ Object (readonly)
Returns the value of attribute mac.
96 97 98 |
# File 'lib/hodmin/hodmin_tools.rb', line 96 def mac @mac end |
#upgradable ⇒ Object (readonly)
Returns the value of attribute upgradable.
96 97 98 |
# File 'lib/hodmin/hodmin_tools.rb', line 96 def upgradable @upgradable end |
Instance Method Details
#config_yaml_filename_homie(fn) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/hodmin/hodmin_pull_config.rb', line 34 def config_yaml_filename_homie(fn) config_extension = '.yaml' if fn.include?('<MAC>') fn.gsub(/<MAC>/, mac) + config_extension else fn.gsub(/[^A-Za-z0-9]/, '') + '-' + mac + config_extension end end |
#create_attr(name, value) ⇒ Object
Helper to create instance variables on the fly:
138 139 140 141 142 143 |
# File 'lib/hodmin/hodmin_tools.rb', line 138 def create_attr(name, value) # replace chars name = remove_special_chars(name) create_method(name.to_sym) { instance_variable_get('@' + name) } instance_variable_set('@' + name, value) end |
#create_method(name, &block) ⇒ Object
Helper to create instance variables on the fly:
126 127 128 |
# File 'lib/hodmin/hodmin_tools.rb', line 126 def create_method(name, &block) self.class.send(:define_method, name, &block) end |
#online? ⇒ Boolean
Helper to determine status of a device (online/offline). Checks it via reading the online-topic of device at time of creating this object. WARNING: If you use this in a longer running program, this info may be outdated. In this case you should create a method that establishes a connection to your broker and reads the online-topic of this device during execution time.
150 151 152 |
# File 'lib/hodmin/hodmin_tools.rb', line 150 def online? online.casecmp('true').zero? end |
#online_status ⇒ Object
Helper to create a string containing ONLINE or OFFLINE. Gets it via reading the online-topic of the device at time of creating this object. WARNING: If you use this in a longer running program, this info may be outdated. In this case you should create a method that establishes a connection to your broker and reads the online-topic of this device during execution time.
159 160 161 |
# File 'lib/hodmin/hodmin_tools.rb', line 159 def online_status online? ? 'ONLINE' : 'OFFLINE' end |
#push_firmware_to_dev(new_firmware) ⇒ Object
Helper to push a firmware-file vai MQTT to our Homie-Device.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/hodmin/hodmin_tools.rb', line 164 def push_firmware_to_dev(new_firmware) bin_file = File.read(new_firmware.file_path) md5_bin_file = Digest::MD5.hexdigest(bin_file) base_topic = configatron.mqtt.base_topic + mac + '/' client = mqtt_connect sended = FALSE client.publish(base_topic + '$implementation/ota/checksum', md5_bin_file, retain = false) sleep 0.1 client.subscribe(base_topic + '$implementation/ota/status') cursor = TTY::Cursor puts ' ' client.get do |_topic, | ms = ms = .split(/ /).first.strip if .include?(' ') if ms == '206' now, ges = .split(/ /).last.strip.split(/\//).map(&:to_i) actual = (now / ges.to_f * 100).round(0) print cursor.column(1) print "Pushing firmware, #{actual}% done" end if ms == '304' puts '304, file already installed. No action needed. ' + break end if ms == '403' puts '403, OTA disabled:' + break end if ms == '400' puts '400, Bad checksum:' + break end if ms == '202' puts '202, pushing file' client.publish(base_topic + '$implementation/ota/firmware', bin_file, retain = false) sended = TRUE end if ms == '200' && sended puts "\nFile-md5=#{md5_bin_file} installed, device #{name} is rebooting" break end end end |
#remove_special_chars(str) ⇒ Object
Helper to remove some special chars from string to avoid problems in instance_variable_set:
131 132 133 134 135 |
# File 'lib/hodmin/hodmin_tools.rb', line 131 def remove_special_chars(str) to_be_replaced = ['%', '!', '(', ')', '&', '?', ',', '.', '^', ' '] to_be_replaced.each{|char| str.gsub!(char,'')} str end |