-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
169 lines (135 loc) · 5.58 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import pathlib
import click
import time
import json
import sys
from pathlib import Path
import subprocess
from scipy.interpolate import interp1d
from scipy import interpolate
import matplotlib.pyplot as plt
import numpy as np
from threading import Thread
import geocoder
from astral.sun import sun
from astral import LocationInfo
import datetime
from timezonefinder import TimezoneFinder
"""
Config Syntax:
characteristic line is a list of tuples. A tuple represents (<minute of day>,<brightness percentage>)
"""
default_json = {
"ddm_path": "C:\\Program Files (x86)\\Dell\\Dell Display Manager\\ddm.exe",
"characteristic_line": [[0, 1], [60, 1], [120, 1], [360, 10], [420, 13], [480, 16], [600, 85], [720, 100],
[840, 100], [1020, 15], [1440, 5]],
}
def validate_config(config):
"""
Validaates the config
ToDo: validate the characteristic curve
:param config: configuration dictionary to validate
:return: throws exception in case of misconfiguration
"""
ddm = Path(config["ddm_path"])
if not ddm.exists():
raise ValueError("ddm.exe could not be found")
# config["characteristic_line"]
def interpolate_characteristic_line(config, showgraph):
"""
Interpolates the datapoints of the brightness characteristic line
ToDo: Use periodic interpolation to eliminate the gap at 24h
"""
minutes_of_day = 24 * 60
# Sort list
l = config["characteristic_line"]
l.sort(key=lambda x: x[0])
result = list(map(list, zip(*l)))
x, y = result
x.append(1440)
y.append(y[len(y)-1])
x.insert(0,0)
y.insert(0,y[len(y)-1])
f = interp1d(x, y)
f2 = interp1d(x, y, kind='cubic')
tck = interpolate.splrep(x, y, s=0)
x_spline = np.arange(0, minutes_of_day, 60)
y_spline = interpolate.splev(x_spline, tck, der=0)
x_samples = np.linspace(min(x), max(x), num=minutes_of_day, endpoint=True)
if showgraph:
x_ = [e / 60 for e in x]
x_samples_ = [e / 60 for e in x_samples]
x_spline = [e / 60 for e in x_spline]
plt.plot(x_, y, 'o', x_samples_, f(x_samples), '-', x_samples_, f2(x_samples), '--', x_spline, y_spline, ':')
plt.legend(['data', 'linear', 'cubic', 'spline'], loc='best')
plt.xlabel('hour')
plt.ylabel('brightness percentage')
image_filename = 'characteristic_curve.png'
plt.savefig(image_filename)
plt.close()
imageViewerFromCommandLine = {'linux': 'xdg-open',
'win32': 'explorer',
'darwin': 'open'}[sys.platform]
subprocess.run([imageViewerFromCommandLine, pathlib.Path(image_filename)])
# use linear interpolation for now, as it provides the minimal gap between 24:00 and 0:00, if configured okay
return x_samples, f(x_samples)
def set_brightness(config, brightness_percentage):
process = subprocess.run([config["ddm_path"], '/SetBrightnessLevel', str(brightness_percentage)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True, check=True)
def wait_for_next_interpolation_step(interval):
"""
:param interval: time interval in minutes
:return:
"""
time.sleep(interval * 60)
def min_of_day(datetime: datetime):
return int(datetime.hour * 60 + datetime.minute)
def generate_characteristic_curve_based_on_sunset_sunrise():
g = geocoder.ip('me')
print(g.latlng)
tf = TimezoneFinder()
time_zone = tf.timezone_at(lat=g.lat, lng=g.lng)
city = LocationInfo(g.city, g.state, str(time_zone), 51.5, -0.116)
sun_info = sun(city.observer, date=datetime.datetime.today())
characteristic_line = [[min_of_day(sun_info["dawn"]), 10],
[min_of_day(sun_info["sunrise"]), 25],
[min_of_day(sun_info["sunrise"]) + 120, 100],
[min_of_day(sun_info["noon"]), 100],
[min_of_day(sun_info["sunset"]) - 120, 100],
[min_of_day(sun_info["sunset"]), 20],
[min_of_day(sun_info["dusk"]), 10]]
return characteristic_line
@click.command()
@click.option('--interval', default=1, help='update interval in minutes')
@click.option('--showgraph', default=False, help='start with interpolation graph')
@click.option('--chracteristic_line', default='sunset',
help='characteristic curve to use: "config" for custom config, "sunset" for calculation based on sunset/sunrise')
def main(interval, showgraph, chracteristic_line):
click.echo('Hello World!')
try:
conf_file = Path("./config.json")
with open(conf_file) as f:
config = json.load(f)
validate_config(config)
except Exception as e:
print(e)
config = default_json
print("Using config:", config)
if chracteristic_line == "sunset":
c_line = generate_characteristic_curve_based_on_sunset_sunrise()
del config['characteristic_line']
config['characteristic_line'] = c_line
x, f_x = interpolate_characteristic_line(config, showgraph)
# run brightness scheduler here
while True:
now = time.localtime()
minute_of_day = now.tm_hour * 60 + now.tm_min
set_brightness(config, int(f_x[minute_of_day]))
print(time.strftime("%x %H:%M:%S", now), "Brightness set to: ", int(f_x[minute_of_day]), '%')
thread = Thread(target=wait_for_next_interpolation_step, args=(interval,))
thread.start()
thread.join()
if __name__ == '__main__':
main()