コード例 #1
0
        public void OpenDoor(int doorIndex)
        {
            if (doorIndex < 0 || doorIndex > 3)
            {
                return;
            }

            doorIndex++; //Input Zero Based. Controller Takes 1 Based For This Command.

            byte[] payload = WGTools.strTobyte("0" + doorIndex.ToString() + "01000000000000000000000000000000000000000000000000");


            byte[] response = SendCommand(WGParams.CommCmd.COMM_OPEN_DOOR, payload);
        }
コード例 #2
0
        public static List <WGController> ScanNet(UInt16 udpPort)
        {
            //Get a List of all NICS, and set up a UDPClient ON Each
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

            List <UdpClient> clients = new List <UdpClient>();

            foreach (NetworkInterface n in nics)
            {
                if (n.NetworkInterfaceType == NetworkInterfaceType.Loopback)
                {
                    continue; //Ignore Loopback Interface
                }
                if (n.OperationalStatus != OperationalStatus.Up)
                {
                    continue; //This adapter is off or not connected
                }
                IPInterfaceProperties ipProps = n.GetIPProperties();
                if (ipProps == null)
                {
                    continue; //IP Not Supported
                }
                IPv4InterfaceProperties ip4Props = ipProps.GetIPv4Properties();
                if (ip4Props == null)
                {
                    continue; //IPv4 Not Supported
                }
                //Build UDPClient List

                foreach (UnicastIPAddressInformation uc in ipProps.UnicastAddresses)
                {
                    if (uc.Address.AddressFamily != AddressFamily.InterNetwork)
                    {
                        continue;
                    }


                    IPEndPoint listenEP  = new IPEndPoint(uc.Address, 0);
                    UdpClient  udpClient = new UdpClient(listenEP);

                    clients.Add(udpClient);
                }
            }


            //Broadcast Status Request to All Interfaces

            foreach (UdpClient c in clients)
            {
                byte[] dgram = WGTools.strTobyte("7EFFFF0111000000000000000000000000000000000000000000000000000010020D");

                IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, 60000);
                int        snt      = c.Send(dgram, dgram.Length, endPoint);
            }

            System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();



            List <ConnectionInfo.ScanResponse> recvResp = new List <ConnectionInfo.ScanResponse>();

            //Listen for 1 second on each interface

            while (watch.ElapsedMilliseconds < 1500)
            {
                foreach (UdpClient c in clients)
                {
                    try
                    {
                        while (true)
                        {
                            //IPEndPoint object will allow us to read datagrams sent from any source.
                            IPEndPoint remEP = new IPEndPoint(IPAddress.Any, 0);
                            c.Client.ReceiveTimeout = 100;
                            byte[] recvBytes = c.Receive(ref remEP);

                            ConnectionInfo.ScanResponse s = new ConnectionInfo.ScanResponse();
                            s.ep        = (IPEndPoint)c.Client.LocalEndPoint;
                            s.respBytes = recvBytes;
                            lock (recvResp)
                            {
                                recvResp.Add(s);
                            }
                        }
                    }
                    catch //Task Done if Socket is closed, or other exception
                    { }
                }
            }

            //Close All the Clientss
            foreach (UdpClient c in clients)
            {
                c.Close();
            }

            List <WGController> controllers = new List <WGController>();

            //Parse Received Bytes
            foreach (ConnectionInfo.ScanResponse s in recvResp)
            {
                byte[] buf = s.respBytes;

                if (buf.Length != 34)
                {
                    continue;  //Verify Packet Length
                }
                if (buf[33] != (byte)13 | buf[0] != (byte)126)
                {
                    continue; //Verify Start and End Bytes
                }
                long checksum = 0;

                for (int i = 1; i <= 30; i++)
                {
                    checksum += buf[i];
                }

                //CheckSum
                if (checksum != (buf[32] * 256L + buf[31]))
                {
                    continue;  //Verify Checksum
                }
                string byteString = WGTools.byteTostr(buf);

                string tst = byteString.Substring(4, 4);
                if (byteString.Substring(6, 4) != "0111")
                {
                    continue; //Validate Response Type
                }
                WGController controller = new WGController();
                controller.Connection.ID  = WGTools.NetToUInt16(buf, 1);
                controller.Connection.MAC = WGTools.NetToMacString(buf, 5);

                controller.Connection.IP_Address = WGTools.NetToIPString(buf, 11); // $"{buf[12]}.{buf[13]}.{buf[14]}.{buf[15]}";
                controller.Connection.Netmask    = WGTools.NetToIPString(buf, 15); //$"{buf[16]}.{buf[17]}.{buf[18]}.{buf[19]}";
                controller.Connection.Gateway    = WGTools.NetToIPString(buf, 19); //$"{buf[20]}.{buf[21]}.{buf[22]}.{buf[23]}";


                controller.Connection.udpPort  = WGTools.NetToUInt16(buf, 23);
                controller.Connection.Password = WGTools.NetToUInt32(buf, 25);

                controller.Connection.HostIP = s.ep.Address;

                controllers.Add(controller);
            }
            return(controllers);
        }