Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@moyashi moyashi commented Feb 5, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.