Skip to content

Commit

Permalink
Merge pull request #162 from blademckain/master
Browse files Browse the repository at this point in the history
changes to support amcrest AD110 and other small bugfixes
  • Loading branch information
tchellomello committed Sep 23, 2020
2 parents 5a9f729 + 8ef01c4 commit 366c0b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
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"]+)'
)

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

0 comments on commit 366c0b6

Please sign in to comment.