Пример #1
0
        public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData)
        {
            Console.WriteLine("POST request: {0}", p.http_url);
            string data = inputData.ReadToEnd();

            p.writeSuccess();
            p.outputStream.WriteLine("<html><body><h1>test server</h1>");
            p.outputStream.WriteLine("<a href=/test>return</a><p>");
            p.outputStream.WriteLine("postbody: <pre>{0}</pre>", data);
        }
Пример #2
0
 public override void handleGETRequest(HttpProcessor p)
 {
     instance.log("HTTP请求:" + p.http_url);
     try {
         var url = p.http_url.Substring(1);
         if (p.http_url.StartsWith("/doPWM"))
         {
             var a = p.http_url.Split('?')[1];
             var node = a.Split('&')[0];
             var value = a.Split('&')[1];
             foreach (var act in instance.actuators)
             {
                 if (act.node_name == node)
                 {
                     if (act.id == "door") { instance.doorlock = false; instance.dooraction = DateTime.Now; }
                     instance.control(act, value);
                     break;
                 }
             }
             url = "ok.html";
         }
         if (p.http_url.StartsWith("/doSwitch"))
         {
             var a = p.http_url.Split('?')[1];
             var node = a.Split('&')[0];
             var value = (int.Parse(node.Substring(node.Length - 1))+1).ToString();
             node = node.Substring(0,node.Length - 1);
             var sign = a.Split('&')[1];
             if (sign == "true") value = "-" + value;
             foreach (var act in instance.actuators)
             {
                 if (act.node_name == node)
                 {
                     instance.control(act, value);
                     break;
                 }
             }
             url = "ok.html";
         }
         if (p.http_url.StartsWith("/initApp"))
         {
             using (var sw = new StreamWriter("init.json"))
             {
                 var firstItem = true;
                 sw.WriteLine("{\"sensors\":[");
                 foreach (var sensor in instance.sensors)
                 {
                     if (!firstItem) sw.Write(",");
                     firstItem = false;
                     sw.WriteLine("{\"type\":\"" + sensor.type + "\",\"name\":\"" + sensor.name + "\"}");
                 }
                 firstItem = true;
                 sw.WriteLine("],\"actuators\":[");
                 foreach (var actuator in instance.actuators)
                 {
                     if (!firstItem) sw.Write(",");
                     firstItem = false;
                     if (actuator.actuator_name == "PWM")
                         sw.WriteLine("{\"type\":\"" + actuator.actuator_name + "\",\"name\":\"" + actuator.display_name + "\",\"node\":\"" + actuator.node_name + "\",\"min\":\"" + actuator.min + "\",\"max\":\"" + actuator.max + "\",\"def\":\"" + actuator.def + "\"}");
                     else if (actuator.actuator_name == "switch")
                     {
                         var arr = "";
                         var first = true;
                         for(int i=0;i<actuator.pic.Length;i++)
                         {
                             if (!first) arr += ",";
                             first = false;
                             arr += "{\"pic\":\"" + actuator.pic[i] + "\",\"dname\":\"" + actuator.dname[i] + "\"}";
                         }
                         sw.WriteLine("{\"type\":\"" + actuator.actuator_name + "\",\"name\":\"" + actuator.display_name + "\",\"node\":\"" + actuator.node_name + "\",\"arr\":[" + arr + "]}");
                     }
                 }
                 sw.WriteLine("]}");
             }
             url = "init.json";
         }
         if (p.http_url.StartsWith("/getSensor"))
         {
             using (var sw = new StreamWriter("sensor.json"))
             {
                 var firstItem = true;
                 sw.WriteLine("{\"sensors\":[");
                 foreach (var sensor in instance.sensors)
                 {
                     if (!firstItem) sw.Write(",");
                     firstItem = false;
                     sw.WriteLine("{\"value\":\"" + (sensor.connected && sensor.status ? sensor.getValue() : "未连接") + "\"}");
                 }
                 sw.WriteLine("]}");
             }
             url = "sensor.json";
         }
         if (p.http_url.StartsWith("/imageHD"))
         {
             instance.captureImage(640, 480);
             url = "out.jpg";
         }
         if (p.http_url.StartsWith("/imageSD"))
         {
             instance.captureImage(240, 180);
             url = "out.jpg";
         }
         if (url == "") url = "index.html";
         if (File.Exists(url))
         {
             p.writeSuccess(getContentType(url));
             Stream fs = File.Open(url, FileMode.Open);
             fs.CopyTo(p.outputStream.BaseStream);
             p.outputStream.BaseStream.Flush();
             fs.Close();
         }
         else
         {
             p.writeFailure();
         }
     }catch(Exception e) { instance.log(e.ToString()); }
 }
Пример #3
0
 public abstract void handlePOSTRequest(HttpProcessor p, StreamReader inputData);
Пример #4
0
 public void listen()
 {
     listener = new TcpListener(port);
     listener.Start();
     while (is_active)
     {
         try {
         TcpClient s = listener.AcceptTcpClient();
         HttpProcessor processor = new HttpProcessor(s, this, instance);
         Thread thread = new Thread(new ThreadStart(processor.process));
         thread.Start();
         Thread.Sleep(1);
         }
         catch (Exception e) { instance.log(e.ToString()); }
     }
 }
Пример #5
0
 public abstract void handleGETRequest(HttpProcessor p);