117 lines
3.3 KiB
C#
117 lines
3.3 KiB
C#
using System;
|
|
using System.Web;
|
|
using System.Net;
|
|
using System.Threading;
|
|
using System.Collections.Generic;
|
|
|
|
namespace DMX2
|
|
{
|
|
public class WebServer : IDisposable
|
|
{
|
|
HttpListener listner;
|
|
|
|
Thread loopthread=null;
|
|
|
|
string page;
|
|
List<string> resources;
|
|
|
|
public WebServer ()
|
|
{
|
|
using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("page.html"))
|
|
using (System.IO.TextReader reader = new System.IO.StreamReader(stream))
|
|
page = reader.ReadToEnd();
|
|
|
|
resources = new List<string>(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames());
|
|
|
|
loopthread = new Thread(new ThreadStart(MainLoop));
|
|
loopthread.Start();
|
|
}
|
|
|
|
void MainLoop ()
|
|
{
|
|
listner = new HttpListener ();
|
|
listner.Prefixes.Add ("http://*:8089/");
|
|
listner.Start ();
|
|
HttpListenerContext context;
|
|
|
|
try {
|
|
while (null!=(context = listner.GetContext())) {
|
|
|
|
Console.WriteLine (context.Request.RawUrl);
|
|
|
|
string responseString = GetResponse (context);
|
|
byte[] buffer = System.Text.Encoding.UTF8.GetBytes (responseString);
|
|
context.Response.ContentLength64 = buffer.Length;
|
|
context.Response.OutputStream.Write (buffer, 0, buffer.Length);
|
|
context.Response.OutputStream.Close ();
|
|
}
|
|
} catch {
|
|
}
|
|
|
|
}
|
|
|
|
string GetResponse (HttpListenerContext ctx)
|
|
{
|
|
HttpListenerRequest req = ctx.Request;
|
|
if (Conduite.Courante == null)
|
|
return "<html><body>Pas de conduite</body></html>";
|
|
if (req.Url.LocalPath.StartsWith ("/res/")) {
|
|
string res = req.Url.LocalPath.Remove(0,5).Replace('/','.');
|
|
Console.WriteLine( res);
|
|
if(res.EndsWith(".js"))
|
|
ctx.Response.ContentType= "application/javascript";
|
|
if(resources.Contains(res))
|
|
using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(res))
|
|
using (System.IO.TextReader reader = new System.IO.StreamReader(stream))
|
|
return reader.ReadToEnd();
|
|
else return "";
|
|
}
|
|
switch (req.QueryString["id"]) {
|
|
default:
|
|
|
|
string univlist = ""; int i = 0;
|
|
foreach (UniversDMX univ in Conduite.Courante.Patches)
|
|
univlist = univlist + string.Format("<option value=\"{0}\">{1}</option>",i++,univ.Nom);
|
|
|
|
|
|
|
|
string data = page.Replace("<UNIVLIST>",univlist);
|
|
return data;
|
|
case "2" :
|
|
int univ, dimmer, val;
|
|
if(!int.TryParse(req.QueryString["univ"],out univ)) return "NOK";
|
|
if(!int.TryParse(req.QueryString["dimmer"],out dimmer)) return "NOK";
|
|
if(!int.TryParse(req.QueryString["val"],out val)) return "NOK";
|
|
if(univ<0 || univ>= Conduite.Courante.Patches.Count) return "NOK";
|
|
if(dimmer<=0 || dimmer > 512) return "NOK";
|
|
if(val <0 || val > 255) return "NOK";
|
|
Conduite.Courante.Patches[univ].AllumageForce = dimmer;
|
|
Conduite.Courante.Patches[univ].AllumageForceVal = val;
|
|
return "OK";
|
|
|
|
case "3":
|
|
int circuit, val;
|
|
if(!int.TryParse(req.QueryString["circuit"],out circuit)) return "NOK";
|
|
if(!int.TryParse(req.QueryString["val"],out val)) return "NOK";
|
|
if(circuit<0 || circuit>= Conduite.Courante.Circuits.Count) return "NOK";
|
|
if(val <0 || val > 255) return "NOK";
|
|
|
|
return "OK";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#region IDisposable implementation
|
|
public void Dispose ()
|
|
{
|
|
listner.Close();
|
|
loopthread.Abort();
|
|
loopthread.Join();
|
|
}
|
|
#endregion
|
|
|
|
|
|
}
|
|
}
|
|
|