# Path: BAC0/core/io/IOExceptions.py
# class OutOfServiceNotSet(Exception):
#     """
#     This exception is used when trying to simulate a point and the out of service property is false.
#     """
# 
#     pass
# 
# class OutOfServiceSet(Exception):
#     """
#     This exception is used when trying to set the out of service property to
#     false to release the simulation...and it doesn't work.
#     """
# 
#     pass
# 
# class NoResponseFromController(Exception):
#     """
#     This exception is used when trying to read or write and there is not answer.
#     """
# 
#     pass
# 
# class ApplicationNotStarted(Exception):
#     """
#     Application not started, no communication available.
#     """
# 
#     pass


from .IOExceptions import (
    OutOfServiceNotSet,
    OutOfServiceSet,
    NoResponseFromController,
    ApplicationNotStarted,
)


class Simulation:
    """
    Global informations regarding simulation
    """

    def sim(self, args):
        """
        Simulate I/O points by setting the Out_Of_Service property, then doing a
        WriteProperty to the point's Present_Value.

        :param args: String with <addr> <type> <inst> <prop> <value> [ <indx> ] [ <priority> ]

        """
        if not self._started:
            raise ApplicationNotStarted("BACnet stack not running - use startApp()")

        # with self.this_application._lock: if use lock...won't be able to call read...
        args = args.split()
        addr, obj_type, obj_inst, prop_id, value = args[:5]

        if self.read("{} {} {} outOfService".format(addr, obj_type, obj_inst)):
            self.write(
                "{} {} {} {} {}".format(addr, obj_type, obj_inst, prop_id, value)
            )
        else:
            try:
                self.write(
                    "{} {} {} outOfService True".format(addr, obj_type, obj_inst)
                )

# Path: BAC0/core/io/IOExceptions.py
# class OutOfServiceNotSet(Exception):
#     """
#     This exception is used when trying to simulate a point and the out of service property is false.
#     """
# 
#     pass
# 
# class OutOfServiceSet(Exception):
#     """
#     This exception is used when trying to set the out of service property to
#     false to release the simulation...and it doesn't work.
#     """
# 
#     pass
# 
# class NoResponseFromController(Exception):
#     """
#     This exception is used when trying to read or write and there is not answer.
#     """
# 
#     pass
# 
# class ApplicationNotStarted(Exception):
#     """
#     Application not started, no communication available.
#     """
# 
#     pass


# Path: BAC0/core/io/Simulate.py
from .IOExceptions import (
    OutOfServiceNotSet,
    OutOfServiceSet,
    NoResponseFromController,
    ApplicationNotStarted,
)


class Simulation:
    """
    Global informations regarding simulation
    """

    def sim(self, args):
        """
        Simulate I/O points by setting the Out_Of_Service property, then doing a
        WriteProperty to the point's Present_Value.

        :param args: String with <addr> <type> <inst> <prop> <value> [ <indx> ] [ <priority> ]

        """
        if not self._started:
            raise ApplicationNotStarted("BACnet stack not running - use startApp()")

        # with self.this_application._lock: if use lock...won't be able to call read...
        args = args.split()
        addr, obj_type, obj_inst, prop_id, value = args[:5]

        if self.read("{} {} {} outOfService".format(addr, obj_type, obj_inst)):
            self.write(
                "{} {} {} {} {}".format(addr, obj_type, obj_inst, prop_id, value)
            )
        else:
            try:
                self.write(
                    "{} {} {} outOfService True".format(addr, obj_type, obj_inst)
                )

            except NoResponseFromController as e:
