This commit is contained in:
2025-07-29 10:41:58 +02:00
parent a4e8addcd5
commit f8292c4651

View File

@@ -87,167 +87,135 @@ f.close()
# Set destination and initialize variables # Set destination and initialize variables
_DEST = "/media/nas/nas-photo/imported" _DEST = "/media/nas/nas-photo/imported"
_DEV = "" _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 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" myCmd = "mount| grep /share/external/DEV3301_1"
while True: while True:
status, output = subprocess.getstatusoutput(myCmd) status, output = subprocess.getstatusoutput(myCmd)
print(status) print(status)
time.sleep(10) time.sleep(10)
if status == 90: if status == 0:
for d in import_drives: _DEV_MOUNT = f"/share/external/DEV3301_1"
logging.debug(f"Working on {d}") _DEV_DIR = f"{_DEV_MOUNT}/DCIM"
_DEV_MOUNT = f"/mnt/{d}" _IMP_FILE = f"{_DEV_MOUNT}/auto_import"
_DEV_DIR = f"{_DEV_MOUNT}/DCIM" _DEV_DATA = f"{_DEV_MOUNT}/MISC/version.txt"
_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] # If version.txt exists, parse device info
c = content.replace("\n","") if os.path.exists(_DEV_DATA):
file = open(_DEV_DATA, 'r')
content = file.read()
file.close()
strToReplace = ','
replacementStr = ''
strToReplaceReversed = strToReplace[::-1]
replacementStrReversed = replacementStr[::-1]
logging.debug("--------------") strValue = content[::-1].replace(strToReplaceReversed, replacementStrReversed, 1)[::-1]
logging.debug(c[-2]) c = content.replace("\n","")
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": logging.debug("--------------")
_DEV = "dji_mini_4" logging.debug(c[-2])
logging.debug("dji_mini_4 registered") 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")
# If auto_import file exists and device is not set, read device info elif wifimac == "XXXDJIMINI4":
if os.path.exists(_IMP_FILE) and _DEV == "": _DEV = "dji_mini_4"
logging.debug("path exist") logging.debug("dji_mini_4 registered")
time.sleep(5)
if _DEV == "":
file = open(_IMP_FILE, 'r')
content = file.read()
_DEV = json.loads(content)["device"]
logging.debug(f"{_DEV} registered") # If auto_import file exists and device is not set, read device info
file.close() if os.path.exists(_IMP_FILE) and _DEV == "":
x = datetime.datetime.now() logging.debug("path exist")
logging.debug(x.year) time.sleep(5)
_DAT = x.strftime("%Y%m%d") if _DEV == "":
# If DCIM folder is empty, unmount and continue file = open(_IMP_FILE, 'r')
if len(os.listdir(_DEV_DIR)) == 0: content = file.read()
logging.debug("Folder is empty!") _DEV = json.loads(content)["device"]
myCmd = f"umount {_DEV_DIR}"
status, output = subprocess.getstatusoutput(myCmd) logging.debug(f"{_DEV} registered")
time.sleep(5) file.close()
continue x = datetime.datetime.now()
# 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" logging.debug(x.year)
# status, output = subprocess.getstatusoutput(myCmd) _DAT = x.strftime("%Y%m%d")
# If device is set and DCIM exists, move files by date # If DCIM folder is empty, unmount and continue
if not _DEV == "": if len(os.listdir(_DEV_DIR)) == 0:
if os.path.exists(_DEV_DIR): logging.debug("Folder is empty!")
for file_or_dir in os.listdir(_DEV_DIR): myCmd = f"umount {_DEV_DIR}"
FULL_PATH = os.path.join(_DEV_DIR, file_or_dir) status, output = subprocess.getstatusoutput(myCmd)
logging.debug(f"Full path : {FULL_PATH}") time.sleep(5)
if os.path.isdir(FULL_PATH): continue
logging.debug(f"Folder: {file_or_dir}") # 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"
for filename in os.listdir(FULL_PATH): # status, output = subprocess.getstatusoutput(myCmd)
logging.debug(filename) # If device is set and DCIM exists, move files by date
c_time = os.path.getctime("{}/{}".format(FULL_PATH,filename)) if not _DEV == "":
dt_c = datetime.datetime.fromtimestamp(c_time) if os.path.exists(_DEV_DIR):
logging.debug(dt_c) for file_or_dir in os.listdir(_DEV_DIR):
_CDAT = dt_c.strftime("%Y-%m-%d") FULL_PATH = os.path.join(_DEV_DIR, file_or_dir)
logging.debug(_CDAT) logging.debug(f"Full path : {FULL_PATH}")
_FDEST = "{}/{}/{}".format(_DEST,_DEV, _CDAT) if os.path.isdir(FULL_PATH):
if not os.path.exists(_FDEST): logging.debug(f"Folder: {file_or_dir}")
cmnd = "mkdir -p {}".format(_FDEST) for filename in os.listdir(FULL_PATH):
status, output = subprocess.getstatusoutput(cmnd) logging.debug(filename)
logging.debug("Moving file {} into {}".format(filename, _FDEST)) c_time = os.path.getctime("{}/{}".format(FULL_PATH,filename))
mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "started","file":filename,"device":_DEV}) dt_c = datetime.datetime.fromtimestamp(c_time)
try: logging.debug(dt_c)
shutil.move("{}/{}".format(FULL_PATH,filename),_FDEST) _CDAT = dt_c.strftime("%Y-%m-%d")
files_imported += 1 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: except:
try:
shutil.move("{}/{}".format(FULL_PATH,filename),"{}/{}".format(_FDEST,filename))
files_imported += 1
except:
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(f"File: {file_or_dir}")
mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "started","file":file_or_dir,"device":_DEV})
try: try:
shutil.move("{}/{}".format(_DEV_DIR,file_or_dir),_FDEST) shutil.move("{}/{}".format(FULL_PATH,filename),"{}/{}".format(_FDEST,filename))
files_imported += 1 files_imported += 1
except: except:
try: logging.debug("failed to move file {}".format(filename))
shutil.move("{}/{}".format(_DEV_DIR,file_or_dir),"{}/{}".format(_FDEST,file_or_dir))
files_imported += 1 #myCmd = "/sbin/hal_app --se_buzzer enc_id=0,mode=100"
except: #subprocess.getstatusoutput(myCmd)
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: else:
logging.debug("Nothing to import") logging.debug(f"File: {file_or_dir}")
mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "finished","file":f"{files_imported}","device":_DEV}) mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "started","file":file_or_dir,"device":_DEV})
try:
else: shutil.move("{}/{}".format(_DEV_DIR,file_or_dir),_FDEST)
logging.debug("Unknown device") 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: 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")