from random import random
import time


class Model:
    ''' 
    The work here is done inside two functions: 
    * Detection is performed in detection_algorithm() which is installed as an 'after' callback on two triggers 
    (getting out of the transient start period, and the reflexive transition based on the main loop timer.) This 
    was implemented this way because there is another trigger (getting out of isolation) that should not trigger it, 
    and this selectivity is not possible with a state-attached callback.

    * Isolation is performed in isolation_algorithm(), which is installed as a state-attached 'on_enter' callback.

    '''
    
    def __init__(self, ahrs, daq, hdf):
        self.ahrs = ahrs
        self.daq = daq
        self.hdf5 = hdf

    
#### this is where the real work happens:            
    def detection_algorithm(self):
        ''' Returns true if there has been a detection, false otherwise. '''
        print('running detection algorithm')
        if random() > 0.5:
            self.fault_detected()

    
    def isolation_algorithm(self):
        ''' Emits a message and returns True if a fault is isolated, returns
        False otherwise. '''
        print('running isolation algorithm')
        if random() > 0.5:
            return True
        return False

    
### State-attached callback for isolation
    def on_enter_isolation(self):
        print('entering isolation')
        if self.isolation_algorithm():
            print('isolated a fault')
        self.isolation_finished()

        
#### these are structural
    def on_enter_idle(self):
        print('entering idle')
        

    def on_enter_start(self):
        print('entering start')
        

    def on_enter_detection(self):
        print('entering detection')

        
    def on_exit_detection(self):
        pass

        
    def on_exit_isolation(self):
        pass


    def on_enter_stop(self):
        print('entering stop')


    def on_exit_stop(self):
        pass
