-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvnc_device_monitor.py
58 lines (49 loc) · 1.85 KB
/
vnc_device_monitor.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
from screenshot_manager import ScreenshotManager
from PIL import Image
import io
from utils.logger import get_logger
from vncdotool import api
class VNCDeviceMonitor:
def __init__(self, device_id, device_config):
self.device_id = device_id
self.config = device_config
self.logger = get_logger(__name__)
self.client = None
async def connect(self):
"""Connect to VNC server"""
try:
host = self.config.get('vnc_host')
port = self.config.get('vnc_port', 5900)
password = self.config.get('vnc_password')
if not (host and password):
raise ValueError("VNC host and password are required")
connection_string = f"vnc://{host}:{port}"
self.client = await api.connect(connection_string, password=password)
return True
except Exception as e:
self.logger.error(f"VNC connection failed: {str(e)}")
return False
async def capture_screenshot(self):
"""Capture screenshot from VNC connection"""
try:
if not self.client:
if not await self.connect():
return None
# Capture screenshot
png_data = await self.client.capture()
# Convert to PIL Image
image = Image.open(io.BytesIO(png_data))
return image
except Exception as e:
self.logger.error(f"VNC screenshot failed: {str(e)}")
await self.disconnect()
return None
async def disconnect(self):
"""Disconnect from VNC server"""
if self.client:
try:
await self.client.disconnect()
except Exception as e:
self.logger.error(f"VNC disconnect error: {str(e)}")
finally:
self.client = None