Skip to content

Instantly share code, notes, and snippets.

@rstropek
Last active May 20, 2021 08:22
Show Gist options
  • Save rstropek/d0541d39bf9f284d6ba6c7e6a2ff9939 to your computer and use it in GitHub Desktop.
Save rstropek/d0541d39bf9f284d6ba6c7e6a2ff9939 to your computer and use it in GitHub Desktop.
Machine Monitor Sample (XAML)
<Window x:Class="MachineMonitor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MachineMonitor"
mc:Ignorable="d"
Title="Machine Monitor" Height="450" Width="800">
<Window.Resources>
<Style TargetType="local:TemperatureChart">
<Setter Property="Width" Value="200" />
<Setter Property="Height" Value="200" />
</Style>
<Style TargetType="Border" x:Key="ChartBorder">
<Setter Property="Margin" Value="15" />
<Setter Property="Padding" Value="15" />
<Setter Property="BorderBrush" Value="DarkGray" />
<Setter Property="CornerRadius" Value="5" />
<Setter Property="BorderThickness" Value="1" />
</Style>
<Style TargetType="TextBlock" x:Key="SensorName">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Margin" Value="0,5,0,0" />
</Style>
<Style TargetType="ListBox">
<Setter Property="Margin" Value="15,15,0,15" />
<Setter Property="MinWidth" Value="100" />
</Style>
</Window.Resources>
<DockPanel>
<ListBox DockPanel.Dock="Left" ItemsSource="{Binding Sensors}" SelectionMode="Multiple"
DisplayMemberPath="Name" Name="SensorSelection" />
<ScrollViewer HorizontalScrollBarVisibility="Disabled">
<ItemsControl ItemsSource="{Binding ElementName=SensorSelection, Path=SelectedItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Style="{StaticResource ChartBorder}">
<StackPanel>
<local:TemperatureChart DataContext="{Binding Temperatures}" />
<TextBlock Text="{Binding Name}" Style="{StaticResource SensorName}" />
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</DockPanel>
</Window>
using System;
namespace MachineMonitor
{
/// <summary>
/// Represents a temperature reading
/// </summary>
public record TemperatureReading(double Value, bool IsCritical);
/// <summary>
/// Simulates a temperature sensor
/// </summary>
/// <remarks>
/// This sample class generates artificial temperature readings based on
/// a sinus curve. The readings are between 0 and <see cref="Amplitude"/>.
/// </remarks>
public class TemperatureSensor
{
// Date and time when sensor was created. Used to generate artifical temperature values.
private readonly DateTimeOffset creationTimestamp = DateTimeOffset.UtcNow;
// Random shift of sinus curve. Makes sure that every sensor produces slightly different values.
private readonly double shift;
// Period in ms for sinus curve
private const long period = 20_000L;
// Amplitude for generated values
public const double Amplitude = 300d;
public TemperatureSensor() => shift = Math.PI * new Random().NextDouble();
public TemperatureReading GetTemperature()
{
// Simulate reading temperatur
var duration = DateTimeOffset.UtcNow.Subtract(creationTimestamp).TotalMilliseconds / period * period;
var reading = (Amplitude / 2d) + Amplitude / 2d * Math.Sin(shift + Math.PI * 2 * duration / period);
return new(reading, reading is > (Amplitude * 0.9d) or < (Amplitude * 0.1d));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment