This commit is contained in:
2025-06-29 12:33:08 +02:00
parent 3a5d91191c
commit 96704cb0de

View File

@@ -1,4 +1,5 @@
#!/usr/bin/python3
# Auto import script for media devices
import os
from posixpath import ismount
import sys
@@ -9,8 +10,44 @@ import shutil
import json
import logging
from random import randrange
from paho.mqtt import client as mqtt_client
class mqtt_sender():
def __init__(self, broker, port, name, password):
self.broker = broker
self.name = name
self.password = password
self.port = port
client_id = "dasdasdasd333"
try:
client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1, client_id)
except:
client = mqtt_client.Client()
client.username_pw_set(self.name, self.password)
def send_mqtt_message(_self, topic, msg):
try:
_self.client.connect(_self.broker,_self.port,60)
_self.client.publish(topic, json.dumps(msg))
_self.client.disconnect()
except ValueError as e:
print("Failed to send")
print(e)
mqtt_s = mqtt_sender("mqtt.home.lan", 1883, "jaydee", "l4c1j4yd33Du5lo")
mqtt_s.send_mqtt_message("sectorq/auto_import/import", {"status": "started"})
# Log file location and log level
LOG_FILE = "/tmp/auto_import.log"
_LOG_LEVEL = "DEBUG"
# Configure logging based on log level
if _LOG_LEVEL == "DEBUG":
logging.basicConfig(filename=LOG_FILE, level=logging.DEBUG, format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.debug('using debug loging')
@@ -23,10 +60,11 @@ elif _LOG_LEVEL == "SCAN":
else:
logging.basicConfig(filename=LOG_FILE, level=logging.INFO, format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.info("script started")
# Get current process id and check for duplicate running processes
pid = os.getpid()
cmnd = "ps -ef|grep test.py|grep -v grep"
status, output = subprocess.getstatusoutput(cmnd)
logging.info(output)
logging.info(pid)
@@ -38,17 +76,21 @@ if int(output) > 0:
logging.info("Running already!")
sys.exit()
# List all disk UUIDs and write output to a temp file
myCmd = "ls /dev/disk/by-uuid"
status, output = subprocess.getstatusoutput(myCmd)
f = open("/tmp/lalala", "w")
contents = f.write(output)
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"]
# Log current user and id
myCmd = "id"
status, output = subprocess.getstatusoutput(myCmd)
logging.debug(output)
@@ -58,10 +100,13 @@ logging.debug(output)
myCmd = "ls /dev/disk/by-uuid"
status, output = subprocess.getstatusoutput(myCmd)
# 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
for d in import_drives:
logging.debug(f"Working on {d}")
_DEV_MOUNT = f"/mnt/{d}"
@@ -77,6 +122,7 @@ for d in import_drives:
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()
@@ -97,6 +143,7 @@ for d in import_drives:
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")
@@ -104,6 +151,7 @@ for d in import_drives:
_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)
@@ -115,6 +163,7 @@ for d in import_drives:
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}"
@@ -123,6 +172,7 @@ for d in import_drives:
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):