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

async_get_image() #12

Open
alechugaro opened this issue Jun 4, 2024 · 2 comments
Open

async_get_image() #12

alechugaro opened this issue Jun 4, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@alechugaro
Copy link

  • imouapi version: 1.0.15
  • Python version: 3.11.8
  • Operating System: windows 10

Description

I want to use the library to capture images
I am using the methods that are in the imouapi.device_entity but I have an error, I don´t know what is the problem,

What I Did

from imouapi.api import ImouAPIClient
from imouapi.device import ImouDiscoverService
from imouapi.device import ImouDevice
from imouapi.device_entity import ImouCamera
import asyncio

async def capture_image(api_client, device_id, image_path):

    device_name = "Cell Go-D270"
    sensor_type = "cameraSD"  # Replace with actual sensor type
    profile = "default"  # Optional, can be left blank if not needed

    camera = ImouCamera(api_client, device_id, device_name, sensor_type, profile)

    # Now you can use camera.async_get_image() to capture an image
    image_data = await camera.async_get_image()

    if image_data is not None:
        with open(image_path, 'wb') as f:
            f.write(image_data)
        print(f"Image saved to {image_path}")
    else:
        print("Failed to capture image")

if __name__ == "__main__":
    # Replace with your actual credentials
    app_id = "lc43ad91ef78744135"
    app_secret = "xxxxxxxxxxxxxxxxxxxxxx"
    device_id = "9L0C1ADPDP5D270"
    session = "GENTEC CAMERA"
    
    # Replace with the desired image path
    image_path = "captured_image.jpg"

    api_client = ImouAPIClient(app_id, app_secret,session)
    discover_service = ImouDiscoverService(api_client)
    asyncio.run(capture_image(api_client, device_id, image_path))

I got the next error

ConnectionFailed: 'str' object has no attribute 'request'
Traceback (most recent call last):
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\api.py", line 190, in _async_call_api
response = await self._session.request("POST", url, json=body, timeout=self._timeout)
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'request'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\api.py", line 155, in _async_call_api
await self.async_connect()
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\api.py", line 117, in async_connect
data = await self._async_call_api("accessToken", {}, True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\api.py", line 192, in _async_call_api
raise ConnectionFailed(f"{exception}") from exception
imouapi.exceptions.ConnectionFailed: 'str' object has no attribute 'request'

ConnectionFailed: 'str' object has no attribute 'request'
Traceback (most recent call last):
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\api.py", line 190, in _async_call_api
response = await self._session.request("POST", url, json=body, timeout=self._timeout)
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'request'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\api.py", line 155, in _async_call_api
await self.async_connect()
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\api.py", line 117, in async_connect
data = await self._async_call_api("accessToken", {}, True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\api.py", line 192, in _async_call_api
raise ConnectionFailed(f"{exception}") from exception
imouapi.exceptions.ConnectionFailed: 'str' object has no attribute 'request'

Too many unsuccesful connection attempts
Traceback (most recent call last):
File "C:\Users\JQU\Downloads\imouapi-master\capture_image.py", line 46, in
asyncio.run(capture_image(api_client, device_id, image_path))
File "C:\Users\JQU\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Users\JQU\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JQU\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 654, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\Users\JQU\Downloads\imouapi-master\capture_image.py", line 25, in capture_image
image_data = await camera.async_get_image()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\device_entity.py", line 582, in async_get_image
data = await self.api_client.async_api_setDeviceSnapEnhanced(self._device_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\api.py", line 553, in async_api_setDeviceSnapEnhanced
return await self._async_call_api(api, payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\JQU\Downloads\imouapi-master\imouapi\api.py", line 160, in _async_call_api
raise NotConnected()
imouapi.exceptions.NotConnected

@marques576
Copy link

Try using "aiohttp" instead of that string in the session of the api_client instance

import aiohttp

import asyncio

async def main():

    async with aiohttp.ClientSession() as session:
        api_client = ImouAPIClient("APP_ID", "APP_SECRET", session)

@user2684 user2684 added the bug Something isn't working label Jul 13, 2024
@user2684
Copy link
Owner

Thanks adding this to the backlog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

No branches or pull requests

3 participants