def main_with_kanban(opt: Options): lprint("start main_with_kanban()") # get cache kanban conn = opt.get_conn() num = opt.get_number() kanban = conn.get_one_kanban(SERVICE_NAME, num) metadata = kanban.get_metadata() audio_path = metadata.get('audio_file_path') if not audio_path: lprint("no found audio file path") sys.exit(1) # get output data path # data_path = kanban.get_data_path() # get previous service list # service_list = kanban.get_services() ######### main function ############# client = speechRecognize() client.speech_recognize_from_file(audio_path) lprint(client.get_result_status) lprint(client.get_result_text) # output after kanban conn.output_kanban( result=client.get_result_status, metadata={ "transcript": client.get_result_text, "audio_file": audio_path }, )
def main(opt: Options): conn = opt.get_conn() num = opt.get_number() dm = DeviceMonitorByGstreamer() metadata = {METADATA_KEY: {}} while True: device_list = dm.get_device_list_id() if metadata[METADATA_KEY] != device_list: lprint("change device status: ", device_list) metadata = {METADATA_KEY: device_list} for serial, device_id in device_list.items(): # output after kanban conn.output_kanban( connection_key=device_id.split('/')[-1], metadata={METADATA_KEY: { serial: device_id }}, ) sleep(EXECUTE_INTERVAL) try: with UpdateDeviceStateToDB() as my: # devices = list(device_list.keys()) for device_id, path in device_list.items(): my.update_up_device_state(device_id, path) my.update_down_device_state() my.check_invalid_state() my.commit_query() lprint("finish to update camera state") except Exception as e: lprint(str(e))
def main_without_kanban(opt: Options): lprint("start main_without_kanban()") # get cache kanban conn = opt.get_conn() num = opt.get_number() #kanban: Kanban = conn.set_kanban(SERVICE_NAME, num) kanban = conn.get_one_kanban(SERVICE_NAME, num) device_name = os.environ["TRANSFER_DEVICE"] ######### main function ############# yesterday = date.today() - timedelta(days=1) file_name = yesterday.strftime('%Y%m%d') + '.json' backup_dir = f'/var/lib/aion/Data/{SERVICE_NAME}_{num}/output/' backup_file = backup_dir + file_name os.makedirs(backup_dir, exist_ok=True) subprocess.run([ 'mongoexport', '-h', 'mongo', '--db', 'AionCore', '--collection', 'kanban', '-o', backup_file, '--query', r'{"finishAt": /^' + yesterday.strftime('%Y-%m-%d') + '/}' ]) # output after kanban conn.output_kanban( result=True, connection_key="default", file_list=[backup_file], metadata={"file_name": file_name}, device_name=device_name, )
def main(opt: Options): # get cache kanban conn = opt.get_conn() num = opt.get_number() #kanban = conn.get_one_kanban(SERVICE_NAME, num) kanban: Kanban = conn.set_kanban(SERVICE_NAME, num) data_path = f"/var/lib/aion/Data/{SERVICE_NAME}_{num}/data" # open config file config = None with open(os.path.join(CONFIG_PATH, "config.json")) as f: config = json.load(f) result_status = True JST = timezone(timedelta(hours=+9), 'JST') backup_time = datetime.now(JST) backup_dir = os.path.join(data_path, backup_time.strftime('%Y%m%d%H%M%S')) backup_file = config.get('ftp-backup-file') backup_save_path = {} for bf in backup_file: backup_save_path[bf] = os.path.join(backup_dir, bf) host = config.get('ftp-server') user = config.get('ftp-user') passwd = config.get('ftp-passwd') # make backup directory for bsp in backup_save_path.values(): dirname = os.path.dirname(bsp) lprint(f"make backup directory: {dirname}") os.makedirs(dirname, exist_ok=True) ### don't mkdir backup_dir becaus dirs are defined in backup_file ### try: ftpc = FtpClient(host, user, passwd) for bf, bsp in backup_save_path.items(): ftpc.get(bf, bsp) except ftplib.all_errors as error: lprint_exception(f"ftp connection failed : {error}") result_status = False # write backup history to db try: with RobotBackupDB() as db: backup_state = 1 if result_status else 2 # 1:succeeded 2:failed for bsp in backup_save_path.values(): db.set_backup_to_db(bsp, backup_time.strftime('%Y-%m-%d %H:%M:%S'), backup_state) finally: # output after kanban conn.output_kanban( result=True, connection_key="key", output_data_path=data_path, process_number=num, ) return
def main_with_kanban(opt: Options): conn = opt.get_conn() num = opt.get_number() try: for kanban in conn.get_kanban_itr(config.MICROSERVICE_NAME, num): metadata = kanban.get_metadata() # Check metadata keys = [ 'deviceName', 'projectName', 'microserviceName', 'projectCommitId', 'status', 'error' ] for key in keys: if key not in metadata: raise RuntimeError(f"Not found '{key}' in metadadata.") main.main(metadata['deviceName'], metadata['projectName'], metadata['microserviceName'], metadata['projectCommitId'], metadata['status'], metadata['error']) conn.output_kanban(metadata=metadata) lprint(f'Output kanban {metadata}.') except Exception as e: lprint(str(e))
def main_without_kanban(opt: Options): lprint("start main_without_kanban()") # get cache kanban conn = opt.get_conn() num = opt.get_number() kanban: Kanban = conn.set_kanban(SERVICE_NAME, num) ######### main function ############# client = TemperatureClient(host='stream-usb-thermo-by-grpc-server-001-srv', port=50051) while True: # temp = client.get_temperature() temp = client.get() # output after kanban if temp is not None: # image_list = temp.img.tolist() # temp_list = temp.temps.tolist() metadata = { "img": temp.image, "temp": temp.temperatures, "timestamp": temp.timestamp, } # NOTE: can not send decode image because size is too big # reciever need to decode image conn.output_kanban( result=True, connection_key="default", process_number=num, metadata=metadata, ) time.sleep(INTERVAL)
def main(opt: Options): conn = opt.get_conn() num = opt.get_number() try: for kanban in conn.get_kanban_itr(SERVICE_NAME, num): metadata = kanban.get_metadata() template_matching_summary = metadata['TemplateMatchingSummary'] if CURRENT_DEVICE_NAME in ["deneb", "elpis", "moca"]: t = TemplateMatchingSummaryWatch("trigger") t.print_template_matching(template_matching_summary) elif CURRENT_DEVICE_NAME in ["lib", "neo", "poseidon", "tartarus"]: t = TemplateMatchingSummaryWatch("vehicle") t.print_template_matching(template_matching_summary) if template_matching_summary['end']['status']: templates = metadata["TemplateMatchingSetTemplates"] if NEXT_DEVICE_NAME: conn.output_kanban( result=True, connection_key="default", metadata=templates, device_name=NEXT_DEVICE_NAME, ) lprint( f"output_kanban to other device {NEXT_DEVICE_NAME} {templates}" ) else: lprint("next_device_name is none") except Exception: traceback.print_exc() sys.exit(1)
def main_with_kanban_itr(opt: Options): lprint("start main_with_kanban_itr()") # get cache kanban conn = opt.get_conn() num = int(opt.get_number()) try: client = speechRecognize() for kanban in conn.get_kanban_itr(SERVICE_NAME, num): metadata = kanban.get_metadata() lprint(metadata) metadata = kanban.get_metadata() audio_path = metadata.get('audio_file_path') if not audio_path: lprint("no found audio file path") continue client.speech_recognize_from_file(audio_path) lprint(client.get_result_status) lprint(client.get_result_text) # output after kanban conn.output_kanban( result=client.get_result_status, metadata={ "transcript": client.get_result_text, "audio_file": audio_path }, ) except Exception as e: lprint(str(e)) finally: pass
def main_without_kanban(opt: Options): lprint("start main_without_kanban()") # get cache kanban conn = opt.get_conn() num = opt.get_number() kanban: Kanban = conn.set_kanban(SERVICE_NAME, num) # get output data path data_path = kanban.get_data_path() # get previous service list service_list = kanban.get_services() print(service_list) ######### main function ############# audio_path = '/var/lib/aion/Data/capture-audio-from-mic_1/20200902013745000.wav' client = speechRecognize() client.speech_recognize_from_file(audio_path) lprint('status: ', client.get_result_status) lprint('transcript: ', client.get_result_text) # output after kanban conn.output_kanban( result=client.get_result_status, metadata={ "transcript": client.get_result_text, "audio_file": audio_path }, )
def main_with_kanban_itr(opt: Options): lprint("start main_with_kanban_itri()") lprint("DISPLAY: ", os.environ.get('DISPLAY')) conn = opt.get_conn() num = int(opt.get_number()) try: for kanban in conn.get_kanban_itr(SERVICE_NAME, num): metadata = kanban.get_metadata() lprint(metadata) screen_name = metadata.get('screen_name') screenshot_id = metadata.get('screenshot_id') if screen_name is None: screen_name = "NO_NAME" if screenshot_id is None: lprint("no screenshot_id") continue lprint(f"screen_name: {screen_name}") lprint(f"screenshot_id : {screenshot_id}") timestamp = datetime.now().strftime("%Y%m%d%H%M%S") screen = ScreenShotService() screen.shot_and_save(SAVE_DIRECTORY, f"{screen_name}_{timestamp}") screen.write_result(screenshot_id) except Exception as e: lprint_exception(e) finally: pass
def main(opt: Options): conn = opt.get_conn() num = opt.get_number() lprint("DISPLAY: ", os.environ.get('DISPLAY')) try: while True: kanban = conn.get_one_kanban(SERVICE_NAME, num) metadata = kanban.get_metadata() screen_name = metadata.get('screen_name') screenshot_id = metadata.get('screenshot_id') if screen_name is None: screen_name = "NO_NAME" lprint(f"screen_name: {screen_name}") lprint(f"screenshot_id : {screenshot_id}") timestamp = datetime.now().strftime("%Y%m%d%H%M%S") screen = ScreenShotService() if screen.check_new_id(screenshot_id): screen.shot_and_save(SAVE_DIRECTORY, f"{screen_name}_{timestamp}") screen.write_result(screenshot_id) else: lprint("not new id") except KanbanNotFoundError: lprint("kanban not found finish") sys.exit(0) except Exception as e: lprint_exception(e)
def main(opt: Options): conn = opt.get_conn() num = opt.get_number() try: for kanban in conn.get_kanban_itr(SERVICE_NAME, num): lprint("recieve picture_list from select-picture-by-time") picture_list = kanban.get_metadata()['picture_list'] if len(picture_list) < 1: raise "ERROR: no picture list" for image_path in picture_list: if not os.path.exists(image_path): lprint(image_path + " dosen't exist.") continue with Request() as r: matching_data_list = r.get_matching_result(picture_list) if len(matching_data_list) > 0: conn.output_kanban( result=True, metadata={"matching_data_list": matching_data_list}, process_number=num, ) lprint("success to send matching_data_list as metadata") except Exception as e: print(str(e))
def main(opt: Options): # get cache kanban conn = opt.get_conn() num = opt.get_number() kanban = conn.get_one_kanban(SERVICE_NAME, num) # get output data path data_path = kanban.get_data_path() # get metadata metadata = kanban.get_metadata() schedule_list = metadata.get('schedule_list') ######### main function ############# job_id = int(schedule_list['job_id']) new_schedules = schedule_list['schedule'] microservice_name = '' # get db conn with JobSchedulerDB() as db: microservice_name = db.get_microservice_name(job_id) if microservice_name == '': lprint(f'microservice is not set for the job: {job_id}') return cj = CronJob() cm = ConfigMap() # get old schedule from k8s-api old_schedule_list = cj.getCurrentCronjobs(microservice_name) # stop job schedule from cronjob for old_schedule_name in old_schedule_list: cronjob_name = f'{old_schedule_name}' envoy_name = f'envoy-config-{old_schedule_name}' cj.deleteCronJob(cronjob_name) cm.deleteEnvoy(envoy_name) sleep(10) # set job schedule to cronjob for row in new_schedules: schedule_id = int(row['schedule_id']) cron_date = row['cron_date'] cronjob_name = f'{microservice_name}-job{schedule_id:03}' envoy_name = f'envoy-config-{microservice_name}-job{schedule_id:03}' cm.createEnvoy(envoy_name) cj.createCronJob(job_id, cronjob_name, microservice_name, cron_date, schedule_id, envoy_name) # output after kanban conn.output_kanban( result=True, connection_key="default", output_data_path=data_path, process_number=num, )
def main(opt: Options): conn = opt.get_conn() num = opt.get_number() kanban = conn.set_kanban(SERVICE_NAME, num) loop = asyncio.get_event_loop() y = send_command.JtektPlcCommunicator(JSON_PATH, ADDRESS, PORT, loop, __file__, TRIGGER_PATH) y.start_to_send(conn)
def main_without_kanban(opt: Options): lprint("start main_without_kanban()") # get cache kanban conn = opt.get_conn() num = opt.get_number() kanban: Kanban = conn.set_kanban(SERVICE_NAME, num) copy( './json/C_MongoBackup.json', '/var/lib/aion/Data/direct-next-service_1/C_MongoBackup.json')
def main(opt: Options): Gst.init(None) # for debug # Gst.debug_set_active(True) # Gst.debug_set_default_threshold(3) conn = opt.get_conn() num = opt.get_number() data_path = "/var/lib/aion/Data/capture-movie-from-rtsp-daemon_" + str(num) p = ConvertToMovieProcesss(conn, data_path=data_path, num=num) for kanban in conn.get_kanban_itr(SERVICE_NAME, num): metadata = kanban.get_metadata() p.add_request(metadata)
def main_with_kanban_itr(opt: Options): lprint("start main_with_kanban_itr()") # get cache kanban conn = opt.get_conn() num = int(opt.get_number()) try: for kanban in conn.get_kanban_itr(SERVICE_NAME, num): metadata = kanban.get_metadata() lprint(metadata) except Exception as e: print(str(e)) finally: pass
def main(opt: Options): # get cache kanban conn = opt.get_conn() num = int(opt.get_number()) try: for kanban in conn.get_kanban_itr(SERVICE_NAME, num): lprint("received kanban") set_to_redis(kanban) except Exception as e: print(str(e)) finally: pass
def main(opt: Options): conn = opt.get_conn() num = opt.get_number() scale = os.environ.get("SCALE") scale = 2 if not isinstance(scale, int) or scale <= 0 else scale debug = os.environ.get("DEBUG") device = DeviceDataList() start_with_config = DEFAULT_START_WITH_CONFIG if os.environ.get( 'START_WITH_CONFIG') is None else os.environ.get('START_WITH_CONFIG') config_path = '/var/lib/aion/Data/stream-usb-video-by-rtsp_' + str( num) + '/config.json' device_config = DeviceConfigController(config_path) try: for kanban in conn.get_kanban_itr(): key = kanban.get_connection_key() if key in CONNECTION_KEY_LIST: device_list = kanban.get_metadata().get("device_list") if not device_list: continue metadata_list = device.start_rtsp_server( device_list, scale, opt.is_docker(), num, start_with_config, device_config) for metadata, num in metadata_list: conn.output_kanban( connection_key="camera_connected", metadata={ "type": "start", "rtsp": metadata, }, process_number=num, ) elif key == "set_focus": auto_focus = kanban.get_metadata().get("auto_focus") if auto_focus == "on": device_config.on_auto_focus() elif auto_focus == "off": device_config.fix_focus_absolute() else: lprint( "invalid metadata (connection_key: {}, auto_focus: {})" .format(key, auto_focus)) finally: device.stop_all_device()
def main(opt: Options): initialize_logger(SERVICE_NAME) logging.getLogger(KUBERNETES_PACKAGE).setLevel(logging.ERROR) conn = opt.get_conn() num = opt.get_number() try: for kanban in conn.get_kanban_itr(SERVICE_NAME, num): operator = Operator(kanban, conn) operator.start_controller() except Exception as e: lprint(e) output_kanban_with_error(conn) lprint(f"{SERVICE_NAME} exit") sleep(60)
def main_without_kanban(opt: Options): lprint("start main_with_kanban()") # get cache kanban conn = opt.get_conn() num = opt.get_number() # kanban = conn.get_one_kanban(SERVICE_NAME, num) kanban = conn.set_kanban(SERVICE_NAME, num) # main function # usb = UsbConnectionMonitor() while True: is_change = False mountpoints = usb.get_mount_points() with UpdateUsbStateToDB() as db: con_usbs = db.get_connected_usb_list() # connected usb new_mountpoints = fillter_new_mountpoint(mountpoints, con_usbs) for mount in new_mountpoints: db.update_usb_state(mount, 1) lprint(f"found usb at:{mount}") is_change = True db.commit_query() # unconnected usb for conneted in con_usbs: if conneted['mountpoint'] not in mountpoints: db.update_unmounted_usb_state(conneted['usb_id']) lprint(f"unconnected usb at: {conneted['mountpoint']}") is_change = True db.commit_query() if is_change: # output after kanban conn.output_kanban( result=True, process_number=num, metadata={ "mountpoints": mountpoints, "mode": "all", "database": DATABASE, "table": "usbs" }, ) time.sleep(EXECUTE_INTERVAL)
def main_with_kanban(opt: Options): conn = opt.get_conn() num = opt.get_number() # kanban: Kanban = conn.get_one_kanban(SERVICE_NAME, num) for kanban in conn.get_kanban_itr(SERVICE_NAME, num): metadata = kanban.get_metadata() add_list = metadata.get('add_list') remove_list = metadata.get('remove_list') # Jsonファイルをロードする with open(JSON_PATH, 'r') as f: command_list = json.load(f) new_command_list = [] for command in command_list.get('command'): new_command = command.copy() for row in remove_list if remove_list else []: if command.get('command') == row['command']: for rm in row['arrayNo']: if rm in new_command['arrayNo']: new_command['arrayNo'].remove(rm) for row in add_list if add_list else []: print(command.get('command'), row['command']) if command.get('command') == row['command']: for ad in row['arrayNo']: new_command['arrayNo'].append(ad) new_command['arrayNo'] = sorted( list(set(new_command['arrayNo']))) new_command_list.append(new_command) with open(JSON_PATH, 'w') as f: json.dump({'command': new_command_list}, f, indent=4) conn.output_kanban( result=True, process_number=num, )
def main(opt: Options): conn = opt.get_conn() num = opt.get_number() # get cache kanban kanban = conn.set_kanban(SERVICE_NAME, num) data_dir = kanban.get_data_path() os.makedirs(data_dir, exist_ok=True) lprint("watching: " + data_dir) event_handler = ChangeHandler(conn, num) observer = Observer() observer.schedule(event_handler, data_dir, recursive=True) observer.start() try: while True: sleep(EXECUTE_INTERVAL) except KeyboardInterrupt: observer.stop() observer.join() del event_handler
def main(opt: Options): conn = opt.get_conn() num = opt.get_number() vehicle_data = { "vehicle": True, "vehicle_name": "", "end": True, } if START_MODE == "by-name": conn.set_kanban(SERVICE_NAME, num) elif START_MODE == "from-kanban": kanban = conn.get_one_kanban(SERVICE_NAME, num) metadata = kanban.get_metadata() vehicle_data = metadata['args'] lprint(f"vehicle_data: {vehicle_data}") templates = [] vehicle = vehicle_data['vehicle'] vehicle_name = vehicle_data['vehicle_name'] end = vehicle_data['end'] try: ms = MySQLClient() if vehicle and not vehicle_name: templates += ms.get_all_vehicles() lprint("set_template all") elif vehicle and vehicle_name: templates += ms.get_by_vehicle_name(vehicle_name) lprint(f"set_template {vehicle_name}") if end: templates += ms.get_end() lprint("set_template end") if templates: with Request() as r: r.set_templates(templates) except Exception as e: print(str(e))
def main(opt: Options): conn = opt.get_conn() num = opt.get_number() kanban: Kanban = conn.set_kanban(SERVICE_NAME, num) # assume /var/lib/aion/Data/select-picture-by-time_1 input_file_path = kanban.get_data_path() input_file_path = input_file_path.replace(SERVICE_NAME, PREV_SERVICE_NAME) select_picture = SelectPictureByTime(input_file_path + "/output") while True: picture_list = select_picture() if picture_list: conn.output_kanban( result=True, process_number=num, metadata={"picture_list": picture_list}, ) lprint("send picture_list ", picture_list) sleep(0.5)
def main_with_kanban(opt: Options): lprint("start main_with_kanban()") # get cache kanban conn = opt.get_conn() num = opt.get_number() kanban = conn.get_one_kanban(SERVICE_NAME, num) # get output data path data_path = kanban.get_data_path() # get previous service list service_list = kanban.get_services() ######### main function ############# # output after kanban conn.output_kanban( result=True, connection_key="key", output_data_path=data_path, process_number=num, )
def main_with_kanban(opt: Options): lprint("start main_with_kanban()") # get cache kanban conn = opt.get_conn() num = opt.get_number() kanban = conn.get_one_kanban(SERVICE_NAME, num) # get output file_list metadata = kanban.get_metadata() file_name = metadata.get("file_name") data_path = kanban.get_data_path() backup_file = '/var/lib/aion/Data/restore-mongo-kanban-backup_1/' + file_name lprint(backup_file) ######### main function ############# subprocess.run([ 'mongoimport', '-h', 'mongo', '--db', 'AionCore', '--collection', 'kanban', '--file', backup_file ]) # output after kanban conn.output_kanban(result=True, connection_key="default")
def main(opt: Options): lprint("start select-picture-sequentially") conn = opt.get_conn() num = opt.get_number() kanban: Kanban = conn.set_kanban(SERVICE_NAME, num) # assume /var/lib/aion/Data/select-picture-by-random_1 input_file_path = kanban.get_data_path() select_picture = SelectPictureByTime(input_file_path) while True: picture_list = select_picture(int(SELECT_NUM)) if picture_list: conn.output_kanban( result=True, process_number=num, metadata={"picture_list": picture_list}, ) lprint("send picture_list ", picture_list) sleep(0.5)
def main(opt: Options): conn = opt.get_conn() num = opt.get_number() try: for kanban in conn.get_kanban_itr(SERVICE_NAME, num): result = False output_metadata = {} metadata = kanban.get_metadata() matching_data_list = metadata['matching_data_list'] lprint( "recieve matching_data_list from template-matching-by-opencv") if CURRENT_DEVICE_NAME in ['tartarus', 'poseidon', 'lib']: ret = vehicle.run(matching_data_list) output_metadata = ret[0] result = ret[1] elif CURRENT_DEVICE_NAME in ['deneb', 'elpis', 'neo', 'moca']: ret = trigger.run(matching_data_list) output_metadata = ret[0] result = ret[1] else: raise TemplateMatchingSummaryError("Device Name " + CURRENT_DEVICE_NAME + " is wrong.") lprint("end status: ", output_metadata['TemplateMatchingSummary']['end']['status']) conn.output_kanban( result=result, connection_key="default", metadata=output_metadata, ) except Exception: traceback.print_exc() sys.exit(1)
def main_without_kanban(opt: Options): lprint("start main_without_kanban()") # get cache kanban conn = opt.get_conn() num = opt.get_number() kanban: Kanban = conn.set_kanban(SERVICE_NAME, num) ######### main function ############# while True: # mongodbから最新の異常履歴を取得 with YaskawaMaintenaceMongodbModel() as mongodb: alarm_cur = mongodb.get_latest_alarm() latest_alarm_data = extractAlarm(alarm_cur) # 故障カテゴリ毎にまとめる latest_alarm_data = arrangeAlarmByCategory(latest_alarm_data) if not latest_alarm_data: lprint("Not found alarm data") time.sleep(INTERVAL) continue with YaskawaMaintenaceMysqlModel() as db: # mysqlから保持している最新の異常履歴を取得 for i in range(YASKAWA_ALARM_CATEGORY_SIZE): category_no = i + 1 db_alarm_data = db.getLatest_N(category_no, 10) # 新しい異常履歴を取得 new_alarms = get_new_alarm(db_alarm_data, latest_alarm_data[i], category_no) for new_alarm in new_alarms: db.append(new_alarm) if new_alarms: db.commit_query() lprint( f"new alarm history has been saved: {str(new_alarms)}") time.sleep(INTERVAL)