Skip to content

Commit

Permalink
Add FakeDevicesHostedService.
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoona committed Sep 17, 2019
1 parent 1d7f3d9 commit a3ac377
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// <copyright file="FakeDevicesHostedService.cs" company="Shuai Zhang">
// Copyright Shuai Zhang. All rights reserved.
// Licensed under the GPLv3 license. See LICENSE file in the project root for full license information.
// </copyright>

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using GeothermalResearchInstitute.ServerConsole.Models;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace GeothermalResearchInstitute.ServerConsole
{
[SuppressMessage("Microsoft.Performance", "CA1812", Justification = "Instantiated with reflection.")]
internal class FakeDevicesHostedService : IHostedService, IDisposable
{
private readonly ICollection<DeviceOptionsEntry> devices;
private readonly IServiceProvider serviceProvider;
private Timer timer = null;
private bool disposedValue = false;

public FakeDevicesHostedService(
IOptions<DeviceOptions> deviceOptions,
IServiceProvider serviceProvider)
{
this.devices = deviceOptions.Value.Devices;
this.serviceProvider = serviceProvider;
}

public Task StartAsync(CancellationToken cancellationToken)
{
this.timer = new Timer(
this.HearbeatEntryPoint,
null,
0,
(long)TimeSpan.FromSeconds(1).TotalMilliseconds);
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
this.timer.Dispose();
this.timer = null;
return Task.CompletedTask;
}

public void Dispose()
{
this.Dispose(true);
}

protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
if (this.timer != null)
{
this.timer.Dispose();
this.timer = null;
}
}

this.disposedValue = true;
}
}

[SuppressMessage("样式", "IDE0060:删除未使用的参数", Justification = "Required for callback delegate.")]
private void HearbeatEntryPoint(object state)
{
foreach (var entry in this.devices)
{
// TODO(zhangshuai.ustc): Implement it.
// 1. Get corresponding grpc client.
// 2. Send heartbeat request according to corresponding states.
// 3. Take action according to heatbeat response & record into states.
}

throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using static Google.Protobuf.WellKnownTypes.FieldMask;
using GrpcDevice = GeothermalResearchInstitute.v1.Device;
using GrpcDeviceMetrics = GeothermalResearchInstitute.v1.DeviceMetrics;
using ModelDeviceMetrics = GeothermalResearchInstitute.ServerConsole.Models.DeviceMetrics;

Expand Down Expand Up @@ -61,15 +62,15 @@ public override Task<ListDevicesResponse> ListDevices(ListDevicesRequest request
{
var deviceOptions = this.serviceProvider.GetRequiredService<IOptionsSnapshot<DeviceOptions>>();
var response = new ListDevicesResponse();
response.Devices.Add(deviceOptions.Value.Devices.Select(d => new Device
response.Devices.Add(deviceOptions.Value.Devices.Select(d => new GrpcDevice
{
Id = ByteString.CopyFrom(d.ComputeIdBinary()),
Name = d.Name,
}));
return Task.FromResult(response);
}

public override Task<Device> GetDevice(GetDeviceRequest request, ServerCallContext context)
public override Task<GrpcDevice> GetDevice(GetDeviceRequest request, ServerCallContext context)
{
var deviceOptions = this.serviceProvider.GetRequiredService<IOptionsSnapshot<DeviceOptions>>();
var deviceBasicInformation = deviceOptions.Value.Devices.SingleOrDefault(d => d.ComputeIdBinary().SequenceEqual(request.Id));
Expand All @@ -87,7 +88,7 @@ public override Task<Device> GetDevice(GetDeviceRequest request, ServerCallConte
deviceAdditionalInformation = new DeviceActualStates();
}

var device = new Device
var device = new GrpcDevice
{
Id = request.Id,
};
Expand Down Expand Up @@ -123,7 +124,7 @@ public override Task<Device> GetDevice(GetDeviceRequest request, ServerCallConte
return Task.FromResult(device);
}

public override Task<Device> UpdateDevice(UpdateDeviceRequest request, ServerCallContext context)
public override Task<GrpcDevice> UpdateDevice(UpdateDeviceRequest request, ServerCallContext context)
{
var deviceOptions = this.serviceProvider.GetRequiredService<IOptionsSnapshot<DeviceOptions>>();
var deviceBasicInformation = deviceOptions.Value.Devices.SingleOrDefault(d => d.ComputeIdBinary().SequenceEqual(request.Device.Id));
Expand Down Expand Up @@ -170,7 +171,7 @@ public override Task<Device> UpdateDevice(UpdateDeviceRequest request, ServerCal

this.bjdireContext.SaveChanges();

var device = new Device
var device = new GrpcDevice
{
Id = request.Device.Id,
};
Expand Down Expand Up @@ -247,7 +248,7 @@ public override Task<HeartbeatResponse> Heartbeat(HeartbeatRequest request, Serv
desiredStates = new DeviceDesiredStates();
}

var device = new Device
var device = new GrpcDevice
{
Id = request.Device.Id,
};
Expand Down

0 comments on commit a3ac377

Please sign in to comment.