Created
March 13, 2024 14:26
-
-
Save M0nteCarl0/cf66b876fe78c743fe27a5cad748eaf2 to your computer and use it in GitHub Desktop.
OptrisTemperature data harvester
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
using System; | |
using System.IO.Ports; | |
using System.Runtime.CompilerServices; | |
using System.Threading; | |
using NLog; | |
public class OptrisTemperature | |
{ | |
private Logger logger | |
{ | |
get | |
{ | |
return LogManager.GetCurrentClassLogger(); | |
} | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public double getTemperature() | |
{ | |
bool flag = !Config.Instance.AggressiveMode; | |
double result; | |
if (flag) | |
{ | |
Thread.Sleep(this.getReadTimeout()); | |
result = this.GetRandomDouble(30.0, 38.0); | |
} | |
else | |
{ | |
double num = -500.0; | |
this.serial = new SerialPort(this.getSerialPortName(), this.getSerialBaudRate(), Parity.None, 8, StopBits.None); | |
try | |
{ | |
this.serial.WriteTimeout = 10000; | |
this.serial.ReadTimeout = 10000; | |
this.serial.Open(); | |
this.serial.DtrEnable = true; | |
this.serial.BaseStream.Flush(); | |
Thread.Sleep(this.getReadTimeout()); | |
this.serial.Write(new byte[] | |
{ | |
1, | |
1 | |
}, 0, 2); | |
byte[] array = new byte[1]; | |
this.serial.Read(array, 0, 1); | |
byte b = array[0]; | |
this.serial.Read(array, 0, 1); | |
byte b2 = array[0]; | |
this.serial.Read(array, 0, 1); | |
byte b3 = array[0]; | |
num = (double)b * 256.0 + (double)b2; | |
this.logger.Error(string.Concat(new string[] | |
{ | |
"bytes = ", | |
b.ToString(), | |
" ", | |
b2.ToString(), | |
" ", | |
b3.ToString() | |
})); | |
num -= 1000.0; | |
num /= 10.0; | |
this.logger.Error("real result = " + num.ToString()); | |
this.serial.DtrEnable = false; | |
} | |
catch (Exception ex) | |
{ | |
this.logger.Error(ex, "Failed to read temperature"); | |
this.logger.Error(ex.StackTrace); | |
return num; | |
} | |
finally | |
{ | |
try | |
{ | |
bool isOpen = this.serial.IsOpen; | |
if (isOpen) | |
{ | |
this.serial.Close(); | |
} | |
} | |
catch (Exception value) | |
{ | |
this.logger.Error<Exception>(value); | |
} | |
} | |
result = num; | |
} | |
return result; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
private double GetRandomDouble(double min, double max) | |
{ | |
Random random = new Random(); | |
return min + random.NextDouble() * (max - min); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public string getSerialPortName() | |
{ | |
string text = Config.ApplicationConfig.ReadINI("temperature", "serial_port"); | |
bool flag = string.IsNullOrWhiteSpace(text); | |
if (flag) | |
{ | |
text = "COM1"; | |
this.logger.Warn("No serial port in config, defaulting to " + text); | |
} | |
return text; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public int getSerialBaudRate() | |
{ | |
int num = Config.ApplicationConfig.ReadInt("temperature", "baudrate"); | |
bool flag = num == -1; | |
if (flag) | |
{ | |
num = 115200; | |
this.logger.Warn("No serial baudrate in config, defaulting to " + num.ToString()); | |
} | |
return num; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public double getMaxTemperature() | |
{ | |
double num = Config.ApplicationConfig.ReadDouble("temperature", "max"); | |
bool flag = Math.Abs(num + 1.0) < 0.001; | |
if (flag) | |
{ | |
num = 37.0; | |
this.logger.Warn("No serial maximum temperature in config, defaulting to " + num.ToString()); | |
} | |
return num; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public double getMinTemperature() | |
{ | |
double num = Config.ApplicationConfig.ReadDouble("temperature", "min"); | |
bool flag = Math.Abs(num + 1.0) < 0.001; | |
if (flag) | |
{ | |
num = 31.0; | |
this.logger.Warn("No serial maximum temperature in config, defaulting to " + num.ToString()); | |
} | |
return num; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public int getReadTimeout() | |
{ | |
int num = Config.ApplicationConfig.ReadInt("temperature", "read_timeout"); | |
bool flag = num == -1; | |
if (flag) | |
{ | |
num = 3000; | |
this.logger.Warn("No read timeout temperature in config, defaulting to " + num.ToString()); | |
} | |
return num; | |
} | |
private SerialPort serial; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment