import requests

modulesAvailable = False

try:
        from time import sleep
        from datetime import datetime
        from gpiozero import MotionSensor
        from gpiozero import LED
        from picamera import PiCamera
        import subprocess
        import shlex

        modulesAvailable = True

except ImportError:
        print('[alb3rt-camera-event] Error')
        requests.get('https://127.0.0.1:4001/api/sensors/camera?type=error', verify=False, headers={'x-system-access-key': '058cd7c17322'})

if modulesAvailable == True:
    camera = PiCamera()
    camera.resolution = (640, 480)
    timestamp = int(datetime.now().strftime('%s')) * 1000
    filename = 'video-%s' % timestamp

    camera.start_preview()
    sleep(2)
    camera.capture('./videos/%s.jpg' % filename)
    camera.stop_preview()

    camera.start_recording('./videos/temp/%s.h264' % filename)
    camera.wait_recording(10)
    camera.stop_recording()

    convertCommand = './scripts/bash/convert.sh ' + filename
    subprocess.call(shlex.split(convertCommand))

    requestUrl = 'https://127.0.0.1:4001/api/sensors/camera?type=off&filename=' + filename
    response = requests.get(requestUrl, verify=False, headers={'x-system-access-key': '058cd7c17322'})