# Path: lib/gpio/servo.py
# class Servo(AbstractClass):
# 
#     @classmethod
#     def is_available(cls):
#         return (super(cls, cls).is_available() and
#                 lib.diagnose.check_python_import('RPi.GPIO'))
# 
#     @classmethod
#     def get_config(cls):
#         config = {}
#         config_path = os.path.join(lib.appPath.CONFIG_PATH, 'gpio.yml');
#         if os.path.exists(config_path):
#             with open(config_path, 'r') as f:
#                 profile = yaml.safe_load(f)
#                 if 'servo' in profile:
#                     profile = profile['servo']
#                     if 'bcm' in profile:
#                         config['bcm'] = profile['bcm']
#         return config
#     
#     def __init__(self,bcm):
#         GPIO.setmode(GPIO.BCM)
#         GPIO.setup(bcm, GPIO.OUT)
#         self.bcm = bcm
#         self._pwm = GPIO.PWM(bcm, 50) 
#         self._pwm.start(7)
# 
#     def get_bcm_port(self):
#         return str(self.bcm)
# 
#     def rotate(self,number=1):
#         for n in range(0,number):
#             for dc in range(0, 180, 1):
#                 dc = 1./18.*dc + 2
#                 self._pwm.ChangeDutyCycle(dc)
#                 time.sleep(0.01)
#             for dc in range(180, 0, -1):
#                 dc = 1./18.*dc + 2
#                 self._pwm.ChangeDutyCycle(dc)
#                 time.sleep(0.01)
# 
#         self.clear()
# 
#     def clear(self):
#         self._pwm.stop()
#         GPIO.cleanup()
# 
# Path: lib/gpio/led.py
# class Led(AbstractClass):
#     @classmethod
#     def is_available(cls):
#         return (super(cls, cls).is_available() and
#                 lib.diagnose.check_python_import('RPi.GPIO'))
# 
#     @classmethod
#     def get_config(cls):
#         config = {}
#         config_path = os.path.join(lib.appPath.CONFIG_PATH, 'gpio.yml');
#         if os.path.exists(config_path):
#             with open(config_path, 'r') as f:
#                 profile = yaml.safe_load(f)
#                 if 'led' in profile:
#                     profile = profile['led']
#                     if 'green_bcm' in profile:
#                         config['green_bcm'] = profile['green_bcm']
#         return config
# 
#     def __init__(self,green_bcm):
#         GPIO.setmode(GPIO.BCM)
#         GPIO.setup(green_bcm, GPIO.OUT)
#         self.green_bcm = green_bcm
#         self.on_state = GPIO.HIGH
#         self.off_state = not self.on_state
# 
#     def get_bcm_port(self):
#         return str(self.green_bcm)
#     def set_on(self):
#         GPIO.output(self.green_bcm, self.on_state)
#     def set_off(self):
#         GPIO.output(self.green_bcm, self.off_state)
#     def is_on(self):
#         return GPIO.input(self.green_bcm) == self.on_state
#     def is_off(self):
#         return GPIO.input(self.green_bcm) == self.off_state
#     def toggle(self):
#         if self.is_on():
#             self.set_off()
#         else:
#             self.set_on()
# 
#     def blink(self, t=0.3):
#         self.set_off()
#         self.set_on()
#         time.sleep(t)
#         self.set_off()
# 
#     def bling(self,number=None):
#         n = 0
#         while  True:
#             if(number is not None and n<number):
#                 n = n+1
#             if(number is not None and n==number):
#                 break
#             self.blink()
#             time.sleep(0.7)
# 
#         GPIO.cleanup()
# 
#     def breath(self,number=None):
#         pwm = GPIO.PWM(self.green_bcm, 70) 
#         pwm.start(0)
#         n=0
#         while True:
#             if(number is not None and n<number):
#                 n = n+1
#             if(number is not None and n==number):
#                 break
#             for dc in range(0, 101, 1):
#                 pwm.ChangeDutyCycle(dc)
#                 time.sleep(0.03)
#             for dc in range(100, -1, 1):
#                 pwm.ChangeDutyCycle(dc)
#                 time.sleep(0.03)
#         pwm.stop()
#         GPIO.cleanup()


import sys,os,time
import psutil
import signal
import lib.appPath
from multiprocessing import Process, Queue, Pipe
from lib.gpio.servo import Servo
from lib.gpio.led import Led
                    f.close()
                    if pid: 
                        print("kill pid: %d"%pid)
                        os.kill(pid,signal.SIGKILL)
        led = Led.get_instance()
        servo = Servo.get_instance()
        led_pid_file = os.path.join(lib.appPath.DATA_PATH,tag+"_"+led.get_bcm_port()+"_led.pid")
        kill_pid(led_pid_file)
        servo_pid_file = os.path.join(lib.appPath.DATA_PATH,tag+"_"+servo.get_bcm_port()+"_servo.pid")
        kill_pid(servo_pid_file)
    
    @classmethod
    def suspend_process(cls,tag):
        '''
        挂起当前gpio进程 （进程id从文件中获取）
        '''
        def suspend(pid_file):
            res = None
            if os.path.exists(pid_file):
                with open(pid_file, 'r') as f:
                    pid = int(f.read())
                    print("---suspend--")
                    print(pid_file)
                    print("-----")
                    f.close()
                    if pid: 
                        print("suspend  pid: %d"%pid)
                        res = psutil.Process(pid).suspend()
            return res


# Path: lib/gpio/servo.py
# class Servo(AbstractClass):
# 
#     @classmethod
#     def is_available(cls):
#         return (super(cls, cls).is_available() and
#                 lib.diagnose.check_python_import('RPi.GPIO'))
# 
#     @classmethod
#     def get_config(cls):
#         config = {}
#         config_path = os.path.join(lib.appPath.CONFIG_PATH, 'gpio.yml');
#         if os.path.exists(config_path):
#             with open(config_path, 'r') as f:
#                 profile = yaml.safe_load(f)
#                 if 'servo' in profile:
#                     profile = profile['servo']
#                     if 'bcm' in profile:
#                         config['bcm'] = profile['bcm']
#         return config
#     
#     def __init__(self,bcm):
#         GPIO.setmode(GPIO.BCM)
#         GPIO.setup(bcm, GPIO.OUT)
#         self.bcm = bcm
#         self._pwm = GPIO.PWM(bcm, 50) 
#         self._pwm.start(7)
# 
#     def get_bcm_port(self):
#         return str(self.bcm)
# 
#     def rotate(self,number=1):
#         for n in range(0,number):
#             for dc in range(0, 180, 1):
#                 dc = 1./18.*dc + 2
#                 self._pwm.ChangeDutyCycle(dc)
#                 time.sleep(0.01)
#             for dc in range(180, 0, -1):
#                 dc = 1./18.*dc + 2
#                 self._pwm.ChangeDutyCycle(dc)
#                 time.sleep(0.01)
# 
#         self.clear()
# 
#     def clear(self):
#         self._pwm.stop()
#         GPIO.cleanup()
# 
# Path: lib/gpio/led.py
# class Led(AbstractClass):
#     @classmethod
#     def is_available(cls):
#         return (super(cls, cls).is_available() and
#                 lib.diagnose.check_python_import('RPi.GPIO'))
# 
#     @classmethod
#     def get_config(cls):
#         config = {}
#         config_path = os.path.join(lib.appPath.CONFIG_PATH, 'gpio.yml');
#         if os.path.exists(config_path):
#             with open(config_path, 'r') as f:
#                 profile = yaml.safe_load(f)
#                 if 'led' in profile:
#                     profile = profile['led']
#                     if 'green_bcm' in profile:
#                         config['green_bcm'] = profile['green_bcm']
#         return config
# 
#     def __init__(self,green_bcm):
#         GPIO.setmode(GPIO.BCM)
#         GPIO.setup(green_bcm, GPIO.OUT)
#         self.green_bcm = green_bcm
#         self.on_state = GPIO.HIGH
#         self.off_state = not self.on_state
# 
#     def get_bcm_port(self):
#         return str(self.green_bcm)
#     def set_on(self):
#         GPIO.output(self.green_bcm, self.on_state)
#     def set_off(self):
#         GPIO.output(self.green_bcm, self.off_state)
#     def is_on(self):
#         return GPIO.input(self.green_bcm) == self.on_state
#     def is_off(self):
#         return GPIO.input(self.green_bcm) == self.off_state
#     def toggle(self):
#         if self.is_on():
#             self.set_off()
#         else:
#             self.set_on()
# 
#     def blink(self, t=0.3):
#         self.set_off()
#         self.set_on()
#         time.sleep(t)
#         self.set_off()
# 
#     def bling(self,number=None):
#         n = 0
#         while  True:
#             if(number is not None and n<number):
#                 n = n+1
#             if(number is not None and n==number):
#                 break
#             self.blink()
#             time.sleep(0.7)
# 
#         GPIO.cleanup()
# 
#     def breath(self,number=None):
#         pwm = GPIO.PWM(self.green_bcm, 70) 
#         pwm.start(0)
#         n=0
#         while True:
#             if(number is not None and n<number):
#                 n = n+1
#             if(number is not None and n==number):
#                 break
#             for dc in range(0, 101, 1):
#                 pwm.ChangeDutyCycle(dc)
#                 time.sleep(0.03)
#             for dc in range(100, -1, 1):
#                 pwm.ChangeDutyCycle(dc)
#                 time.sleep(0.03)
#         pwm.stop()
#         GPIO.cleanup()


# Path: lib/gpio/manager.py
import sys,os,time
import psutil
import signal
import lib.appPath
from multiprocessing import Process, Queue, Pipe
from lib.gpio.servo import Servo
from lib.gpio.led import Led

                    f.close()
                    if pid: 
                        print("kill pid: %d"%pid)
                        os.kill(pid,signal.SIGKILL)
        led = Led.get_instance()
        servo = Servo.get_instance()
        led_pid_file = os.path.join(lib.appPath.DATA_PATH,tag+"_"+led.get_bcm_port()+"_led.pid")
        kill_pid(led_pid_file)
        servo_pid_file = os.path.join(lib.appPath.DATA_PATH,tag+"_"+servo.get_bcm_port()+"_servo.pid")
        kill_pid(servo_pid_file)
    
    @classmethod
    def suspend_process(cls,tag):
        '''
        挂起当前gpio进程 （进程id从文件中获取）
        '''
        def suspend(pid_file):
            res = None
            if os.path.exists(pid_file):
                with open(pid_file, 'r') as f:
                    pid = int(f.read())
                    print("---suspend--")
                    print(pid_file)
                    print("-----")
                    f.close()
                    if pid: 
                        print("suspend  pid: %d"%pid)
                        res = psutil.Process(pid).suspend()
            return res


        led = Led.get_instance()