Skip to content

Commit

Permalink
Merge pull request #43 from pawlizio/master
Browse files Browse the repository at this point in the history
Reboot function added
  • Loading branch information
Julius2342 committed Jul 5, 2020
2 parents 790ee7a + 8462e24 commit c4ae11e
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyvlx/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, loop, config):
self.transport = None
self.frame_received_cbs = []
self.connected = False
self.connection_counter = 0

def __del__(self):
"""Destruct connection."""
Expand All @@ -89,6 +90,7 @@ async def connect(self):
ssl=self.create_ssl_context(),
)
self.connected = True
self.connection_counter += 1

def register_frame_received_cb(self, callback):
"""Register frame received callback."""
Expand Down
7 changes: 7 additions & 0 deletions pyvlx/frame_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
FrameNodeStatePositionChangedNotification,
FramePasswordEnterConfirmation,
FramePasswordEnterRequest,
FrameGatewayRebootRequest,
FrameGatewayRebootConfirmation,
FrameSessionFinishedNotification,
FrameSetNodeNameConfirmation,
FrameSetNodeNameRequest,
Expand Down Expand Up @@ -84,6 +86,11 @@ def create_frame(command):
if command == Command.GW_PASSWORD_ENTER_CFM:
return FramePasswordEnterConfirmation()

if command == Command.GW_REBOOT_REQ:
return FrameGatewayRebootRequest()
if command == Command.GW_REBOOT_CFM:
return FrameGatewayRebootConfirmation()

if command == Command.GW_CS_DISCOVER_NODES_REQ:
return FrameDiscoverNodesRequest()
if command == Command.GW_CS_DISCOVER_NODES_CFM:
Expand Down
4 changes: 4 additions & 0 deletions pyvlx/frames/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
FramePasswordEnterRequest,
PasswordEnterConfirmationStatus,
)
from .frame_reboot import (
FrameGatewayRebootRequest,
FrameGatewayRebootConfirmation
)
from .frame_set_node_name import (
FrameSetNodeNameConfirmation,
FrameSetNodeNameRequest,
Expand Down
31 changes: 31 additions & 0 deletions pyvlx/frames/frame_reboot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Module for reboot frame classes."""
from pyvlx.const import Command

from .frame import FrameBase


class FrameGatewayRebootRequest(FrameBase):
"""Frame for requesting version."""

PAYLOAD_LEN = 0

def __init__(self):
"""Init Frame."""
super().__init__(Command.GW_REBOOT_REQ)

def __str__(self):
"""Return human readable string."""
return '<FrameGatewayRebootRequest/>'

class FrameGatewayRebootConfirmation(FrameBase):
"""Frame for response for get version requests."""

PAYLOAD_LEN = 0

def __init__(self):
"""Init Frame."""
super().__init__(Command.GW_REBOOT_CFM)

def __str__(self):
"""Return human readable string."""
return '<FrameGatewayRebootConfirmation/>'
6 changes: 6 additions & 0 deletions pyvlx/pyvlx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .login import Login
from .node_updater import NodeUpdater
from .nodes import Nodes
from .reboot import Reboot
from .scenes import Scenes
from .set_utc import set_utc

Expand Down Expand Up @@ -48,6 +49,11 @@ async def connect(self):
if not login.success:
raise PyVLXException("Login to KLF 200 failed, check credentials")

async def reboot_gateway(self):
PYVLXLOG.warning("KLF 200 reboot initiated")
reboot = Reboot(pyvlx=self)
await reboot.do_api_call()

async def update_version(self):
"""Retrieve version and protocol version from API."""
get_version = GetVersion(pyvlx=self)
Expand Down
24 changes: 24 additions & 0 deletions pyvlx/reboot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Module for handling the login to API."""
from .api_event import ApiEvent
from .frames import FrameGatewayRebootRequest, FrameGatewayRebootConfirmation
from .log import PYVLXLOG

class Reboot(ApiEvent):
"""Class for handling login to API."""

def __init__(self, pyvlx):
"""Initialize login class."""
super().__init__(pyvlx=pyvlx)
self.pyvlx = pyvlx

async def handle_frame(self, frame):
"""Handle incoming API frame, return True if this was the expected frame."""
if isinstance(frame, FrameGatewayRebootConfirmation):
PYVLXLOG.warning("KLF200 is rebooting")
self.pyvlx.connection.connected = False
return True
return False

def request_frame(self):
"""Construct initiating frame."""
return FrameGatewayRebootRequest()
28 changes: 28 additions & 0 deletions test/frame_gw_reboot_cfm_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Unit tests for FrameGatewayRebootConfirmation."""
import unittest

from pyvlx.frame_creation import frame_from_raw
from pyvlx.frames import FrameGatewayRebootConfirmation


class TestFrameRebootConfirmation(unittest.TestCase):
"""Test class FrameSetUTCConfirmation."""

# pylint: disable=too-many-public-methods,invalid-name

EXAMPLE_FRAME = b'\x00\x03\x00\x02\x01'

def test_bytes(self):
"""Test FrameGatewayRebootConfirmation."""
frame = FrameGatewayRebootConfirmation()
self.assertEqual(bytes(frame), self.EXAMPLE_FRAME)

def test_frame_from_raw(self):
"""Test parse FrameGatewayRebootConfirmation from raw."""
frame = frame_from_raw(self.EXAMPLE_FRAME)
self.assertTrue(isinstance(frame, FrameGatewayRebootConfirmation))

def test_str(self):
"""Test string representation of FrameGatewayRebootConfirmation."""
frame = FrameGatewayRebootConfirmation()
self.assertEqual(str(frame), "<FrameGatewayRebootConfirmation/>")
28 changes: 28 additions & 0 deletions test/frame_gw_reboot_req_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Unit tests for FrameGatewayRebootRequest."""
import unittest

from pyvlx.frame_creation import frame_from_raw
from pyvlx.frames import FrameGatewayRebootRequest


class TestFrameRebootRequest(unittest.TestCase):
"""Test class FrameGatewayRebootRequest."""

# pylint: disable=too-many-public-methods,invalid-name

EXAMPLE_FRAME = b'\x00\x03\x00\x01\x02'

def test_bytes(self):
"""Test FrameGatewayRebootRequest."""
frame = FrameGatewayRebootRequest()
self.assertEqual(bytes(frame), self.EXAMPLE_FRAME)

def test_frame_from_raw(self):
"""Test parse FrameGatewayRebootRequest from raw."""
frame = frame_from_raw(self.EXAMPLE_FRAME)
self.assertTrue(isinstance(frame, FrameGatewayRebootRequest))

def test_str(self):
"""Test string representation of FrameGatewayRebootRequest."""
frame = FrameGatewayRebootRequest()
self.assertEqual(str(frame), "<FrameGatewayRebootRequest/>")

0 comments on commit c4ae11e

Please sign in to comment.