Ejemplo n.º 1
0
 internal void Start()
 {
     if (!Active)
     {
         WFLogging.Log("Starting WeatherFlow data collection thread.");
         udp_thread = new Thread(new ThreadStart(WeatherFlowThread));
         udp_thread.IsBackground = true;
         udp_thread.Start();
     }
 }
Ejemplo n.º 2
0
        // This is handling the SetParent service only at this point but could
        // be expanded to handle other ISY WSDL reuests if needed.
        internal void SendWSDLReqeust(string service, string parent, string node)
        {
            string          url = "http://" + WF_Config.ISY + "/services";
            HttpWebRequest  request;
            HttpWebResponse response;
            string          reqString = "";

            reqString  = "<? version=\'1.0\' encoding=\'utf-8\'?>";
            reqString += "<s:Envelope>";
            reqString += "<s:Body>";
            reqString += "<u:" + service + " xmlns:u=\'urn:udi-com:service:X_Insteon_Lighting_Service:1\'>";

            reqString += "<node>" + node + "</node>";
            reqString += "<nodeType>1</nodeType>";
            reqString += "<parent>" + parent + "</parent>";
            reqString += "<parentType>1</parentType>";

            reqString += "</u:" + service + ">";
            reqString += "</s:Body>";
            reqString += "</s:Envelope>";
            reqString += "\r\n";

            request             = (HttpWebRequest)HttpWebRequest.Create(url);
            request.KeepAlive   = true;
            request.Method      = "POST";
            request.ContentType = "text/xml; charset-utf-8";
            request.Headers.Add("Authorization", Authorize());
            request.Headers.Add("SOAPAction", "urn:udi-com:device:X_Insteon_Lighting_Service:1#" + service);

            Stream data = request.GetRequestStream();

            data.Write(Encoding.ASCII.GetBytes(reqString), 0, reqString.Length);
            data.Flush();
            data.Close();

            response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                WFLogging.Log("Grouped " + node + " as a child of " + parent);
            }
            else
            {
                WFLogging.Error("Group of " + node + " under " + parent + "failed: " + response.StatusDescription);
            }

            response.Close();

            return;
        }
Ejemplo n.º 3
0
        internal static void callback(IAsyncResult result)
        {
            UdpClient  u = (UdpClient)((UdpState)(result.AsyncState)).client;
            IPEndPoint e = (IPEndPoint)((UdpState)(result.AsyncState)).ep;

            Byte[] receiveBytes  = u.EndReceive(result, ref e);
            string receiveString = Encoding.ASCII.GetString(receiveBytes);
            //Console.WriteLine("Received: {0}", receiveString);

            // Parse IP address / port from recieved text
            int i1 = receiveString.IndexOf("//");
            int i2 = receiveString.IndexOf("/desc");

            if (i1 > 0)
            {
                i1        += 2;
                ISYAddress = receiveString.Substring(i1, (i2 - i1));
            }
            WFLogging.Log("Found ISY at " + ISYAddress);
        }
Ejemplo n.º 4
0
        internal void Server()
        {
            Thread      handleClient;
            TcpListener server = new TcpListener(IPAddress.Parse(Address), Port);

            server.Start();
            WFLogging.Log("Logging server has started on port " + Port.ToString());

            // Add a new log client to get new events and send over
            // the websocket.
            WFLogging.AddListener(WSLog);

            while (true)
            {
                WFLogging.Info("Waiting for log client to connect");
                client       = server.AcceptTcpClient();
                handleClient = new Thread(() => ClientHandler(client));
                handleClient.Start();
            }
        }
Ejemplo n.º 5
0
        //  Look for UPNP broadcast messages from an ISY. If it finds
        //  one, then use it.
        //
        //  FIXME: How do we know this is the right ISY?  What if there
        //  is more than one on the network?
        internal static string IsyAutoDetect()
        {
            UdpClient  listen_udp;
            IPAddress  group_ip;
            IPEndPoint group_ep;
            string     ip = "";

            byte[] recv_data;
            int    i;
            string buf   = "";
            int    tries = 100;

            using (listen_udp = new UdpClient()) {
                listen_udp.ExclusiveAddressUse = false;
                listen_udp.Client.SetSocketOption(SocketOptionLevel.Socket,
                                                  SocketOptionName.ReuseAddress, true);
                group_ip = IPAddress.Parse("239.255.255.250");
                //group_ep = new IPEndPoint(IPAddress.Any, 1900);
                group_ep = new IPEndPoint(IPAddress.Any, 20034);

                try {
                    listen_udp.Client.Bind(group_ep);
                } catch (Exception e) {
                    WFLogging.Error("Failed to bind to broadcast address");
                    WFLogging.Error(e.Message);
                    return("");
                }

                listen_udp.EnableBroadcast = true;

                try {
                    listen_udp.JoinMulticastGroup(group_ip);
                } catch (Exception e) {
                    WFLogging.Error("Failed to join Multicast group: " + e.Message);
                    //listen_udp.Close();
                    return("");
                }

                // Set the timeout at 90 seconds.  If we haven't received anything in
                // that time, we probably won't.
                listen_udp.Client.ReceiveTimeout = 90000;

                while ((ip == ""))
                {
                    try {
                        recv_data = listen_udp.Receive(ref group_ep);
                    } catch {
                        WFLogging.Error("Timed out trying to discover ISY.");
                        return("");
                    }
                    if (recv_data.Length != 0)
                    {
                        // Found somelthing
                        buf = Encoding.ASCII.GetString(recv_data);

                        // Now see if this is really an ISY
                        if (buf.Contains("X_Insteon") == false)
                        {
                            if (--tries == 0)
                            {
                                WFLogging.Error("Failed to detect ISY on the network.");
                                return("");
                            }
                        }
                        else
                        {
                            // This really is an ISY.  Pull the location field
                            // from the string.
                            i = buf.IndexOf("LOCATION:");
                            if ((i > 0))
                            {
                                ip = buf.Substring((i + 9)).Split('\r')[0];
                            }
                        }
                    }
                }
                listen_udp.DropMulticastGroup(group_ip);
                //listen_udp.Close();
            }

            WFLogging.Log(("Found an ISY: " + ip));
            return(ip);
        }
Ejemplo n.º 6
0
        internal void Start()
        {
            byte[] buf = new byte[512];
            int    len;
            bool   header = false;

            while (Started)
            {
                int retries = 0;

                WFLogging.Warning("Attempt to start already active WebSocket connection " + retries.ToString());
                Thread.Sleep(1000);

                if (retries++ > 10)
                {
                    WFLogging.Error("Giving up after 10 attempts.");
                    return;
                }
            }

            client = new TcpClient(Host, Port);
            if (!client.Connected)
            {
                WFLogging.Error("Client not connected to " + Port);
            }

            WFLogging.Log("Starting communication with websocket server.");
            finished = false;
            Started  = true;

            // Send header
            var seckeybytes = Encoding.UTF8.GetBytes(seckey);

            client.Client.Send(Encoding.ASCII.GetBytes("GET " + Path + " HTTP/1.1\r\n"));
            client.Client.Send(Encoding.ASCII.GetBytes("Host: " + Host + ":" + Port.ToString() + "\r\n"));
            client.Client.Send(Encoding.ASCII.GetBytes("Upgrade: websocket\r\n"));
            client.Client.Send(Encoding.ASCII.GetBytes("Connection: Upgrade\r\n"));
            client.Client.Send(Encoding.ASCII.GetBytes("Pragma: no-cache\r\n"));
            client.Client.Send(Encoding.ASCII.GetBytes("Origin: http://" + Host + "\r\n"));
            client.Client.Send(Encoding.ASCII.GetBytes("Cache-Control: no-cache\r\n"));
            client.Client.Send(Encoding.ASCII.GetBytes("Sec-WebSocket-Key: " + System.Convert.ToBase64String(seckeybytes) + "\r\n"));
            client.Client.Send(Encoding.ASCII.GetBytes("Sec-WebSocket-Version: 13\r\n"));
            client.Client.Send(Encoding.ASCII.GetBytes("\r\n"));

            WFLogging.Info("    Waiting for handshake");
            Thread.Sleep(100);
            // Receive handshake
            while (!header)
            {
                if (client.Client.Available > 0)
                {
                    len = client.Client.Receive(buf, (client.Client.Available - 1), SocketFlags.None);
                    if (len > 0)
                    {
                        string strbuf = Encoding.ASCII.GetString(buf, 0, len);
                        // Just look for the a websock type response, ignore the rest of the headers
                        if (strbuf.Contains("HTTP/1.1 101"))
                        {
                            header = true;
                        }
                    }
                }
                Thread.Sleep(500);
            }

            receive_thread = new Thread(new ThreadStart(ReceiveLoop));
            receive_thread.IsBackground = true;
            receive_thread.Start();
        }
Ejemplo n.º 7
0
        private static string MakeDocs()
        {
            string page;

            page  = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
            page += "<meta http-equiv=\"cache-control\" content=\"no-cache\">\n";
            page += "<meta http-equiv=\"expires\" content=\"0\">\n";
            page += "<meta http-equiv=\"pragma\" content=\"no-cache\">\n";
            page += "<meta http-equiv=\"Content-Language\" content=\"en\">\n";
            page += "<meta charset=\"UTF-8\">\n";
            page += "<meta name=\"google\" content=\"notranslate\">\n";
            page += "<style>\n";
            page += "	body { font-family: Sans-Serif; }\n";
            page += "</style>\n";
            page += "<title>WeatherFlow Nodeserver Web Interface</title>\n";
            page += "</head><body>\n";
            page += "<div align=\"center\" style=\"width: 920px; margin: 0 auto;\">\n";
            page += WFNServer.MakeMenu();

            foreach (string addr in WeatherFlowNS.NS.NodeList.Keys)
            {
                page += "<table border=\"1\" style=\"width: 900px; border-collapse:collapse; box-shadow: 4px 4px 4px #999; \">";
                if (WeatherFlowNS.NS.NodeList[addr] == "WF_AirUS")
                {
                    page += AirDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_Air")
                {
                    page += AirDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_AirUK")
                {
                    page += AirDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_AirD")
                {
                    page += AirDeviceDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_SkyUS")
                {
                    page += SkyDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_SkyUK")
                {
                    page += SkyDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_Sky")
                {
                    page += SkyDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_SkyD")
                {
                    page += SkyDeviceDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_Hub")
                {
                    page += HubDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_Lightning")
                {
                    page += LightningDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_LightningUS")
                {
                    page += LightningDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_LightningUK")
                {
                    page += LightningDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_RapidWind")
                {
                    page += RapidDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_RapidWindUS")
                {
                    page += RapidDoc(addr);
                }
                else if (WeatherFlowNS.NS.NodeList[addr] == "WF_RapidWindUK")
                {
                    page += RapidDoc(addr);
                }
                else
                {
                    WFLogging.Log("Unknown node type " + WeatherFlowNS.NS.NodeList[addr]);
                }
                page += "</table>";
                page += "<p>";
            }

            page += "</div>";
            page += "</body>\n";
            page += "</html>\n";

            return(page);
        }