/// <summary> /// Initializes a new instance. /// </summary> /// <param name="uri"></param> public RnetBus(Uri uri) { Contract.Requires<ArgumentNullException>(uri != null); Contract.Ensures(Uri != null); Contract.Ensures(Controllers != null); Contract.Ensures(Client != null); Contract.Ensures(State == RnetBusState.Stopped); RnetTraceSource.Default.TraceInformation("RnetBus:ctor Uri={0}", uri); this.uri = uri; this.controllers = new RnetControllerCollection(this); this.state = RnetBusState.Stopped; // start new client this.client = new RnetClient(Uri); this.client.StateChanged += Client_StateChanged; this.client.MessageReceived += Client_MessageReceived; this.client.MessageSent += Client_MessageSent; this.client.UnhandledException += Client_UnhandledException; this.client.Connection.StateChanged += Connection_StateChanged; }
/// <summary> /// Stops the RNET bus. /// </summary> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task Stop(CancellationToken cancellationToken) { Contract.Requires(Client != null); using (await lck.LockAsync()) { if (state != RnetBusState.Started) throw new RnetException("Bus is already stopped or stopping."); // our intentions are to be stopped state = RnetBusState.Stopped; SetState(RnetBusState.Stopping); await Client.Stop(); SetState(RnetBusState.Stopped); } }
/// <summary> /// Sets the current state. /// </summary> /// <param name="state"></param> void SetState(RnetBusState state) { OnStateChanged(new RnetBusStateEventArgs(this.state = state)); }
/// <summary> /// Starts the RNET bus. /// </summary> /// <param name="keypadId"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task Start(SynchronizationContext synchronizationContext, RnetKeypadId keypadId, CancellationToken cancellationToken) { Contract.Requires<ArgumentNullException>(synchronizationContext != null); Contract.Requires<ArgumentNullException>(keypadId != RnetKeypadId.Null); Contract.Requires<ArgumentNullException>(cancellationToken != null); using (await lck.LockAsync(cancellationToken)) { if (state == RnetBusState.Started) throw new RnetException("Bus is already started or starting."); // our intentions are to be started state = RnetBusState.Started; // begin starting SetState(RnetBusState.Starting); this.synchronizationContext = synchronizationContext; Controllers.Clear(); // generate minimal required model and insert new local device var c = Controllers[RnetControllerId.Root]; var z = c.Zones[RnetZoneId.Zone1]; var d = new RnetLocalDevice(z, keypadId); z.Devices[keypadId] = this.localDevice = d; // start client running await Client.Start(); SetState(RnetBusState.Started); // activate the path to the local device d.Activate(); // initiate device scan await Scan(); } }
/// <summary> /// Initializes a new instance. /// </summary> /// <param name="state"></param> internal RnetBusStateEventArgs(RnetBusState state) { State = state; }