-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRendererController.cs
176 lines (161 loc) · 7.38 KB
/
RendererController.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using LightCAD.Model.Entities.Playing;
using LightCAD.Model.Interfaces;
using LightCAD.Model.LT;
using LightCAD.Model.Tools;
using LightControlService.Controllers.Logging;
using LightControlService.Hubs;
using LightControlService.Models.RequestModels;
using Microsoft.AspNetCore.Mvc;
using System.Drawing;
namespace LightControlService.Controllers
{
public class RendererController : Controller
{
private bool startStopAnimation;
private readonly IScenarioPlayer _scenarioPlayer;
private readonly ScenarioManager _scenarioManager;
private ProjectChanger _pchanger;
private LCHub _lcHub;
private readonly IScheduleAnalyzer _schedulerAnalyzer;
public RendererController(IScenarioPlayer scenarioPlayer, ScenarioManager scenarioManager, ProjectChanger pchanger, LCHub lcHub, IScheduleAnalyzer schedulerAnalyzer)
{
startStopAnimation = true;
_scenarioPlayer = scenarioPlayer;
_scenarioPlayer.FrameChanged += OnFrameChanged;
_scenarioManager = scenarioManager;
_pchanger = pchanger;
_lcHub = lcHub;
_schedulerAnalyzer = schedulerAnalyzer;
}
[HttpPost]
public void PlayScenario([FromBody] ScenarioNameId snd)
{
var scenario = _scenarioManager.GetPrimitives<Scenario>().ToList().First(w => w.Id == snd.ScenarioId && w.Name == snd.ScenarioName);
if (_scenarioPlayer.InitializeItemId == 0 || _scenarioPlayer.InitializeItemId != scenario.Id)
_scenarioPlayer.Initialize(scenario);
if (snd.ElapsedTicks != 0)
_scenarioPlayer.Rewind((long)snd.ElapsedTicks * 1000);
_scenarioPlayer.Start();
}
private async void OnFrameChanged(object sender, EventArgs e)
{
if (startStopAnimation)
{
var frame = _scenarioPlayer.Frame;
var scenario = new WebScenario();
scenario.lst = new List<WebScenarios>();
scenario.TotalTicks = _scenarioPlayer.PlayItem.TotalTicks;
scenario.ElapsedTicks = (float)Math.Round((float)_scenarioPlayer.ElapsedTicks / 1000, 2);
foreach (var item in frame.GetDictionary())
{
Color frameColor = Color.FromArgb(item.Value[0].Red, item.Value[0].Green, item.Value[0].Blue);
string hexColor = string.Format("#{0:X2}{1:X2}{2:X2}", frameColor.R, frameColor.G, frameColor.B);
scenario.lst.Add(new WebScenarios() { LampId = item.Key, Color = hexColor });//, Color=item.Value });
}
//var sendingFrame = JsonConvert.SerializeObject();
try
{
await _lcHub.NewFrame(scenario);
}
catch (Exception ex)
{
LogManager.GetInstance().ApplicationException(ex.ToString());
}
}
}
[HttpPost]
public async void PauseScenario([FromBody] ScenarioNameId snd)
{
var frame = _scenarioPlayer.Frame;
var scenarioToFront = new WebScenario();
scenarioToFront.TotalTicks = (float)Math.Round((float)_scenarioPlayer.PlayItem.TotalTicks / 1000, 2);
scenarioToFront.ElapsedTicks = (float)Math.Round((float)_scenarioPlayer.ElapsedTicks / 1000, 2);
var scenario = _scenarioManager.GetPrimitives<Scenario>().ToList().First(w => w.Id == snd.ScenarioId && w.Name == snd.ScenarioName);
_scenarioPlayer.Initialize(scenario);
_scenarioPlayer.Stop();
scenarioToFront.lst = new List<WebScenarios>();
foreach (var item in frame.GetDictionary())
{
Color frameColor = Color.FromArgb(item.Value[0].Red, item.Value[0].Green, item.Value[0].Blue);
string hexColor = $"#{frameColor.R:X2}{frameColor.G:X2}{frameColor.B:X2}";
scenarioToFront.lst.Add(new WebScenarios() { LampId = item.Key, Color = hexColor });//, Color=item.Value });
}
try
{
await Task.Run(() => _lcHub.NewFrame(scenarioToFront));
}
catch (Exception ex)
{
LogManager.GetInstance().ApplicationException(ex.ToString());
}
}
[HttpPost]
public async void StopScenario([FromBody] ScenarioNameId snd)
{
var frame = _scenarioPlayer.Frame;
var scenarioToFront = new WebScenario();
scenarioToFront.TotalTicks = (float)Math.Round((float)_scenarioPlayer.PlayItem.TotalTicks / 1000, 2);
scenarioToFront.ElapsedTicks = (float)Math.Round((float)_scenarioPlayer.ElapsedTicks / 1000, 2);
var scenario = _scenarioManager.GetPrimitives<Scenario>().ToList().First(w => w.Id == snd.ScenarioId && w.Name == snd.ScenarioName);
_scenarioPlayer.Initialize(scenario);
_scenarioPlayer.Stop();
scenarioToFront.lst = new List<WebScenarios>();
foreach (var item in frame.GetDictionary())
{
Color frameColor = Color.Black;
string hexColor = $"#{frameColor.R:X2}{frameColor.G:X2}{frameColor.B:X2}";
scenarioToFront.lst.Add(new WebScenarios() { LampId = item.Key, Color = hexColor });//, Color=item.Value });
}
try
{
await Task.Run(() => _lcHub.NewFrame(scenarioToFront));
_scenarioPlayer.SwitchOffLamps();
}
catch (Exception ex)
{
LogManager.GetInstance().ApplicationException(ex.ToString());
}
}
[HttpPost]
public void StartStopAnimation([FromBody] StartStopScheduler s3)
{
_scenarioPlayer.Stop();
startStopAnimation = s3.action;
_scenarioPlayer.Start();
}
[HttpGet]
public JsonResult GetCurrentScenario() {
var resultScenario = new ScenarioNameId();
resultScenario.IsPlaying = _scenarioPlayer.IsPlay;
if (_schedulerAnalyzer.CurrentScheduleTask != null)
{
resultScenario.ScenarioId = _schedulerAnalyzer.CurrentScheduleTask.ScheduleItem.Scenario.Id;
resultScenario.ScenarioName = _schedulerAnalyzer.CurrentScheduleTask.ScheduleItem.Scenario.Name;
resultScenario.TotalTicks = _schedulerAnalyzer.CurrentScheduleTask.ScheduleItem.Scenario.TotalTicks;
resultScenario.ElapsedTicks = (float)Math.Round((float)_scenarioPlayer.ElapsedTicks / 1000, 2);
}
else
{
resultScenario = _pchanger.CurrentProject.Scenarios[0];
}
return Json(resultScenario);
}
[HttpPost]
public void RewindScenario([FromBody]RewindWebScenario rws)
{
try
{
if (rws.tick != null)
{
var scenario = _scenarioManager.GetPrimitives<Scenario>().ToList().First(w => w.Id == rws.scenarioId);
_scenarioPlayer.Initialize(scenario);
_scenarioPlayer.Rewind((long)rws.tick * 1000);
}
}
catch(Exception ex)
{
LogManager.GetInstance().ApplicationException(ex.ToString());
}
}
}
}