mirror of
https://gitlab.sectorq.eu/jaydee/auto_import.git
synced 2025-12-14 10:34:53 +01:00
build
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
# Auto import script for media devices
|
||||||
import os
|
import os
|
||||||
from posixpath import ismount
|
from posixpath import ismount
|
||||||
import sys
|
import sys
|
||||||
@@ -9,8 +10,44 @@ import shutil
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from random import randrange
|
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_FILE = "/tmp/auto_import.log"
|
||||||
_LOG_LEVEL = "DEBUG"
|
_LOG_LEVEL = "DEBUG"
|
||||||
|
|
||||||
|
# Configure logging based on log level
|
||||||
if _LOG_LEVEL == "DEBUG":
|
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.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')
|
logging.debug('using debug loging')
|
||||||
@@ -23,10 +60,11 @@ elif _LOG_LEVEL == "SCAN":
|
|||||||
else:
|
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.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")
|
logging.info("script started")
|
||||||
|
|
||||||
|
# Get current process id and check for duplicate running processes
|
||||||
pid = os.getpid()
|
pid = os.getpid()
|
||||||
cmnd = "ps -ef|grep test.py|grep -v grep"
|
cmnd = "ps -ef|grep test.py|grep -v grep"
|
||||||
status, output = subprocess.getstatusoutput(cmnd)
|
status, output = subprocess.getstatusoutput(cmnd)
|
||||||
|
|
||||||
logging.info(output)
|
logging.info(output)
|
||||||
logging.info(pid)
|
logging.info(pid)
|
||||||
|
|
||||||
@@ -38,17 +76,21 @@ if int(output) > 0:
|
|||||||
logging.info("Running already!")
|
logging.info("Running already!")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
# List all disk UUIDs and write output to a temp file
|
||||||
myCmd = "ls /dev/disk/by-uuid"
|
myCmd = "ls /dev/disk/by-uuid"
|
||||||
status, output = subprocess.getstatusoutput(myCmd)
|
status, output = subprocess.getstatusoutput(myCmd)
|
||||||
f = open("/tmp/lalala", "w")
|
f = open("/tmp/lalala", "w")
|
||||||
contents = f.write(output)
|
contents = f.write(output)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
# Set destination and initialize variables
|
||||||
_DEST = "/media/nas/nas-photo/imported"
|
_DEST = "/media/nas/nas-photo/imported"
|
||||||
_DEV = ""
|
_DEV = ""
|
||||||
import_drives = []
|
import_drives = []
|
||||||
|
|
||||||
drives = ["6362-3938","3239-3439", "5766-67E6","5766-67A3"]
|
drives = ["6362-3938","3239-3439", "5766-67E6","5766-67A3"]
|
||||||
|
|
||||||
|
# Log current user and id
|
||||||
myCmd = "id"
|
myCmd = "id"
|
||||||
status, output = subprocess.getstatusoutput(myCmd)
|
status, output = subprocess.getstatusoutput(myCmd)
|
||||||
logging.debug(output)
|
logging.debug(output)
|
||||||
@@ -58,10 +100,13 @@ logging.debug(output)
|
|||||||
myCmd = "ls /dev/disk/by-uuid"
|
myCmd = "ls /dev/disk/by-uuid"
|
||||||
status, output = subprocess.getstatusoutput(myCmd)
|
status, output = subprocess.getstatusoutput(myCmd)
|
||||||
|
|
||||||
|
# Find drives to import
|
||||||
for l in output.splitlines():
|
for l in output.splitlines():
|
||||||
if l in drives:
|
if l in drives:
|
||||||
logging.debug(l)
|
logging.debug(l)
|
||||||
import_drives.append(l)
|
import_drives.append(l)
|
||||||
|
|
||||||
|
# Main import loop for each drive
|
||||||
for d in import_drives:
|
for d in import_drives:
|
||||||
logging.debug(f"Working on {d}")
|
logging.debug(f"Working on {d}")
|
||||||
_DEV_MOUNT = f"/mnt/{d}"
|
_DEV_MOUNT = f"/mnt/{d}"
|
||||||
@@ -77,6 +122,7 @@ for d in import_drives:
|
|||||||
status, output = subprocess.getstatusoutput(myCmd)
|
status, output = subprocess.getstatusoutput(myCmd)
|
||||||
logging.debug(output)
|
logging.debug(output)
|
||||||
if os.path.ismount(_DEV_MOUNT):
|
if os.path.ismount(_DEV_MOUNT):
|
||||||
|
# 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()
|
content = file.read()
|
||||||
@@ -97,6 +143,7 @@ for d in import_drives:
|
|||||||
logging.debug(_DEV_CONT)
|
logging.debug(_DEV_CONT)
|
||||||
logging.debug(_DEV_CONT["wifi mac"])
|
logging.debug(_DEV_CONT["wifi mac"])
|
||||||
wifimac = _DEV_CONT["wifi mac"]
|
wifimac = _DEV_CONT["wifi mac"]
|
||||||
|
# Identify device by wifi mac
|
||||||
if wifimac == "2474f742c017":
|
if wifimac == "2474f742c017":
|
||||||
_DEV = "gopro9_1"
|
_DEV = "gopro9_1"
|
||||||
logging.debug("gopro9_1 registered")
|
logging.debug("gopro9_1 registered")
|
||||||
@@ -104,6 +151,7 @@ for d in import_drives:
|
|||||||
_DEV = "dji_mini_4"
|
_DEV = "dji_mini_4"
|
||||||
logging.debug("dji_mini_4 registered")
|
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 == "":
|
if os.path.exists(_IMP_FILE) and _DEV == "":
|
||||||
logging.debug("path exist")
|
logging.debug("path exist")
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
@@ -115,6 +163,7 @@ for d in import_drives:
|
|||||||
x = datetime.datetime.now()
|
x = datetime.datetime.now()
|
||||||
logging.debug(x.year)
|
logging.debug(x.year)
|
||||||
_DAT = x.strftime("%Y%m%d")
|
_DAT = x.strftime("%Y%m%d")
|
||||||
|
# If DCIM folder is empty, unmount and continue
|
||||||
if len(os.listdir(_DEV_DIR)) == 0:
|
if len(os.listdir(_DEV_DIR)) == 0:
|
||||||
logging.debug("Folder is empty!")
|
logging.debug("Folder is empty!")
|
||||||
myCmd = f"umount {_DEV_DIR}"
|
myCmd = f"umount {_DEV_DIR}"
|
||||||
@@ -123,6 +172,7 @@ for d in import_drives:
|
|||||||
continue
|
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"
|
# 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)
|
# status, output = subprocess.getstatusoutput(myCmd)
|
||||||
|
# If device is set and DCIM exists, move files by date
|
||||||
if not _DEV == "":
|
if not _DEV == "":
|
||||||
if os.path.exists(_DEV_DIR):
|
if os.path.exists(_DEV_DIR):
|
||||||
for file_or_dir in os.listdir(_DEV_DIR):
|
for file_or_dir in os.listdir(_DEV_DIR):
|
||||||
|
|||||||
Reference in New Issue
Block a user