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

Event stream parsing cannot handle arrays #204

Closed
bdevel opened this issue Jan 5, 2022 · 3 comments
Closed

Event stream parsing cannot handle arrays #204

bdevel opened this issue Jan 5, 2022 · 3 comments

Comments

@bdevel
Copy link

bdevel commented Jan 5, 2022

It seems the _build_payload function in src/amcrest/event.py cannot handle JSON style arrays as provided by my camera event stream.

Package: amcrest==1.9.3

Camera Details:

{'device_name': 'xxxxxxxx',
 'device_type': 'IP8M-T2669E-AI',
 'sw_version': '2.800.00AC000.0.R',
 'version': '1.0.15'}

Event as seen by curl:

--myboundary
Content-Type: text/plain
Content-Length:218

Code=SmartMotionVehicle;action=Start;index=0;data={
   "RegionName" : [ "Driveway" ],
   "WindowId" : [ 0 ],
   "object" : [
      {
         "Rect" : [ 104, 2320, 1232, 4048 ],
         "VehicleID" : 0
      }
   ]
}

Parsed as:

{'Code': 'SmartMotionVehicle',
 'action': 'Start',
 'data': {'Rect': '[',
          'RegionName': '[',
          'VehicleID': '0',
          'WindowId': '[',
          'object': '['},
 'index': '0'}

Issue, 'RegionName' value is parsed as '[' where [ "Driveway" ] would be expected.

Another example event:

--myboundary
Content-Type: text/plain
Content-Length:795

Code=CrossLineDetection;action=Stop;index=0;data={
   "Class" : "Normal",
   "CountInGroup" : 1,
   "DetectLine" : [
      [ 3349, 1776 ],
      [ 3349, 4033 ]
   ],
   "Direction" : "LeftToRight",
   "EventID" : 14637,
   "GroupID" : 32,
   "Name" : "Road heading north",
   "Object" : {
      "Action" : "Appear",
      "BoundingBox" : [ 2312, 1624, 3768, 2712 ],
      "BrandYear" : 0,
      "CarLogoIndex" : 0,
      "Category" : "Unknown",
      "Center" : [ 3040, 2168 ],
      "Confidence" : 0,
      "MainColor" : [ 0, 0, 0, 0 ],
      "ObjectID" : 170819,
      "ObjectType" : "Vehicle",
      "RelativeID" : 0,
      "Speed" : 0,
      "SubBrand" : 0,
      "Text" : "Unknown"
   },
   "PTS" : 49945272770.0,
   "RuleID" : 5,
   "Track" : [],
   "UTC" : 1641378155,
   "UTCMS" : 256
}

Parsed as:

{'Code': 'CrossLineDetection',
 'action': 'Stop',
 'data': {'Action': 'Appear',
          'BoundingBox': '[',
          'BrandYear': '0,',
          'CarLogoIndex': '0,',
          'Category': 'Unknown',
          'Center': '[',
          'Class': 'Normal',
          'Confidence': '0,',
          'CountInGroup': '1,',
          'DetectLine': '[',
          'Direction': 'LeftToRight',
          'EventID': '14637,',
          'GroupID': '32,',
          'MainColor': '[',
          'Name': 'Road heading north',
          'Object': '{',
          'ObjectID': '170819,',
          'ObjectType': 'Vehicle',
          'PTS': '49945272770.0,',
          'RelativeID': '0,',
          'RuleID': '5,',
          'Speed': '0,',
          'SubBrand': '0,',
          'Text': 'Unknown',
          'Track': '[],',
          'UTC': '1641378155,',
          'UTCMS': '256'},
 'index': '0'}

From what I see in my camera, it may be safe to assume data={ can parse as JSON.

@bdevel bdevel changed the title Event steam parsing cannot handle arrays Event stream parsing cannot handle arrays Jan 5, 2022
@rkben
Copy link
Contributor

rkben commented Mar 7, 2022

would a try/except json.loads() on data={...} and fall back to regex; be appropriate?

edit: drop mention

@flacjacket
Copy link
Collaborator

I think something like that seems reasonable. I'm not sure what the original regex is designed to accomplish, it was added in #162 to handle the AD110. As long as there is a good way to handle this while not breaking the doorbell functionality, that would be great. I don't have one to test, though.

flacjacket added a commit to flacjacket/python-amcrest that referenced this issue Mar 13, 2022
It looks like the first two characters after the ContentLength is
specified are a carriage return. This has thrown off the json string
parsing leading to hand rolling regex which fails in odd ways (see tchellomello#204).
Now, pull out a couple extra characters to get past this leading
carriage return. If the leading characters are not there, we should have
a trailing \r\n\r\n which is safe to add, and will just be stripped out.
With this change in place, we are able to use json parsing on data, with
a fallback to sending the data string.
flacjacket added a commit to flacjacket/python-amcrest that referenced this issue Mar 13, 2022
It looks like the first two characters after the ContentLength is
specified are a carriage return. This has thrown off the json string
parsing leading to hand rolling regex which fails in odd ways (see tchellomello#204).
Now, pull out a couple extra characters to get past this leading
carriage return. If the leading characters are not there, we should have
a trailing \r\n\r\n which is safe to add, and will just be stripped out.
With this change in place, we are able to use json parsing on data, with
a fallback to sending the data string.
@flacjacket
Copy link
Collaborator

I think this is addressed in #212 that makes the change to be able to json.loads the data in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants