Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pyhik/hikvision.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,13 @@ def process_stream(self, tree):
raw_etype = self.element_text(tree, 'eventType')
if raw_etype is None:
# Nothing identifies the event, so there is nothing to report.
_LOGGING.debug('Alert packet carries no event type, ignoring.')
# Log the packet itself: without it there is no way to tell from
# a user's debug log whether the type is missing, nested
# elsewhere, or sitting in a namespace we did not expect.
if _LOGGING.isEnabledFor(logging.DEBUG):
_LOGGING.debug(
'Alert packet carries no event type, ignoring: %s',
ET.tostring(tree, encoding='unicode'))
return

try:
Expand Down
17 changes: 17 additions & 0 deletions test/test_hikvision.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,23 @@ def test_process_stream_ignores_unsupported_event_type(self):

self.assertFalse(camera.fetch_attributes("Motion", 1)[0])

def test_process_stream_logs_packet_without_event_type(self):
camera = make_camera()
tree = ET.fromstring(
ALERT_XML.replace("<eventType>{etype}</eventType>", "")
.format(extra="<channelName>doroga</channelName>"))

with self.assertLogs("pyhik.hikvision", level=logging.DEBUG) as logs:
camera.process_stream(tree)

ignored = [line for line in logs.output
if "carries no event type" in line]
self.assertEqual(len(ignored), 1)
# The raw packet, namespace included, is in the log line.
self.assertIn("doroga", ignored[0])
self.assertIn("http://www.hikvision.com/ver20/XMLSchema", ignored[0])
self.assertFalse(camera.fetch_attributes("Motion", 1)[0])

def test_process_stream_still_handles_known_event_type(self):
camera = make_camera()
camera.process_stream(alert())
Expand Down
Loading