Skip to content

Instantly share code, notes, and snippets.

@ErikDeBruijn
Last active September 30, 2020 13:13
Show Gist options
  • Save ErikDeBruijn/ef66517cb615e2381c4975dcc83caf88 to your computer and use it in GitHub Desktop.
Save ErikDeBruijn/ef66517cb615e2381c4975dcc83caf88 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#BitBar Metadata
# <bitbar.title>Frequency of the synchronous grid</bitbar.title>
# <bitbar.version>v0.1</bitbar.version>
# <bitbar.author>Erik de Bruijn</bitbar.author>
# <bitbar.author.github>Erik de Bruijn</bitbar.author.github>
# <bitbar.desc>Displays the current frequency of the grid and the time it is ahead or lagging.</bitbar.desc>
#
# This script in to be used with Bitbar as a toolbar plugin for OS X.
# It will display the grid freqency and time offset compared to the actual time (leading/lagging seconds)
#
require 'net/http'
require 'uri'
require 'json'
# require 'pp'
Source = Struct.new(:time_delta, :frequency, :name)
# No permission asked. Please check the Terms of Service of the website before using this script.
def fetch_from_swissgrid
url1 = "https://www.swissgrid.ch/bin/services/apicache?path=/content/swissgrid/en/home/operation/regulation/frequency/jcr:content/parsys/chart_copy"
result = JSON.parse(Net::HTTP.get(URI(url1)))
frequency = result["table"][0]["value"].split(" ").first.to_f
time_delta = result["table"][1]["value"].split(" ").first.to_i
Source.new(time_delta, frequency, :swissgrid)
rescue
nil
end
# No permission yet! Don't use URL2, please.
def fetch_from_netzfrequenzmessung_de
url2 = "https://netzfrequenzmessung.de:9080/frequenz01.xml?#{rand.to_s[2..6]}"
result2_body = Net::HTTP.get(URI(url2))
frequency = result2_body.match(/<f>(.*)<\/f>/)[1].to_f
time_delta = result2_body.match(/<dt>(.*)<\/dt>/)[1].to_i
Source.new(time_delta, frequency, :netzfrequenzmessung)
end
source = fetch_from_swissgrid()
# Don't use this one: (only a fallback if anything)
unless source.is_a?(Source)
# source = fetch_from_netzfrequenzmessung_de()
fallback = source.name.to_s
end
# Grid shutdown: 2.4 Hz off nominal = all connected generation and devices will be shutdown.
# Load shedding starts at 1 Hz below nominal and is increased with every 0.2 Hz further devations
# https://www.mainsfrequency.com/shedding.htm
frequency_margin = 0.1
freq_offset = source.frequency - 50
warning = freq_offset.abs > frequency_margin ? "!" : ""
warning = source.time_delta.abs > 20 ? "!!" : "" if warning == ""
color = warning == "" ? "" : "color=red"
puts "⚡️⚖️ #{source.frequency} Hz #{warning} #{source.time_delta}s #{fallback} | #{color}"
puts "---"
puts "Show Frequency Graph | href=https://www.swissgrid.ch/en/home/operation/regulation/frequency.html"
puts "Show mainsfrequency.com | href=https://www.mainsfrequency.com/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment