forked from andrius/neohubapi
Cache firmware version when connecting to hub.
This commit is contained in:
parent
1549113abd
commit
1fe59a4255
17
neohub.py
17
neohub.py
|
@ -3,14 +3,18 @@
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
class NeoHub:
|
class NeoHub:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self._logger = logging.getLogger('neohub')
|
||||||
|
self._firmware_version = 0
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
async def connect(self, host='Neo-Hub', port='4242'):
|
async def connect(self, host='Neo-Hub', port='4242'):
|
||||||
self._reader, self._writer = await asyncio.open_connection(host, port)
|
self._reader, self._writer = await asyncio.open_connection(host, port)
|
||||||
|
self._firmware_version = await self._firmware()
|
||||||
|
|
||||||
|
|
||||||
async def send(self, message):
|
async def send(self, message):
|
||||||
|
@ -20,10 +24,21 @@ class NeoHub:
|
||||||
|
|
||||||
data = await self._reader.read(4096)
|
data = await self._reader.read(4096)
|
||||||
json_string = data.decode('utf-8')
|
json_string = data.decode('utf-8')
|
||||||
|
self._logger.debug(f"Received message: {json_string}")
|
||||||
return json.loads(json_string)
|
return json.loads(json_string)
|
||||||
|
|
||||||
|
def firmware(self):
|
||||||
|
return self._firmware_version
|
||||||
|
|
||||||
async def firmware(self):
|
async def firmware(self):
|
||||||
|
return self._firmware_version
|
||||||
|
|
||||||
|
async def _firmware(self):
|
||||||
|
'''
|
||||||
|
NeoHub firmware version
|
||||||
|
'''
|
||||||
|
|
||||||
message = {"FIRMWARE": 0}
|
message = {"FIRMWARE": 0}
|
||||||
result = await self.send(message)
|
result = await self.send(message)
|
||||||
return result['firmware version']
|
firmware_version = int(result['firmware version'])
|
||||||
|
return firmware_version
|
||||||
|
|
5
test.py
5
test.py
|
@ -3,8 +3,11 @@
|
||||||
# SPDX-FileCopyrightText: 2020 Andrius Štikonas <andrius@stikonas.eu>
|
# SPDX-FileCopyrightText: 2020 Andrius Štikonas <andrius@stikonas.eu>
|
||||||
# SPDX-License-Identifier: LGPL-3.0-or-later
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import neohub
|
import neohub
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
async def run():
|
async def run():
|
||||||
hub = neohub.NeoHub()
|
hub = neohub.NeoHub()
|
||||||
|
@ -12,4 +15,6 @@ async def run():
|
||||||
result = await hub.firmware()
|
result = await hub.firmware()
|
||||||
print(f"Firmware: {result}")
|
print(f"Firmware: {result}")
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
asyncio.run(run())
|
asyncio.run(run())
|
||||||
|
|
Loading…
Reference in New Issue