# coding: utf-8
import os
import ConfigParser
import argparse
from apscheduler.scheduler import Scheduler
from datetime import datetime

from modules.light import read_light
from modules.soil_moisture import get_moisture
from modules.soil_temperature import get_celsius
from modules.temperature_and_humidity import get_temperature_and_humidity
from api import publish_data, publish_image
from output import write_data, write_image

parser = argparse.ArgumentParser(description="Farmy Raspberry Pi Client")
parser.add_argument('--mode', dest='mode', type=str, default='once', help="Set mode. 'hold' or 'once'")
parser.add_argument('--config', dest='config_path', type=str, required=True)

config = ConfigParser.ConfigParser()


def fetch_data(file_path, dht_pin, plant_id, api_key):
    print("Fetch Data {}".format(datetime.now()))
    data = dict(
        light=read_light(),
        soil_moisture=get_moisture(),
        soil_temperature=get_celsius(),
    )
    temperature, humidity = get_temperature_and_humidity(dht_pin)
    if temperature is not None:
        data.update(dict(
            temperature=temperature,
            humidity=humidity
        ))
    now = datetime.now()
    data.update(dict(
        ts=int((now - datetime.fromtimestamp(0)).total_seconds()),
        dt=now.strftime("%Y/%m/%d %H:%M:%S")
    ))
    print(data)
    write_data(data, file_path)
    publish_data(data, plant_id, api_key)


def fetch_image(file_path, camera_type, plant_id, api_key):
    if camera_type == 'web':
        from modules.camera.webcam import take_picture_web
        image_raw = take_picture_web()
    elif camera_type == 'pi':
        from modules.camera.pi import take_picture_pi
        image_raw = take_picture_pi()
    else:
        raise ValueError("camera_type `{}` invalid".format(camera_type))
    write_image(image_raw, file_path)
    print('Take Picture by {}. Save to {}'.format(camera_type, file_path))
    publish_image(image_raw, plant_id, api_key)


def check_device(camera_type):
    if camera_type == "web":
        from modules.camera.webcam import web_camera
        if web_camera is None:
            raise ValueError("web camera not found.")
    elif camera_type == "pi":
        from modules.camera.pi import pi_camera
        if pi_camera is None:
            raise ValueError("Pi Camera not found.")
    else:
        raise ValueError("camera_type `{}` invalid".format(camera_type))


def main():
    args = parser.parse_args()
    mode = args.mode
    config_path = args.config_path
    config.read(config_path)
    camera_type = config.get('device', 'camera_type')
    file_path = config.get('log', 'file_path')
    dht_pin = config.getint('device', 'dht_pin')
    plant_id = config.get('api', 'plant_id')
    api_key = config.get('api', 'api_key')

    check_device(camera_type)
    print("Farmy device init.")
    image_path = os.path.join(file_path, 'photos')

    if not os.path.exists(image_path):
        os.makedirs(image_path)

    if mode == 'hold':
        print('Starting...')
        sched = Scheduler()
        sched.start()
        sched.add_cron_job(fetch_data, minute="*/10",
                           args=[file_path, dht_pin, plant_id, api_key])  # run every 10 minute
        sched.add_cron_job(fetch_image, minute="*/10",
                           args=[image_path, camera_type, plant_id, api_key])  # run every 10 minute
        raw_input("Press enter to exit the program\n")
    elif mode == 'once':
        fetch_data(file_path, dht_pin, plant_id, api_key)
        fetch_image(file_path, camera_type, plant_id, api_key)
    else:
        print('`--mode` option invalid. `hold`/`once`')


if __name__ == "__main__":
    main()

@@ -1470,24 +1470,124 @@
 , api_key):%0A
+    light = read_light()%0A    if light %3C= 5:%0A        print('Too dark to take photo.')%0A        return%0A
     if camer
@@ -3731,17 +3731,20 @@
 . %60hold%60
-/
+ or 
 %60once%60')

Add photo light check