Class: ESPHome::Cli::Monitor
- Inherits:
-
Object
- Object
- ESPHome::Cli::Monitor
- Includes:
- Colors
- Defined in:
- lib/esphome/cli/monitor.rb
Constant Summary collapse
- HEADER_ROWS =
3
Constants included from Colors
Colors::ANSI_COLOR_MAP, Colors::ANSI_ESCAPE_REGEX
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#name_width ⇒ Object
readonly
Returns the value of attribute name_width.
-
#win ⇒ Object
readonly
Returns the value of attribute win.
Class Method Summary collapse
Instance Method Summary collapse
- #add(_level, message = nil, progname = nil, render: true) ⇒ Object (also: #log)
-
#initialize(device, actions: false, device_log_level: nil, connection_log_level: nil, log_level: nil, dump_config: false) ⇒ Monitor
constructor
A new instance of Monitor.
- #resize_pending? ⇒ Boolean
- #run ⇒ Object
Constructor Details
#initialize(device, actions: false, device_log_level: nil, connection_log_level: nil, log_level: nil, dump_config: false) ⇒ Monitor
Returns a new instance of Monitor.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/esphome/cli/monitor.rb', line 45 def initialize(device, actions: false, device_log_level: nil, connection_log_level: nil, log_level: nil, dump_config: false) @device = device @win = nil @entities_by_key = {} @entities = [] @log_lines = [] @current_entity = -1 @sub_active = false @name_width = 0 @device.device_logger = LoggerWrapper.new(self, level: device_log_level || Logger::DEBUG) @device.connection_logger = LoggerWrapper.new(self, level: connection_log_level || Logger::WARN) @logger = LoggerWrapper.new(self, level: log_level || Logger::INFO) @actions = actions @dump_config = dump_config @winch_trapped = false @logwin = nil @screen_initialized = false @resize_pending = false @ui_events = Queue.new @log_mutex = Mutex.new end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
43 44 45 |
# File 'lib/esphome/cli/monitor.rb', line 43 def logger @logger end |
#name_width ⇒ Object (readonly)
Returns the value of attribute name_width.
42 43 44 |
# File 'lib/esphome/cli/monitor.rb', line 42 def name_width @name_width end |
#win ⇒ Object (readonly)
Returns the value of attribute win.
42 43 44 |
# File 'lib/esphome/cli/monitor.rb', line 42 def win @win end |
Class Method Details
.run ⇒ Object
37 38 39 |
# File 'lib/esphome/cli/monitor.rb', line 37 def run(...) new(...).run end |
Instance Method Details
#add(_level, message = nil, progname = nil, render: true) ⇒ Object Also known as: log
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/esphome/cli/monitor.rb', line 193 def add(_level, = nil, progname = nil, render: true) if .nil? = if block_given? yield else progname end end log_line = "[#{Time.now.strftime("%H:%M:%S.%L")}] #{}" @log_mutex.synchronize do @log_lines << log_line @log_lines.shift if @log_lines.size > [20, log_area_height].max end puts log_line unless @screen_initialized queue_ui_event(:log) if render end |
#resize_pending? ⇒ Boolean
213 214 215 |
# File 'lib/esphome/cli/monitor.rb', line 213 def resize_pending? @resize_pending end |
#run ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 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 |
# File 'lib/esphome/cli/monitor.rb', line 80 def run @device. do |entity_or_log_line| if entity_or_log_line.respond_to?(:key) && (entity_wrapper = @entities_by_key[entity_or_log_line.key]) entity_wrapper.touch queue_ui_event(:entity, entity_wrapper) elsif entity_or_log_line.is_a?(Action) logger.info(entity_or_log_line.inspect) else logger.warn("Unexpected message #{entity_or_log_line.inspect}") end end @device.on_connect do logger.info("Connected") @name_width = @device.entities.values.map { |e| e.name.length }.max || 0 @entities_by_key = {} @entities = [] @device.entities.values.sort_by(&:name).each_with_index do |entity, idx| simple_name = entity.class.name.split("::").last entity_class = if Entities.const_defined?(simple_name) Entities.const_get(simple_name, false) else Entity end entity_wrapper = entity_class.new(self, entity, idx) @entities_by_key[entity.key] = entity_wrapper @entities << entity_wrapper end @current_entity = @entities.index { |e| e.respond_to?(:activate) } || -1 queue_ui_event(:render_all) @device.stream_states @device.stream_actions if @actions @device.stream_log(dump_config: @dump_config) if @device.device_logger.level < Logger::FATAL end @device.on_disconnect do logger.info("Gracefully disconnected") reconnect end install_winch_handler @device.connect ensure_screen process_ui_events begin Thread.new do @device.loop rescue DeviceConnectionError, NotConnectedError, IOError, SocketError, SystemCallError, Timeout::Error => e logger.warn("Connection lost: #{e}") @device.disconnect reconnect retry rescue => e error("UNHANDLED EXCEPTION: #{e}", render: false) e.backtrace.each do |line| error(" #{line}", render: false) end queue_ui_event(:log) retry end loop do process_ui_events case @win.getch when Curses::Key::UP new_entity = @entities.reverse.find { |e| e.index < @current_entity && e.respond_to?(:activate) }&.index next unless new_entity @entities[@current_entity]&.print(@win) if @current_entity >= 0 @current_entity = new_entity @entities[@current_entity].print(@win, active: true) @win.refresh when Curses::Key::DOWN new_entity = @entities.find { |e| e.index > @current_entity && e.respond_to?(:activate) }&.index next unless new_entity @entities[@current_entity]&.print(@win) if @current_entity >= 0 @current_entity = new_entity @entities[@current_entity].print(@win, active: true) @win.refresh when Curses::Key::LEFT next unless @current_entity >= 0 @entities[@current_entity].move_left @entities[@current_entity].print(@win, active: true) @win.refresh when Curses::Key::RIGHT next unless @current_entity >= 0 @entities[@current_entity].move_right @entities[@current_entity].print(@win, active: true) @win.refresh when "\n".ord entity_wrapper = @entities[@current_entity] activate_entity(entity_wrapper) if entity_wrapper.respond_to?(:activate) when 27 # Esc break when nil next end end rescue Interrupt # exiting ensure close_screen end @device.disconnect end |