#! /usr/bin/env python3
#-*- coding: utf-8 -*-
#----------------------------------------------------------------------
# clear && python3 /home/scripts_python/powerdown_soft_shutdown.py
# /home/scripts_shell/powerdown_soft_shutdown.sh
#----------------------------------------------------------------------
#sudo systemctl start   powerdown_soft_shutdown.service
#sudo systemctl stop    powerdown_soft_shutdown.service
#sudo systemctl restart powerdown_soft_shutdown.service
#sudo systemctl status  powerdown_soft_shutdown.service
#----------------------------------------------------------------------

#----------------------------------------------------------------------
# bibliothèques "système"
#----------------------------------------------------------------------
import os
import glob
from   subprocess import *
import time

import RPi.GPIO as GPIO

#----------------------------------------------------------------------
# constantes & variables
#----------------------------------------------------------------------
BIPS_ON = False
BIPS_ON = True

POWER_DOWN    = 26
BUZZER        = 5
TFT_BACKLIGHT = 18

BIP_ON            = 0.025
BIP_OFF           = 0.045
BIP_ON_POWEROFF   = 0.070
BIP_OFF_POWEROFF  = 0.070

serial_interface = "/dev/ttyAMA0"

#----------------------------------------------------------------------
DEBUG = True
DEBUG = False

#----------------------------------------------------------------------
# 
#----------------------------------------------------------------------
def run_cmd(cmd):
	try :
		p = Popen(cmd, shell = True, stdout = PIPE, stderr = PIPE)
		
		cmd_retour    = p.communicate()[0]
		chaine_retour = cmd_retour.decode()
		
		p.wait()
		
		run_cmd_return_code = p.returncode
		if ( run_cmd_return_code != 0 ) :
			chaine_retour = "NOK!!!"
			
	except :
		chaine_retour = "NOK!!!"
	
	finally :	
		return chaine_retour
						
#----------------------------------------------------------------------
def AQCM_shutdown():
	try:
		
		cmd1 = "sudo poweroff"

		if ( run_cmd(cmd1) != "NOK!!!" ) :
			time.sleep(0.5)
			return True
		else :
			return False

	except:
		return False
		
#----------------------------------------------------------------------



#----------------------------------------------------------------------
# début du code exécutable
#----------------------------------------------------------------------
try:
	print( "powerdown_soft_shutdown : démarrage..." )
	
	GPIO.setmode(GPIO.BCM)
	GPIO.setwarnings(False)
	GPIO.setup(POWER_DOWN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
	GPIO.setup(BUZZER, GPIO.OUT, initial=GPIO.LOW)
	
	compteur_powerdown = 0
	VECTEUR_BP_PBC     = [1,1,1,1,1,1,1,1]	

	while True :
	
		for i in range (0,7):
			VECTEUR_BP_PBC[i] = VECTEUR_BP_PBC[i+1]
		VECTEUR_BP_PBC[7] = GPIO.input(POWER_DOWN)
		
		#print( VECTEUR_BP_PBC )
		#print( compteur_powerdown )
		time.sleep(0.125)

		if ( VECTEUR_BP_PBC == [1,1,1,1,1,1,1,1] ) :

			#----------------------------------------------------------------------

			if ( compteur_powerdown > 0 ) :

				print( "  powerdown_soft_shutdown : avertissement : power is UP !!!" )
				print( " " )

				if BIPS_ON :
					for j in range (0,2):
						GPIO.output(BUZZER, GPIO.HIGH)
						time.sleep(BIP_ON)
						GPIO.output(BUZZER, GPIO.LOW)
						time.sleep(BIP_OFF)

			compteur_powerdown = 0
	
		if ( VECTEUR_BP_PBC == [0,0,0,0,0,0,0,0] ) :
		
			print( "  powerdown_soft_shutdown : avertissement : power is DOWN !!!" )
			print( " " )

			if ( compteur_powerdown == 0 ) :
				if BIPS_ON :
					for j in range (0,3):
						GPIO.output(BUZZER, GPIO.HIGH)
						time.sleep(BIP_ON)
						GPIO.output(BUZZER, GPIO.LOW)
						time.sleep(BIP_OFF)

			VECTEUR_BP_PBC  = [1,1,1,1,0,0,0,0]
			VECTEUR_BP_PBC  = [1,1,1,1,1,1,1,0]
			compteur_powerdown += 1
			
			#----------------------------------------------------------------------
			if ( compteur_powerdown > 1 ) :
		
				if True :
				#if AQCM_shutdown() :
					GPIO.setup(TFT_BACKLIGHT, GPIO.OUT, initial=GPIO.LOW)
					GPIO.output(TFT_BACKLIGHT, GPIO.LOW)
					print( "  powerdown_soft_shutdown : le raspberry pi s'arrête..." )

					if BIPS_ON :
						for j in range (0,3):
							GPIO.output(BUZZER, GPIO.HIGH)
							time.sleep(BIP_ON_POWEROFF)
							GPIO.output(BUZZER, GPIO.LOW)
							time.sleep(BIP_OFF_POWEROFF)
							
					time.sleep(5.000)

				else :
					print( "  powerdown_soft_shutdown : le raspberry pi ne veut pas s'arrêter !!!" )

					if BIPS_ON :
						for j in range (0,10):
							GPIO.output(BUZZER, GPIO.HIGH)
							time.sleep(BIP_ON)
							GPIO.output(BUZZER, GPIO.LOW)
							time.sleep(BIP_OFF)

			#----------------------------------------------------------------------

except Exception as e0: 
	print("powerdown_soft_shutdown : exception -> " + str(e0) )
	print(" ")
		
finally:
	print("powerdown_soft_shutdown : sortie." )
	print(" ")

#----------------------------------------------------------------------
