将您的飞利浦 Hue 灯与 Travis 构建的状态同步

分享此内容
分享此内容

让我们在这个星期五玩点有趣的!使用您的飞利浦 Hue 灯将构建的颜色(失败时为红色,通过时为绿色)与您的 Travis CI 构建同步。目前它具有基本功能,如果 Travis CI 构建通过,您的 Hue 灯将变为绿色。n 如果您的 Travis CI 构建失败,它将变为红色。

用法和先决条件

  • 飞利浦 Hue 照明
  • 树莓派

让我们从 Ruby 版本开始,我将在最后提供的存储库中提供一个 Python 版本。目前这就是 hue_lighting.rb

require 'hue'
require 'json'
require 'travis/client'

hue = nil

unless File.exists?('settings.json')
  puts "Please setup which Hue lights you want to link on Travis CI first"
  exit
end

begin
  puts "Locating the Hue bridge"

  hue = Hue::Client.new("travis-ci-on-the-piiii")

  puts "Hue bridge found at #{client.bridge.ip}\n\n"
rescue Hue::NoBridgeFound => e
  puts "Sorry but no Hue bridge could be found"
  exit
rescue => e

end

begin
  hue = Hue::Client.new("travis-philips-hue")

  puts "Looks like the bridge knows who you are!\n\n"
rescue Hue::LinkButtonNotPressed => e
  puts "Please setup which Hue lights you want to link on Travis CI first"
  exit
end

TRANSITION_TIME = 2
PASSED = { hue: 25500, brightness: 255, saturation: 255 }
FAILED = { hue: 65535, brightness: 255, saturation: 255 }

file = File.read('settings.json')
monitored_lights = JSON.parse(file)['lights']

message = "setup to sync with Travis CI repos"

case monitored_lights.size
when 0
  puts "No lights #{message}"
  puts "Please setup which Hue lights you want to link on Travis CI first"
  exit
when 1
  puts "1 light #{message}"
else
  puts "#{monitored_lights.size} lights #{message}"
end

def repo_status(travis, slug)
  repo = travis.repo(slug)
  master = repo.branch('master')
  master.state
rescue => e
  puts "had an error fetching the status of repo:#{slug}, moving on"
  raise
end

def update_light(hue, light_id, state)
  case state
  when 'passed'
    hue.light(light_id).set_state PASSED, TRANSITION_TIME
  when 'failed'
    hue.light(light_id).set_state FAILED, TRANSITION_TIME
  end
rescue => e
  puts "had an error updating light:#{light_id}, moving on"
  raise
end

puts "Starting monitoring: #{monitored_lights.collect { |ml| ml["repo"] }}\n\n"
travis = Travis::Client.new
loop do
  travis.clear_cache
  monitored_lights.each do |ml|
    begin
      puts "checking status of #{ml["repo"]}"
      state = repo_status(travis, ml["repo"])
      puts "the most recent master build on #{ml["repo"]} #{state}"
      update_light(hue, ml["light_id"], state)
    rescue => e
    end
  end
  sleep 3
 end

现在做一个会通过的构建,再做一个会失败的构建。您的飞利浦 Hue 灯应该与构建状态同步,即通过则为绿色,失败则为红色。

Travis CI

这是我的 .travis.yml,这是 Python 的,但您可以使用 Ruby 来替换此示例

dist: jammy
addons:
  apt:
    packages:
      - coreutils
language:
- python
python:
- '3.9'
script:
- chmod u+x "./hue.py"
- echo "light changing to {$BUILDSTATUS}"

结论

现在您的灯光应该根据您的构建状态变为红色和绿色!谁知道 Travis 不仅仅是构建,为什么不玩得开心呢?

像往常一样,如果您有任何问题,任何问题,请发送电子邮件至 [email protected].

祝您构建愉快!

© 版权所有 2024,保留所有权利
© 版权所有 2024,保留所有权利
© 版权所有 2024,保留所有权利