Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes to support amcrest AD110 and other small bugfixes #162

Merged
merged 10 commits into from
Sep 23, 2020
Merged
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
29 changes: 24 additions & 5 deletions src/amcrest/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
from .exceptions import CommError

_LOGGER = logging.getLogger(__name__)
_START_STOP = re.compile(r"Code=([^;]+);action=(Start|Stop)", flags=re.S)

_REG_PARSE_KEY_VALUE = re.compile(r"(?P<key>.+?)(?:=)(?P<value>.+?)(?:;|$)")
_REG_PARSE_MALFORMED_JSON = re.compile(
r'(?P<key>"[^"\\]*(?:\\.[^"\\]*)*"|[^\s"]+)\s:\s(?P<value>"[^"\\]*(?:\\.[^"\\]*)*"|[^\s"]+)'
blademckain marked this conversation as resolved.
Show resolved Hide resolved
)

def _event_lines(ret):
line = ""
Expand Down Expand Up @@ -225,11 +227,28 @@ def event_stream(self, eventcodes, retries=None, timeout_cmd=None):
ret.close()

def event_actions(self, eventcodes, retries=None, timeout_cmd=None):
"""Return a stream of event (code, start) tuples."""
"""Return a stream of event (code, payload) tuples."""
for event_info in self.event_stream(eventcodes, retries, timeout_cmd):
_LOGGER.debug("%s event info: %r", self, event_info)
for code, action in _START_STOP.findall(event_info):
yield code, action == "Start"
payload = dict()
for Key, Value \
in _REG_PARSE_KEY_VALUE.findall(
event_info.strip().replace('\n', '')
):
if Key == 'data':
tmpData = dict()
for DataKey, DataValue \
in _REG_PARSE_MALFORMED_JSON.findall(Value):
tmpData[DataKey.replace('"', '')] = \
DataValue.replace('"', '')
Value = tmpData
payload[Key] = Value
_LOGGER.debug(
"%s generate new event, code: %s , payload: %s",
self, payload['Code'], payload
)
yield payload['Code'], payload



class NoHeaderErrorFilter(logging.Filter):
Expand Down
8 changes: 4 additions & 4 deletions src/amcrest/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ def record_mode(self):
2: 'Stop',
None: 'Unknown'}

ret = self.command(
'configManager.cgi?action=getConfig&name=RecordMode'
)

try:
ret = self.command(
'configManager.cgi?action=getConfig&name=RecordMode'
)

status = int([s for s in ret.content.decode(
'utf-8').split() if 'Mode=' in s][0].split('=')[-1])

Expand Down