Skip to content

Sensor complete

Uffe Björklund edited this page Jun 16, 2015 · 3 revisions
using System.Threading.Tasks;
using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;

public class TempModel
{
    /// <summary>
    /// Object temperature
    /// </summary>
    public double obj { get; set; }
    /// <summary>
    /// Ambient temperature
    /// </summary>
    public double amb { get; set; }
}

/// <summary>
/// Controller that sensor clients will use    
/// </summary>
public class Sensor : XSocketController
{
    /// <summary>
    /// The name of the sensor
    /// Should be set from the sensor when it is connected
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Last known value for this sensor
    /// </summary>
    public TempModel LastValue { get; set; }

    public Sensor()
    {
        this.Name = "N/A - Waiting for sensor input";
        this.LastValue = new TempModel();            
    }

    /// <summary>
    /// A sensor connected, tell all monitoring clients about it.
    /// </summary>
    public override Task OnOpened()
    {
        var objToSend = new {id = this.ConnectionId, this.LastValue.obj, this.LastValue.amb, name = this.Name};
        this.InvokeToAll<Monitor>(objToSend, "sensorConnected");            
        return base.OnOpened();
    }
    
    /// <summary>
    /// A sensor disconnected, tell all the monitoring clients about it.
    /// </summary>
    public override Task OnClosed()
    {
        this.InvokeToAll<Monitor>(this.ConnectionId, "sensorDisconnected");            
        return base.OnClosed();
    }

    /// <summary>
    /// Will send the infomation to clients on the monitor controller that has templimit set to a value lower than obj/amb
    /// </summary>
    /// <param name="model"></param>
    public void IrTempChange(TempModel model)
    {
        //set last know value so that new monitoring clients can get the information
        this.LastValue = model;
        var objToSend = new {id = this.ConnectionId, model.obj, model.amb, name = Name};
        //Send only the clients mathching the expression
        this.InvokeTo<Monitor>(p => p.TempLimit <= model.obj || p.TempLimit <= model.amb, objToSend,"irtempchange");              
    }

    /// <summary>
    /// Tell all monitoring client that IR-temp was enabled for this sensor
    /// </summary>
    public void IrTempEnabled()
    {
        this.InvokeToAll<Monitor>(this.ConnectionId, "irtempenabled");            
    }

    /// <summary>
    /// Tell all monitoring client that IR-temp was disabled for this sensor
    /// </summary>
    public void IrTempDisabled()
    {         
        this.InvokeToAll<Monitor>(this.ConnectionId, "irTempDisabled");            
    }
}
Clone this wiki locally