# Path: py_arduino_web/storage.py
# class Storage(BaseStorage):
# 
#     def get_pin(self, pin, is_digital):
#         """
#         Returns a Pin instance.
#         """
#         return Pin(pin, is_digital,)
# 
#     def get_pin_by_id(self, pin_id):
#         """
#         Returns the Pin instance identified by 'pin_id',
#         or None if no Pin exists with that identifier.
#         """
#         return None
# 
# def default_label(pin, is_digital):
#     """Returns a default label for a pin"""
#     if is_digital:
#         return 'Digital pin #{0}'.format(pin)
#     else:
#         return 'Analog pin #{0}'.format(pin)
# 
# class BaseStorage():
# 
#     def enhanceArduinoTypeStruct(self, arduino_type_struct):
#         """
#         Returns an enhanced copy of `arduino_type_struct` with data from storage.
# 
#         The `arduino_type_struct` must be previously
#         enhanced by `PyArduino.enhanceArduinoTypeStruct()`.
# 
#         To each item of `digital_pins_struct`, adds:
#             + pk (int)
#             + label (str)
#             + pin_id (str)
#             + enabled_in_web (bool)
#         To each item of `analog_pins_struct`, adds:
#             + pk (int)
#             + label (str)
#             + pin_id (str)
#             + enabled_in_web (bool)
#         """
#         # arduino_type_struct['digital_pins_items']
#         # arduino_type_struct['analog_pins_items']
#         assert 'digital_pins_items' in arduino_type_struct
#         assert 'analog_pins_items' in arduino_type_struct
#         assert 'digital_pins_struct' in arduino_type_struct
#         assert 'analog_pins_struct' in arduino_type_struct
# 
#         # create 'structs' for each digital pin
#         for dp, dp_struct in zip(
#             arduino_type_struct['digital_pins_items'],
#             arduino_type_struct['digital_pins_struct']):
# 
#             # Assert we are 'synchronized'
#             assert dp == dp_struct['pin']
# 
#             d_pin_obj = self.get_pin(dp, True)
#             dp_struct.update({
#                 'pk': d_pin_obj.pk,
#                 #'pin': dp,
#                 #'digital': True,
#                 #'pwm': (dp in arduino_type_struct['pwm_pin_list']),
#                 'label': d_pin_obj.label,
#                 'pin_id': d_pin_obj.pin_id,
#                 'enabled_in_web': d_pin_obj.enabled_in_web,
#                 #'status.mode': d_pin_status.mode,
#                 #'status.read_value': d_pin_status.read_value,
#                 #'status.written_value': d_pin_status.written_value,
#             })
# 
#         del dp  # to avoid bugs (happened!)
#         del dp_struct  # to avoid bugs (happened!)
#         del d_pin_obj  # to avoid bugs (happened!)
# 
#         # create 'structs' for each analog pin
#         for ap, ap_struct in zip(
#             arduino_type_struct['analog_pins_items'],
#             arduino_type_struct['analog_pins_struct']):
# 
#             # Assert we are 'synchronized'
#             assert ap == ap_struct['pin']
# 
#             a_pin_obj = self.get_pin(ap, False)
#             ap_struct.update({
#                 'pk': a_pin_obj.pk,
#                 #'pin': ap,
#                 #'digital': True,
#                 #'pwm': False,
#                 'label': a_pin_obj.label,
#                 'pin_id': a_pin_obj.pin_id,
#                 'enabled_in_web': a_pin_obj.enabled_in_web,
#                 #'status.mode': a_pin_status.mode,
#                 #'status.read_value': a_pin_status.read_value,
#                 #'status.written_value': a_pin_status.written_value,
#             })
# 
#         return arduino_type_struct


import logging
from django.db import models
from py_arduino_web.storage import Storage, default_label, BaseStorage



FallbackStorage = Storage()

logger = logging.getLogger(__name__)


class Pin(models.Model):
    pin = models.PositiveIntegerField()
    digital = models.BooleanField()
    label = models.CharField(max_length=64,
        help_text="Descriptive text for a pin. Ej: 'Status Led'")
    pin_id = models.CharField(max_length=64, unique=True, null=True, blank=True,
        help_text="Unique identifier for internal use of a pin. Ej: 'status-led'")
    enabled_in_web = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        if self.pin_id is not None and len(self.pin_id.strip()) == 0:
            self.pin_id = None
        super(Pin, self).save(*args, **kwargs) # Call the "real" save() method.

    def __unicode__(self):
        if self.digital:
            ret = u"Digital pin #{0}".format(self.pin)
        else:
            ret = u"Analog pin #{0}".format(self.pin)
        if self.label:

# Path: py_arduino_web/storage.py
# class Storage(BaseStorage):
# 
#     def get_pin(self, pin, is_digital):
#         """
#         Returns a Pin instance.
#         """
#         return Pin(pin, is_digital,)
# 
#     def get_pin_by_id(self, pin_id):
#         """
#         Returns the Pin instance identified by 'pin_id',
#         or None if no Pin exists with that identifier.
#         """
#         return None
# 
# def default_label(pin, is_digital):
#     """Returns a default label for a pin"""
#     if is_digital:
#         return 'Digital pin #{0}'.format(pin)
#     else:
#         return 'Analog pin #{0}'.format(pin)
# 
# class BaseStorage():
# 
#     def enhanceArduinoTypeStruct(self, arduino_type_struct):
#         """
#         Returns an enhanced copy of `arduino_type_struct` with data from storage.
# 
#         The `arduino_type_struct` must be previously
#         enhanced by `PyArduino.enhanceArduinoTypeStruct()`.
# 
#         To each item of `digital_pins_struct`, adds:
#             + pk (int)
#             + label (str)
#             + pin_id (str)
#             + enabled_in_web (bool)
#         To each item of `analog_pins_struct`, adds:
#             + pk (int)
#             + label (str)
#             + pin_id (str)
#             + enabled_in_web (bool)
#         """
#         # arduino_type_struct['digital_pins_items']
#         # arduino_type_struct['analog_pins_items']
#         assert 'digital_pins_items' in arduino_type_struct
#         assert 'analog_pins_items' in arduino_type_struct
#         assert 'digital_pins_struct' in arduino_type_struct
#         assert 'analog_pins_struct' in arduino_type_struct
# 
#         # create 'structs' for each digital pin
#         for dp, dp_struct in zip(
#             arduino_type_struct['digital_pins_items'],
#             arduino_type_struct['digital_pins_struct']):
# 
#             # Assert we are 'synchronized'
#             assert dp == dp_struct['pin']
# 
#             d_pin_obj = self.get_pin(dp, True)
#             dp_struct.update({
#                 'pk': d_pin_obj.pk,
#                 #'pin': dp,
#                 #'digital': True,
#                 #'pwm': (dp in arduino_type_struct['pwm_pin_list']),
#                 'label': d_pin_obj.label,
#                 'pin_id': d_pin_obj.pin_id,
#                 'enabled_in_web': d_pin_obj.enabled_in_web,
#                 #'status.mode': d_pin_status.mode,
#                 #'status.read_value': d_pin_status.read_value,
#                 #'status.written_value': d_pin_status.written_value,
#             })
# 
#         del dp  # to avoid bugs (happened!)
#         del dp_struct  # to avoid bugs (happened!)
#         del d_pin_obj  # to avoid bugs (happened!)
# 
#         # create 'structs' for each analog pin
#         for ap, ap_struct in zip(
#             arduino_type_struct['analog_pins_items'],
#             arduino_type_struct['analog_pins_struct']):
# 
#             # Assert we are 'synchronized'
#             assert ap == ap_struct['pin']
# 
#             a_pin_obj = self.get_pin(ap, False)
#             ap_struct.update({
#                 'pk': a_pin_obj.pk,
#                 #'pin': ap,
#                 #'digital': True,
#                 #'pwm': False,
#                 'label': a_pin_obj.label,
#                 'pin_id': a_pin_obj.pin_id,
#                 'enabled_in_web': a_pin_obj.enabled_in_web,
#                 #'status.mode': a_pin_status.mode,
#                 #'status.read_value': a_pin_status.read_value,
#                 #'status.written_value': a_pin_status.written_value,
#             })
# 
#         return arduino_type_struct


# Path: py_arduino_web/dj/models.py
import logging
from django.db import models
from py_arduino_web.storage import Storage, default_label, BaseStorage




FallbackStorage = Storage()

logger = logging.getLogger(__name__)


class Pin(models.Model):
    pin = models.PositiveIntegerField()
    digital = models.BooleanField()
    label = models.CharField(max_length=64,
        help_text="Descriptive text for a pin. Ej: 'Status Led'")
    pin_id = models.CharField(max_length=64, unique=True, null=True, blank=True,
        help_text="Unique identifier for internal use of a pin. Ej: 'status-led'")
    enabled_in_web = models.BooleanField(default=True)

    def save(self, *args, **kwargs):
        if self.pin_id is not None and len(self.pin_id.strip()) == 0:
            self.pin_id = None
        super(Pin, self).save(*args, **kwargs) # Call the "real" save() method.

    def __unicode__(self):
        if self.digital:
            ret = u"Digital pin #{0}".format(self.pin)
        else:
            ret = u"Analog pin #{0}".format(self.pin)
        if self.label:

            ret = u"{0} ('{1}')".format(ret, self.label)