forked from aspnet/AzureSignalR-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlightControl.cs
100 lines (79 loc) · 2.69 KB
/
FlightControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
using System.Timers;
using Interlocked = System.Threading.Interlocked;
namespace Microsoft.Azure.SignalR.Samples.FlightMap
{
class FlightRecord
{
public string Icao { get; set; }
public long PosTime { get; set; }
public double Lat { get; set; }
public double Long { get; set; }
}
public class FlightControl : IFlightControl
{
private const int DefaultInterval = 1000;
private readonly Timer timer;
private int index = 0;
private int totalVisitors = 0;
private int speed = 1;
private FlightRecord[][] flightData;
private IHubContext<FlightMapSampleHub> context;
public FlightControl(IHubContext<FlightMapSampleHub> context, IConfiguration configuration)
{
this.context = context;
var dataUrl = configuration["DataFileUrl"];
string data;
if (File.Exists(dataUrl)) data = File.ReadAllText(dataUrl);
else
{
var client = new HttpClient();
data = client.GetStringAsync(dataUrl).GetAwaiter().GetResult();
}
flightData = JsonConvert.DeserializeObject<FlightRecord[][]>(data);
timer = new Timer(DefaultInterval / speed);
timer.Elapsed += BroadcastFlights;
Start();
}
public int VisitorJoin()
{
return Interlocked.Increment(ref totalVisitors);
}
public int VisitorLeave()
{
return Interlocked.Decrement(ref totalVisitors);
}
public void Start()
{
timer.Start();
}
public void Restart()
{
index = 0;
}
public void Stop()
{
timer.Stop();
}
public void SetSpeed(int speed)
{
this.speed = speed;
timer.Interval = DefaultInterval / speed;
}
public void BroadcastFlights(Object source, ElapsedEventArgs e)
{
var curr = flightData[index];
var currTime = curr[0].PosTime;
long serverTimestamp = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;
context.Clients.All.SendAsync("updateAircraft", DefaultInterval / speed, curr, index, serverTimestamp, currTime);
index = (index + 1) % flightData.Length;
}
}
}