Last active
February 5, 2020 01:52
-
-
Save moyashi/b27c88e2edacc2e6f7e6910856864aeb to your computer and use it in GitHub Desktop.
OctoPrintを接続した3Dプリンターの動作状況をmacOS用アプリBitBarを使ってステータスバーに表示するRubyスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BitBar: https://github.com/matryer/bitbar
OctoPrint: https://octoprint.org