-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
526 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Drawing; | ||
using System.Globalization; | ||
using System.Windows.Data; | ||
|
||
|
||
namespace Examples.WPF.Converters; | ||
|
||
[ValueConversion(typeof(bool), typeof(bool))] | ||
public class ColorHashConverter : IValueConverter { | ||
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { | ||
|
||
var hashCode = value.GetHashCode(); | ||
var randColor = GenerateRandomVibrantColor(new Random(hashCode)); | ||
|
||
System.Windows.Media.Brush outBrush = new System.Windows.Media.SolidColorBrush(new System.Windows.Media.Color { | ||
R = randColor.R, | ||
G = randColor.G, | ||
B = randColor.B, | ||
A = 255, | ||
}); | ||
|
||
return outBrush; | ||
|
||
} | ||
|
||
private Color GenerateRandomVibrantColor(Random random) { | ||
|
||
byte red = (byte)random.Next(256); | ||
byte green = (byte)random.Next(256); | ||
byte blue = (byte)random.Next(256); | ||
|
||
Color color = Color.FromArgb(255, red, green, blue); | ||
|
||
// Ensure the color is vibrant and colorful | ||
while (!IsVibrantColor(color)) { | ||
red = (byte)random.Next(256); | ||
green = (byte)random.Next(256); | ||
blue = (byte)random.Next(256); | ||
color = Color.FromArgb(255,red, green, blue); | ||
} | ||
|
||
return color; | ||
} | ||
|
||
private bool IsVibrantColor(Color color) { | ||
|
||
int minBrightness = 100; | ||
int maxBrightness = 200; | ||
int minSaturation = 150; | ||
|
||
int brightness = (int)(color.GetBrightness() * 255); | ||
int saturation = (int)(color.GetSaturation() * 255); | ||
|
||
return brightness >= minBrightness && brightness <= maxBrightness && saturation >= minSaturation; | ||
|
||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using MewtocolNet.Registers; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
|
||
namespace MewtocolNet.Events { | ||
|
||
public delegate void PlcReconnectEventHandler(object sender, ReconnectArgs e); | ||
|
||
public class ReconnectArgs : EventArgs, INotifyPropertyChanged { | ||
|
||
private TimeSpan retryCountDownRemaining; | ||
|
||
public event PropertyChangedEventHandler PropertyChanged; | ||
|
||
public int ReconnectTry { get; internal set; } | ||
|
||
public int MaxAttempts { get; internal set; } | ||
|
||
public TimeSpan RetryCountDownTime { get; internal set; } | ||
|
||
public TimeSpan RetryCountDownRemaining { | ||
get => retryCountDownRemaining; | ||
private set { | ||
retryCountDownRemaining = value; | ||
OnPropChange(); | ||
} | ||
} | ||
|
||
private System.Timers.Timer countDownTimer; | ||
|
||
internal ReconnectArgs(int currentAttempt, int totalAttempts, TimeSpan delayBetween) { | ||
|
||
ReconnectTry = currentAttempt; | ||
MaxAttempts = totalAttempts; | ||
RetryCountDownTime = delayBetween; | ||
|
||
//start countdown timer | ||
RetryCountDownRemaining = RetryCountDownTime; | ||
|
||
var interval = 100; | ||
var intervalTS = TimeSpan.FromMilliseconds(interval); | ||
|
||
countDownTimer = new System.Timers.Timer(100); | ||
|
||
countDownTimer.Elapsed += (s, e) => { | ||
if (RetryCountDownRemaining <= TimeSpan.Zero) { | ||
StopTimer(); | ||
return; | ||
} | ||
RetryCountDownRemaining -= intervalTS; | ||
}; | ||
|
||
countDownTimer.Start(); | ||
|
||
} | ||
|
||
internal void ConnectionSuccess () { | ||
|
||
StopTimer(); | ||
|
||
} | ||
|
||
private void StopTimer () { | ||
|
||
countDownTimer?.Stop(); | ||
RetryCountDownRemaining = TimeSpan.Zero; | ||
|
||
} | ||
|
||
private void OnPropChange([CallerMemberName] string propertyName = null) { | ||
|
||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.