From 47d73763fdc4fb5f2bbbea52646c5a0887252cc1 Mon Sep 17 00:00:00 2001 From: mcotton Date: Wed, 31 May 2023 23:16:11 -0500 Subject: [PATCH] got a working version of get_list_of_events() --- EagleEyev3/__init__.py | 78 +++++++++++-- Playground.ipynb | 246 +++++++++++++++++++++++++++++++++++------ 2 files changed, 283 insertions(+), 41 deletions(-) diff --git a/EagleEyev3/__init__.py b/EagleEyev3/__init__.py index 1f244f3..a8bf2ab 100644 --- a/EagleEyev3/__init__.py +++ b/EagleEyev3/__init__.py @@ -11,8 +11,6 @@ logging.basicConfig(level=logging.INFO) - - class EagleEyev3(): """ Class representing the EagleEyev3 client. @@ -238,7 +236,17 @@ class EagleEyev3(): if response.status_code == 200: success = True - self.cameras = [Camera(id=i['id'], name=i['name'], status=i['status'], account_id=i['accountId'], bridge_id=i['bridgeId']) for i in response_json['results']] + self.cameras = [ + Camera(id=i['id'],\ + name=i['name'],\ + status=i['status'],\ + account_id=i['accountId'],\ + bridge_id=i['bridgeId'],\ + user_base_url=self.user_base_url,\ + een_instance=self) + for i in response_json['results']] + for camera in self.cameras: + camera.user_base_url = self.user_base_url else: success = False @@ -396,14 +404,19 @@ class EagleEyev3(): } + + + class Device(): - def __init__(self, id=None, name=None, status=None, account_id=None): + def __init__(self, id=None, name=None, status=None, account_id=None, user_base_url=None, een_instance=None): self.id = id self.name = name self.status = status self.account_id = account_id + self.user_base_url = user_base_url, + self.een_instance = een_instance def get_id(self): return self.id @@ -430,11 +443,62 @@ class Device(): class Camera(Device): - def __init__(self, id=None, name=None, status=None, account_id=None, bridge_id=None): - super().__init__(id=id, name=name, status=status, account_id=account_id) + def __init__(self, id=None, name=None, status=None, account_id=None, bridge_id=None, user_base_url=None, een_instance=None): + super().__init__(id=id, name=name, status=status, account_id=account_id, user_base_url=user_base_url, een_instance=een_instance) self.bridge_id = bridge_id self.previews = [] self.videos = [] - + self.events = { + 'status': [], + 'motion': [] + } + + def get_list_of_events(self, start_timestamp=None, end_timestamp=None): + """ + Obtains the list of events. + Returns: + dict: Dictionary containing the success status, response HTTP status code, and data. + """ + + if start_timestamp == None or end_timestamp == None: + return { + "success": False, + "response_http_status": None, + "data": { 'msg': 'get_list_of_events called without required args, needs start_timestamp, end_timestamp' } + } + + url = f"https://{self.user_base_url}/api/v3.0/events?pageSize=100&include=data.een.deviceCloudStatusUpdate.v1&startTimestamp__gte={start_timestamp}&endTimestamp__lte={end_timestamp}&actor=camera%3A{self.id}" + + headers = { + "Authorization": f"Bearer {self.een_instance.access_token}", + "Accept": "application/json" + } + + response = requests.get(url, headers=headers) + response_json = response.json() + + logging.debug(f"{response.status_code} returned from {url} with {headers}") + logging.info(f"{response.status_code} in get_list_of_events") + + if response.status_code == 200: + success = True + # filter events by type + [self.events['status'].append(i) for i in response.json()['results'] if i['type'] == 'een.deviceCloudStatusUpdateEvent.v1'] + [self.events['motion'].append(i) for i in response.json()['results'] if i['type'] == 'een.motionDetectionEvent.v1'] + + # remove duplicates + seen = set() + self.events['status'] = [event for event in self.events['status'] if event['id'] not in seen and not seen.add(event['id'])] + + seen = set() + self.events['motion'] = [event for event in self.events['motion'] if event['id'] not in seen and not seen.add(event['id'])] + else: + success = False + + return { + "success": success, + "response_http_status": response.status_code, + "data": response_json + } diff --git a/Playground.ipynb b/Playground.ipynb index bdf02d4..1f44d2a 100644 --- a/Playground.ipynb +++ b/Playground.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 46, "id": "06d91db2", "metadata": {}, "outputs": [], @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 47, "id": "1394471a", "metadata": {}, "outputs": [], @@ -50,7 +50,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 48, "id": "486a2537", "metadata": {}, "outputs": [], @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 49, "id": "e14e2be5-a5f9-4b8c-ae60-76c61cb61b8b", "metadata": {}, "outputs": [ @@ -78,7 +78,7 @@ "'Mark Cotton - mcotton@mcottondesign.com'" ] }, - "execution_count": 8, + "execution_count": 49, "metadata": {}, "output_type": "execute_result" } @@ -89,7 +89,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 50, "id": "4ef47ae2-a010-4b7e-87f6-3dbf0a047e16", "metadata": {}, "outputs": [ @@ -99,7 +99,7 @@ "'eyJraWQiOiI2ODYxYjBjYS0wZjI2LTExZWQtODYxZC0wMjQyYWMxMjAwMDIiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJjYWZlZGVmMiIsImF1ZCI6InZtcy5hcGkiLCJpc3MiOiJ2bXMuYXV0aC52MSIsInZtc19hY2NvdW50IjoiMDAwMjgyMDEiLCJleHAiOjE2ODYwMTQ0NjUsImlhdCI6MTY4NTQwOTY2NiwianRpIjoiNDBkMThmZTA0YTZlNWE5NDI0OWE4NmVjNmE5NDA0YzYiLCJjbGllbnRfaWQiOiJQUkVWSUVXLU1BUkstV0VCIiwidm1zX2NsdXN0ZXIiOiJjMDEyIn0.RAVxuQtnb9WSv4jp4hNqEyPaVzlpK6GqodFAUybzrnGiPR9tHE5OIanCZf2aX5IGAyOk9Kn5UYD4h96iIt8cpCXIyrFPKGE3WtspAxYNBwczjAWTuZQ6lOxH2eIeo3QjoTeOZaL8JEfCmc5-NyK4xaTrnb0nuhUnWzB4Vo9xtqzn2mupYUEyOvkgM0nOaTxPlrJLRNjNKDWKwMtuE2wo94HbjeOi8lSvQcMPvm07qvS1nFEaTjzpHCdwENcK6ghEUv5wzbUsDm5FYZFi29MyGLLiwv6csGD8lY0C3FFsG5EbMFzNAqS-v-CriPgA-3X9rpgLiWNIjSFG8vj7ZzCtsg'" ] }, - "execution_count": 9, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -118,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 51, "id": "bb457850", "metadata": {}, "outputs": [], @@ -128,7 +128,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 52, "id": "c43f1db1", "metadata": {}, "outputs": [ @@ -141,7 +141,7 @@ " ✅ [1002584c] - Safe]" ] }, - "execution_count": 11, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } @@ -152,7 +152,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 53, "id": "ad33bf2a-ea36-4ec1-8335-8a44089f5f7e", "metadata": {}, "outputs": [ @@ -160,13 +160,13 @@ "data": { "text/plain": [ "[? [10090759] - Benny Camera,\n", - " ❌ [1003e10b] - Driveway,\n", - " ❌ [100ba388] - Front Door,\n", + " ? [1003e10b] - Driveway,\n", + " ? [100ba388] - Front Door,\n", " ? [100b7b3b] - Max Camera,\n", - " ❌ [1009ae55] - Office]" + " ? [1009ae55] - Office]" ] }, - "execution_count": 12, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } @@ -177,10 +177,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "id": "f4c6fe67", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[? [10090759] - Benny Camera,\n", + " ? [1003e10b] - Driveway,\n", + " ? [100ba388] - Front Door,\n", + " ? [100b7b3b] - Max Camera,\n", + " ? [1009ae55] - Office]" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "[i for i in een.cameras if not i.is_online()]" ] @@ -203,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 55, "id": "ef1ea137-d9fb-4f6f-b241-fb019c8e3230", "metadata": {}, "outputs": [], @@ -214,7 +229,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 56, "id": "0841aef0-89c1-45ac-b501-a9196f481d49", "metadata": {}, "outputs": [], @@ -225,17 +240,17 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 57, "id": "ae53f810-cb77-48e3-b148-72ef7038bb89", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'2023-05-31T07:13:07.708-05:00'" + "'2023-05-31T23:01:04.271-05:00'" ] }, - "execution_count": 15, + "execution_count": 57, "metadata": {}, "output_type": "execute_result" } @@ -246,17 +261,17 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 58, "id": "4d708ccb-80b1-4d88-aa2e-d93a71cbdc04", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'2023-05-31T01:13:12.552-05:00'" + "'2023-05-31T17:01:04.640-05:00'" ] }, - "execution_count": 16, + "execution_count": 58, "metadata": {}, "output_type": "execute_result" } @@ -281,7 +296,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 59, "id": "a9d81b0d-72e6-4404-b30e-3a341a9e6639", "metadata": {}, "outputs": [ @@ -289,7 +304,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{\"id\":\"37fb661e-c98c-4f29-bc28-cf241a59b998\",\"subscriptionConfig\":{\"lifeCycle\":\"temporary\",\"timeToLiveSeconds\":900},\"deliveryConfig\":{\"type\":\"serverSentEvents.v1\",\"sseUrl\":\"https://api.c012.eagleeyenetworks.com/api/v3.0/sse/eventSubscriptions/37fb661e-c98c-4f29-bc28-cf241a59b998\"}}\n" + "{\"id\":\"2470978d-1285-446f-9504-f2399a4e2819\",\"subscriptionConfig\":{\"lifeCycle\":\"temporary\",\"timeToLiveSeconds\":900},\"deliveryConfig\":{\"type\":\"serverSentEvents.v1\",\"sseUrl\":\"https://api.c012.eagleeyenetworks.com/api/v3.0/sse/eventSubscriptions/2470978d-1285-446f-9504-f2399a4e2819\"}}\n" ] } ], @@ -320,7 +335,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 60, "id": "53d3bff7-848f-4669-ab66-57597313fa0d", "metadata": { "editable": true, @@ -340,7 +355,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 61, "id": "91cfe32d-db80-40fb-8ea9-d60f8a647b0d", "metadata": {}, "outputs": [ @@ -350,7 +365,7 @@ "" ] }, - "execution_count": 19, + "execution_count": 61, "metadata": {}, "output_type": "execute_result" } @@ -361,19 +376,182 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "id": "2016a9cf-9ce8-48af-8e3f-c058024c544e", "metadata": {}, "outputs": [], "source": [ - "for msg in messages:\n", - " print(msg)" + "#for msg in messages:\n", + "# print(msg)" + ] + }, + { + "cell_type": "markdown", + "id": "8c140aaf-766f-4255-94ef-199d17cbc7a6", + "metadata": {}, + "source": [ + "## Getting list of Events ##" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "2a5c78ed-eb96-45aa-a773-1681671a0d42", + "metadata": {}, + "outputs": [], + "source": [ + "end_timestamp = datetime.now(tz=tz).isoformat(timespec='milliseconds')" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "id": "14132706-1d29-4fb9-b5b0-3dc7519118aa", + "metadata": {}, + "outputs": [], + "source": [ + "start_timestamp = (datetime.now(tz=tz) - timedelta(hours=6)).isoformat(timespec='milliseconds')" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "id": "74e78ee1-33b8-4a88-9d23-cd6281603a5b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2023-05-31T17:12:35.355-05:00 2023-05-31T23:12:34.754-05:00\n" + ] + } + ], + "source": [ + "print(start_timestamp, end_timestamp)" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "id": "c84c30dd-4b7c-415b-8e6f-e77de70d1924", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "for cam in een.cameras:\n", + " blah = cam.get_list_of_events(start_timestamp=start_timestamp, end_timestamp=end_timestamp)" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "id": "f6c11a52-63e1-4b65-8149-4d0e31d13870", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'type': 'een.deviceCloudStatusUpdateEvent.v1',\n", + " 'id': '100d8666-2023-05-31T22:01:22.001Z-een.deviceCloudStatusUpdateEvent.v1',\n", + " 'startTimestamp': '2023-05-31T22:01:22.001+00:00',\n", + " 'endTimestamp': None,\n", + " 'span': True,\n", + " 'accountId': '00028201',\n", + " 'actorId': '100d8666',\n", + " 'actorAccountId': '00028201',\n", + " 'actorType': 'camera',\n", + " 'creatorId': 'een.systemHealth',\n", + " 'dataSchemas': ['data.een.deviceCloudStatusUpdate.v1'],\n", + " 'data': [{'type': 'een.deviceCloudStatusUpdateEvent.v1',\n", + " 'creatorId': 'een.systemHealth',\n", + " 'newStatus': {'connectionStatus': 'online'}}]},\n", + " {'type': 'een.deviceCloudStatusUpdateEvent.v1',\n", + " 'id': '100d8666-2023-05-31T22:11:35.901Z-een.deviceCloudStatusUpdateEvent.v1',\n", + " 'startTimestamp': '2023-05-31T22:11:35.901+00:00',\n", + " 'endTimestamp': None,\n", + " 'span': True,\n", + " 'accountId': '00028201',\n", + " 'actorId': '100d8666',\n", + " 'actorAccountId': '00028201',\n", + " 'actorType': 'camera',\n", + " 'creatorId': 'een.systemHealth',\n", + " 'dataSchemas': ['data.een.deviceCloudStatusUpdate.v1'],\n", + " 'data': [{'type': 'een.deviceCloudStatusUpdateEvent.v1',\n", + " 'creatorId': 'een.systemHealth',\n", + " 'newStatus': {'connectionStatus': 'online'}}]},\n", + " {'type': 'een.deviceCloudStatusUpdateEvent.v1',\n", + " 'id': '100d8666-2023-05-31T22:12:35.355Z-een.deviceCloudStatusUpdateEvent.v1',\n", + " 'startTimestamp': '2023-05-31T22:12:35.355+00:00',\n", + " 'endTimestamp': None,\n", + " 'span': True,\n", + " 'accountId': '00028201',\n", + " 'actorId': '100d8666',\n", + " 'actorAccountId': '00028201',\n", + " 'actorType': 'camera',\n", + " 'creatorId': 'een.systemHealth',\n", + " 'dataSchemas': ['data.een.deviceCloudStatusUpdate.v1'],\n", + " 'data': [{'type': 'een.deviceCloudStatusUpdateEvent.v1',\n", + " 'creatorId': 'een.systemHealth',\n", + " 'newStatus': {'connectionStatus': 'online'}}]}]" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "een.cameras[2].events['status']" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "id": "67d4f79b-2b43-4bdb-9068-28ac9d8d921c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "106" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(een.cameras[2].events['motion'])" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "9bf0a03d-cede-4e66-af88-ec17de19ef22", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Cash Register'" + ] + }, + "execution_count": 116, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "een.cameras[2].name" ] }, { "cell_type": "code", "execution_count": null, - "id": "bf23672e-f808-442f-8393-3c0e1f84c984", + "id": "287cbf7b-52c4-4aac-abeb-be94d0594c19", "metadata": {}, "outputs": [], "source": []