Method: LinuxStat::Battery.devices_stat
- Defined in:
- lib/linux_stat/battery.rb
.devices_stat ⇒ Object
A linux system can have multiple batteries. This method attempts to find all of them and return a Hash based on the availibility of the information.
This method is somewhat slower than other Battery module singleton methods. Because it does various checks and opens various files from /sys/
For example, a sample output can be like this (taken on a test system):
LinuxStat::Battery.devices_stat
=> {:AC=>{:type=>"Mains", :online=>1},
:BAT0=>CYMGM77”, :manufacturer=>“Samsung SDI”, :type=>“Battery”, :status=>“Full”, :capacity=>100, :voltage_min_design=>11.4, :charge_full_design=>3.684, :charge_full_design_wh=>42.0, :voltage_now=>12.558, :charge_now=>2.087, :charge_now_wh=>26.21, :charge_full_wh=>23.79, :charge_percentage=>100.0, :hidpp_battery_0=>Keyboard”, :manufacturer=>“Logitech”, :type=>“Battery”, :status=>“Discharging”, :online=>1}
If you need info about lots of batteries, use this method. If the informations are not available, it will return empty Hash for each devices. If the system has no batteries, it will return an empty Hash.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/linux_stat/battery.rb', line 201 def devices_stat h = {} Dir["/sys/class/power_supply/*/".freeze].tap(&:sort!).each do |x| # model name mn_file = File.join(x, 'model_name'.freeze).freeze model_name = File.readable?(mn_file) ? IO.read(mn_file).strip : nil # manufacturer m_f = File.join(x, 'manufacturer'.freeze).freeze manufacturer = File.readable?(m_f) ? IO.read(m_f).strip : nil # type t_f = File.join(x, 'type'.freeze).freeze type = File.readable?(t_f) ? IO.read(t_f).strip : nil # capacity c_file = File.join(x, 'capacity'.freeze).freeze capacity = File.readable?(c_file) ? IO.read(c_file).to_i : nil # voltage now vn_f = File.join(x, 'voltage_now'.freeze).freeze voltage_now = File.readable?(vn_f) ? IO.read(vn_f).to_i.fdiv(1_000_000) : nil # charge now cn_f = File.join(x, 'charge_now'.freeze).freeze charge_now = if File.readable?(cn_f) IO.read(cn_f).to_i.fdiv(1_000_000) else en_f = File.join(x, 'energy_now'.freeze) if File.readable?(en_f) IO.read(en_f) .to_i.fdiv(1_000_000) else nil end end # voltage min design vmd_f = File.join(x, 'voltage_min_design'.freeze).freeze v_m_d = File.readable?(vmd_f) ? IO.read(vmd_f).to_i.fdiv(1_000_000) : nil # charge full design cfd_f = File.join(x, 'charge_full_design'.freeze).freeze c_f_d = File.readable?(cfd_f) ? IO.read(cfd_f).to_i.fdiv(1_000_000) : nil # charge full cf_f = File.join(x, 'charge_full'.freeze).freeze charge_full = if File.readable?(cf_f) IO.read(cf_f).to_i.fdiv(1_000_000) else ef_f = File.join(x, 'energy_full'.freeze) if File.readable?(ef_f) IO.read(ef_f).to_i.fdiv(1_000_000) else nil end end # status s_f = File.join(x, 'status'.freeze).freeze status = File.readable?(s_f) ? IO.read(s_f).strip : nil # online o_f = File.join(x, 'online'.freeze).freeze online = File.readable?(o_f) ? IO.read(o_f).to_i : nil charge_percentage = if charge_now && charge_full charge_now.*(100).fdiv(charge_full).round(2) else nil end # full_charge_design (WH) c_f_d_wh = if c_f_d && v_m_d v_m_d.*(c_f_d).round(2) else nil end c_n_wh = if voltage_now && charge_now voltage_now.*(charge_now).round(2) else nil end c_f_wh = if v_m_d && charge_full v_m_d.*(charge_full).round(2) else nil end ret = {} ret[:model] = model_name if model_name ret[:manufacturer] = manufacturer if manufacturer ret[:type] = type if type ret[:status] = status if status ret[:online] = online if online ret[:capacity] = capacity if capacity ret[:voltage_min_design] = v_m_d if v_m_d ret[:charge_full_design] = c_f_d if c_f_d ret[:charge_full_design_wh] = c_f_d_wh if c_f_d_wh ret[:voltage_now] = voltage_now if voltage_now ret[:charge_now] = charge_now if charge_now ret[:charge_now_wh] = c_n_wh if c_n_wh ret[:charge_full_wh] = c_f_wh if c_f_wh ret[:charge_percentage] = charge_percentage if charge_percentage h.merge!(File.split(x)[-1].to_sym => ret) end h end |