|
| 1 | +import aiohttp |
1 | 2 | import json |
2 | 3 | import logging |
3 | | -import requests |
4 | 4 |
|
| 5 | +from .exceptions import FullyKioskError |
5 | 6 |
|
6 | | -class FullyKiosk: |
7 | | - def __init__(self, host, port, password): |
8 | | - self.host = host |
9 | | - self.port = port |
10 | | - self.password = password |
| 7 | +_LOGGER = logging.getLogger(__name__) |
11 | 8 |
|
12 | | - self._deviceInfo = None |
| 9 | +RESPONSE_STATUS = "status" |
| 10 | +RESPONSE_STATUSTEXT = "statustext" |
| 11 | +RESPONSE_ERRORSTATUS = "Error" |
13 | 12 |
|
14 | | - def sendCommand(self, cmd, **kwargs): |
15 | | - url = f"http://{self.host}:{self.port}/?cmd={cmd}&password={self.password}&type=json" |
16 | | - for key, value in kwargs.items(): |
17 | | - if value is not None: |
18 | | - url = url + f"&{key}={value}" |
19 | 13 |
|
20 | | - try: |
21 | | - result = json.loads(requests.get(url, timeout=10).content) |
22 | | - return result |
23 | | - except requests.exceptions.Timeout: |
24 | | - print("Timeout error") |
| 14 | +class FullyKiosk: |
| 15 | + def __init__(self, session, host, port, password): |
| 16 | + self._rh = _RequestsHandler(session, host, port) |
| 17 | + self._password = password |
| 18 | + self._deviceInfo = None |
| 19 | + |
| 20 | + async def sendCommand(self, cmd, **kwargs): |
| 21 | + data = await self._rh.get( |
| 22 | + cmd=cmd, password=self._password, type="json", **kwargs |
| 23 | + ) |
| 24 | + if RESPONSE_STATUS in data and data[RESPONSE_STATUS] == RESPONSE_ERRORSTATUS: |
| 25 | + raise FullyKioskError(RESPONSE_ERRORSTATUS, data[RESPONSE_STATUSTEXT]) |
| 26 | + return data |
25 | 27 |
|
26 | | - def getDeviceInfo(self): |
27 | | - result = self.sendCommand("deviceInfo") |
| 28 | + async def getDeviceInfo(self): |
| 29 | + result = await self.sendCommand("deviceInfo") |
28 | 30 | self._deviceInfo = result |
29 | 31 | return self._deviceInfo |
30 | 32 |
|
31 | 33 | @property |
32 | 34 | def deviceInfo(self): |
33 | 35 | return self._deviceInfo |
34 | 36 |
|
35 | | - def startScreensaver(self): |
36 | | - return self.sendCommand("startScreensaver") |
| 37 | + async def startScreensaver(self): |
| 38 | + await self.sendCommand("startScreensaver") |
37 | 39 |
|
38 | | - def stopScreensaver(self): |
39 | | - return self.sendCommand("stopScreensaver") |
| 40 | + async def stopScreensaver(self): |
| 41 | + await self.sendCommand("stopScreensaver") |
40 | 42 |
|
41 | | - def screenOn(self): |
42 | | - return self.sendCommand("screenOn") |
| 43 | + async def screenOn(self): |
| 44 | + await self.sendCommand("screenOn") |
43 | 45 |
|
44 | | - def screenOff(self): |
45 | | - return self.sendCommand("screenOff") |
| 46 | + async def screenOff(self): |
| 47 | + await self.sendCommand("screenOff") |
46 | 48 |
|
47 | | - def setScreenBrightness(self, brightness): |
48 | | - return self.sendCommand( |
| 49 | + async def setScreenBrightness(self, brightness): |
| 50 | + await self.sendCommand( |
49 | 51 | "setStringSetting", key="screenBrightness", value=brightness |
50 | 52 | ) |
51 | 53 |
|
52 | | - def setAudioVolume(self, volume, stream=None): |
53 | | - return self.sendCommand("setAudioVolume", volume=volume, stream=stream) |
| 54 | + async def setAudioVolume(self, volume, stream=None): |
| 55 | + await self.sendCommand("setAudioVolume", level=volume, stream=stream) |
54 | 56 |
|
55 | | - def restartApp(self): |
56 | | - return self.sendCommand("restartApp") |
| 57 | + async def restartApp(self): |
| 58 | + await self.sendCommand("restartApp") |
57 | 59 |
|
58 | | - def loadStartUrl(self): |
59 | | - return self.sendCommand("loadStartUrl") |
| 60 | + async def loadStartUrl(self): |
| 61 | + await self.sendCommand("loadStartUrl") |
60 | 62 |
|
61 | | - def loadUrl(self, url): |
62 | | - return self.sendCommand("loadUrl", url=url) |
| 63 | + async def loadUrl(self, url): |
| 64 | + await self.sendCommand("loadUrl", url=url) |
63 | 65 |
|
64 | | - def playSound(self, url, stream=None): |
65 | | - return self.sendCommand("playSound", url=url, stream=stream) |
| 66 | + async def playSound(self, url, stream=None): |
| 67 | + await self.sendCommand("playSound", url=url, stream=stream) |
66 | 68 |
|
67 | | - def stopSound(self): |
68 | | - return self.sendCommand("stopSound") |
| 69 | + async def stopSound(self): |
| 70 | + await self.sendCommand("stopSound") |
69 | 71 |
|
70 | | - def toForeground(self): |
71 | | - return self.sendCommand("toForeground") |
| 72 | + async def toForeground(self): |
| 73 | + await self.sendCommand("toForeground") |
72 | 74 |
|
73 | | - def startApplication(self, application): |
74 | | - return self.sendCommand("startApplication", package=application) |
| 75 | + async def startApplication(self, application): |
| 76 | + await self.sendCommand("startApplication", package=application) |
75 | 77 |
|
76 | | - def setConfigurationString(self, setting, stringValue): |
77 | | - return self.sendCommand("setStringSetting", key=setting, value=stringValue) |
| 78 | + async def setConfigurationString(self, setting, stringValue): |
| 79 | + await self.sendCommand("setStringSetting", key=setting, value=stringValue) |
78 | 80 |
|
79 | | - def setConfigurationBool(self, setting, boolValue): |
80 | | - return self.sendCommand("setBooleanSetting", key=setting, value=boolValue) |
| 81 | + async def setConfigurationBool(self, setting, boolValue): |
| 82 | + await self.sendCommand("setBooleanSetting", key=setting, value=boolValue) |
81 | 83 |
|
82 | | - def enableLockedMode(self): |
83 | | - return self.sendCommand("enableLockedMode") |
| 84 | + async def enableLockedMode(self): |
| 85 | + await self.sendCommand("enableLockedMode") |
84 | 86 |
|
85 | | - def disableLockedMode(self): |
86 | | - return self.sendCommand("disableLockedMode") |
| 87 | + async def disableLockedMode(self): |
| 88 | + await self.sendCommand("disableLockedMode") |
87 | 89 |
|
88 | | - def lockKiosk(self): |
89 | | - return self.sendCommand("lockKiosk") |
| 90 | + async def lockKiosk(self): |
| 91 | + await self.sendCommand("lockKiosk") |
90 | 92 |
|
91 | | - def unlockKiosk(self): |
92 | | - return self.sendCommand("unlockKiosk") |
| 93 | + async def unlockKiosk(self): |
| 94 | + await self.sendCommand("unlockKiosk") |
93 | 95 |
|
94 | | - def rebootDevice(self): |
95 | | - return self.sendCommand("rebootDevice") |
| 96 | + async def rebootDevice(self): |
| 97 | + await self.sendCommand("rebootDevice") |
| 98 | + |
| 99 | + |
| 100 | +class _RequestsHandler: |
| 101 | + """Internal class to create FullyKiosk requests""" |
| 102 | + |
| 103 | + def __init__(self, session: aiohttp.ClientSession, host, port): |
| 104 | + self.headers = {"Accept": "application/json"} |
| 105 | + |
| 106 | + self.session = session |
| 107 | + self.host = host |
| 108 | + self.port = port |
| 109 | + |
| 110 | + async def get(self, **kwargs): |
| 111 | + url = f"http://{self.host}:{self.port}" |
| 112 | + params = [] |
| 113 | + |
| 114 | + for key, value in kwargs.items(): |
| 115 | + if value is not None: |
| 116 | + params.append((key, str(value))) |
| 117 | + |
| 118 | + _LOGGER.debug("Sending request to: %s", url) |
| 119 | + _LOGGER.debug("Parameters: %s", params) |
| 120 | + async with self.session.get( |
| 121 | + url, headers=self.headers, params=params |
| 122 | + ) as response: |
| 123 | + if response.status != 200: |
| 124 | + _LOGGER.warning( |
| 125 | + "Invalid response from Fully Kiosk Browser API: %s", response.status |
| 126 | + ) |
| 127 | + raise FullyKioskError(response.status, await response.text()) |
| 128 | + |
| 129 | + data = await response.json(content_type="text/html") |
| 130 | + _LOGGER.debug(json.dumps(data)) |
| 131 | + return data |
0 commit comments