mac bookのバッテリー状態をはてなグラフに記録してみよう(OS X 10.9 Mavericks対応版)

OS X 10.5 Leoaprd時代に書いた記事
mac bookのバッテリー状態をはてなグラフに記録してみよう - mac日記
が古くなって、
Mavericks(ruby2.0)で動かない(gemのHatena::API::Graphが動かない)
はてなグラフAPIも2014年3月にOAuth認証以外は受け付けなくなって、グラフデータが追加できない、など、ぼろぼろだったので動くように修正したコードを上げておきます。

MacBook Pro battery capacity (mAh)



MacBook Pro battery cycle (times)



#!/usr/bin/ruby
require 'rubygems'
require 'oauth'
require 'Date'

class BatteryLog
  CONSUMER_KEY = 'your consumer key'
  CONSUMER_SECRET = 'your consumer key secret' 
  ACCESS_TOKEN = 'your access token'
  ACCESS_TOKEN_SECRET = 'your access token secret'

  GRAPH_NAME_CAPA = 'macbook pro battery capacity (mAh)'
  GRAPH_NAME_CYCLE = 'macbook pro battery cycle time'

  BATTERY_LOG = 'battery.log'
  DEBUG_LOG = 'battery.dbg.log'
  LOG_PATH = File.dirname(__FILE__)

  def run
    logging
    post
  end

  def logging
    if logged?
      _debug("no write log")
      return
    end

    capa, cycle = get_status
    _logging(Time.new, capa, cycle)
    _debug("write log")
  end

  def logged?
    Date.today == latest_log_date
  end

  def _logging(time, capa, cycle)
    l = sprintf("%s,%d,%d,*", time.strftime("%Y/%m/%d %H:%M:%S"), capa, cycle)

    io = open(battery_log(), "a")
    io.puts(l)
    io.close
  end

  def decode(l)
    if /(\d{4}\/\d{2}\/\d{2}).+?,(\d+),(\d+)/ =~ l
      [Date.parse($1), $2, $3]
    else
      nil
    end
  end

  def post
    temp_file_name = battery_log() + ".tmp";

    r = open(battery_log(), "r")
    w = open(temp_file_name, "w")
    while l = r.gets
      l.chomp!
      if !posted?(l)
        if _post(l)
          l = posted!(l)
        end
      end

      w.puts(l)
    end

    r.close
    w.close

    File.rename(temp_file_name, battery_log())
  end

  def _post(l)
    date, capa, cycle = decode(l)

    if date
      post_hatena(date, capa, cycle)
    else
      _debug("failure decode [" + l + "]")
    end
  end

  def posted?(l)
    /-$/ =~ l
  end

  def posted!(l)
    l.sub(/,\*$/, ",-")
  end

  def get_status
    s = eval(`/usr/sbin/ioreg -w0 -l | grep LegacyBatteryInfo`.gsub(/=/, '=>').gsub(/^.*\{/, '{'))
      capa = s["Capacity"]
      cycle = s["Cycle Count"]

      if (capa && cycle)
        [capa, cycle]
      else
        [-1, -1]
      end
  end

  def latest_log_date
    if (!File.file?(battery_log()))
      return nil;
    end

    log = battery_log()
    tail = `tail -n1 #{log}`

    if /(\d{4}\/\d{2}\/\d{2})/ =~ tail
      Date.parse($1)
    else
      nil
    end
  end

  def post_hatena(_date, capa, cycle)
    _debug(sprintf("post to hatena graph ... (%s, %d, %d)", _date, capa, cycle))

    ret = true

    consumer = OAuth::Consumer.new(
      CONSUMER_KEY,
      CONSUMER_SECRET
    )
    access_token = OAuth::AccessToken.new(consumer, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    begin
      response = access_token.post(
        'http://graph.hatena.ne.jp/api/data',
        {
        :graphname => GRAPH_NAME_CAPA,
        :date => _date,
        :value => capa
        }
      )
      if response.code != '201'
        _debug(response.inspect)
      end

      response = access_token.post(
        'http://graph.hatena.ne.jp/api/data',
        {
        :graphname => GRAPH_NAME_CYCLE,
        :date => _date,
        :value => cycle
        }
      )
      if response.code != '201'
        _debug(response.inspect)
      end

    rescue
      ret = false
    end

    _debug(ret ? "success" : "failure")

    ret
  end

  def _debug(l)
    if debug_log().size > 0
      io = open(debug_log(), "a")
      io.puts(Time.new.strftime("%Y/%m/%d %H:%M:%S") + " " + l)
      io.close
    end
  end

  def battery_log()
    LOG_PATH + "/" + BATTERY_LOG
  end

  def debug_log()
    LOG_PATH + "/" + DEBUG_LOG
  end
end

BatteryLog.new.run

bootcampとVMware Fusion 3 でのPrintScreen

Apple Keybord US配列 テンキー付を使っているんだけど
PrintScreenで戸惑いました。

  • bootcampの時

F14: PrintScreen
option + F14: Alt + PrintScreen

  • WMware Fusion 3 の時

F13: PrintScreen
option + F13: Alt + PrintScreen

でした。
テンキー付AppleキーボードならJIS/US配列問わないと思います。

MacBookやWireless Keyboardの人は
F13やF14なんてキーはないんで、

Fn + Shift + F11 : PrintScreen
option + Fn + Shift + F11 : Alt + PrintScreen

となるそうです。

vdiやvhdなど仮想HDDファイルの形式を変換する方法

VirtualBoxを使っている人なら、別途変換ソフトなどを用意しなくても、VirtualBoxに変換ツールが付いてました。

コマンドラインから、

VBoxManage internalcommands converthd [-srcformat VDI|VMDK|VHD|RAW] [-dstformat VDI|VMDK|VHD|RAW]  

と入力するだけ。
入力、出力ともに、ファイル形式は4種に類対応しているみたいです。
VirtualBox便利!
他の仮想化ソフトにも、こういうツールついてるかもしれませんが。

言わずもがなですが、
VBoxManageコマンドはVirtualBoxをインストールしたディレクトリに入っているので、パスを通すなり、そのディレクトリまで移動してからコマンド実行してくださいね。