This commit is contained in:
2025-07-29 15:26:30 +02:00
parent d45dc100d9
commit f86221a757

View File

@@ -85,166 +85,169 @@ files_imported = 0
while True: while True:
myCmd = "mount| grep /share/external/DEV3301_1" myCmd = "mount| grep /share/external/DEV3301| grep -v grep | awk '{ print $3 }'"
status, output = subprocess.getstatusoutput(myCmd) status, output = subprocess.getstatusoutput(myCmd)
print(output) print(output)
print(status) print(status)
if status != 0: devs = output.splitlines()
if len(devs) == 0:
time.sleep(10) time.sleep(10)
else: else:
print("Device mounted") for _DEV_MOUNT in devs:
myCmd = "/sbin/hal_app --se_buzzer enc_id=0,mode=101" print("Device mounted")
status, output = subprocess.getstatusoutput(myCmd) myCmd = "/sbin/hal_app --se_buzzer enc_id=0,mode=101"
status, output = subprocess.getstatusoutput(myCmd)
_DEV_MOUNT = f"/share/external/DEV3301_1"
_DEV_DIR = f"{_DEV_MOUNT}/DCIM" # _DEV_MOUNT = f"/share/external/DEV3301_1"
_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"
# If version.txt exists, parse device info # If version.txt exists, parse device info
if os.path.exists(_DEV_DATA): if os.path.exists(_DEV_DATA):
file = open(_DEV_DATA, 'r') 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() content = file.read()
_DEV = json.loads(content)["device"] file.close()
strToReplace = ','
replacementStr = ''
strToReplaceReversed = strToReplace[::-1]
replacementStrReversed = replacementStr[::-1]
logging.debug(f"{_DEV} registered") strValue = content[::-1].replace(strToReplaceReversed, replacementStrReversed, 1)[::-1]
file.close() c = content.replace("\n","")
x = datetime.datetime.now()
logging.debug(x.year) logging.debug("--------------")
_DAT = x.strftime("%Y%m%d") logging.debug(c[-2])
# If DCIM folder is empty, unmount and continue logging.debug("--------------")
if len(os.listdir(_DEV_DIR)) == 0: logging.debug(strValue)
logging.debug("Folder is empty!") _DEV_CONT = json.loads(strValue)
myCmd = f"umount {_DEV_DIR}" logging.debug(_DEV_CONT)
status, output = subprocess.getstatusoutput(myCmd) 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) time.sleep(5)
continue if _DEV == "":
# 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" file = open(_IMP_FILE, 'r')
# status, output = subprocess.getstatusoutput(myCmd) content = file.read()
# If device is set and DCIM exists, move files by date _DEV = json.loads(content)["device"]
moved_fine = True
if not _DEV == "": logging.debug(f"{_DEV} registered")
if os.path.exists(_DEV_DIR): file.close()
for file_or_dir in os.listdir(_DEV_DIR): x = datetime.datetime.now()
FULL_PATH = os.path.join(_DEV_DIR, file_or_dir) logging.debug(x.year)
logging.debug(f"Full path : {FULL_PATH}") _DAT = x.strftime("%Y%m%d")
if os.path.isdir(FULL_PATH): # If DCIM folder is empty, unmount and continue
logging.debug(f"Folder: {file_or_dir}") if len(os.listdir(_DEV_DIR)) == 0:
for filename in os.listdir(FULL_PATH): logging.debug("Folder is empty!")
logging.debug(filename) myCmd = f"umount {_DEV_DIR}"
c_time = os.path.getctime("{}/{}".format(FULL_PATH,filename)) status, output = subprocess.getstatusoutput(myCmd)
dt_c = datetime.datetime.fromtimestamp(c_time) time.sleep(5)
logging.debug(dt_c) continue
_CDAT = dt_c.strftime("%Y-%m-%d") # 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(_CDAT) # status, output = subprocess.getstatusoutput(myCmd)
_FDEST = "{}/{}/{}".format(_DEST,_DEV, _CDAT) # If device is set and DCIM exists, move files by date
if not os.path.exists(_FDEST): moved_fine = True
cmnd = "mkdir -p {}".format(_FDEST) if not _DEV == "":
status, output = subprocess.getstatusoutput(cmnd) if os.path.exists(_DEV_DIR):
logging.debug("Moving file {} into {}".format(filename, _FDEST)) for file_or_dir in os.listdir(_DEV_DIR):
mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "started","file":filename,"device":_DEV}) 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.copy("{}/{}".format(FULL_PATH,filename),_FDEST)
myCmd = f"rsync -avz {FULL_PATH}/{filename} {_FDEST}"
status, output = subprocess.getstatusoutput(myCmd)
files_imported += 1
except:
try:
#shutil.copy("{}/{}".format(FULL_PATH,filename),"{}/{}".format(_FDEST,filename))
myCmd = f"rsync -avz {FULL_PATH}/{filename} {_FDEST}"
logging.debug(myCmd)
status, output = subprocess.getstatusoutput(myCmd)
logging.debug(output)
logging.debug(status)
files_imported += 1
except:
logging.debug("failed to move file {}".format(filename))
moved_fine = False
#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.copy("{}/{}".format(FULL_PATH,filename),_FDEST) #shutil.copy("{}/{}".format(_DEV_DIR,file_or_dir),_FDEST)
myCmd = f"rsync -avz {FULL_PATH}/{filename} {_FDEST}" myCmd = f"rsync -avz {_DEV_DIR}/{file_or_dir} {_FDEST}"
logging.debug(myCmd)
status, output = subprocess.getstatusoutput(myCmd) status, output = subprocess.getstatusoutput(myCmd)
files_imported += 1 files_imported += 1
except: except:
try: try:
#shutil.copy("{}/{}".format(FULL_PATH,filename),"{}/{}".format(_FDEST,filename)) #shutil.copy("{}/{}".format(_DEV_DIR,file_or_dir),"{}/{}".format(_FDEST,file_or_dir))
myCmd = f"rsync -avz {FULL_PATH}/{filename} {_FDEST}" myCmd = f"rsync -avz {_DEV_DIR}/{file_or_dir} {_FDEST}"
logging.debug(myCmd) logging.debug(myCmd)
status, output = subprocess.getstatusoutput(myCmd) status, output = subprocess.getstatusoutput(myCmd)
logging.debug(output) logging.debug(output)
logging.debug(status) logging.debug(status)
files_imported += 1 files_imported += 1
except: except:
logging.debug("failed to move file {}".format(filename)) logging.debug("failed to move file {}".format(file_or_dir))
moved_fine = False moved_fine = False
#myCmd = "/sbin/hal_app --se_buzzer enc_id=0,mode=100"
#subprocess.getstatusoutput(myCmd)
else: if moved_fine:
logging.debug(f"File: {file_or_dir}") print("All files moved successfully")
mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "started","file":file_or_dir,"device":_DEV}) print(f"Removing {_DEV_DIR}*")
try: myCmd = f"rm -rf {_DEV_DIR}/*"
#shutil.copy("{}/{}".format(_DEV_DIR,file_or_dir),_FDEST) status, output = subprocess.getstatusoutput(myCmd)
myCmd = f"rsync -avz {_DEV_DIR}/{file_or_dir} {_FDEST}" logging.debug("Umounting")
logging.debug(myCmd)
status, output = subprocess.getstatusoutput(myCmd)
files_imported += 1
myCmd = f"sudo umount {_DEV_MOUNT}"
except:
try:
#shutil.copy("{}/{}".format(_DEV_DIR,file_or_dir),"{}/{}".format(_FDEST,file_or_dir))
myCmd = f"rsync -avz {_DEV_DIR}/{file_or_dir} {_FDEST}"
logging.debug(myCmd)
status, output = subprocess.getstatusoutput(myCmd)
logging.debug(output)
logging.debug(status)
files_imported += 1
except:
logging.debug("failed to move file {}".format(file_or_dir))
moved_fine = False
if moved_fine:
print("All files moved successfully")
print(f"Removing {_DEV_DIR}*")
myCmd = f"rm -rf {_DEV_DIR}/*"
status, output = subprocess.getstatusoutput(myCmd) status, output = subprocess.getstatusoutput(myCmd)
logging.debug("Umounting") logging.debug(output)
logging.debug(status)
myCmd = "/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)
myCmd = f"sudo umount {_DEV_MOUNT}" else:
status, output = subprocess.getstatusoutput(myCmd) logging.debug("Nothing to import")
logging.debug(output) mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "finished","file":f"{files_imported}","device":_DEV})
logging.debug(status)
myCmd = "/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)
else:
logging.debug("Nothing to import")
mqtt_s.send_mqtt_message("sectorq/auto_import/job", {"status": "finished","file":f"{files_imported}","device":_DEV})
else: else:
logging.debug("Unknown device") logging.debug("Unknown device")