def tcpserve(cfg, recmng, vvpmng): """ tcpserve """ ipaddr = cfg.comm_port.address serve_port = cfg.comm_port.tcp_port try: if ipaddr is '': raise AppException('get local ip exp') if int(serve_port) <= 0 or int(serve_port) > 65535: raise AppException('port num err') except AppException as ex: APPLOGGER.error(ex) sys.exit(1) host, port = ipaddr, int(serve_port) server = None try: server = TcpCtlServer((host, port), TcpCtlHandler, cfg, recmng, vvpmng) except socket.error as ex: APPLOGGER.error(ex) sys.exit(1) APPLOGGER.info('Server Up IP=%s PORT=%s', ipaddr, serve_port) if server: server.serve_forever() else: raise AppException('server start err')
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)
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)
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()
def main(): """ serve for all """ config_path = '/home/pi/server/config/raspicam.cfg' cfg_parser = ConfigReader(config_path) cfg = cfg_parser.parser() recmng = RecordMng(cfg.record) vvpmng = VideoProcessMng(cfg.video) global PROCESSMNG PROCESSMNG = vvpmng hyserve = HybirdServer() # you can start tcp server or http server try: hyserve.setservices('httpserver', httpserve, (cfg, recmng, vvpmng)) hyserve.setservices('tcpserver', tcpserve, (cfg, recmng, vvpmng)) hyserve.setservices('vodserver', vodserve, (cfg, )) hyserve.serve() except AppException as ex: APPLOGGER.error(ex)
def main(): """ serve for all """ config_path = '/home/pi/server/config/raspicam.cfg' cfg_parser = ConfigReader(config_path) cfg = cfg_parser.parser() recmng = RecordMng(cfg.record) vvpmng = VideoProcessMng(cfg.video) global PROCESSMNG PROCESSMNG = vvpmng hyserve = HybirdServer() # you can start tcp server or http server try: hyserve.setservices('httpserver', httpserve, (cfg, recmng, vvpmng)) hyserve.setservices('tcpserver', tcpserve, (cfg, recmng, vvpmng)) hyserve.setservices('vodserver', vodserve, (cfg,)) hyserve.serve() except AppException as ex: APPLOGGER.error(ex)
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)
def httpserve(cfg, recmng, vvpmng): """ httpserve """ ipaddr = cfg.comm_port.address serve_port = cfg.comm_port.http_port try: if ipaddr is '': raise AppException('get local ip exp') if int(serve_port) <= 0 or int(serve_port) > 65535: raise AppException('port num err') if cfg == None: raise AppException('cfg is null') else: from raspiserver.utils import ConfigObject if type(cfg) != ConfigObject: raise AppException('parameter type not correct') except AppException as ex: APPLOGGER.error(ex) sys.exit(1) APPLOGGER.info('Server Up IP=%s PORT=%s', ipaddr, serve_port) server = None try: server = HttpCtlServer((ipaddr, serve_port), \ HttpCtlHandler, cfg, recmng, vvpmng) except socket.error as ex: APPLOGGER.error(ex) sys.exit(1) if server: server.serve_forever() else: APPLOGGER.error('http server start error')
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()
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()
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)
def gen_recordfname(self): """ generate the video filename """ currtime = gmtime() rec_sub_dir_name = '' rec_sub_dir_name += str(currtime.tm_year) + '_' rec_sub_dir_name += str(currtime.tm_mon) + '_' rec_sub_dir_name += str(currtime.tm_mday) day_recdir = self.__recordbase + '/' + rec_sub_dir_name filename = str(currtime.tm_hour) + ':' + \ str(currtime.tm_min) + ':' + \ str(currtime.tm_sec) whole_fname = day_recdir + '/' + filename try: if exists(day_recdir): if isdir(day_recdir): if exists(whole_fname): remove(whole_fname) else: remove(day_recdir) makedirs(day_recdir) else: makedirs(day_recdir) except OSError as ex: APPLOGGER.error(ex) # can record but don't have enough space try: if self.have_space(): return whole_fname else: if self.cycle: self.__free_space() return whole_fname raise AppException('no space') except AppException as ex: APPLOGGER.info('free space is: ' + str(self.get_freespaces())) APPLOGGER.info('limit is : ' + str(self.lefthrhold)) APPLOGGER.error(ex)
def __process_req(self, data): """ process req """ data = data.strip(' \n') if len(data) <= 0: return callinfo = data.lower() callinfo = '__' + callinfo callinfo = callinfo.split('|') splitlen = len(callinfo) if splitlen < 1 or splitlen > 2: APPLOGGER.warn('request parameter not correct') return try: callback = getattr(self, '_' + \ self.__class__.__name__ + callinfo[0]) if callback != None and callable(callback): if splitlen == 1: callback() else: callback(data) else: APPLOGGER.error('request callback error') except AttributeError as ex: APPLOGGER.error(ex)
def gen_recordfname(self): """ generate the video filename """ currtime = gmtime() rec_sub_dir_name = "" rec_sub_dir_name += str(currtime.tm_year) + "_" rec_sub_dir_name += str(currtime.tm_mon) + "_" rec_sub_dir_name += str(currtime.tm_mday) day_recdir = self.__recordbase + "/" + rec_sub_dir_name filename = str(currtime.tm_hour) + ":" + str(currtime.tm_min) + ":" + str(currtime.tm_sec) whole_fname = day_recdir + "/" + filename try: if exists(day_recdir): if isdir(day_recdir): if exists(whole_fname): remove(whole_fname) else: remove(day_recdir) makedirs(day_recdir) else: makedirs(day_recdir) except OSError as ex: APPLOGGER.error(ex) # can record but don't have enough space try: if self.have_space(): return whole_fname else: if self.cycle: self.__free_space() return whole_fname raise AppException("no space") except AppException as ex: APPLOGGER.info("free space is: " + str(self.get_freespaces())) APPLOGGER.info("limit is : " + str(self.lefthrhold)) APPLOGGER.error(ex)
def vodserve(cfg): """ vod serve """ ipaddr = cfg.comm_port.address serve_port = cfg.comm_port.vod_port try: if ipaddr is '': raise AppException('get local ip exp') if int(serve_port) <= 0 or int(serve_port) > 65535: raise AppException('port num err') except AppException as ex: APPLOGGER.error(ex) sys.exit(1) APPLOGGER.info('Server Up IP=%s PORT=%s', ipaddr, serve_port) server = None try: server = VODServer((ipaddr, serve_port), VODReqHandler) except sockerror as ex: APPLOGGER.error(ex) sys.exit(1) if server: server.serve_forever() else: APPLOGGER.error('vod server start error')
def parser(self): """ parser func """ try: if exists(self.__path) and isfile(self.__path): with open(self.__path) as fhandler: try: config_parser = ConfigParser() config_parser.readfp(fhandler) if config_parser.has_option('video', 'width'): self.__config.video.width = \ config_parser.getint('video', 'width') if config_parser.has_option('video', 'height'): self.__config.video.height = \ config_parser.getint('video', 'height') if config_parser.has_option('video', 'fps'): self.__config.video.fps = \ config_parser.getint('video', 'fps') if config_parser.has_option('video', 'bitrate'): self.__config.video.bitrate = \ config_parser.getint('video', 'bitrate') if config_parser.has_option('video', 'brightness'): self.__config.video.brightness = \ config_parser.getint('video', 'brightness') if config_parser.has_option('video', 'rtsp_port'): self.__config.video.rtsp_port = \ config_parser.getint('video', 'rtsp_port') if config_parser.has_option('record', 'base'): self.__config.record.base = \ config_parser.get('record', 'base') if config_parser.has_option('record', 'cycle'): self.__config.record.cycle = \ config_parser.getboolean('record', 'cycle') if config_parser.has_option('record', 'limit'): self.__config.record.fsp_limit = \ config_parser.getint('record', 'limit') if config_parser.has_option('common', 'tcp_port'): self.__config.comm_port.tcp_port = \ config_parser.getint('common', 'tcp_port') if config_parser.has_option('common', 'http_port'): self.__config.comm_port.http_port = \ config_parser.getint('common', 'http_port') if config_parser.has_option('common', 'vod_port'): self.__config.comm_port.vod_port = \ config_parser.getint('common', 'vod_port') # assign default values if value is invalid if not self.__config.comm_port.http_port: self.__config.comm_port.http_port = 8080 if not self.__config.comm_port.tcp_port: self.__config.comm_port.tcp_port = 9999 if not self.__config.comm_port.vod_port: self.__config.comm_port.vod_port = 9001 self.__config.comm_port.address = get_local_ip() except (AppException, ConfigError) as ex: APPLOGGER.error(ex) else: raise AppException('config file error') except OSError as ex: APPLOGGER.error(ex) return self.__config