Beispiel #1
0
 public override Task <RunningParameter> UpdateRunningParameter(
     UpdateRunningParameterRequest request,
     ServerCallContext context)
 {
     return(this.Invoke(
                (client, request, deadline) => client.UpdateRunningParameterAsync(request, deadline),
                request.DeviceId,
                request,
                context));
 }
Beispiel #2
0
        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];
            for (int i = 0; i < 0x18; i++)
            {
                bytes[i] = 0xFF;
            }

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

                    break;

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

                    break;

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

                    break;

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

                    break;

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

                    break;

                case "water_pump_frequency_hertz":
                    if (!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(),
            });
        }
Beispiel #3
0
        public override AsyncUnaryCall <RunningParameter> UpdateRunningParameterAsync(UpdateRunningParameterRequest request, Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default)
        {
            RunningParameter runningParameter = RunningParameters[request.DeviceId];

            if (request.UpdateMask == null)
            {
                runningParameter.MergeFrom(request.RunningParameter);
            }
            else
            {
                request.UpdateMask.Merge(request.RunningParameter, runningParameter);
            }

            return(TestCalls.AsyncUnaryCall(
                       Task.FromResult(runningParameter),
                       Task.FromResult(new Metadata()),
                       () => Status.DefaultSuccess,
                       () => new Metadata(),
                       () => { }));
        }