2017-01-21 11:23:29 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the current color temperature of redshift
|
|
|
|
|
|
|
|
Requires the following executable:
|
|
|
|
* redshift
|
2019-08-21 19:12:58 +02:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* redshift.location : location provider, either of "geoclue2" (default), \
|
|
|
|
"ipinfo" (requires the requests package), or "manual"
|
|
|
|
* redshift.lat : latitude if location is set to "manual"
|
|
|
|
* redshift.lon : longitude if location is set to "manual"
|
2017-01-21 11:23:29 +01:00
|
|
|
"""
|
|
|
|
|
2017-12-29 11:18:07 +01:00
|
|
|
import threading
|
2019-08-21 19:12:58 +02:00
|
|
|
import requests
|
2017-12-29 11:18:07 +01:00
|
|
|
|
2017-01-21 11:23:29 +01:00
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
2019-08-21 18:26:59 +02:00
|
|
|
|
2017-12-29 11:18:07 +01:00
|
|
|
def is_terminated():
|
|
|
|
for thread in threading.enumerate():
|
|
|
|
if thread.name == "MainThread" and not thread.is_alive():
|
|
|
|
return True
|
|
|
|
return False
|
2017-01-21 11:23:29 +01:00
|
|
|
|
2019-08-21 18:26:59 +02:00
|
|
|
|
2019-08-21 19:12:58 +02:00
|
|
|
def get_redshift_value(widget, location, lat, lon):
|
2017-12-29 11:18:07 +01:00
|
|
|
while True:
|
|
|
|
if is_terminated():
|
|
|
|
return
|
|
|
|
widget.get("condition").acquire()
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
widget.get("condition").wait(1)
|
|
|
|
except RuntimeError:
|
|
|
|
continue
|
|
|
|
break
|
2018-01-01 08:59:21 +01:00
|
|
|
widget.get("condition").release()
|
2017-01-21 11:23:29 +01:00
|
|
|
|
2019-08-21 19:12:58 +02:00
|
|
|
command = ["redshift", "-p", "-l"]
|
|
|
|
if location == "manual":
|
|
|
|
command.append(lat + ":" + lon)
|
|
|
|
else:
|
|
|
|
command.append("geoclue2")
|
|
|
|
|
2017-12-29 11:18:07 +01:00
|
|
|
try:
|
2019-08-21 19:12:58 +02:00
|
|
|
res = bumblebee.util.execute(" ".join(command))
|
2017-12-29 11:18:07 +01:00
|
|
|
except Exception:
|
|
|
|
res = ""
|
|
|
|
widget.set("temp", "n/a")
|
|
|
|
widget.set("transition", None)
|
2019-01-16 20:13:28 +01:00
|
|
|
widget.set("state", "day")
|
2017-12-29 11:18:07 +01:00
|
|
|
for line in res.split("\n"):
|
2019-01-16 20:13:28 +01:00
|
|
|
line = line.lower()
|
|
|
|
if "temperature" in line:
|
2017-12-29 11:18:07 +01:00
|
|
|
widget.set("temp", line.split(" ")[2])
|
2019-01-16 20:13:28 +01:00
|
|
|
if "period" in line:
|
|
|
|
state = line.split(" ")[1]
|
2017-01-21 11:23:29 +01:00
|
|
|
if "day" in state:
|
2017-12-29 11:18:07 +01:00
|
|
|
widget.set("state", "day")
|
2017-01-21 11:23:29 +01:00
|
|
|
elif "night" in state:
|
2017-12-29 11:18:07 +01:00
|
|
|
widget.set("state", "night")
|
2017-01-21 11:23:29 +01:00
|
|
|
else:
|
2017-12-29 11:18:07 +01:00
|
|
|
widget.set("state", "transition")
|
|
|
|
widget.set("transition", " ".join(line.split(" ")[2:]))
|
|
|
|
|
2019-08-21 18:26:59 +02:00
|
|
|
|
2017-12-29 11:18:07 +01:00
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
widget = bumblebee.output.Widget(full_text=self.text)
|
|
|
|
super(Module, self).__init__(engine, config, widget)
|
2019-08-21 19:12:58 +02:00
|
|
|
|
|
|
|
self._location = self.parameter("location", "geoclue2")
|
|
|
|
self._lat = self.parameter("lat", None)
|
|
|
|
self._lon = self.parameter("lon", None)
|
|
|
|
|
|
|
|
# Even if location method is set to manual, if we have no lat or lon,
|
|
|
|
# fall back to the geoclue2 method.
|
|
|
|
if self._location == "manual" and (self._lat is None
|
|
|
|
or self._lon is None):
|
|
|
|
self._location == "geoclue2"
|
|
|
|
|
|
|
|
if self._location == "ipinfo":
|
|
|
|
try:
|
|
|
|
location_url = "http://ipinfo.io/json"
|
|
|
|
location = requests.get(location_url).json()
|
|
|
|
self._lat, self._lon = location["loc"].split(",")
|
|
|
|
self._lat = str(float(self._lat))
|
|
|
|
self._lon = str(float(self._lon))
|
|
|
|
self._location = "manual"
|
|
|
|
except Exception:
|
|
|
|
# Fall back to geoclue2.
|
|
|
|
self._location = "geoclue2"
|
|
|
|
|
2017-12-29 11:18:07 +01:00
|
|
|
self._text = ""
|
|
|
|
self._condition = threading.Condition()
|
|
|
|
widget.set("condition", self._condition)
|
2019-08-21 18:26:59 +02:00
|
|
|
self._thread = threading.Thread(target=get_redshift_value,
|
2019-08-21 19:12:58 +02:00
|
|
|
args=(widget, self._location,
|
|
|
|
self._lat, self._lon))
|
2017-12-29 11:18:07 +01:00
|
|
|
self._thread.start()
|
|
|
|
self._condition.acquire()
|
|
|
|
self._condition.notify()
|
|
|
|
self._condition.release()
|
|
|
|
|
|
|
|
def text(self, widget):
|
|
|
|
return "{}".format(self._text)
|
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
widget = widgets[0]
|
|
|
|
self._condition.acquire()
|
|
|
|
self._condition.notify()
|
|
|
|
self._condition.release()
|
|
|
|
temp = widget.get("temp", "n/a")
|
2017-09-20 08:59:23 +02:00
|
|
|
self._text = temp
|
2017-12-29 11:18:07 +01:00
|
|
|
transition = widget.get("transition", None)
|
2017-09-20 08:59:23 +02:00
|
|
|
if transition:
|
|
|
|
self._text = "{} {}".format(temp, transition)
|
2017-01-21 11:23:29 +01:00
|
|
|
|
|
|
|
def state(self, widget):
|
2017-12-29 11:18:07 +01:00
|
|
|
return widget.get("state", None)
|
2017-01-21 11:23:29 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|