-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
WebcamsModel.py
113 lines (97 loc) · 3.81 KB
/
WebcamsModel.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
# Copyright (c) 2022 Aldo Hoeben / fieldOfView
# OctoPrintPlugin is released under the terms of the AGPLv3 or higher.
try:
from cura.ApplicationMetadata import CuraSDKVersion
except ImportError: # Cura <= 3.6
CuraSDKVersion = "6.0.0"
if CuraSDKVersion >= "8.0.0":
from PyQt6.QtCore import Qt
else:
from PyQt5.QtCore import Qt
from UM.Qt.ListModel import ListModel
from UM.Logger import Logger
from typing import List, Dict, Any, Union
class WebcamsModel(ListModel):
def __init__(
self,
protocol: str,
address: str,
port: int = 80,
basic_auth_string: str = "",
parent=None,
) -> None:
super().__init__(parent)
self._protocol = protocol
self._address = address
self._port = port
self._basic_auth_string = basic_auth_string
try:
user_role = Qt.ItemDataRole.UserRole
except AttributeError:
user_role = Qt.UserRole
self.addRoleName(user_role + 1, "name")
self.addRoleName(user_role + 2, "stream_url")
self.addRoleName(user_role + 3, "rotation")
self.addRoleName(user_role + 4, "mirror")
def deserialise(self, data: List[Dict[str, Any]]) -> None:
items = []
for webcam in data:
item = {
"name": "_default",
"stream_url": "",
"rotation": 0,
"mirror": False,
}
stream_url = ""
if "streamUrl" in webcam and webcam["streamUrl"] != None: # from /webcam/
stream_url = webcam["streamUrl"].strip()
elif "URL" in webcam and webcam["URL"] != None: # from /plugins/multicam
stream_url = webcam["URL"].strip()
if not stream_url: # empty string or None
continue
elif stream_url[:4].lower() == "http": # absolute uri
item["stream_url"] = stream_url
elif stream_url[:2] == "//": # protocol-relative
item["stream_url"] = "%s:%s" % (self._protocol, stream_url)
elif stream_url[:1] == ":": # domain-relative (on another port)
item["stream_url"] = "%s://%s%s" % (
self._protocol,
self._address,
stream_url,
)
elif stream_url[:1] == "/": # domain-relative (on same port)
if not self._basic_auth_string:
item["stream_url"] = "%s://%s:%d%s" % (
self._protocol,
self._address,
self._port,
stream_url,
)
else:
item["stream_url"] = "%s://%s@%s:%d%s" % (
self._protocol,
self._basic_auth_string,
self._address,
self._port,
stream_url,
)
else:
Logger.log("w", "Unusable stream url received: %s", stream_url)
item["stream_url"] = ""
if "rotate90" in webcam:
item["rotation"] = -90 if webcam["rotate90"] else 0
if webcam["flipH"] and webcam["flipV"]:
item["mirror"] = False
item["rotation"] += 180 # type: ignore
elif webcam["flipH"]:
item["mirror"] = True
item["rotation"] += 180 # type: ignore
elif webcam["flipV"]:
item["mirror"] = True
else:
item["mirror"] = False
if "name" in webcam and webcam["name"] != None:
item["name"] = webcam["name"]
items.append(item)
if self._items != items:
self.setItems(items)