forked from fieldOfView/Cura-OctoPrintPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebcamsModel.py
79 lines (64 loc) · 3.11 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
# Copyright (c) 2020 Aldo Hoeben / fieldOfView
# OctoPrintPlugin is released under the terms of the AGPLv3 or higher.
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
self.addRoleName(Qt.UserRole + 1, "name")
self.addRoleName(Qt.UserRole + 2, "stream_url")
self.addRoleName(Qt.UserRole + 3, "rotation")
self.addRoleName(Qt.UserRole + 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)