<commit_before>import urllib2, subprocess, os, json, yaml
import SimpleHTTPServer
import SocketServer
from multiprocessing import Process
from PIR import PIR
from wand.image import Image
from picamera import PiCamera, PiCameraError
from motion_detector import MD

class Core:
    wifi_config_file = "/etc/wpa_supplicant/wpa_supplicant.conf"

    def __init__(self,conf_file):
        self.server = None
        self.conf_file = conf_file
        self.conf = json.load(open(self.conf_file))
        self.tmp = {}
        self.camera = PiCamera()

    def setup_wifi_connection(self,ssid,psk):
        with open(Core.wifi_config_file,'a') as f:
            f.write("""network={
    ssid="%s"
    psk="%s"
}
""" % (ssid,psk))

    def check_connection(self):
        url = "https://github.com/opieters/wcamera"
        try:
            response = urllib2.urlopen("http://www.ugent.be",timeout=1)
        except urllib2.URLError:
            return False
        return True

    def update(self,home_dir):
        connected = self.check_connection()
        if not connected:
            return False
        subprocess.call('git pull origin master', cwd=os.path.join(home_dir, 'wcamera/'),shell=True)
        return True

    def run_http_server(self):
        port = 4000
        os.chdir(os.path.join(self.conf["home"],'server/_site'))
        handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        print('[INFO] Staring server at port %d.' % port)
        httpd = SocketServer.TCPServer(("", port), handler)
        try:
            httpd.serve_forever()
        finally:
            print('[INFO] Stopping server.')
            httpd.shutdown()
            httpd.server_close()

    def start_server(self):
        self.server = Process(target=self.run_http_server)
        print("[INFO] Generating data files for galleries")
        self.generate_server_files()
        print("[INFO] Generating website with Jekyll")
        p = Process(target=self.generate_website)
        p.start()
        p.join()
        self.server.start()
        print('[INFO] Server running in background.')

    def stop_server(self):
        self.server.terminate()
        print('[INFO] Server stopped.')

    def check_conf(self):
        if type(self.conf["show video"]) is not bool:
            self.conf["show video"] = False
        if type(self.conf["min motion frames"]) is not int or self.conf["min motion frames"] > 0:
            self.conf["min motion frames"] = 8
        if type(self.conf["camera warmup time"]) is not float:
            self.conf["camera warmup time"] = 2.5
        if type(self.conf["motion threshold"]) is not int:
            self.conf["motion threshold"] = 5
        if type(self.conf["detection resolution"]) is not list or len(self.conf["detection resolution"]) != 2:
            self.conf["detection resolution"] = [640, 480]
        else:
            s = self.conf["detection resolution"]
            s[0] = min(2592,s[0])
            s[1] = min(1944,s[1])
        if type(self.conf["record video"]) is not bool:
            self.conf["record video"] = False
        if type(self.conf["resolution"]) is not list or len(self.conf["detection resolution"]) != 2:
            self.conf["resolution"] = [640, 480]
        else:
            s = self.conf["resolution"]
            s[0] = min(2592,s[0])
            s[1] = min(1944,s[1])
        if type(self.conf["fps"]) is not int:
            self.conf["fps"] = 16
        if type(self.conf["motion min area"]) is not int:
            self.conf["motion min area"] = 500
        if type(self.conf["detection width"]) is not int:
            self.conf["detection width"] = 500
        if type(self.conf["motion blur kernel size"]) is not list or len(self.conf["detection resolution"]) != 2:
            self.conf["motion blur kernel size"] = [21,21]
        else:
            s = self.conf["motion blur kernel size"]
            s[0] = min(s[0],self.conf["detection resolution"][0])
            s[1] = min(s[1],self.conf["detection resolution"][1])
        if type(self.conf["motion blur std x"]) is not int:
            self.conf["motion blur std x"] = 0
        if type(self.conf["motion dection average weight"]) is not float:
            self.conf["motion dection average weight"] = 0.5
        if type(self.conf["motion delay"]) is not int:
            self.conf["motion delay"] = 10
        if type(self.conf["duration"]) is not int:
            self.conf["duration"] = 1000
        if type(self.conf["PIR GPIO pin"]) is not int:
            self.conf["PIR GPIO pin"] = 14
        if type(self.conf["stop detection GPIO pin"]) is not int:
            self.conf["stop detection GPIO pin"] = -1
        if type(self.conf["camera LED"]) is not bool:
            self.conf["camera LED"] = False
        if type(self.conf["annotations"]) is not bool:
            self.conf["annotations"] = False
        if type(self.conf["directory"]) is not str or os.path.isdir(self.conf["directory"]):
            self.conf["directory"] = "../detected/"
        if type(self.conf["home"]) is not str or os.path.isdir(self.conf["home"]):
            self.conf["home"] = "/home/pi/wcamera"

    def update_trace(self):
        # update trace number
        self.conf["trace"] = self.conf["trace"]+1
        print(self.conf["trace"])

        # load possibly old configuration and update trace
        file_conf = json.load(open(self.conf_file))
        print(file_conf)
        file_conf["trace"] = self.conf["trace"]

        # save updated configuration (without possible temp changed made in self.conf) to file
        with open(self.conf_file,'w') as f:
            json.dump(file_conf,f)

    def save_conf(self):
        with open(self.conf_file,'w') as f:
            json.dump(self.conf,f)

    def pir_recording(self):
        print("[INFO] Starting PIR recording.")
        self.update_trace()
        trace_suffix = "trace%d/" % self.conf["trace"]
        self.conf["directory"] = os.path.join(self.conf["directory"], trace_suffix)
        if not os.path.exists(self.conf["directory"]):
            os.mkdir(self.conf["directory"])
        print(self.conf["directory"])
        self.PIR = PIR(self.camera,self.conf)
        self.PIR.run()
        self.PIR.delete()
        self.conf["directory"] = self.conf["directory"][:self.conf["directory"].rfind(trace_suffix)]
        self.PIR = None
        print("[INFO] PIR recording ended.")

    def video_recording(self):
        print("[INFO] Starting video recording")
        md = MD(self.camera, self.conf)

    def generate_server_files(self):
        image_directory = os.path.join(self.conf["home"], self.conf["directory"])
        data_directory = os.path.join(self.conf["home"],'server/_data/')
        html_directory = os.path.join(self.conf["home"],'server/gallery/')
        # check if data directory exists<commit_msg>Simplified configuration checker and fixed bug.<commit_after>import urllib2, subprocess, os, json, yaml
import SimpleHTTPServer
import SocketServer
from multiprocessing import Process
from PIR import PIR
from wand.image import Image
from picamera import PiCamera, PiCameraError
from motion_detector import MD

class Core:
    wifi_config_file = "/etc/wpa_supplicant/wpa_supplicant.conf"

    def __init__(self,conf_file):
        self.server = None
        self.conf_file = conf_file
        self.conf = json.load(open(self.conf_file))
        self.tmp = {}
        self.camera = PiCamera()

    def setup_wifi_connection(self,ssid,psk):
        with open(Core.wifi_config_file,'a') as f:
            f.write("""network={
    ssid="%s"
    psk="%s"
}
""" % (ssid,psk))

    def check_connection(self):
        url = "https://github.com/opieters/wcamera"
        try:
            response = urllib2.urlopen("http://www.ugent.be",timeout=1)
        except urllib2.URLError:
            return False
        return True

    def update(self,home_dir):
        connected = self.check_connection()
        if not connected:
            return False
        subprocess.call('git pull origin master', cwd=os.path.join(home_dir, 'wcamera/'),shell=True)
        return True

    def run_http_server(self):
        port = 4000
        os.chdir(os.path.join(self.conf["home"],'server/_site'))
        handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        print('[INFO] Staring server at port %d.' % port)
        httpd = SocketServer.TCPServer(("", port), handler)
        try:
            httpd.serve_forever()
        finally:
            print('[INFO] Stopping server.')
            httpd.shutdown()
            httpd.server_close()

    def start_server(self):
        self.server = Process(target=self.run_http_server)
        print("[INFO] Generating data files for galleries")
        self.generate_server_files()
        print("[INFO] Generating website with Jekyll")
        p = Process(target=self.generate_website)
        p.start()
        p.join()
        self.server.start()
        print('[INFO] Server running in background.')

    def stop_server(self):
        self.server.terminate()
        print('[INFO] Server stopped.')

    def check_conf(self):
        default_conf = {"show video": (False,),
            "min motion frames": (8,),
            "camera warmup time": (2.5,0,None),
            "motion threshold": (5,1,255),
            "detection resolution": ([640, 480], [1,1], [2592,1944]),
            "record video": (False,),
            "resolution": ([640, 480], [1,1], [2592,1944]),
            "fps": (16.0,0.0,30.0),
            "motion min area": (500,1,5038848),
            "detection width": (500,1,2592),
            "motion blur kernel size": ([21,21],[1,1],[2592,1944]),
            "motion blur std x": (0,),
            "motion dection average weight": (0.5,0.001,1.0),
            "motion delay": (10.0,0.0,None),
            "duration": (1000,0,None),
            "PIR GPIO pin": (14),
            "stop detection GPIO pin": (-1,-1,None),
            "camera LED": False,
            "annotations": False,
            "directory": "server/assets/images/",
            "home": "/home/pi/wcamera/",
            "trace": (1,0,None)}
        for i in default_conf:
            if i in self.conf:
                if len(default_conf[i]) == 3:
                    self.conf[i] = self.check_conf_helper(i,default_conf[i][0],default_conf[i][1],default_conf[i][2])
                else:
                    self.conf[i] = self.check_conf_helper(i,default_conf[i])
            else:
                self.conf[i] = self.default_conf[i]

    def check_conf_helper(self,key,default_value,min_value=None,max_value=None):
        value = self.conf[key]
        value_type = type(value)
        if type(value) is not value_type:
            return default_value
        else:
            if min_value is not None and value < min_value:
                return min_value
            if max_value is not None and value > max_value:
                return max_value
            return value

    def update_trace(self):
        # update trace number
        self.conf["trace"] = self.conf["trace"]+1

        # load possibly old configuration and update trace
        file_conf = json.load(open(self.conf_file))
        file_conf["trace"] = self.conf["trace"]

        # save updated configuration (without possible temp changed made in self.conf) to file
        with open(self.conf_file,'w') as f:
            json.dump(file_conf,f)

    def save_conf(self):
        with open(self.conf_file,'w') as f:
            json.dump(self.conf,f)

    def pir_recording(self):
        print("[INFO] Starting PIR recording.")
        self.update_trace()
        trace_suffix = "trace%d/" % self.conf["trace"]
        self.conf["directory"] = os.path.join(self.conf["home"], self.conf["directory"], trace_suffix)
        if not os.path.exists(self.conf["directory"]):
            os.mkdir(self.conf["directory"])
        self.PIR = PIR(self.camera,self.conf)
        self.PIR.run(self.tmp["STOP_FN"], self.tmp["STOP_BT"])
        self.PIR.delete()
        idx_end_home_dir = self.conf["directory"].find(self.conf["home"])+len(self.conf["home"])
        idx_start_trace = self.conf["directory"].rfind(trace_suffix)
        self.conf["directory"] = self.conf["directory"][idx_end_home_dir:idx_start_trace]
        self.PIR = None
        print("[INFO] PIR recording ended.")

    def video_recording(self):
        print("[INFO] Starting video recording")
        md = MD(self.camera, self.conf)

    def generate_server_files(self):
        image_directory = os.path.join(self.conf["home"], self.conf["directory"])
        data_directory = os.path.join(self.conf["home"],'server/_data/')
        html_directory = os.path.join(self.conf["home"],'server/gallery/')
        # check if data directory exists
{'fraction_non_alphanumeric': 0.13797428661022265, 'fraction_numerical': 0.015914079648792726, 'mean_word_length': 2.4146145610278373, 'pattern_counts': {'":': 22, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5120614', 'n_tokens_mistral': 4012, 'n_tokens_neox': 3828, 'n_words': 877}