def test_reset_circuit(circuit): """Reset creates a new empty circuit.""" assert circuit is edzed.get_circuit() assert list(circuit.getblocks()) == [] blk = Noop('test') assert list(circuit.getblocks()) == [blk] edzed.reset_circuit() newcircuit = edzed.get_circuit() assert newcircuit is not circuit assert list(newcircuit.getblocks()) == []
async def ptest(circuit, delay, slog): ie1 = edzed.InputExp('ie', duration=0.25, expired="exp", initdef="ok1", persistent=True) state = {} circuit.set_persistent_data(state) asyncio.create_task(circuit.run_forever()) await asyncio.sleep(0.1) await circuit.shutdown() assert ie1.key in state # circuit 2 await asyncio.sleep(delay) edzed.reset_circuit() circuit = edzed.get_circuit() circuit.set_persistent_data(state) logger = TimeLogger('logger') ie2 = edzed.InputExp('ie', duration=0.25, expired="exp", initdef="ok2", persistent=True, on_output=edzed.Event(logger)) asyncio.create_task(circuit.run_forever()) await asyncio.sleep(0.30) await circuit.shutdown() logger.compare(slog)
async def test_persistent(circuit): td = edzed.TimeSpan("pers", persistent=True) storage = {} circuit.set_persistent_data(storage) asyncio.create_task(circuit.run_forever()) await circuit.wait_init() assert td.get_state() == [] conf = edzed.TimeSpan.parse( "2001August31.23:2:3-31.may2008 0:0,2011August31.1:2:3-31.may2018 1:59") td.event('reconfig', span=conf) assert td.get_state() == conf await circuit.shutdown() assert storage[td.key] == conf edzed.reset_circuit() td = edzed.TimeSpan("pers", persistent=True) circuit = edzed.get_circuit() circuit.set_persistent_data(storage) asyncio.create_task(circuit.run_forever()) await circuit.wait_init() assert td.get_state() == conf await circuit.shutdown() assert storage[td.key] == conf
import asyncio import random import edzed def get_temperature(): """Fake room thermometer (Celsius scale).""" t = random.uniform(20.0, 28.0) print(f" T={t:.1f}") return t edzed.ValuePoll('thermometer', func=get_temperature, interval=1) edzed.Compare('thermostat', low=22.0, high=24.0, on_output=edzed.Event('heater')).connect('thermometer') edzed.OutputFunc('heater', func=lambda hot: print(f"Heater {'off' if hot else 'on'}"), on_error=None) if __name__ == '__main__': print('Press ctrl-C to stop') circuit = edzed.get_circuit() asyncio.run(circuit.run_forever())
def circuit(): """Return a new empty circuit.""" edzed.reset_circuit() return edzed.get_circuit()