diff --git a/auto_import_v2_auto.py b/auto_import_v2_auto.py index af49ce5..c575d05 100644 --- a/auto_import_v2_auto.py +++ b/auto_import_v2_auto.py @@ -87,167 +87,135 @@ f.close() # Set destination and initialize variables _DEST = "/media/nas/nas-photo/imported" _DEV = "" -import_drives = [] - -drives = ["6362-3938","3239-3439", "5766-67E6","5766-67A3", "60C2-1369"] - -# Log current user and id -myCmd = "id" -status, output = subprocess.getstatusoutput(myCmd) -logging.debug(output) -myCmd = "whoami" -status, output = subprocess.getstatusoutput(myCmd) -logging.debug(output) -myCmd = "ls /dev/disk/by-uuid" -status, output = subprocess.getstatusoutput(myCmd) - files_imported = 0 -# Find drives to import -for l in output.splitlines(): - if l in drives: - logging.debug(l) - import_drives.append(l) - -# Main import loop for each drive - myCmd = "mount| grep /share/external/DEV3301_1" while True: status, output = subprocess.getstatusoutput(myCmd) print(status) time.sleep(10) - if status == 90: - for d in import_drives: - logging.debug(f"Working on {d}") - _DEV_MOUNT = f"/mnt/{d}" - _DEV_DIR = f"{_DEV_MOUNT}/DCIM" - _IMP_FILE = f"{_DEV_MOUNT}/auto_import" - _DEV_DATA = f"{_DEV_MOUNT}/MISC/version.txt" - myCmd = f"mkdir -p {_DEV_MOUNT}" - logging.debug(myCmd) - status, output = subprocess.getstatusoutput(myCmd) - myCmd = f"sudo mount --uuid {d} {_DEV_MOUNT}" - #myCmd = f"sudo mount /dev/sde1 {_DEV_MOUNT}" - logging.debug(myCmd) - status, output = subprocess.getstatusoutput(myCmd) - logging.debug(output) - if os.path.ismount(_DEV_MOUNT): - # If version.txt exists, parse device info - if os.path.exists(_DEV_DATA): - file = open(_DEV_DATA, 'r') - content = file.read() - file.close() - strToReplace = ',' - replacementStr = '' - strToReplaceReversed = strToReplace[::-1] - replacementStrReversed = replacementStr[::-1] - - strValue = content[::-1].replace(strToReplaceReversed, replacementStrReversed, 1)[::-1] - c = content.replace("\n","") - - logging.debug("--------------") - logging.debug(c[-2]) - logging.debug("--------------") - logging.debug(strValue) - _DEV_CONT = json.loads(strValue) - logging.debug(_DEV_CONT) - logging.debug(_DEV_CONT["wifi mac"]) - wifimac = _DEV_CONT["wifi mac"] - # Identify device by wifi mac - if wifimac == "2474f742c017": - _DEV = "gopro9_1" - logging.debug("gopro9_1 registered") - elif wifimac == "2474f742c017": - _DEV = "gopro9_1" - logging.debug("gopro9_1 registered") - - elif wifimac == "XXXDJIMINI4": - _DEV = "dji_mini_4" - logging.debug("dji_mini_4 registered") - - # If auto_import file exists and device is not set, read device info - if os.path.exists(_IMP_FILE) and _DEV == "": - logging.debug("path exist") - time.sleep(5) - if _DEV == "": - file = open(_IMP_FILE, 'r') - content = file.read() - _DEV = json.loads(content)["device"] - - logging.debug(f"{_DEV} registered") - file.close() - x = datetime.datetime.now() - logging.debug(x.year) - _DAT = x.strftime("%Y%m%d") - # If DCIM folder is empty, unmount and continue - if len(os.listdir(_DEV_DIR)) == 0: - logging.debug("Folder is empty!") - myCmd = f"umount {_DEV_DIR}" - status, output = subprocess.getstatusoutput(myCmd) - time.sleep(5) - continue - # myCmd = "/sbin/hal_app --se_buzzer enc_id=0,mode=101;sleep 1;/sbin/hal_app --se_buzzer enc_id=0,mode=101;sleep 1;/sbin/hal_app --se_buzzer enc_id=0,mode=101" - # status, output = subprocess.getstatusoutput(myCmd) - # If device is set and DCIM exists, move files by date - if not _DEV == "": - if os.path.exists(_DEV_DIR): - for file_or_dir in os.listdir(_DEV_DIR): - FULL_PATH = os.path.join(_DEV_DIR, file_or_dir) - logging.debug(f"Full path : {FULL_PATH}") - if os.path.isdir(FULL_PATH): - logging.debug(f"Folder: {file_or_dir}") - for filename in os.listdir(FULL_PATH): - logging.debug(filename) - c_time = os.path.getctime("{}/{}".format(FULL_PATH,filename)) - dt_c = datetime.datetime.fromtimestamp(c_time) - logging.debug(dt_c) - _CDAT = dt_c.strftime("%Y-%m-%d") - logging.debug(_CDAT) - _FDEST = "{}/{}/{}".format(_DEST,_DEV, _CDAT) - if not os.path.exists(_FDEST): - cmnd = "mkdir -p {}".format(_FDEST) - status, output = subprocess.getstatusoutput(cmnd) - logging.debug("Moving file {} into {}".format(filename, _FDEST)) - mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "started","file":filename,"device":_DEV}) - try: - shutil.move("{}/{}".format(FULL_PATH,filename),_FDEST) - files_imported += 1 - - - except: - try: - shutil.move("{}/{}".format(FULL_PATH,filename),"{}/{}".format(_FDEST,filename)) - files_imported += 1 - except: - logging.debug("failed to move file {}".format(filename)) + if status == 0: + _DEV_MOUNT = f"/share/external/DEV3301_1" + _DEV_DIR = f"{_DEV_MOUNT}/DCIM" + _IMP_FILE = f"{_DEV_MOUNT}/auto_import" + _DEV_DATA = f"{_DEV_MOUNT}/MISC/version.txt" - #myCmd = "/sbin/hal_app --se_buzzer enc_id=0,mode=100" - #subprocess.getstatusoutput(myCmd) - else: - logging.debug(f"File: {file_or_dir}") - mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "started","file":file_or_dir,"device":_DEV}) - try: - shutil.move("{}/{}".format(_DEV_DIR,file_or_dir),_FDEST) + # If version.txt exists, parse device info + if os.path.exists(_DEV_DATA): + file = open(_DEV_DATA, 'r') + content = file.read() + file.close() + strToReplace = ',' + replacementStr = '' + strToReplaceReversed = strToReplace[::-1] + replacementStrReversed = replacementStr[::-1] + + strValue = content[::-1].replace(strToReplaceReversed, replacementStrReversed, 1)[::-1] + c = content.replace("\n","") + + logging.debug("--------------") + logging.debug(c[-2]) + logging.debug("--------------") + logging.debug(strValue) + _DEV_CONT = json.loads(strValue) + logging.debug(_DEV_CONT) + logging.debug(_DEV_CONT["wifi mac"]) + wifimac = _DEV_CONT["wifi mac"] + # Identify device by wifi mac + if wifimac == "2474f742c017": + _DEV = "gopro9_1" + logging.debug("gopro9_1 registered") + elif wifimac == "2474f742c017": + _DEV = "gopro9_1" + logging.debug("gopro9_1 registered") + + elif wifimac == "XXXDJIMINI4": + _DEV = "dji_mini_4" + logging.debug("dji_mini_4 registered") + + # If auto_import file exists and device is not set, read device info + if os.path.exists(_IMP_FILE) and _DEV == "": + logging.debug("path exist") + time.sleep(5) + if _DEV == "": + file = open(_IMP_FILE, 'r') + content = file.read() + _DEV = json.loads(content)["device"] + + logging.debug(f"{_DEV} registered") + file.close() + x = datetime.datetime.now() + logging.debug(x.year) + _DAT = x.strftime("%Y%m%d") + # If DCIM folder is empty, unmount and continue + if len(os.listdir(_DEV_DIR)) == 0: + logging.debug("Folder is empty!") + myCmd = f"umount {_DEV_DIR}" + status, output = subprocess.getstatusoutput(myCmd) + time.sleep(5) + continue + # myCmd = "/sbin/hal_app --se_buzzer enc_id=0,mode=101;sleep 1;/sbin/hal_app --se_buzzer enc_id=0,mode=101;sleep 1;/sbin/hal_app --se_buzzer enc_id=0,mode=101" + # status, output = subprocess.getstatusoutput(myCmd) + # If device is set and DCIM exists, move files by date + if not _DEV == "": + if os.path.exists(_DEV_DIR): + for file_or_dir in os.listdir(_DEV_DIR): + FULL_PATH = os.path.join(_DEV_DIR, file_or_dir) + logging.debug(f"Full path : {FULL_PATH}") + if os.path.isdir(FULL_PATH): + logging.debug(f"Folder: {file_or_dir}") + for filename in os.listdir(FULL_PATH): + logging.debug(filename) + c_time = os.path.getctime("{}/{}".format(FULL_PATH,filename)) + dt_c = datetime.datetime.fromtimestamp(c_time) + logging.debug(dt_c) + _CDAT = dt_c.strftime("%Y-%m-%d") + logging.debug(_CDAT) + _FDEST = "{}/{}/{}".format(_DEST,_DEV, _CDAT) + if not os.path.exists(_FDEST): + cmnd = "mkdir -p {}".format(_FDEST) + status, output = subprocess.getstatusoutput(cmnd) + logging.debug("Moving file {} into {}".format(filename, _FDEST)) + mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "started","file":filename,"device":_DEV}) + try: + shutil.move("{}/{}".format(FULL_PATH,filename),_FDEST) + files_imported += 1 + + + except: + try: + shutil.move("{}/{}".format(FULL_PATH,filename),"{}/{}".format(_FDEST,filename)) files_imported += 1 - except: - try: - shutil.move("{}/{}".format(_DEV_DIR,file_or_dir),"{}/{}".format(_FDEST,file_or_dir)) - files_imported += 1 - except: - logging.debug("failed to move file {}".format(file_or_dir)) - logging.debug("Umounting") - myCmd = f"sudo umount {_DEV_MOUNT}" - status, output = subprocess.getstatusoutput(myCmd) - logging.debug(output) - logging.debug(status) + logging.debug("failed to move file {}".format(filename)) + + #myCmd = "/sbin/hal_app --se_buzzer enc_id=0,mode=100" + #subprocess.getstatusoutput(myCmd) else: - logging.debug("Nothing to import") - mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "finished","file":f"{files_imported}","device":_DEV}) - - else: - logging.debug("Unknown device") - + logging.debug(f"File: {file_or_dir}") + mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "started","file":file_or_dir,"device":_DEV}) + try: + shutil.move("{}/{}".format(_DEV_DIR,file_or_dir),_FDEST) + files_imported += 1 + + except: + try: + shutil.move("{}/{}".format(_DEV_DIR,file_or_dir),"{}/{}".format(_FDEST,file_or_dir)) + files_imported += 1 + except: + logging.debug("failed to move file {}".format(file_or_dir)) + logging.debug("Umounting") + myCmd = f"sudo umount {_DEV_MOUNT}" + status, output = subprocess.getstatusoutput(myCmd) + logging.debug(output) + logging.debug(status) else: - logging.debug("Usb does not connected") + logging.debug("Nothing to import") + mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "finished","file":f"{files_imported}","device":_DEV}) + + else: + logging.debug("Unknown device") + + else: + logging.debug("Usb does not connected")