Skip to content

Commit

Permalink
Tested UpdateRunningParameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoona committed Dec 13, 2019
1 parent 30bbdfc commit c339e46
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public FakePlc(byte[] id)
WaterPumpWorkingMode = WaterPumpWorkingMode.FixedFlowRate,
};

public RunningParameter RunningParameter { get; } = new RunningParameter
{
SummerHeaterCelsiusDegree = 11F,
WinterHeaterCelsiusDegree = 13F,
ColdPowerKilowatt = 17F,
WarmPowerKilowatt = 19F,
WaterPumpFlowRateCubicMeterPerHour = 23F,
WaterPumpFrequencyHertz = 29F,
};

public void Dispose()
{
this.Dispose(true);
Expand Down Expand Up @@ -151,6 +161,13 @@ private void BackgroundTaskEntryPoint()
this.UpdateWorkingMode(content);
responseFrame = this.CreateGetWorkingModeResponseFrame();
break;
case PlcMessageType.GetRunningParameterRequest:
responseFrame = this.CreateGetRunningParameterResponseFrame();
break;
case PlcMessageType.UpdateRunningParameterRequest:
this.UpdateRunningParameter(content);
responseFrame = this.CreateGetRunningParameterResponseFrame();
break;
default:
responseFrame = null;
break;
Expand Down Expand Up @@ -223,6 +240,16 @@ private void UpdateWorkingMode(byte[] content)
}
}

private void UpdateRunningParameter(byte[] content)
{
this.RunningParameter.SummerHeaterCelsiusDegree = BitConverter.ToSingle(content.AsSpan(0, 4));
this.RunningParameter.WinterHeaterCelsiusDegree = BitConverter.ToSingle(content.AsSpan(4, 4));
this.RunningParameter.ColdPowerKilowatt = BitConverter.ToSingle(content.AsSpan(8, 4));
this.RunningParameter.WarmPowerKilowatt = BitConverter.ToSingle(content.AsSpan(12, 4));
this.RunningParameter.WaterPumpFlowRateCubicMeterPerHour = BitConverter.ToSingle(content.AsSpan(16, 4));
this.RunningParameter.WaterPumpFrequencyHertz = BitConverter.ToSingle(content.AsSpan(20, 4));
}

private PlcFrame CreateGetMetricResponse()
{
byte[] responseContent = new byte[0x20];
Expand Down Expand Up @@ -270,5 +297,23 @@ private PlcFrame CreateGetWorkingModeResponseFrame()
PlcMessageType.GetWorkingModeResponse,
ByteString.CopyFrom(responseContent));
}

private PlcFrame CreateGetRunningParameterResponseFrame()
{
byte[] responseContent = new byte[0x18];
using (var writer = new BinaryWriter(new MemoryStream(responseContent)))
{
writer.Write(this.RunningParameter.SummerHeaterCelsiusDegree);
writer.Write(this.RunningParameter.WinterHeaterCelsiusDegree);
writer.Write(this.RunningParameter.ColdPowerKilowatt);
writer.Write(this.RunningParameter.WarmPowerKilowatt);
writer.Write(this.RunningParameter.WaterPumpFlowRateCubicMeterPerHour);
writer.Write(this.RunningParameter.WaterPumpFrequencyHertz);
}

return PlcFrame.Create(
PlcMessageType.GetRunningParameterResponse,
ByteString.CopyFrom(responseContent));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// </copyright>

using System;
using System.Buffers.Binary;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -236,6 +237,138 @@ public async Task<WorkingMode> UpdateWorkingModeAsync(UpdateWorkingModeRequest r
};
}

public async Task<RunningParameter> GetRunningParameterAsync(
GetRunningParameterRequest request,
DateTime? deadline)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}

PlcFrame response = await this.InvokeAsync(
PlcFrame.Create(PlcMessageType.GetRunningParameterRequest, ByteString.Empty),
deadline)
.ConfigureAwait(false);
if (response.FrameHeader.MessageType != PlcMessageType.GetRunningParameterResponse)
{
throw new InvalidDataException(
"Response message type mismatch: " + response.FrameHeader.MessageType);
}

using var reader = new BinaryReader(new MemoryStream(response.FrameBody.ToByteArray()));
return new RunningParameter
{
SummerHeaterCelsiusDegree = reader.ReadSingle(),
WinterHeaterCelsiusDegree = reader.ReadSingle(),
ColdPowerKilowatt = reader.ReadSingle(),
WarmPowerKilowatt = reader.ReadSingle(),
WaterPumpFlowRateCubicMeterPerHour = reader.ReadSingle(),
WaterPumpFrequencyHertz = reader.ReadSingle(),
};
}

public async Task<RunningParameter> UpdateRunningParameterAsync(
UpdateRunningParameterRequest request,
DateTime? deadline)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}

if (request.UpdateMask == null)
{
request.UpdateMask = FieldMask.FromFieldNumbers<RunningParameter>(
RunningParameter.Descriptor.Fields.InFieldNumberOrder().Select(f => f.FieldNumber));
}

byte[] bytes = new byte[0x18];
foreach (string path in request.UpdateMask.Paths)
{
switch (path)
{
case "summer_heater_celsius_degree":
if (!BitConverter.TryWriteBytes(
bytes.AsSpan(0, 4),
request.RunningParameter.SummerHeaterCelsiusDegree))
{
throw new InvalidOperationException();
}

break;
case "winter_heater_celsius_degree":
if (!BitConverter.TryWriteBytes(
bytes.AsSpan(4, 4),
request.RunningParameter.WinterHeaterCelsiusDegree))
{
throw new InvalidOperationException();
}

break;
case "cold_power_kilowatt":
if (!BitConverter.TryWriteBytes(
bytes.AsSpan(8, 4),
request.RunningParameter.ColdPowerKilowatt))
{
throw new InvalidOperationException();
}

break;
case "warm_power_kilowatt":
if (!BitConverter.TryWriteBytes(
bytes.AsSpan(12, 4),
request.RunningParameter.WarmPowerKilowatt))
{
throw new InvalidOperationException();
}

break;
case "water_pump_flow_rate_cubic_meter_per_hour":
if (!BitConverter.TryWriteBytes(
bytes.AsSpan(16, 4),
request.RunningParameter.WaterPumpFlowRateCubicMeterPerHour))
{
throw new InvalidOperationException();
}

break;
case "water_pump_frequency_hertz":
if (!BitConverter.TryWriteBytes(
bytes.AsSpan(20, 4),
request.RunningParameter.WaterPumpFrequencyHertz))
{
throw new InvalidOperationException();
}

break;
default:
throw new InvalidDataException("Unrecognized update mask " + path);
}
}

PlcFrame response = await this.InvokeAsync(
PlcFrame.Create(PlcMessageType.UpdateRunningParameterRequest, ByteString.CopyFrom(bytes)),
deadline)
.ConfigureAwait(false);
if (response.FrameHeader.MessageType != PlcMessageType.GetRunningParameterResponse)
{
throw new InvalidDataException(
"Response message type mismatch: " + response.FrameHeader.MessageType);
}

using var reader = new BinaryReader(new MemoryStream(response.FrameBody.ToByteArray()));
return new RunningParameter
{
SummerHeaterCelsiusDegree = reader.ReadSingle(),
WinterHeaterCelsiusDegree = reader.ReadSingle(),
ColdPowerKilowatt = reader.ReadSingle(),
WarmPowerKilowatt = reader.ReadSingle(),
WaterPumpFlowRateCubicMeterPerHour = reader.ReadSingle(),
WaterPumpFrequencyHertz = reader.ReadSingle(),
};
}

private Task<PlcFrame> InvokeAsync(PlcFrame request, DateTime? deadline)
{
if (this.closingCancellationTokenSource.IsCancellationRequested)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ public override Task<WorkingMode> GetWorkingMode(GetWorkingModeRequest request,
context);
}

public override Task<WorkingMode> UpdateWorkingMode(UpdateWorkingModeRequest request, ServerCallContext context)
public override Task<WorkingMode> UpdateWorkingMode(
UpdateWorkingModeRequest request,
ServerCallContext context)
{
return this.Invoke(
(client, request, deadline) => client.UpdateWorkingModeAsync(request, deadline),
Expand All @@ -214,6 +216,28 @@ public override Task<WorkingMode> UpdateWorkingMode(UpdateWorkingModeRequest req
context);
}

public override Task<RunningParameter> GetRunningParameter(
GetRunningParameterRequest request,
ServerCallContext context)
{
return this.Invoke(
(client, request, deadline) => client.GetRunningParameterAsync(request, deadline),
request.DeviceId,
request,
context);
}

public override Task<RunningParameter> UpdateRunningParameter(
UpdateRunningParameterRequest request,
ServerCallContext context)
{
return this.Invoke(
(client, request, deadline) => client.UpdateRunningParameterAsync(request, deadline),
request.DeviceId,
request,
context);
}

protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using System.Threading.Tasks;
using System.Windows;
using GeothermalResearchInstitute.v2;
using GeothermalResearchInstitute.Wpf.Common;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Prism.Commands;
using Prism.Mvvm;

Expand Down Expand Up @@ -233,47 +235,61 @@ public WaterPumpWorkingMode UpdatingWaterPumpWorkingMode

public async Task LoadAsync()
{
this.CurrentRunningParameter = await this.client.GetRunningParameterAsync(
new GetRunningParameterRequest
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
},
deadline: DateTime.UtcNow.AddMilliseconds(500));
this.UpdatingRunningParameter = this.CurrentRunningParameter.Clone();

this.CurrentWorkingMode = await this.client.GetWorkingModeAsync(
new GetWorkingModeRequest
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
},
deadline: DateTime.UtcNow.AddSeconds(5));
this.UpdatingWorkingMode = this.CurrentWorkingMode.Clone();
try
{
this.CurrentRunningParameter = await this.client.GetRunningParameterAsync(
new GetRunningParameterRequest
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
},
deadline: DateTime.UtcNow.AddMilliseconds(1500));
this.UpdatingRunningParameter = this.CurrentRunningParameter.Clone();

this.CurrentWorkingMode = await this.client.GetWorkingModeAsync(
new GetWorkingModeRequest
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
},
deadline: DateTime.UtcNow.AddSeconds(5));
this.UpdatingWorkingMode = this.CurrentWorkingMode.Clone();
}
catch (RpcException e)
{
e.ShowMessageBox();
}
}

private async void ExecuteUpdateCommand()
{
this.CurrentRunningParameter = await this.client.UpdateRunningParameterAsync(
new UpdateRunningParameterRequest
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
RunningParameter = this.UpdatingRunningParameter,
},
deadline: DateTime.UtcNow.AddMilliseconds(500));
this.UpdatingRunningParameter = this.CurrentRunningParameter.Clone();

this.CurrentWorkingMode = await this.client.UpdateWorkingModeAsync(
new UpdateWorkingModeRequest
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
WorkingMode = this.UpdatingWorkingMode,
UpdateMask = FieldMask.FromStringEnumerable<WorkingMode>(new[]
try
{
this.CurrentRunningParameter = await this.client.UpdateRunningParameterAsync(
new UpdateRunningParameterRequest
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
RunningParameter = this.UpdatingRunningParameter,
},
deadline: DateTime.UtcNow.AddMilliseconds(5000));
this.UpdatingRunningParameter = this.CurrentRunningParameter.Clone();

this.CurrentWorkingMode = await this.client.UpdateWorkingModeAsync(
new UpdateWorkingModeRequest
{
"device_flow_rate_control_mode",
"water_pump_working_mode",
}),
},
deadline: DateTime.UtcNow.AddSeconds(5));
this.UpdatingWorkingMode = this.CurrentWorkingMode.Clone();
DeviceId = this.ViewModelContext.SelectedDevice.Id,
WorkingMode = this.UpdatingWorkingMode,
UpdateMask = FieldMask.FromStringEnumerable<WorkingMode>(new[]
{
"device_flow_rate_control_mode",
"water_pump_working_mode",
}),
},
deadline: DateTime.UtcNow.AddSeconds(5));
this.UpdatingWorkingMode = this.CurrentWorkingMode.Clone();
}
catch (RpcException e)
{
e.ShowMessageBox();
}
}
}
}
Loading

0 comments on commit c339e46

Please sign in to comment.