Quellcode durchsuchen

more tweaks and influx logging

Thomas B vor 2 Monaten
Ursprung
Commit
e5cacfdbc2
2 geänderte Dateien mit 56 neuen und 10 gelöschten Zeilen
  1. 39
    10
      client/brightness.py
  2. 17
    0
      client/influx.py

+ 39
- 10
client/brightness.py Datei anzeigen

@@ -3,17 +3,18 @@
3 3
 import lux
4 4
 import ddc
5 5
 import time
6
+import influx
6 7
 
7 8
 filter_fact = 0.9
8 9
 
9
-c_in = 0.6, -60.0, # in_a, in_b
10
+c_in = 0.6, -30.0, # in_a, in_b
10 11
 calibration = {
11 12
     "HPN:HP 27xq:CNK1072BJY": [
12
-        1.0, 30.0, # out_a, out_b
13
+        1.0, 10.0, # out_a, out_b
13 14
     ],
14 15
 
15 16
     "MSI:MSI G27CQ4:": [
16
-        1.0, 20.0, # out_a, out_b
17
+        1.0, 0.0, # out_a, out_b
17 18
     ],
18 19
 }
19 20
 
@@ -53,19 +54,47 @@ if __name__ == "__main__":
53 54
 
54 55
     brightness = lux.read_brightness(usb)
55 56
     print("Brightness:", brightness)
57
+    last_brightness = brightness
56 58
 
57 59
     print()
58 60
     print("{}: Starting main loop".format(time.ctime()))
59 61
     print()
60 62
 
63
+    time_brightness = time.time()
64
+    time_displays = time.time()
65
+
61 66
     while True:
67
+        # read brightness at approx. 1Hz with low-pass filtering
68
+        time.sleep(1.0)
62 69
         brightness = filter_lux(brightness, lux.read_brightness(usb))
63 70
 
64
-        for d in disps:
65
-            val = lux_to_disp(d["name"], brightness)
66
-            if val != d["prev"]:
67
-                d["prev"] = val
68
-                print("{}: Setting \"{}\" to {}".format(time.ctime(), d["name"], val))
69
-                ddc.ddc_set(d["_id"], val)
71
+        # print brightness changes at most every 5s
72
+        if (time.time() - time_brightness) > 5.0:
73
+            time_brightness = time.time()
74
+
75
+            if int(brightness) != last_brightness:
76
+                last_brightness = int(brightness)
77
+                print("{}: Brightness: {}".format(time.ctime(), last_brightness))
78
+
79
+            try:
80
+                influx.write("brightness,location=pc-back", "lux", brightness)
81
+            except:
82
+                pass
83
+
84
+        # set displays at most every 10s
85
+        if (time.time() - time_displays) > 10.0:
86
+            time_displays = time.time()
87
+
88
+            for d in disps:
89
+                val = lux_to_disp(d["name"], brightness)
90
+                if val != d["prev"]:
91
+                    d["prev"] = val
92
+                    print("{}: Setting \"{}\" to {}".format(time.ctime(), d["name"], val))
93
+                    ddc.ddc_set(d["_id"], val)
94
+
95
+                    try:
96
+                        name = '_'.join(d["name"].split())
97
+                        influx.write("brightness,location=" + name, "backlight", val)
98
+                    except:
99
+                        pass
70 100
 
71
-        time.sleep(1.0)

+ 17
- 0
client/influx.py Datei anzeigen

@@ -0,0 +1,17 @@
1
+#!/usr/bin/env python
2
+
3
+import aiohttp
4
+import asyncio
5
+
6
+influx_host = 'http://INFLUX_DB_IP_HERE:8086'
7
+influx_path = '/write?db=INFLUX_DB_NAME_HERE'
8
+
9
+cache = {}
10
+
11
+async def async_write(id_tags, name, value):
12
+    data = id_tags + " " + name + "=" + str(float(value))
13
+    async with aiohttp.ClientSession(influx_host) as session:
14
+        await session.post(influx_path, data=data)
15
+
16
+def write(id_tags, name, value):
17
+    asyncio.run(async_write(id_tags, name, value))

Laden…
Abbrechen
Speichern