Ejemplo n.º 1
0
 public override Task <Switch> UpdateSwitch(UpdateSwitchRequest request, ServerCallContext context)
 {
     return(this.Invoke(
                (client, request, deadline) => client.UpdateSwitchAsync(request, deadline),
                request.DeviceId,
                request,
                context));
 }
Ejemplo n.º 2
0
        public override AsyncUnaryCall <Switch> UpdateSwitchAsync(UpdateSwitchRequest request, Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default)
        {
            Switch switchInfo = Switches[request.DeviceId] ?? new Switch();

            if (request.UpdateMask == null)
            {
                switchInfo.MergeFrom(request.Switch);
            }
            else
            {
                request.UpdateMask.Merge(request.Switch, switchInfo);
            }

            Switches[request.DeviceId] = switchInfo;

            return(TestCalls.AsyncUnaryCall(
                       Task.FromResult(switchInfo.Clone()),
                       Task.FromResult(new Metadata()),
                       () => Status.DefaultSuccess,
                       () => new Metadata(),
                       () => { }));
        }
Ejemplo n.º 3
0
        public async Task <Switch> UpdateSwitchAsync(UpdateSwitchRequest request, DateTime?deadline)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.UpdateMask.Paths.Count != 1)
            {
                throw new ArgumentException("Update not exact 1 field", nameof(request));
            }

            byte[] bytes = new byte[0x07];
            switch (request.UpdateMask.Paths.Single())
            {
            case "device_power_on":
                bytes[0] = (byte)(0x10 | (request.Switch.DevicePowerOn ? 1 : 0));
                break;

            case "exhauster_power_on":
                bytes[1] = (byte)(0x10 | (request.Switch.ExhausterPowerOn ? 1 : 0));
                break;

            case "heater_auto_on":
                bytes[2] = (byte)(0x10 | (request.Switch.HeaterAutoOn ? 1 : 0));
                break;

            case "heater_power_on":
                bytes[3] = (byte)(0x10 | (request.Switch.HeaterPowerOn ? 1 : 0));
                break;

            case "heater_fan_on":
                bytes[4] = (byte)(0x10 | (request.Switch.HeaterFanOn ? 1 : 0));
                break;

            case "heater_compressor_on":
                bytes[5] = (byte)(0x10 | (request.Switch.HeaterCompressorOn ? 1 : 0));
                break;

            case "heater_four_way_reversing_on":
                bytes[6] = (byte)(0x10 | (request.Switch.HeaterFourWayReversingOn ? 1 : 0));
                break;

            default:
                throw new InvalidDataException("Unrecognized update mask " + request.UpdateMask.Paths.Single());
            }

            PlcFrame response = await this.InvokeAsync(
                PlcFrame.Create(PlcMessageType.UpdateSwitchRequest, ByteString.CopyFrom(bytes)),
                deadline)
                                .ConfigureAwait(false);

            if (response.FrameHeader.MessageType != PlcMessageType.GetSwitchResponse)
            {
                throw new InvalidDataException(
                          "Response message type mismatch: " + response.FrameHeader.MessageType);
            }

            using var reader = new BinaryReader(new MemoryStream(response.FrameBody.ToByteArray()));
            return(new Switch
            {
                DevicePowerOn = reader.ReadByte() != 0,
                ExhausterPowerOn = reader.ReadByte() != 0,
                HeaterAutoOn = reader.ReadByte() != 0,
                HeaterPowerOn = reader.ReadByte() != 0,
                HeaterFanOn = reader.ReadByte() != 0,
                HeaterCompressorOn = reader.ReadByte() != 0,
                HeaterFourWayReversingOn = reader.ReadByte() != 0,
            });
        }