'''
Created on 19 janv. 2026

@author: denis
'''
import os
os.environ['BOARD_DEVICE']="RPI"

from django.core.management.base import BaseCommand
from django.conf import settings
from modules import gpiolib as gpio, devices as dev

RELAY_KEY = 'Relay_'
RELAY_MSG = 'Vanne'
# devices
DEVICES = []
for i in range(settings.RELAY_NUMBER):
    active = 1
    DEVICES.append( { 'type': 'RELAY_SHIFTOUT', 'index': i, 'key': f'{RELAY_KEY}{i}', 'label': f'{RELAY_MSG} {i}', 'status': 0, 'active': active, } )

DEVICES_REGISTERED = DEVICES


class Command(BaseCommand):

    def add_arguments(self, parser):
        super(Command, self).add_arguments(parser)
        #parser.add_argument("--task", action="store", dest="task", default='start_all')

    def handle(self, *args, **options):
        try:
            shift_out_register = gpio.ShiftOutRegister74HC595(
                latch_pin=settings.CTL_LATCH_PIN,
                clock_pin=settings.CTL_CLOCK_PIN,
                data_pin=settings.CTL_DATA_PIN,
                number=settings.CTL_NUMBER,
            )

            for device in settings.DEVICES_REGISTERED:
                relay = dev.Relay(shift_out_register, device)
                relay.relay_start(duration=5)

        except Exception as e:
            print("Command", e)

