Example #1
0
class FlowSensorSaveThresholdsTest(unittest.TestCase):
    def setUp(self):
        self.flow = FlowSensor()

    def test_1_volume_duration_valid(self):
        recorded_data = {
            'volume': config['MIN_FLOW_VOLUME_FOR_SAVE'] + 100,
            'duration': config['MAX_FLOW_DURATION_FOR_SAVE'] + 100
        }
        satisfied = self.flow.are_thresholds_satisfied(recorded_data)
        self.assertTrue(satisfied)

    def test_2_invalid_volume(self):
        recorded_data = {
            'volume': config['MIN_FLOW_VOLUME_FOR_SAVE'] - 100,
            'duration': config['MAX_FLOW_DURATION_FOR_SAVE'] + 100
        }
        satisfied = self.flow.are_thresholds_satisfied(recorded_data)
        self.assertTrue(satisfied)

    def test_3_invalid_duration(self):
        recorded_data = {
            'volume': config['MIN_FLOW_VOLUME_FOR_SAVE'] + 100,
            'duration': config['MAX_FLOW_DURATION_FOR_SAVE'] - 100
        }
        satisfied = self.flow.are_thresholds_satisfied(recorded_data)
        self.assertTrue(satisfied)

    def test_4_volume_duration_invalid(self):
        recorded_data = {
            'volume': config['MIN_FLOW_VOLUME_FOR_SAVE'] - 100,
            'duration': config['MAX_FLOW_DURATION_FOR_SAVE'] - 100
        }
        satisfied = self.flow.are_thresholds_satisfied(recorded_data)
        self.assertFalse(satisfied)
Example #2
0
class FlowSensorRecordPulseTest(unittest.TestCase):
    def setUp(self):
        self.flow = FlowSensor()

    def test_1_record_one(self):
        self.assertEqual(self.flow.pulses, 0)
        self.flow.record_pulse()
        self.assertEqual(self.flow.pulses, 1)

    def test_2_record_many_sequential(self):
        self.assertEqual(self.flow.pulses, 0)
        num_records = 100
        for _ in range(num_records):
            self.flow.record_pulse()
        self.assertEqual(self.flow.pulses, num_records)

    def test_2_record_many_parallel(self):
        self.assertEqual(self.flow.pulses, 0)
        num_records = 100
        threads = [
            threading.Thread(target=self.flow.record_pulse)
            for _ in range(num_records)
        ]
        for t in threads:
            t.start()
        for t in threads:
            t.join()
        self.assertEqual(self.flow.pulses, num_records)
Example #3
0
class FlowSensorResetDataTest(unittest.TestCase):
    def setUp(self):
        self.flow = FlowSensor()

    def test_1_reset_pulse_to_zero(self):
        self.flow.pulses = 100
        self.flow.last_read_time = None
        recorded_data = {'recorded_pulses': 100, 'new_time': time.time()}
        self.flow.reset_flow_data(recorded_data)
        self.assertEqual(self.flow.pulses, 0)
        self.assertIsNotNone(self.flow.last_read_time)

    def test_2_reset_pulse_to_non_zero(self):
        self.flow.pulses = 200
        self.flow.last_read_time = None
        recorded_data = {'recorded_pulses': 100, 'new_time': time.time()}
        self.flow.reset_flow_data(recorded_data)
        self.assertEqual(self.flow.pulses, 100)
        self.assertIsNotNone(self.flow.last_read_time)
Example #4
0
class FlowSensorSaveFlowProcessTest(unittest.TestCase):
    def setUp(self):
        self.flow = FlowSensor()
        self.config_backup = config.copy()

    def tearDown(self):
        config.update(self.config_backup)

    def test_1_process_success(self):
        self.flow.pulses = 10000
        last_read_time = self.flow.last_read_time
        success = self.flow.save_flow_process()
        self.assertTrue(success)
        self.assertEqual(self.flow.pulses, 0)
        self.assertIsNot(self.flow.last_read_time, last_read_time)

    def test_2_process_fail_invalid_key(self):
        TestUtils.configure_invalid_product_key()
        pulses = self.flow.pulses = 10000
        last_read_time = self.flow.last_read_time
        success = self.flow.save_flow_process()
        self.assertFalse(success)
        self.assertEqual(self.flow.pulses, pulses)
        self.assertIs(self.flow.last_read_time, last_read_time)

    def test_3_process_fail_server_down(self):
        TestUtils.configure_server_down()
        pulses = self.flow.pulses = 10000
        last_read_time = self.flow.last_read_time
        success = self.flow.save_flow_process()
        self.assertFalse(success)
        self.assertEqual(self.flow.pulses, pulses)
        self.assertIs(self.flow.last_read_time, last_read_time)

    def test_4_process_fail_threshold_not_satisfied(self):
        last_read_time = self.flow.last_read_time
        success = self.flow.save_flow_process()
        self.assertFalse(success)
        self.assertEqual(self.flow.pulses, 0)
        self.assertIs(self.flow.last_read_time, last_read_time)
Example #5
0
class FlowSensorReadFlowDataTest(unittest.TestCase):
    def setUp(self):
        self.flow = FlowSensor()

    def test_1_read(self):
        pulses = self.flow.pulses = 100
        recorded_data = self.flow.read_flow_data()
        expected_volume = float(pulses) / config['PULSES_PER_LITRE']
        expected_duration = recorded_data['new_time'] - self.flow.last_read_time
        self.assertEqual(
            recorded_data, {
                'new_time': recorded_data['new_time'],
                'recorded_pulses': pulses,
                'volume': expected_volume,
                'duration': expected_duration
            })
Example #6
0
 def setUp(self):
     self.flow = FlowSensor()
     self.config_backup = config.copy()
Example #7
0
 def test_3_server_down(self):
     TestUtils.configure_server_down()
     volume = 10
     duration = 20
     success = FlowSensor.send_flow_data(volume, duration)
     self.assertFalse(success)
Example #8
0
 def test_2_invalid_request(self):
     TestUtils.configure_invalid_product_key()
     volume = 10
     duration = 20
     success = FlowSensor.send_flow_data(volume, duration)
     self.assertFalse(success)
Example #9
0
 def test_2_invalid_flow_info_None(self):
     volume = -1
     duration = None
     success = FlowSensor.send_flow_data(volume, duration)
     self.assertFalse(success)
Example #10
0
 def test_1_valid_request(self):
     volume = 10
     duration = 20
     success = FlowSensor.send_flow_data(volume, duration)
     self.assertTrue(success)
Example #11
0
 def setUp(self):
     self.flow = FlowSensor()
Example #12
0
from PinsControl import PinsController
from ValveControl import Valve

# App entry point
active = True

# Configure logging
SprinklerLogging.configure_logging()

logger = logging.getLogger(__name__)

# Start components
pins_controller = PinsController()
valve = Valve()
valve.start()
flow_sensor = FlowSensor()
flow_sensor.start()


# Cleanup
def cleanup(sig_num, stack_frame):
    logger.info('Interrupted by sig_num %d. Cleaning up before exit', sig_num)
    valve.stop()
    flow_sensor.stop()
    pins_controller.clean_up()
    global active
    active = False


signals = [signal.SIGINT, signal.SIGTERM]
for s in signals: