EagleEyev3/Playground.ipynb

582 lines
14 KiB
Plaintext
Raw Normal View History

2023-05-30 03:31:56 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "d4582341",
"metadata": {},
"source": [
"# EagleEyev3 Playground #\n",
"\n",
2023-05-30 04:05:38 +00:00
"To make this playground work, it is easier to read the `access_token` off the filesystem but you can always run the example server `python server.py` to go thorugh the Oauth2 flow. By default it will save the `access_token` into a file named `.lazy_login`. The module looks for that file and tries reading t"
2023-05-30 03:31:56 +00:00
]
},
{
"cell_type": "markdown",
"id": "51b8b66e",
2023-05-31 03:30:22 +00:00
"metadata": {},
2023-05-30 03:31:56 +00:00
"source": [
"## Adjust Log Level"
]
},
{
"cell_type": "code",
"execution_count": 46,
2023-05-30 03:31:56 +00:00
"id": "06d91db2",
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"logger = logging.getLogger()\n",
"logger.setLevel('WARN')"
]
},
{
"cell_type": "markdown",
"id": "8355d241",
"metadata": {},
"source": [
"## Import Module"
]
},
{
"cell_type": "code",
"execution_count": 47,
2023-05-30 03:31:56 +00:00
"id": "1394471a",
"metadata": {},
"outputs": [],
"source": [
"from EagleEyev3 import EagleEyev3"
]
},
{
"cell_type": "code",
"execution_count": 48,
2023-05-30 03:31:56 +00:00
"id": "486a2537",
"metadata": {},
"outputs": [],
"source": [
"een = EagleEyev3()"
]
},
2023-05-30 04:05:38 +00:00
{
"cell_type": "markdown",
"id": "0311109c-869c-4190-97c1-a6e717a8eeba",
2023-05-31 03:30:22 +00:00
"metadata": {},
2023-05-30 04:05:38 +00:00
"source": [
"## Who am I and what is my access_token"
]
},
{
"cell_type": "code",
"execution_count": 49,
2023-05-30 04:05:38 +00:00
"id": "e14e2be5-a5f9-4b8c-ae60-76c61cb61b8b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Mark Cotton - mcotton@mcottondesign.com'"
]
},
"execution_count": 49,
2023-05-30 04:05:38 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f\"{een.current_user['firstName']} {een.current_user['lastName']} - {een.current_user['email']}\""
]
},
{
"cell_type": "code",
"execution_count": 50,
2023-05-30 04:05:38 +00:00
"id": "4ef47ae2-a010-4b7e-87f6-3dbf0a047e16",
2023-05-31 03:30:22 +00:00
"metadata": {},
2023-05-30 04:05:38 +00:00
"outputs": [
{
"data": {
"text/plain": [
"'eyJraWQiOiI2ODYxYjBjYS0wZjI2LTExZWQtODYxZC0wMjQyYWMxMjAwMDIiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJjYWZlZGVmMiIsImF1ZCI6InZtcy5hcGkiLCJpc3MiOiJ2bXMuYXV0aC52MSIsInZtc19hY2NvdW50IjoiMDAwMjgyMDEiLCJleHAiOjE2ODYwMTQ0NjUsImlhdCI6MTY4NTQwOTY2NiwianRpIjoiNDBkMThmZTA0YTZlNWE5NDI0OWE4NmVjNmE5NDA0YzYiLCJjbGllbnRfaWQiOiJQUkVWSUVXLU1BUkstV0VCIiwidm1zX2NsdXN0ZXIiOiJjMDEyIn0.RAVxuQtnb9WSv4jp4hNqEyPaVzlpK6GqodFAUybzrnGiPR9tHE5OIanCZf2aX5IGAyOk9Kn5UYD4h96iIt8cpCXIyrFPKGE3WtspAxYNBwczjAWTuZQ6lOxH2eIeo3QjoTeOZaL8JEfCmc5-NyK4xaTrnb0nuhUnWzB4Vo9xtqzn2mupYUEyOvkgM0nOaTxPlrJLRNjNKDWKwMtuE2wo94HbjeOi8lSvQcMPvm07qvS1nFEaTjzpHCdwENcK6ghEUv5wzbUsDm5FYZFi29MyGLLiwv6csGD8lY0C3FFsG5EbMFzNAqS-v-CriPgA-3X9rpgLiWNIjSFG8vj7ZzCtsg'"
]
},
"execution_count": 50,
2023-05-30 04:05:38 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"een.access_token"
]
},
2023-05-30 03:31:56 +00:00
{
"cell_type": "markdown",
"id": "a22ff6c2",
"metadata": {},
"source": [
"## Get Cameras"
]
},
{
"cell_type": "code",
"execution_count": 51,
2023-05-30 03:31:56 +00:00
"id": "bb457850",
"metadata": {},
"outputs": [],
"source": [
"ret = een.get_list_of_cameras()"
]
},
{
"cell_type": "code",
"execution_count": 52,
2023-05-30 03:31:56 +00:00
"id": "c43f1db1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[✅ [1001423e] - ATM & Wine,\n",
" ✅ [100d8666] - Cash Register,\n",
" ✅ [10012735] - Fuel Dock,\n",
" ✅ [1002584c] - Safe]"
]
},
"execution_count": 52,
2023-05-30 03:31:56 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[i for i in een.cameras if i.is_online()]"
]
},
{
"cell_type": "code",
"execution_count": 53,
2023-05-30 04:05:38 +00:00
"id": "ad33bf2a-ea36-4ec1-8335-8a44089f5f7e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[ [10090759] - Benny Camera,\n",
" [1003e10b] - Driveway,\n",
" [100ba388] - Front Door,\n",
2023-05-30 04:05:38 +00:00
" [100b7b3b] - Max Camera,\n",
" [1009ae55] - Office]"
2023-05-30 04:05:38 +00:00
]
},
"execution_count": 53,
2023-05-30 04:05:38 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[i for i in een.cameras if not i.is_online()]"
]
},
{
"cell_type": "code",
"execution_count": 54,
2023-05-30 03:31:56 +00:00
"id": "f4c6fe67",
"metadata": {},
"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"
}
],
2023-05-30 03:31:56 +00:00
"source": [
"[i for i in een.cameras if not i.is_online()]"
]
2023-05-30 04:05:38 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"id": "b0ec786b-ed07-4486-a4d7-a7483e5a02ce",
"metadata": {},
"outputs": [],
"source": []
2023-05-31 03:30:22 +00:00
},
{
"cell_type": "markdown",
"id": "295b9ef8-df88-447e-b787-0098b4778f6a",
"metadata": {},
"source": [
"## User timezone fun"
]
},
{
"cell_type": "code",
"execution_count": 55,
2023-05-31 03:30:22 +00:00
"id": "ef1ea137-d9fb-4f6f-b241-fb019c8e3230",
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime, timedelta\n",
"from pytz import timezone"
]
},
{
"cell_type": "code",
"execution_count": 56,
2023-05-31 03:30:22 +00:00
"id": "0841aef0-89c1-45ac-b501-a9196f481d49",
"metadata": {},
"outputs": [],
"source": [
"tz = timezone(een.current_user['timeZone']['timeZone'])\n",
"now = datetime.now(tz=tz)"
]
},
{
"cell_type": "code",
"execution_count": 57,
2023-05-31 03:30:22 +00:00
"id": "ae53f810-cb77-48e3-b148-72ef7038bb89",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2023-05-31T23:01:04.271-05:00'"
2023-05-31 03:30:22 +00:00
]
},
"execution_count": 57,
2023-05-31 03:30:22 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"now.isoformat(timespec='milliseconds')"
]
},
{
"cell_type": "code",
"execution_count": 58,
2023-05-31 03:30:22 +00:00
"id": "4d708ccb-80b1-4d88-aa2e-d93a71cbdc04",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2023-05-31T17:01:04.640-05:00'"
2023-05-31 03:30:22 +00:00
]
},
"execution_count": 58,
2023-05-31 03:30:22 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(datetime.now(tz=tz) - timedelta(hours=6)).isoformat(timespec='milliseconds')"
]
},
{
"cell_type": "markdown",
"id": "f4d8c154-400a-4af9-b677-29190b588408",
2023-05-31 15:35:19 +00:00
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
2023-05-31 03:30:22 +00:00
"source": [
"## Event Subscription"
]
},
{
"cell_type": "code",
"execution_count": 59,
2023-05-31 03:30:22 +00:00
"id": "a9d81b0d-72e6-4404-b30e-3a341a9e6639",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"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"
2023-05-31 03:30:22 +00:00
]
}
],
"source": [
"import requests\n",
"\n",
"url = \"https://api.c012.eagleeyenetworks.com/api/v3.0/eventSubscriptions\"\n",
"\n",
"payload = {\n",
" \"deliveryConfig\": {\"type\": \"serverSentEvents.v1\"},\n",
" \"filters\": [\n",
" {\n",
2023-05-31 15:35:19 +00:00
" \"actors\": [\"camera:100d8666\"],\n",
2023-05-31 03:30:22 +00:00
" \"types\": [{\"id\": \"een.motionDetectionEvent.v1\"}]\n",
" }\n",
" ]\n",
"}\n",
"headers = {\n",
" \"accept\": \"application/json\",\n",
" \"content-type\": \"application/json\",\n",
2023-05-31 15:35:19 +00:00
" \"authorization\": f\"Bearer {een.access_token}\"\n",
2023-05-31 03:30:22 +00:00
"}\n",
"\n",
"response = requests.post(url, json=payload, headers=headers)\n",
"\n",
"print(response.text)"
]
},
{
"cell_type": "code",
"execution_count": 60,
2023-05-31 03:30:22 +00:00
"id": "53d3bff7-848f-4669-ab66-57597313fa0d",
"metadata": {
2023-05-31 15:35:19 +00:00
"editable": true,
"scrolled": true,
"slideshow": {
"slide_type": ""
},
"tags": []
2023-05-31 03:30:22 +00:00
},
"outputs": [],
"source": [
"#pip install sseclient\n",
"from sseclient import SSEClient\n",
"\n",
2023-05-31 15:35:19 +00:00
"messages = SSEClient(f\"{response.json()['deliveryConfig']['sseUrl']}\", headers=headers)"
2023-05-31 03:30:22 +00:00
]
},
{
"cell_type": "code",
"execution_count": 61,
2023-05-31 03:30:22 +00:00
"id": "91cfe32d-db80-40fb-8ea9-d60f8a647b0d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Response [200]>"
]
},
"execution_count": 61,
2023-05-31 03:30:22 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages.resp"
]
},
{
"cell_type": "code",
"execution_count": 62,
2023-05-31 03:30:22 +00:00
"id": "2016a9cf-9ce8-48af-8e3f-c058024c544e",
"metadata": {},
2023-05-31 15:35:19 +00:00
"outputs": [],
2023-05-31 03:30:22 +00:00
"source": [
"#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"
2023-05-31 03:30:22 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "287cbf7b-52c4-4aac-abeb-be94d0594c19",
2023-05-31 03:30:22 +00:00
"metadata": {},
"outputs": [],
"source": []
2023-05-30 03:31:56 +00:00
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2023-05-30 04:05:38 +00:00
"version": "3.8.10"
2023-05-30 03:31:56 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}