This commit is contained in:
2025-07-29 10:33:56 +02:00
parent 46e831b372
commit 7196922084

View File

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