Skip to content

Commit

Permalink
Tested UpdateWorkingMode.
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoona committed Dec 13, 2019
1 parent 4e45ab8 commit 30bbdfc
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public FakePlc(byte[] id)
HeaterFourWayReversingOn = true,
};

public WorkingMode WorkingMode { get; } = new WorkingMode
{
DeviceWorkingMode = DeviceWorkingMode.TemperatureDetermination,
DeviceFlowRateControlMode = DeviceFlowRateControlMode.VariableFrequency,
WaterPumpWorkingMode = WaterPumpWorkingMode.FixedFlowRate,
};

public void Dispose()
{
this.Dispose(true);
Expand Down Expand Up @@ -137,6 +144,13 @@ private void BackgroundTaskEntryPoint()
this.UpdateSwitch(content);
responseFrame = this.CreateGetSwitchResponseFrame();
break;
case PlcMessageType.GetWorkingModeRequest:
responseFrame = this.CreateGetWorkingModeResponseFrame();
break;
case PlcMessageType.UpdateWorkingModeRequest:
this.UpdateWorkingMode(content);
responseFrame = this.CreateGetWorkingModeResponseFrame();
break;
default:
responseFrame = null;
break;
Expand All @@ -153,26 +167,6 @@ private void BackgroundTaskEntryPoint()
}
}

private PlcFrame CreateGetMetricResponse()
{
byte[] responseContent = new byte[0x20];
using (var writer = new BinaryWriter(new MemoryStream(responseContent)))
{
writer.Write(this.Metric.OutputWaterCelsiusDegree);
writer.Write(this.Metric.InputWaterCelsiusDegree);
writer.Write(this.Metric.HeaterOutputWaterCelsiusDegree);
writer.Write(this.Metric.EnvironmentCelsiusDegree);
writer.Write(this.Metric.OutputWaterPressureMeter);
writer.Write(this.Metric.InputWaterPressureMeter);
writer.Write(this.Metric.HeaterPowerKilowatt);
writer.Write(this.Metric.WaterPumpFlowRateCubicMeterPerHour);
}

return PlcFrame.Create(
PlcMessageType.GetMetricResponse,
ByteString.CopyFrom(responseContent));
}

private void UpdateSwitch(ReadOnlySpan<byte> content)
{
if ((content[0] & 0x10) != 0)
Expand Down Expand Up @@ -211,6 +205,44 @@ private void UpdateSwitch(ReadOnlySpan<byte> content)
}
}

private void UpdateWorkingMode(byte[] content)
{
if (content[0] != 0)
{
this.WorkingMode.DeviceWorkingMode = (DeviceWorkingMode)content[0];
}

if (content[1] != 0)
{
this.WorkingMode.DeviceFlowRateControlMode = (DeviceFlowRateControlMode)content[1];
}

if (content[2] != 0)
{
this.WorkingMode.WaterPumpWorkingMode = (WaterPumpWorkingMode)content[2];
}
}

private PlcFrame CreateGetMetricResponse()
{
byte[] responseContent = new byte[0x20];
using (var writer = new BinaryWriter(new MemoryStream(responseContent)))
{
writer.Write(this.Metric.OutputWaterCelsiusDegree);
writer.Write(this.Metric.InputWaterCelsiusDegree);
writer.Write(this.Metric.HeaterOutputWaterCelsiusDegree);
writer.Write(this.Metric.EnvironmentCelsiusDegree);
writer.Write(this.Metric.OutputWaterPressureMeter);
writer.Write(this.Metric.InputWaterPressureMeter);
writer.Write(this.Metric.HeaterPowerKilowatt);
writer.Write(this.Metric.WaterPumpFlowRateCubicMeterPerHour);
}

return PlcFrame.Create(
PlcMessageType.GetMetricResponse,
ByteString.CopyFrom(responseContent));
}

private PlcFrame CreateGetSwitchResponseFrame()
{
byte[] responseContent = new byte[0x07];
Expand All @@ -226,5 +258,17 @@ private PlcFrame CreateGetSwitchResponseFrame()
PlcMessageType.GetSwitchResponse,
ByteString.CopyFrom(responseContent));
}

private PlcFrame CreateGetWorkingModeResponseFrame()
{
byte[] responseContent = new byte[0x03];
responseContent[0] = (byte)this.WorkingMode.DeviceWorkingMode;
responseContent[1] = (byte)this.WorkingMode.DeviceFlowRateControlMode;
responseContent[2] = (byte)this.WorkingMode.WaterPumpWorkingMode;

return PlcFrame.Create(
PlcMessageType.GetWorkingModeResponse,
ByteString.CopyFrom(responseContent));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,77 @@ public async Task<Switch> UpdateSwitchAsync(UpdateSwitchRequest request, DateTim
};
}

public async Task<WorkingMode> GetWorkingModeAsync(GetWorkingModeRequest request, DateTime? deadline)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}

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

using var reader = new BinaryReader(new MemoryStream(response.FrameBody.ToByteArray()));
return new WorkingMode
{
DeviceWorkingMode = (DeviceWorkingMode)reader.ReadByte(),
DeviceFlowRateControlMode = (DeviceFlowRateControlMode)reader.ReadByte(),
WaterPumpWorkingMode = (WaterPumpWorkingMode)reader.ReadByte(),
};
}

public async Task<WorkingMode> UpdateWorkingModeAsync(UpdateWorkingModeRequest request, DateTime? deadline)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}

byte[] bytes = new byte[0x03];
foreach (string path in request.UpdateMask.Paths)
{
switch (path)
{
case "device_working_mode":
bytes[0] = (byte)request.WorkingMode.DeviceWorkingMode;
break;
case "device_flow_rate_control_mode":
bytes[1] = (byte)request.WorkingMode.DeviceFlowRateControlMode;
break;
case "water_pump_working_mode":
bytes[2] = (byte)request.WorkingMode.WaterPumpWorkingMode;
break;
default:
throw new InvalidDataException("Unrecognized update mask " + path);
}
}

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

using var reader = new BinaryReader(new MemoryStream(response.FrameBody.ToByteArray()));
return new WorkingMode
{
DeviceWorkingMode = (DeviceWorkingMode)reader.ReadByte(),
DeviceFlowRateControlMode = (DeviceFlowRateControlMode)reader.ReadByte(),
WaterPumpWorkingMode = (WaterPumpWorkingMode)reader.ReadByte(),
};
}

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 @@ -196,6 +196,24 @@ orderby m.Timestamp descending
return Task.FromResult(response);
}

public override Task<WorkingMode> GetWorkingMode(GetWorkingModeRequest request, ServerCallContext context)
{
return this.Invoke(
(client, request, deadline) => client.GetWorkingModeAsync(request, deadline),
request.DeviceId,
request,
context);
}

public override Task<WorkingMode> UpdateWorkingMode(UpdateWorkingModeRequest request, ServerCallContext context)
{
return this.Invoke(
(client, request, deadline) => client.UpdateWorkingModeAsync(request, deadline),
request.DeviceId,
request,
context);
}

protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
Expand Down

0 comments on commit 30bbdfc

Please sign in to comment.