Skip to content

Instantly share code, notes, and snippets.

@moyashi
Last active February 5, 2020 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moyashi/b27c88e2edacc2e6f7e6910856864aeb to your computer and use it in GitHub Desktop.
Save moyashi/b27c88e2edacc2e6f7e6910856864aeb to your computer and use it in GitHub Desktop.
OctoPrintを接続した3Dプリンターの動作状況をmacOS用アプリBitBarを使ってステータスバーに表示するRubyスクリプト
#!/usr/local/opt/ruby/bin/ruby
require 'open-uri'
require 'json'
require 'time'
OCTPRINT_IP = "192.168.1.33"
HEADER = "[C]"
begin
r = open("http://#{OCTPRINT_IP}/api/job", open_timeout: 1, read_timeout: 1)
json = JSON.parse(r.read())
if ((json["state"] =~ /^Offline/) != nil) then
mes = "オフライン"
elsif (json["state"] == "Operational") then
mes = "待機中"
elsif (json["state"] == "Connecting") then
mes = "接続中"
elsif (json["state"] == "Printing") then
# 印刷中
if (json["progress"]["completion"] != nil) then
completion = json["progress"]["completion"].round(2).to_s + "%"
else
completion = "-%"
end
if (json["progress"]["printTimeLeft"] != nil) then
printTimeLeft = json["progress"]["printTimeLeft"]
if (printTimeLeft == 0) then
printTimeLeft = "-%"
else
printTimeLeft = Time.at(printTimeLeft).utc.strftime('%X')
end
else
# まだ残り時間が算出されていない
printTimeLeft = "計算中"
end
mes = "#{completion} / #{printTimeLeft}"
end
rescue => exception
mes = "接続不能"
end
puts "#{HEADER}#{mes}"
@moyashi
Copy link
Author

moyashi commented Feb 5, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment