#!/usr/bin/python3.5
# Copyright 2017 Digital
#
# This file is part of DigiLib.
#
# DigiLib is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DigiLib is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DigiLib.  If not, see <http://www.gnu.org/licenses/>.

import logging
import digilib.network

log = logging.getLogger(__name__+"")
lpin = logging.getLogger(__name__+".pin")

client = digilib.network.Client("perry",5069,af_family="AF_INET")
client.daemon = True
client.start()
client.connect()

#
# class PinBase(object):
#     """docstring for PinBase."""
#     # lockdir = "/var/lock/pins/"
#     lockdir = "lock/pins/"
#     pin_number = None
#     lockfile = lockdir + str(pin_number)
#     value_high = True
#     value_low = False
#     value = value_low
#     def __init__(self,pin_number):
#         super(PinBase,self).__init__()
#         self.pin_number = pin_number
#         self.lockfile = self.lockdir + str(self.pin_number)

class PinBase(object):
    """PinBase is the base class for all classes representing a gpio pin"""
    pin_number = None
    value = None
    def __init__(self,pin_number):
        super(PinBase,self).__init__()
        self.pin_number = pin_number
        self.value = self.value_low
    def output(self,value):
        lpin.info(
            "pin {} set to {}".format(
                str(self.pin_number),
                str(value)
            )
        )
        self.value = value
    def read(self):
        lpin.debug(
            "pin {} has value {}".format(
                str(self.pin_number),
                str(self.value)
            )
        )
        return self.value

class RemotePin(PinBase):
    """RemotePin represents a remote pin"""
    def output(self,value):
        lpin.info(
            "pin {} set to {}".format(
                str(self.pin_number),
                str(value)
            )
        )
        self.value = value
        client.send("set {} {}".format(
                str(self.pin_number),
                str(value)
            )
        )
    def read(self):
        lpin.debug(
            "pin {} has value {}".format(
                str(self.pin_number),
                str(self.value)
            )
        )
        return self.value

class DigitalPin(RemotePin):
    value_high = True
    value_low = False
    def __init__(self,pin_number):
        super(DigitalPin,self).__init__(pin_number)

class AnalogPin(PinBase):
    value_high = 1
    value_low = 0
    def __init__(self,pin_number):
        super(AnalogPin,self).__init__()

class PinControllerBase(object):
    """docstring for PinControllerBase.
    PinControllerBase is the base class for all classes controlling a physical
    device connected to multiple pins
    """
    pins = []
    def __init__(self):
        super(PinControllerBase, self).__init__()

class PCEngine(PinControllerBase):
    """Test Class"""
    max_speed=1
    speed = 0
    turn_on_speed = 1
    is_on = False
    def __init__(self,pin_on_off,pin_analog):
        super(PCEngine, self).__init__()
        self.pin_on_off = self.make_pin(pin_on_off)
        self.pin_analog = self.make_pin(pin_analog)
        self.pins.append(self.pin_on_off)
        self.pins.append(self.pin_analog)
    def make_pin(self,*args):
        return DigitalPin(*args)
    def set_speed(self,speed):
        self.pin_analog.output(speed)
        self.speed = speed
    def turn_on(self,speed=turn_on_speed):
        if self.is_on:
            return
        self.set_speed(speed)
        self.pin_on_off.output(1)
        self.is_on = True
    def turn_off(self,speed=turn_on_speed):
        if not self.is_on:
            return
        self.set_speed(speed)
        self.pin_on_off.output(1)
        self.is_on = True


class PinAPIBase(object):
    """docstring for PinAPI.
    PinAPIBase is the base class for all classes providing an api to multiple
    PinController.
    """
    def __init__(self):
        super(PinAPIBase, self).__init__()




if __name__ == "__main__":
    pass













#