Ejemplo n.º 1
0
def __test():
    """ test function """
    from raspiserver.utils import ConfigReader
    cfg_parser = ConfigReader('./config/raspicam.cfg')
    cfg = cfg_parser.parser()
    videocmd = RaspvidCmd(cfg.video)
    APPLOGGER.debug(videocmd.cmd())
Ejemplo n.º 2
0
def __test():
    """ test function """
    from raspiserver.utils import ConfigReader
    cfg_parser = ConfigReader('./config/raspicam.cfg')
    cfg = cfg_parser.parser()
    videocmd = RaspvidCmd(cfg.video)
    APPLOGGER.debug(videocmd.cmd())
Ejemplo n.º 3
0
    def __free_space(self):
        """ free disk space for record if self.cycle == True """
        rec_files = self.get_recordfiles()

        def filesort(file1, file2):
            """ sort by create time """
            ctime1 = getctime(file1)
            ctime2 = getctime(file2)
            if ctime1 < ctime2:
                return -1
            elif ctime1 == ctime2:
                return 0
            else:
                return 1

        rec_files.sort(cmp=filesort)

        def getfsize(path):
            """ get size for %.3f """
            return float("%.3f" % (getsize(path) / 1024.0 / 1024.0))

        for item in rec_files:
            if self.get_freespaces() < self.lefthrhold:
                APPLOGGER.debug(getvodfile())
                if getvodfile().strip(" ") == item.strip(" "):
                    APPLOGGER.debug("in use: " + getvodfile())
                    continue
                else:
                    APPLOGGER.debug("relase space is : " + str(getfsize(item)))
                    remove(item)
            else:
                break
        APPLOGGER.debug("free space is: " + str(self.get_freespaces()))
Ejemplo n.º 4
0
    def __free_space(self):
        """ free disk space for record if self.cycle == True """
        rec_files = self.get_recordfiles()
        def filesort(file1, file2):
            """ sort by create time """
            ctime1 = getctime(file1)
            ctime2 = getctime(file2)
            if ctime1 < ctime2:
                return -1
            elif ctime1 == ctime2:
                return 0
            else:
                return 1

        rec_files.sort(cmp=filesort)
        def getfsize(path):
            """ get size for %.3f """
            return float('%.3f' % (getsize(path) / 1024.0 / 1024.0))
        for item in rec_files:
            if self.get_freespaces() < self.lefthrhold:
                APPLOGGER.debug(getvodfile())
                if getvodfile().strip(' ') == item.strip(' '):
                    APPLOGGER.debug('in use: ' + getvodfile())
                    continue
                else:
                    APPLOGGER.debug('relase space is : ' + str(getfsize(item)))
                    remove(item)
            else:
                break
        APPLOGGER.debug('free space is: ' + str(self.get_freespaces()))
Ejemplo n.º 5
0
    def __change(self, form):
        """ change video process params """
        self.vvpmng.getlock()
        try:

            def getvalue(src):
                """ set value """
                retval = -1
                try:
                    retval = int(src)
                except ValueError as ex:
                    APPLOGGER.warn(ex)
                return retval

            def setpara(key, form, dst):
                """ set params """
                if key in form.keys():
                    tmpval = -1
                    tmpval = getvalue(form[key].value)
                    if tmpval != -1:
                        dst = tmpval
                return dst

            self.vvpmng.process_cmd.bright = \
                    setpara('para_bright', form, self.vvpmng.process_cmd.bright)
            self.vvpmng.process_cmd.fps = \
                    setpara('para_fps', form, self.vvpmng.process_cmd.fps)
            self.vvpmng.process_cmd.bitrate = \
                    setpara('para_bitrate', form, \
                    self.vvpmng.process_cmd.bitrate)
            self.vvpmng.process_cmd.width = \
                    setpara('para_width', form, self.vvpmng.process_cmd.width)
            self.vvpmng.process_cmd.height = \
                    setpara('para_height', form, self.vvpmng.process_cmd.height)

            APPLOGGER.debug(self.vvpmng.process_cmd.cmd())
            self.__sendmsg(200, self.vvpmng.process_cmd.cmd())

            if not self.vvpmng.isset():
                self.vvpmng.start()
                return
            if self.vvpmng.isrun():
                self.vvpmng.stop()
                self.vvpmng.setprocess(None)
                self.vvpmng.start()
            else:
                self.vvpmng.start()
        finally:
            self.vvpmng.releaselock()
Ejemplo n.º 6
0
    def __watch_dog(self):
        """ watch the free space """
        freespace = 0
        while 1:
            self.getlock()
            try:
                if not self.have_space() and self.cycle:
                    self.__free_space()
            except OSError:
                pass
            finally:
                freespace = self.get_freespaces()
                self.releaselock()

            APPLOGGER.debug("free space is: " + str(freespace) + "MB")
            sleep(2)
Ejemplo n.º 7
0
    def __change(self, form):
        """ change video process params """
        self.vvpmng.getlock()
        try:
            def getvalue(src):
                """ set value """
                retval = -1
                try:
                    retval = int(src)
                except ValueError as ex:
                    APPLOGGER.warn(ex)
                return retval
            def setpara(key, form, dst):
                """ set params """
                if key in form.keys():
                    tmpval = -1
                    tmpval = getvalue(form[key].value)
                    if tmpval != -1:
                        dst = tmpval
                return dst

            self.vvpmng.process_cmd.bright = \
                    setpara('para_bright', form, self.vvpmng.process_cmd.bright)
            self.vvpmng.process_cmd.fps = \
                    setpara('para_fps', form, self.vvpmng.process_cmd.fps)
            self.vvpmng.process_cmd.bitrate = \
                    setpara('para_bitrate', form, \
                    self.vvpmng.process_cmd.bitrate)
            self.vvpmng.process_cmd.width = \
                    setpara('para_width', form, self.vvpmng.process_cmd.width)
            self.vvpmng.process_cmd.height = \
                    setpara('para_height', form, self.vvpmng.process_cmd.height)

            APPLOGGER.debug(self.vvpmng.process_cmd.cmd())
            self.__sendmsg(200, self.vvpmng.process_cmd.cmd())

            if not self.vvpmng.isset():
                self.vvpmng.start()
                return
            if self.vvpmng.isrun():
                self.vvpmng.stop()
                self.vvpmng.setprocess(None)
                self.vvpmng.start()
            else:
                self.vvpmng.start()
        finally:
            self.vvpmng.releaselock()
Ejemplo n.º 8
0
    def __watch_dog(self):
        """ watch the free space """
        freespace = 0
        while 1:
            self.getlock()
            try:
                if not self.have_space() and self.cycle:
                    self.__free_space()
            except OSError:
                pass
            finally:
                freespace = self.get_freespaces()
                self.releaselock()

            APPLOGGER.debug('free space is: ' + \
                    str(freespace) + 'MB')
            sleep(2)
Ejemplo n.º 9
0
    def __change(self, data):
        """ change video process paramters
            change|cmd1=opt, cmd2=opt2, ... """
        data = data.lstrip('change|')
        data = data.split(',')
        APPLOGGER.debug(data)
        paradict = {}
        try:
            paradict = dict([item.split('=') for item in data if item != ''])
            if not paradict:
                raise AppException('paradict dict is empty')
        except AppException as ex:
            APPLOGGER.error(ex)
            return

        if 'brightness' in paradict:
            self.vvpmng.process_cmd.bright = int(paradict['brightness'])
        if 'bitrate' in paradict:
            self.vvpmng.process_cmd.bitrate = int(paradict['bitrate'])
        if 'fps' in paradict:
            self.vvpmng.process_cmd.fps = int(paradict['fps'])
        if 'height' in paradict:
            self.vvpmng.process_cmd.height = int(paradict['height'])
        if 'width' in paradict:
            self.vvpmng.process_cmd.width = int(paradict['width'])

        # just change parameter no record here
        self.vvpmng.process_cmd.record = False
        self.vvpmng.process_cmd.recordfname = ''

        self.vvpmng.getlock()
        try:
            if not self.vvpmng.isset():
                self.vvpmng.start()
                return
            if self.vvpmng.isrun():
                self.vvpmng.stop()
                self.vvpmng.setprocess(None)
                self.vvpmng.start()
            else:
                self.vvpmng.start()
        finally:
            self.vvpmng.releaselock()
        self.request.sendall(self.vvpmng.process_cmd.cmd())
        self.request.close()
Ejemplo n.º 10
0
    def __change(self, data):
        """ change video process paramters
            change|cmd1=opt, cmd2=opt2, ... """
        data = data.lstrip('change|')
        data = data.split(',')
        APPLOGGER.debug(data)
        paradict = {}
        try:
            paradict = dict([item.split('=') for item in data if item != ''])
            if not paradict:
                raise AppException('paradict dict is empty')
        except AppException as ex:
            APPLOGGER.error(ex)
            return

        if 'brightness' in paradict:
            self.vvpmng.process_cmd.bright = int(paradict['brightness'])
        if 'bitrate' in paradict:
            self.vvpmng.process_cmd.bitrate = int(paradict['bitrate'])
        if 'fps' in paradict:
            self.vvpmng.process_cmd.fps = int(paradict['fps'])
        if 'height' in paradict:
            self.vvpmng.process_cmd.height = int(paradict['height'])
        if 'width' in paradict:
            self.vvpmng.process_cmd.width = int(paradict['width'])

        # just change parameter no record here
        self.vvpmng.process_cmd.record = False
        self.vvpmng.process_cmd.recordfname = ''

        self.vvpmng.getlock()
        try:
            if not self.vvpmng.isset():
                self.vvpmng.start()
                return
            if self.vvpmng.isrun():
                self.vvpmng.stop()
                self.vvpmng.setprocess(None)
                self.vvpmng.start()
            else:
                self.vvpmng.start()
        finally:
            self.vvpmng.releaselock()
        self.request.sendall(self.vvpmng.process_cmd.cmd())
        self.request.close()
Ejemplo n.º 11
0
 def __init__(self, cfg):
     self.__reclock = Lock()
     self.__recordbase = cfg.base.strip("'") if cfg.base != "" else "/home/pi/records"
     self.lefthrhold = cfg.fsp_limit if cfg.fsp_limit != -1 else 100
     self.cycle = cfg.cycle if cfg.cycle != None else False
     try:
         if not exists(self.__recordbase):
             makedirs(self.__recordbase)
         elif not isdir(self.__recordbase):
             remove(self.__recordbase)
             makedirs(self.__recordbase)
     except OSError as ex:
         APPLOGGER.error(ex)
         APPLOGGER.info("pls use sudo")
         sys.exit(1)
     self.__watchthr = None
     self.__watchthr = Thread(target=self.__watch_dog)
     self.__watchthr.setDaemon(True)
     self.__watchthr.setName("watchthr")
     self.__watchthr.start()
     APPLOGGER.debug(cfg.base)
Ejemplo n.º 12
0
 def do_POST(self):
     """ POST """
     APPLOGGER.debug(self.path)
     try:
         _environ = {'REQUEST_METHOD': 'POST',
                     'CONTENT_TYPE': self.headers['Content-Type'],}
     except KeyError as ex:
         _environ = {}
         APPLOGGER.warn(ex)
     form = cgi.FieldStorage(fp=self.rfile,
                             headers=self.headers,
                             environ=_environ)
     action = self.path
     callinfo = action.replace('/', ' ').strip(' ')
     callinfo = '__' + callinfo
     try:
         callback = getattr(self, '_' + self.__class__.__name__ + callinfo)
         if callback != None and callable(callback):
             callback(form)
         else:
             APPLOGGER.debug(self.path)
             APPLOGGER.debug(str(form))
             self.send_response(503)
             self.end_headers()
     except AttributeError as ex:
         APPLOGGER.error(ex)
Ejemplo n.º 13
0
 def do_POST(self):
     """ POST """
     APPLOGGER.debug(self.path)
     try:
         _environ = {
             'REQUEST_METHOD': 'POST',
             'CONTENT_TYPE': self.headers['Content-Type'],
         }
     except KeyError as ex:
         _environ = {}
         APPLOGGER.warn(ex)
     form = cgi.FieldStorage(fp=self.rfile,
                             headers=self.headers,
                             environ=_environ)
     action = self.path
     callinfo = action.replace('/', ' ').strip(' ')
     callinfo = '__' + callinfo
     try:
         callback = getattr(self, '_' + self.__class__.__name__ + callinfo)
         if callback != None and callable(callback):
             callback(form)
         else:
             APPLOGGER.debug(self.path)
             APPLOGGER.debug(str(form))
             self.send_response(503)
             self.end_headers()
     except AttributeError as ex:
         APPLOGGER.error(ex)
Ejemplo n.º 14
0
    def __record(self):
        """ record video file """
        recfname = ''
        can_rec = False
        self.recmng.getlock()
        try:
            if self.recmng.have_space() or self.recmng.cycle == True:
                recfname = self.recmng.gen_recordfname()
                if recfname == '':
                    raise AppException('record file name is null')
                can_rec = True
            else:
                raise AppException('no space to record')
        except AppException as ex:
            APPLOGGER.error(ex)
        finally:
            self.recmng.releaselock()

        if not can_rec:
            return

        self.vvpmng.getlock()
        self.vvpmng.process_cmd.record = True
        self.vvpmng.process_cmd.recordfname = recfname
        APPLOGGER.debug(self.vvpmng.process_cmd.cmd())
        try:
            if not self.vvpmng.isset():
                self.vvpmng.start()
                return
            if self.vvpmng.isrun():
                self.vvpmng.stop()
                self.vvpmng.setprocess(None)
                self.vvpmng.start()
            else:
                self.vvpmng.start()
        finally:
            self.vvpmng.releaselock()
        self.request.sendall(self.vvpmng.process_cmd.cmd())
        self.request.close()
Ejemplo n.º 15
0
    def __record(self, form):
        """ record video """
        _ = form
        recfname = ''
        can_rec = False
        self.recmng.getlock()
        try:
            if self.recmng.have_space() or self.recmng.cycle == True:
                recfname = self.recmng.gen_recordfname()
                if recfname == '':
                    raise AppException('record file name is null')
                can_rec = True
            else:
                raise AppException('no space to record')
        except AppException as ex:
            APPLOGGER.error(ex)
        finally:
            self.recmng.releaselock()

        if not can_rec:
            return

        self.vvpmng.getlock()
        self.vvpmng.process_cmd.record = True
        self.vvpmng.process_cmd.recordfname = recfname
        APPLOGGER.debug(self.vvpmng.process_cmd.cmd())
        try:
            if not self.vvpmng.isset():
                self.vvpmng.start()
                return
            if self.vvpmng.isrun():
                self.vvpmng.stop()
                self.vvpmng.setprocess(None)
                self.vvpmng.start()
            else:
                self.vvpmng.start()
        finally:
            self.__sendmsg(200, 'record start')
            self.vvpmng.releaselock()
Ejemplo n.º 16
0
 def __init__(self, cfg):
     self.__reclock = Lock()
     self.__recordbase = cfg.base.strip("'") \
             if cfg.base != '' else '/home/pi/records'
     self.lefthrhold = cfg.fsp_limit if cfg.fsp_limit != -1 else 100
     self.cycle = cfg.cycle if cfg.cycle != None else False
     try:
         if not exists(self.__recordbase):
             makedirs(self.__recordbase)
         elif not isdir(self.__recordbase):
             remove(self.__recordbase)
             makedirs(self.__recordbase)
     except OSError as ex:
         APPLOGGER.error(ex)
         APPLOGGER.info('pls use sudo')
         sys.exit(1)
     self.__watchthr = None
     self.__watchthr = Thread(target=self.__watch_dog)
     self.__watchthr.setDaemon(True)
     self.__watchthr.setName('watchthr')
     self.__watchthr.start()
     APPLOGGER.debug(cfg.base)
Ejemplo n.º 17
0
 def handle(self):
     APPLOGGER.debug('theading number is ' + str(threading.activeCount()))
     data = self.request.recv(self.maxbuf)
     self.__process_req(data)
Ejemplo n.º 18
0
def __test():
    """ test func """
    from raspiserver.utils import ConfigReader

    cfg_parser = ConfigReader("./config/raspicam.cfg")
    cfg = cfg_parser.parser()
    recordmng = RecordMng(cfg.record)
    allfiles = recordmng.get_recordfiles()
    APPLOGGER.debug(allfiles)
    APPLOGGER.debug(len(allfiles))

    APPLOGGER.debug(recordmng.get_freespaces())
    APPLOGGER.debug(recordmng.have_space())
    recordmng.lefthrhold = 330
    recordmng.cycle = True
    APPLOGGER.debug(recordmng.have_space())
    APPLOGGER.debug(recordmng.gen_recordfname())
Ejemplo n.º 19
0
 def handle(self):
     APPLOGGER.debug('theading number is ' + str(threading.activeCount()))
     data = self.request.recv(self.maxbuf)
     self.__process_req(data)
Ejemplo n.º 20
0
def __test():
    """ test func """
    from raspiserver.utils import ConfigReader
    cfg_parser = ConfigReader('./config/raspicam.cfg')
    cfg = cfg_parser.parser()
    recordmng = RecordMng(cfg.record)
    allfiles = recordmng.get_recordfiles()
    APPLOGGER.debug(allfiles)
    APPLOGGER.debug(len(allfiles))

    APPLOGGER.debug(recordmng.get_freespaces())
    APPLOGGER.debug(recordmng.have_space())
    recordmng.lefthrhold = 330
    recordmng.cycle = True
    APPLOGGER.debug(recordmng.have_space())
    APPLOGGER.debug(recordmng.gen_recordfname())