Files
laatzen/Common/OmniPlus/OmniPlusConnection.cs
T

432 lines
14 KiB
C#

namespace OmniPlus
{
using OmniPlus.Connections;
using OmniPlus.Features;
using OmniPlus.Hanlers;
using System;
using System.IO.Ports;
using static OmniPlus.Features.Features;
public sealed class OmniPlusConnection
{
private Func<bool> connected;
private readonly UI1236Stream ui1236;
private readonly UniProStream unipro;
public OmniPlusConnection(string portName)
{
this.ui1236 = new UI1236Stream(new SerialPortSettings
{
BaudRate = 115200,
DataBits = 8,
Parity = Parity.None,
PortName = portName,
StopBits = StopBits.One
});
this.unipro = new UniProStream(new SerialPortSettings
{
BaudRate = 2400,
DataBits = 8,
Parity = Parity.None,
PortName = portName,
StopBits = StopBits.One
});
}
internal MessageHandler MessageHandler;
public bool Connected
=> this.connected?.Invoke() ?? false;
public bool ConnectIrda()
{
this.unipro.Dispose();
this.ui1236.Open();
this.connected = () => this.ui1236.IsOpen;
this.MessageHandler = this.ui1236.Write;
return this.ui1236.IsOpen;
}
public bool ConnectUniPro()
{
this.ui1236.Dispose();
this.unipro.Open();
this.connected = () => this.unipro.IsOpen;
this.MessageHandler = this.unipro.Write;
return this.unipro.IsOpen;
}
public void Disconnect()
{
this.unipro.Dispose();
this.ui1236.Dispose();
}
public FactoryID ViewFactoryID()
{
var response = this.MessageHandler.Invoke(VIEW_FACTORY_ID);
return new FactoryID(response);
}
public ProgrammableID ViewProgrammableID()
{
var response = this.MessageHandler.Invoke(VIEW_PROGRAMMABLE_ID);
return new ProgrammableID(response);
}
public VersionAndType ViewVersionAndType()
{
var response = this.MessageHandler.Invoke(VIEW_VERSION_AND_TYPE);
return new VersionAndType(response);
}
public CustomerText ViewCustomerText()
{
var response = this.MessageHandler.Invoke(VIEW_CUSTOMER_TEXT);
return new CustomerText(response);
}
public ReadingDigits ViewReadingDigits()
{
var response = this.MessageHandler.Invoke(VIEW_READING_DIGITS);
return new ReadingDigits(response);
}
public ReadingUnits ViewReadingUnits()
{
var response = this.MessageHandler.Invoke(VIEW_READING_UNITS);
return new ReadingUnits(response);
}
public RateUnits ViewRateUnits()
{
var response = this.MessageHandler.Invoke(VIEW_RATE_UNITS);
return new RateUnits(response);
}
public ReadingResolution ViewReadingResolution()
{
var response = this.MessageHandler.Invoke(VIEW_READING_RESOLUTION);
return new ReadingResolution(response);
}
public RateResolution ViewRateResolution()
{
var response = this.MessageHandler.Invoke(VIEW_RATE_RESOLUTION);
return new RateResolution(response);
}
public Readings ViewReadings(Readings reading)
{
var response = this.MessageHandler.Invoke(VIEW_READINGS);
return new Readings(response);
}
public ReadingMode ViewReadingMode()
{
var response = this.MessageHandler.Invoke(VIEW_READING_MODE);
return new ReadingMode(response);
}
public BuildInfo ViewBuildInformation()
{
var response = this.MessageHandler.Invoke(VIEW_BUILD_INFORMATION);
return new BuildInfo(response);
}
public SystemParameters ViewSystemParameters()
{
var response = this.MessageHandler.Invoke(DEVICE_SPECIFIC, VIEW_SYSTEM_PARAMETERS);
return new SystemParameters(response);
}
public ConfigurationParameters ViewConfigurationParameters()
{
var response = this.MessageHandler.Invoke(DEVICE_SPECIFIC, VIEW_CONFIGURATION_PARAMETERS, 0x01);
return new ConfigurationParameters(response);
}
public InputVolume ViewInputVolume()
{
var response = this.MessageHandler.Invoke(DEVICE_SPECIFIC, VIEW_INPUT_VOLUME);
return new InputVolume(response);
}
public ChamberCalibrationResponse StartChamberCalibration(ushort rotations, ushort duration)
{
var response = this.MessageHandler.Invoke(new ChamberCalibrationRequest
{
Rotations = rotations,
Duration = duration,
Control = ChamberCalibrationRequest.ACTIVATE_TEST
}[DEVICE_SPECIFIC, CHAMBER_CALIBRATION]);
return new ChamberCalibrationResponse(response);
}
public ChamberCalibrationResponse ViewChamberCalibration()
{
var response = this.MessageHandler.Invoke(DEVICE_SPECIFIC, CHAMBER_CALIBRATION, ChamberCalibrationRequest.REQUEST_RESULTS);
return new ChamberCalibrationResponse(response);
}
public SystemTime ViewSystemTime()
{
var response = this.MessageHandler.Invoke(DEVICE_SPECIFIC, VIEW_SYSTEM_TIME);
return new SystemTime(response);
}
public RegisterSettings ViewRegisterSettings()
{
var response = this.MessageHandler.Invoke(DEVICE_SPECIFIC, VIEW_REGISTER_SETTINGS);
return new RegisterSettings(response);
}
public FeatureResponse ViewRegisterSettings(FeatureRequest request)
{
var response = this.MessageHandler.Invoke(request[DEVICE_SPECIFIC, SET_REGISTER_SETTINGS]);
return new RegisterSettings(response);
}
public AlarmsState ViewAlarmsState()
{
var response = this.MessageHandler.Invoke(VIEW_ALARMS_STATE, 0xFF, 0x00, 0x00);
return new AlarmsState(response);
}
public AlarmSettings ViewAlarmSettings(byte alarmID)
{
var response = this.MessageHandler.Invoke(VIEW_ALARM_SETTINGS, 0x00, alarmID, 0x01);
return new AlarmSettings(response);
}
public FactorySeal ViewFactorySeal()
{
var response = this.MessageHandler.Invoke(DEVICE_SPECIFIC, VIEW_FACTORY_SEAL);
return new FactorySeal(response);
}
public FeatureResponse ClearAlarms()
{
var response = this.MessageHandler.Invoke(CLEAR_ALARMS, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
return new FeatureResponse(response);
}
public FeatureResponse ClearDisplay()
{
var response = this.MessageHandler.Invoke(SET_READING_UNITS, 0xFF);
return new FeatureResponse(response);
}
public FeatureResponse EnableNFC()
{
var response = this.MessageHandler.Invoke(DEVICE_SPECIFIC, METROLOGY_TEST, 0x02);
return new FeatureResponse(response);
}
public FeatureResponse Set(Alarm alarm)
{
var response = this.MessageHandler.Invoke(alarm[SET_ALARM_SETTINGS]);
return new FeatureResponse(response);
}
public FeatureResponse Set(ASCIIString asciiString)
{
var response = this.MessageHandler.Invoke(asciiString[SET_READING_PRESET]);
return new FeatureResponse(response);
}
public FeatureResponse Set(BuildInfo buildInformation)
{
var response = this.MessageHandler.Invoke(buildInformation[SET_BUILD_INFORMATION]);
return new FeatureResponse(response);
}
public FeatureResponse Set(ConfigurationParametersSet configurationParameters)
{
var response = this.MessageHandler.Invoke(configurationParameters[DEVICE_SPECIFIC, SET_CONFIGURATION_PARAMETERS]);
return new FeatureResponse(response);
}
public FeatureResponse Set(CustomerText customerText)
{
var response = this.MessageHandler.Invoke(customerText[SET_CUSTOMER_TEXT]);
return new FeatureResponse(response);
}
public FeatureResponse Set(DeviceID deviceID)
{
var response = this.MessageHandler.Invoke(deviceID[DEVICE_SPECIFIC, SET_DEVICE_ID]);
return new FeatureResponse(response);
}
public FeatureResponse Set(FactoryID factoryID)
{
var response = this.MessageHandler.Invoke(factoryID[SET_FACTORY_ID]);
return new FeatureResponse(response);
}
public FeatureResponse Set(FactorySeal factorySeal)
{
var response = this.MessageHandler.Invoke(factorySeal[DEVICE_SPECIFIC, SET_FACTORY_SEAL]);
return new FeatureResponse(response);
}
public FeatureResponse Set(InputVolume inputVolume)
{
var response = this.MessageHandler.Invoke(inputVolume[DEVICE_SPECIFIC, SET_INPUT_VOLUME]);
return new FeatureResponse(response);
}
public FeatureResponse Set(ManifacturingTime manifacturingTime)
{
var response = this.MessageHandler.Invoke(manifacturingTime[DEVICE_SPECIFIC, SET_MANIFACTURING_TIME]);
return new FeatureResponse(response);
}
public FeatureResponse Set(ProgrammableID programmableID)
{
var response = this.MessageHandler.Invoke(programmableID[SET_PROGRAMMABLE_ID]);
return new FeatureResponse(response);
}
public FeatureResponse Set(RateUnits rateUnits)
{
var response = this.MessageHandler.Invoke(rateUnits[SET_RATE_UNITS]);
return new FeatureResponse(response);
}
public FeatureResponse Set(RateResolution rateResolution)
{
var response = this.MessageHandler.Invoke(rateResolution[SET_READING_RESOLUTION]);
return new FeatureResponse(response);
}
public FeatureResponse Set(ReadingDigits readingDigits)
{
var response = this.MessageHandler.Invoke(readingDigits[SET_READING_DIGITS]);
return new FeatureResponse(response);
}
public FeatureResponse Set(ReadingMode readingMode)
{
var response = this.MessageHandler.Invoke(readingMode[SET_READING_MODE]);
return new FeatureResponse(response);
}
public FeatureResponse Set(ReadingPreset readingPreset)
{
var response = this.MessageHandler.Invoke(readingPreset[SET_READING_PRESET]);
return new FeatureResponse(response);
}
public FeatureResponse Set(ReadingUnits readingUnits)
{
var response = this.MessageHandler.Invoke(readingUnits[SET_READING_UNITS]);
return new FeatureResponse(response);
}
public FeatureResponse Set(ReadingResolution readingResolution)
{
var response = this.MessageHandler.Invoke(readingResolution[SET_READING_RESOLUTION]);
return new FeatureResponse(response);
}
public FeatureResponse Set(RegisterSettings request)
{
var response = this.MessageHandler.Invoke(request[DEVICE_SPECIFIC, SET_REGISTER_SETTINGS]);
return new FeatureResponse(response);
}
public FeatureResponse Set(SystemTime request = null)
{
if (request is null)
{
request = new SystemTime();
}
var response = this.MessageHandler.Invoke(request[DEVICE_SPECIFIC, SET_SYSTEM_TIME]);
return new FeatureResponse(response);
}
public FeatureResponse Set(params byte[] parameters)
{
var response = this.MessageHandler.Invoke(parameters);
return new FeatureResponse(response);
}
public FeatureResponse Unseal()
{
// TODO: Implement factory unseal.
// 1. ((View Seconds Active) >> 8) &0xFFFF
// 2. CRC16_iPERL(View Factory ID),
// 3. ViewCustomerText.Substring(0, 4).Sum(x => x.Count(x => x == 1))
// 4. ViewReadingPreset.Substring(3, 4)
// - The ASCII values in hex for those characters are 0x33 34 35 36 which when reinterpreted as a uint32BE represents
// - the value 859059510 in decimal. That, mod 11 is 1, so VVVV is 0x0001.
// - When the input data is a string, and does not contain the number of characters required for this algorithm,
// - the value used is the input data written over the value “Factory”.
// - For example, if View Customer Text returns the string “Boo”, the value used in the CCCC step would be “Bootory.”
// - Note that due to the reliance on time and reading, the hash may become invalidated before you are able
// - to send the unseal command.Because of this, if the register responds with Unable to execute,
// - you should recalculate the hash which should last for a longer period if SSSS is large / close to rolling over.
// - Also, if the 7th digit rolls over during unsealing, the hash will not be valid.
// - Current system time is written into manufacturing date/ time if the manufacturing date/ time has not been set
// - when factory seal is set.
return new FeatureResponse();
}
}
}