def off(self) -> bool: """ Turns off LED strip :return led_off_err: bool """ info = inspect.getframeinfo(frame=inspect.stack()[1][0]) err_msg_base = 'FILE: ' + info.filename + ' ' + \ 'FUNCTION: ' + info.function led_off_err = False try: for pixel in range(self.led.numPixels()): self.led.setPixelColor(n=pixel, color=self.color()) self.led.show() log = 'Successfully turned off LEDs.' logger.info(msg=log) print(log) except Exception as exc: led_off_err = True log = 'Failed to turn off LEDs.' logger.error(msg=log) logger.error(msg=exc) print(log) print(exc) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=self.err_xmit_url, err_msg=err_msg) return led_off_err
def on(self, led_set_dict: dict) -> bool: """ Turns on LED strip and passes to each identical settings :param led_set_dict: dict :return led_on_err: bool """ info = inspect.getframeinfo(frame=inspect.stack()[1][0]) err_msg_base = 'FILE: ' + info.filename + ' ' + \ 'FUNCTION: ' + info.function led_on_err = False try: for pixel in range(self.led.numPixels()): self.led.setPixelColor(n=pixel, color=self.color( red=led_set_dict['r'], green=led_set_dict['g'], blue=led_set_dict['b'], white=led_set_dict['w'])) self.led.show() log = 'Successfully turned on LEDs.' logger.info(msg=log) print(log) except Exception as exc: led_on_err = True log = 'Failed to turn on LEDs.' logger.error(msg=log) logger.error(msg=exc) print(log) print(exc) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=self.err_xmit_url, err_msg=err_msg) return led_on_err
def __init__(self, err_xmit_url: str, led_cfg_dict: dict) -> None: """ Instantiates object :param err_xmit_url: str :param led_cfg_dict: dict """ self.err_xmit_url = err_xmit_url info = inspect.getframeinfo(frame=inspect.stack()[1][0]) err_msg_base = 'FILE: ' + info.filename + ' ' + \ 'FUNCTION: ' + info.function # Create LED object for pixels attached to given pin try: self.led = rpi_ws281x.PixelStrip( num=led_cfg_dict['count'], pin=led_cfg_dict['pin'], freq_hz=led_cfg_dict['freq_hz'], dma=led_cfg_dict['dma'], invert=led_cfg_dict['invert'], brightness=led_cfg_dict['brightness'], channel=led_cfg_dict['channel'], strip_type=rpi_ws281x.SK6812_STRIP_GRBW) self.led.begin() log = 'LEDs initialized.' logger.info(msg=log) print(log) self.off() except Exception as exc: log = 'Failed to initialize LEDs.' logger.error(msg=log) logger.error(msg=exc) print(log) print(exc) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=self.err_xmit_url, err_msg=err_msg)
def snap_shot( err_xmit_url: str, led_cfg_dict: dict, led_set_dict: dict, cam_cfg_dict: dict, ) -> (any, bool): """ Captures and saves original image :param err_xmit_url: dict :param led_cfg_dict: dict :param led_set_dict: dict :param cam_cfg_dict: dict :return img_orig: stream :return img_orig_err: bool """ logfile = 'januswm-capture' logger = logging.getLogger(logfile) info = inspect.getframeinfo(frame=inspect.stack()[1][0]) err_msg_base = 'FILE: ' + info.filename + ' ' + \ 'FUNCTION: ' + info.function img_orig = None img_orig_err = False cmd_err_count = 0 timeout = 30 # Turn flash on flash = led.LED( err_xmit_url=err_xmit_url, led_cfg_dict=led_cfg_dict ) led_on_err = flash.on(led_set_dict=led_set_dict) if not led_on_err: while cmd_err_count < 2: # signal.alarm(timeout) try: camera = PiCamera() camera.sensor_mode = cam_cfg_dict['mode'] camera.resolution = (cam_cfg_dict['width'], cam_cfg_dict['height']) camera.shutter_speed = cam_cfg_dict['shutter'] camera.sharpness = cam_cfg_dict['sharpness'] camera.saturation = cam_cfg_dict['saturation'] camera.rotation = cam_cfg_dict['rotation'] camera.exposure_mode = cam_cfg_dict['exposure'] img_orig = BytesIO() # Quality is handled by PIL next step downstream camera.capture( output=img_orig, format='jpeg', quality=100 ) img_orig.seek(0) camera.close() break except Exception as exc: img_orig_err = True cmd_err_count += 1 log = 'Timeout took place at {0} seconds'. \ format(timeout) logger.error(msg=exc) logger.error(msg=log) print(exc) print(log) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors( err_xmit_url=err_xmit_url, err_msg=err_msg ) else: img_orig_err = True log = 'LED flash failed to fire, no image captured.' logger.error(msg=log) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors( err_xmit_url=err_xmit_url, err_msg=err_msg ) # Turn flash off flash.off() return img_orig, img_orig_err
def video( err_xmit_url: str, led_cfg_dict: dict, led_set_dict: dict, cam_cfg_dict: dict, img_orig_url: str, time_record: int, ) -> bool: """ Captures and saves video :param err_xmit_url: dict :param led_cfg_dict: dict :param led_set_dict: dict :param cam_cfg_dict: dict :param img_orig_url: str :param time_record: int :return img_orig_err: bool """ logfile = 'januswm-video' logger = logging.getLogger(logfile) info = inspect.getframeinfo(frame=inspect.stack()[1][0]) err_msg_base = 'FILE: ' + info.filename + ' ' + \ 'FUNCTION: ' + info.function img_orig_err = False cmd_err_count = 0 rstill_check = False timeout = time_record + 10 def timeout_handler( signum, frame ): raise Exception('Timeout occurred for raspistill capture') signal.signal( signal.SIGALRM, timeout_handler ) # Turn flash on flash = led.LED( err_xmit_url=err_xmit_url, led_cfg_dict=led_cfg_dict ) led_on_err = flash.on(led_set_dict=led_set_dict) if not led_on_err: while cmd_err_count < 2: signal.alarm(timeout) try: cmd_str0 = 'raspivid ' +\ '-ss ' + str(cam_cfg_dict['shutter']) + ' ' +\ '-sh ' + str(cam_cfg_dict['sharpness']) + ' ' +\ '-sa ' + str(cam_cfg_dict['saturation']) + ' ' +\ '-rot ' + str(cam_cfg_dict['rotation']) + ' ' + \ '-fps 5 ' +\ '-sg 60000 ' + \ '-t ' + str(time_record) + ' ' +\ '-n ' +\ '-o ' + img_orig_url cmd_err0, rtn_code0, std_out0 = os_cmd.os_cmd( err_xmit_url=err_xmit_url, cmd_str=cmd_str0 ) if (not cmd_err0) and (rtn_code0 == 0): break else: img_orig_err = True cmd_err_count += 1 if rtn_code0 == 64: log = 'Bad command line parameter for raspistill'. \ format(img_orig_url, rtn_code0) logger.error(msg=log) err_msg = err_msg_base + ' ' +\ 'MESSAGE: ' + log + '\n' errors.errors( err_xmit_url=err_xmit_url, err_msg=err_msg ) break elif rtn_code0 == 70: rstill_check = True log = 'Software or camera error for raspistill'. \ format(img_orig_url, rtn_code0) logger.error(msg=log) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors( err_xmit_url=err_xmit_url, err_msg=err_msg ) else: signal.alarm(0) break except Exception as exc: img_orig_err = True cmd_err_count += 1 rstill_check = True log = 'Timeout took place at {0} seconds'. \ format(timeout) logger.error(msg=exc) logger.error(msg=log) print(exc) print(log) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors( err_xmit_url=err_xmit_url, err_msg=err_msg ) finally: signal.alarm(0) if not rstill_check: log = 'Checking if raspistill improperly shutdown.' logger.info(msg=log) print(log) cmd_str1 = 'ps ' + \ '-aux' cmd_err1, rtn_code1, std_out1 = os_cmd.os_cmd( err_xmit_url=err_xmit_url, cmd_str=cmd_str1 ) if not cmd_err1: processes = std_out1.split('\n') logger.info(msg=processes[0]) print(processes[0]) nbr_fields = len(processes[0].split()) - 1 found_pid = False for line in processes: fields = line.split(None, nbr_fields) if fields and (fields[0] == 'root') and ('raspistill' in fields[nbr_fields]): found_pid = True logger.info(msg=line) print(line) os.kill( int(fields[1]), signal.SIGTERM ) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + 'Killed raspistill process.' + '\n' errors.errors( err_xmit_url=err_xmit_url, err_msg=err_msg ) if not found_pid: log = 'No raspistill processes found.' logger.info(msg=log) print(log) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors( err_xmit_url=err_xmit_url, err_msg=err_msg ) else: img_orig_err = True log = 'LED flash failed to fire, no image captured.' logger.error(msg=log) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors( err_xmit_url=err_xmit_url, err_msg=err_msg ) # Turn flash off flash.off() return img_orig_err
def reduce_image( img_orig_stream, err_xmit_url: str, img_orig_url: str, img_dest_url: str, img_dest_qual: int = 100, ) -> (bool, str): """ Crops and saves given image :param img_orig_stream :param err_xmit_url: dict :param img_orig_url: str :param img_dest_url: str :param img_dest_qual: int :return img_copy_err: bool """ logfile = 'januswm-capture' logger = logging.getLogger(logfile) img_copy_err = False img_orig = None try: if img_orig_stream is not None: img_orig = Image.open(fp=img_orig_stream) elif os.path.isfile(path=img_orig_url): img_orig = Image.open(fp=img_orig_url) else: img_copy_err = True log = 'OS failed to locate image {0} to save.'. \ format(img_orig_url) logger.error(msg=log) print(log) if not err_xmit_url == '': info = inspect.getframeinfo(frame=inspect.stack()[1][0]) err_msg_base = 'FILE: ' + info.filename + ' ' + \ 'FUNCTION: ' + info.function err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=err_xmit_url, err_msg=err_msg) if not img_copy_err: img_orig = img_orig.crop(box=(0, 0, 740, 740)) img_orig.save(fp=img_dest_url, format='jpeg', optimize=True, quality=img_dest_qual) img_orig.close() log = 'PIL successfully saved image {0}.'.\ format(img_dest_url) logger.info(msg=log) except Exception as exc: img_copy_err = True log = 'PIL failed to save image {0}.'.format(img_dest_url) logger.error(msg=log) logger.error(msg=exc) print(log) print(exc) if not err_xmit_url == '': info = inspect.getframeinfo(frame=inspect.stack()[1][0]) err_msg_base = 'FILE: ' + info.filename + ' ' + \ 'FUNCTION: ' + info.function err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=err_xmit_url, err_msg=err_msg) return img_copy_err
def snap_shot(sw_mode: str, err_xmit_url: str, led_cfg_dict: dict, led_set_dict: dict, cam_cfg_dict: dict, img_orig_url: str, img_orig_qual: int = 100) -> tuple: """ Captures and saves original image :param sw_mode: str :param err_xmit_url: dict :param led_cfg_dict: dict :param led_set_dict: dict :param cam_cfg_dict: dict :param img_orig_url: str :param img_orig_qual: int :return img_orig: picamera object :return img_orig_err: bool """ logfile = 'januswm-capture' logger = logging.getLogger(logfile) info = inspect.getframeinfo(frame=inspect.stack()[1][0]) err_msg_base = 'FILE: ' + info.filename + ' ' + \ 'FUNCTION: ' + info.function img_orig_err = False cmd_err_count = 0 rstill_check = False timeout = 30 img_orig = None def timeout_handler(signum, frame): raise Exception('Timeout occurred for raspistill capture') signal.signal(signal.SIGALRM, timeout_handler) # Turn flash on flash = led.LED(err_xmit_url=err_xmit_url, led_cfg_dict=led_cfg_dict) led_on_err = flash.on(led_set_dict=led_set_dict) if not led_on_err: while cmd_err_count < 2: signal.alarm(timeout) try: if sw_mode == 'rs': cmd_str0 = 'raspistill ' + \ '-md ' + str(cam_cfg_dict['mode']) + ' ' + \ '-w ' + str(cam_cfg_dict['width']) + ' ' + \ '-h ' + str(cam_cfg_dict['height']) + ' ' + \ '-ss ' + str(cam_cfg_dict['shutter']) + ' ' + \ '-sh ' + str(cam_cfg_dict['sharpness']) + ' ' + \ '-sa ' + str(cam_cfg_dict['saturation']) + ' ' + \ '-rot ' + str(cam_cfg_dict['rotation']) + ' ' + \ '-ex ' + str(cam_cfg_dict['exposure']) + ' ' + \ '-n ' + \ '-o ' + img_orig_url + ' ' + \ '-e ' + str(cam_cfg_dict['encoding']) if str(cam_cfg_dict['encoding']) == 'jpg': cmd_str0 += ' -q ' + str(img_orig_qual) cmd_err0, rtn_code0, std_out0 = os_cmd.os_cmd( err_xmit_url=err_xmit_url, cmd_str=cmd_str0) if (not cmd_err0) and (rtn_code0 == 0): break else: img_orig_err = True cmd_err_count += 1 if rtn_code0 == 64: log = 'Bad command line parameter for raspistill'. \ format(img_orig_url, rtn_code0) logger.error(msg=log) print(log) err_msg = err_msg_base + ' ' +\ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=err_xmit_url, err_msg=err_msg) break elif rtn_code0 == 70: rstill_check = True log = 'Software or camera error for raspistill'. \ format(img_orig_url, rtn_code0) logger.error(msg=log) print(log) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=err_xmit_url, err_msg=err_msg) else: signal.alarm(0) break elif sw_mode == 'pc': camera = PiCamera() camera.sensor_mode = cam_cfg_dict['mode'] camera.resolution = (cam_cfg_dict['width'], cam_cfg_dict['height']) camera.shutter_speed = cam_cfg_dict['shutter'] camera.sharpness = cam_cfg_dict['sharpness'] camera.saturation = cam_cfg_dict['saturation'] camera.rotation = cam_cfg_dict['rotation'] camera.exposure_mode = cam_cfg_dict['exposure'] # if img_orig_qual == 100: # if str(cam_cfg_dict['encoding']) == 'png': # camera.capture( # output=img_orig_url, # format='png' # ) # elif str(cam_cfg_dict['encoding']) == 'jpg': # camera.capture( # output=img_orig_url, # format='jpeg', # quality=100 # ) # elif img_orig_qual <= 100: img_orig = BytesIO() if str(cam_cfg_dict['encoding']) == 'png': camera.capture(output=img_orig, format='png') elif str(cam_cfg_dict['encoding']) == 'jpg': camera.capture(output=img_orig, format='jpeg', quality=100) img_orig.seek(0) camera.close() break except Exception as exc: img_orig_err = True cmd_err_count += 1 rstill_check = True log = 'Timeout took place at {0} seconds'. \ format(timeout) logger.error(msg=exc) logger.error(msg=log) print(exc) print(log) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=err_xmit_url, err_msg=err_msg) finally: signal.alarm(0) if (sw_mode == 'rs') and not rstill_check: log = 'Checking if raspistill improperly shutdown.' logger.info(msg=log) print(log) cmd_str1 = 'ps ' + \ '-aux' cmd_err1, rtn_code1, std_out1 = os_cmd.os_cmd( err_xmit_url=err_xmit_url, cmd_str=cmd_str1) if not cmd_err1: processes = std_out1.split('\n') logger.info(msg=processes[0]) print(processes[0]) nbr_fields = len(processes[0].split()) - 1 found_pid = False for line in processes: fields = line.split(None, nbr_fields) if fields and (fields[0] == 'root') and ('raspistill' in fields[nbr_fields]): found_pid = True logger.info(msg=line) print(line) os.kill(int(fields[1]), signal.SIGTERM) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + 'Killed raspistill process.' + '\n' errors.errors(err_xmit_url=err_xmit_url, err_msg=err_msg) if not found_pid: log = 'No raspistill processes found.' logger.info(msg=log) print(log) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=err_xmit_url, err_msg=err_msg) else: img_orig_err = True log = 'LED flash failed to fire, no image captured.' logger.error(msg=log) err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=err_xmit_url, err_msg=err_msg) # Turn flash off flash.off() return img_orig, img_orig_err
def os_cmd( err_xmit_url: str, cmd_str: str, ) -> tuple: """ Executes operating system command :param err_xmit_url: str :param cmd_str: str :return cmd_err: bool :return cmd_std_out: any """ cmd_err = False logger.info(cmd_str) print(cmd_str) process = subprocess.Popen(cmd_str.split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) std_out, std_err = process.communicate() std_out = std_out.decode('utf-8') std_err = std_err.decode('utf-8') rtn_code = process.returncode if std_out == '': log = 'No reported output from command.' logger.info(msg=log) print(log) else: logger.info(msg=std_out) if std_err == '': log = 'No reported error from command.' logger.info(msg=log) print(log) else: cmd_err = True log = 'Error reported by command: {0}.'.format(std_err) logger.error(msg=log) print(std_err) if not err_xmit_url == '': info = inspect.getframeinfo(frame=inspect.stack()[1][0]) err_msg_base = 'FILE: ' + info.filename + ' ' + \ 'FUNCTION: ' + info.function err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=err_xmit_url, err_msg=err_msg) if rtn_code == 0: log = 'Successfully executed command and returned code: {0}.'.format( rtn_code) logger.info(msg=log) print(log) else: cmd_err = True log = 'Unsuccessfully executed command and returned code: {0}.'.format( rtn_code) logger.error(msg=log) print(log) if not err_xmit_url == '': info = inspect.getframeinfo(frame=inspect.stack()[1][0]) err_msg_base = 'FILE: ' + info.filename + ' ' + \ 'FUNCTION: ' + info.function err_msg = err_msg_base + ' ' + \ 'MESSAGE: ' + log + '\n' errors.errors(err_xmit_url=err_xmit_url, err_msg=err_msg) return cmd_err, rtn_code, std_out