Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_decorated_device_notification(self):
plc = pyads.Connection(TEST_SERVER_AMS_NET_ID, TEST_SERVER_AMS_PORT)
@plc.notification(pyads.PLCTYPE_INT)
def callback(handle, name, timestamp, value):
print (handle, name, timestamp, value)
with plc:
plc.add_device_notification("a", pyads.NotificationAttrib(20), callback)
plc.write_by_name("a", 1, pyads.PLCTYPE_INT)
pyads.open_port()
adr = pyads.get_local_address()
pyads.close_port()
self.assertIsNotNone(adr)
self.assertIsNone(pyads.get_local_address())
self.assertIsNone(pyads.read_state(adr))
self.assertIsNone(pyads.read_device_info(adr))
self.assertIsNone(
pyads.read_write(adr, 1, 2, pyads.PLCTYPE_INT, 1, pyads.PLCTYPE_INT)
)
self.assertIsNone(pyads.read(adr, 1, 2, pyads.PLCTYPE_INT))
self.assertIsNone(pyads.read_by_name(adr, "hello", pyads.PLCTYPE_INT))
self.assertIsNone(
pyads.add_device_notification(
adr, "test", pyads.NotificationAttrib(4), lambda x: x
)
def test_device_notification(self):
def callback(notification, data):
pass
handle_name = "test"
attr = pyads.NotificationAttrib(8)
requests = self.test_server.request_history
with self.plc:
notification, user = self.plc.add_device_notification(
handle_name, attr, callback
)
# Assert that Read/Write command was used to get the handle by name
self.assert_command_id(requests[0], constants.ADSCOMMAND_READWRITE)
# Assert that ADDDEVICENOTIFICATION was used to add device notification
self.assert_command_id(requests[1], constants.ADSCOMMAND_ADDDEVICENOTE)
self.plc.del_device_notification(notification, user)
# Assert that ADDDEVICENOTIFICATION was used to add device notification
self.assert_command_id(requests[2], constants.ADSCOMMAND_DELDEVICENOTE)
plc = pyads.Connection("127.0.0.1.1.1", 851)
self.assertIsNone(plc.get_local_address())
self.assertIsNone(plc.read_state())
self.assertIsNone(plc.read_device_info())
self.assertIsNone(plc.read_write(1, 2, pyads.PLCTYPE_INT, 1, pyads.PLCTYPE_INT))
self.assertIsNone(plc.read(1, 2, pyads.PLCTYPE_INT))
self.assertIsNone(plc.read_by_name("hello", pyads.PLCTYPE_INT))
self.assertIsNone(plc.get_handle("hello"))
self.assertIsNone(
plc.read_structure_by_name(
"hello", (("", pyads.PLCTYPE_BOOL, 1), ("", pyads.PLCTYPE_BOOL, 1))
)
)
self.assertIsNone(
plc.add_device_notification(
"test", pyads.NotificationAttrib(4), lambda x: x
)
def add_device_notification(self, name, plc_datatype, callback):
"""Add a notification to the ADS devices."""
import pyads
attr = pyads.NotificationAttrib(ctypes.sizeof(plc_datatype))
with self._lock:
try:
hnotify, huser = self._client.add_device_notification(
name, attr, self._device_notification_callback
)
except pyads.ADSError as err:
_LOGGER.error("Error subscribing to %s: %s", name, err)
else:
hnotify = int(hnotify)
self._notification_items[hnotify] = NotificationItem(
hnotify, huser, name, plc_datatype, callback
)
_LOGGER.debug(
"Added device notification %d for variable %s", hnotify, name