Skip to content

Commit

Permalink
Merge pull request #112 from dxpke/connection-addr-setget
Browse files Browse the repository at this point in the history
Add getters/setters for connection netid and port
  • Loading branch information
stlehmann authored May 2, 2020
2 parents 5fde7cb + 9201df6 commit d39784d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* [#111](https://github.com/stlehmann/pyads/pull/111) test cases for notification decorators
* [#113](https://github.com/stlehmann/pyads/pull/113) Add option not to check for data size
* [#118](https://github.com/stlehmann/pyads/pull/118) Add support for arrays in notification decorator
* [#112](https://github.com/stlehmann/pyads/pull/112) Add getters/setters for connection netid and port

### Changed
* [#128](https://github.com/stlehmann/pyads/pull/128) Deprecation warning for older non-class functions. In
Expand Down
24 changes: 24 additions & 0 deletions pyads/ads.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,30 @@ def __init__(self, ams_net_id, ams_net_port, ip_address=None):
self._open = False
self._notifications = {} # type: Dict[int, str]

@property
def ams_netid(self):
# type: () -> str
return self._adr.netid

@ams_netid.setter
def ams_netid(self, netid):
# type: (str) -> None
if self._open:
raise AttributeError("Setting netid is not allowed while connection is open.")
self._adr.netid = netid

@property
def ams_port(self):
# type: () -> int
return self._adr.port

@ams_port.setter
def ams_port(self, port):
# type: (int) -> None
if self._open:
raise AttributeError("Setting port is not allowed while connection is open.")
self._adr.port = port

def __enter__(self):
# type: () -> Connection
"""Open on entering with-block."""
Expand Down
18 changes: 18 additions & 0 deletions tests/test_connection_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ def test_open_twice(self):
# connection should now be closed
self.assertFalse(self.plc.is_open)

def test_netid_port(self):
self.assertEqual(self.plc.ams_netid, TEST_SERVER_AMS_NET_ID)
self.assertEqual(self.plc.ams_port, TEST_SERVER_AMS_PORT)
with self.assertRaises(ValueError):
self.plc.ams_netid = "1.1.1.1.1.1.1"
self.plc.ams_netid = "1.1.1.1.1.1"
self.assertEqual(self.plc.ams_netid, "1.1.1.1.1.1")
self.plc.ams_port = 1
self.assertEqual(self.plc.ams_port, 1)

# test for AttributeError when trying to set netid or port
# for an open connection
self.plc._open = True
with self.assertRaises(AttributeError):
self.plc.ams_netid = "1.1.1.1.1.2"
with self.assertRaises(AttributeError):
self.plc.ams_port = 2

def test_read_device_info(self):
with self.plc:
name, version = self.plc.read_device_info()
Expand Down

0 comments on commit d39784d

Please sign in to comment.