Skip to content

Instantly share code, notes, and snippets.

@Coro365
Last active March 9, 2021 14:49
Show Gist options
  • Save Coro365/ee4b91e17a63e84e4d7958b21af9a801 to your computer and use it in GitHub Desktop.
Save Coro365/ee4b91e17a63e84e4d7958b21af9a801 to your computer and use it in GitHub Desktop.
Display home environment from influxdb in macOS menu bar.
#!/usr/bin/env ruby
# coding: utf-8
# <bitbar.title>HomeEnv</bitbar.title>
# <bitbar.version>v0.1</bitbar.version>
# <bitbar.author>Coro365</bitbar.author>
# <bitbar.author.github>Coro365</bitbar.author.github>
# <bitbar.desc>Display home environment from influxdb</bitbar.desc>
# <bitbar.image></bitbar.image>
# <bitbar.dependencies>ruby, influxdb</bitbar.dependencies>
# <bitbar.abouturl>https://coro3.net</bitbar.abouturl>
require 'open-uri'
require 'json'
require 'uri'
# your influxdb address
ADDR = 'http://192.168.0.210:8086'
def influx_get(query)
influx_url = URI.join(ADDR, query_endoce(query))
influx_json = JSON.parse(URI.open(influx_url).read)
influx_json.dig("results", 0, "series", 0, 'values', 0, 1).to_s
end
def home_env_get(field, location)
influx_get("query?db=home-sensor&q=select value from #{field} where location='#{location}' order by desc limit 1")
end
def weather_get
influx_get("query?db=darksky&q=select summary from this_week order by desc limit 1")
end
def query_endoce(query)
query.gsub!(/\s/, '%20')
query.gsub!(/'/, '%27')
query
end
def get(field, location)
locations = {myroom: 1, veranda: 2, living: 3, entrance: 4, bath: 5, wash: 6, kitchen: 8}
fields = {temperature: {icon: '🌡', unit: '℃'},
humidity: {icon: '💧', unit: '%'},
co2: {icon: '😮‍💨', unit: 'ppm'},
pressure: {icon: '🌪️', unit: 'hPa'},
light: {icon: '💡', unit: 'lx'},
doorkey: {icon: '🚪', unit: nil}}
value = home_env_get(field, locations[location])
fields[field][:icon] + value + fields[field][:unit]
end
envs = {location: :myroom, fields: [:temperature, :humidity, :co2]},
{location: :veranda, fields: [:temperature, :humidity, :pressure]},
{location: :bath, fields: [:temperature, :humidity]}
messages = envs.map do |env|
loc_name = env[:location].to_s.capitalize
env_data = env[:fields].map { |field| get(field, env[:location]) }.join(' / ')
"[#{loc_name}: #{env_data}]"
end
messages.push(weather_get)
puts messages.join(" ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment