aboutsummaryrefslogtreecommitdiffstats
path: root/upsmartmonitor.py
blob: a23bacbb3721897b784f64b418ae7181f0457b24 (plain)
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
import subprocess
import tempfile
import pyautogui
import cv2
import os

def get_window_geometries():
    return [
        s.split()[:6] + [" ".join(s.split()[7:])] 
        for s in subprocess.run(["wmctrl", "-l", "-G"], stdout = subprocess.PIPE).stdout.decode().split("\n")[:-1]
    ]

def find_upsmart_geometry():
    for g in get_window_geometries():
        if g[-1] == "UPSmart":
            return g
    raise Exception("Couldn't find UPSMart application")

def focus_upsmart():
    # it would be much nicer to use an API such as wmctrl to focus the application...
    # but it doesn't seem to work with UPSmart specifically, perhaps because you need
    # to start it as root
    geom = find_upsmart_geometry()

    pyautogui.moveTo(int(geom[2]), int(geom[3]), duration = 0.5)
    pyautogui.click()

def main():
    focus_upsmart()

    with tempfile.TemporaryDirectory() as td:
        fp = os.path.join(td, "upsmart.jpg")
        subprocess.run(["gnome-screenshot", "--file=%s" % fp, "-w"])

        im = cv2.imread(fp)
        # im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
        print(fp)

        cv2.imshow("im", im)
        cv2.waitKey(0)
        
        # plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
        # plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
        # plt.show()


if __name__ == "__main__":
    main()