Ejemplo n.º 1
0
    public void initDeviceConnection(TcpConnection _conn, string[] _formatData)
    {
        NetworkDevice tempDevice = _enabledConnections.Find(x => x.id == _formatData[2]);

        if (tempDevice != null)
        {
            _enabledConnections.Remove(tempDevice);
            tempDevice.stop();
            tempDevice = null;
        }

        _disabledConnections.Remove(_conn);
        NetworkDevice device = new NetworkDevice(_conn, _formatData[2], _formatData[3]);

        _enabledConnections.Add(device);

        _conn.OnRecieveTcpPackage += (string _data) => {
            string[] formatData = _data.Split(';');
            Console.WriteLine(string.Format("[SERVER] DEVICE[{0}] recieved data: {1}", _conn.networkId, _data));

            if (formatData[0] == "COMPLETE")
            {
                foreach (NetworkUser user in _userConnections)
                {
                    user.updateDevice(device);
                }
            }
            else if (formatData[0] == "RET")
            {
                foreach (NetworkUser user in _userConnections)
                {
                    user.send("DEVUPD;" + device.id + ";" + _data);
                }
                Setting_Device_Commmand[] list = SettingsController.commands;
                if (list != null)
                {
                    foreach (Setting_Device_Commmand comm in list)
                    {
                        if (comm.deviceName == device.id && comm.id == formatData[1])
                        {
                            if (comm.type == SettingDeviceCommandType.SET)
                            {
                                TreeLightController.set(new Setting_RGB(), false);
                            }
                            else
                            {
                                TreeLightController.blink(new Setting_RGB(), 3);
                            }
                        }
                    }
                }
            }
        };

        _conn.OnConnectionClosed += () => {
            _enabledConnections.Remove(device);
        };

        _conn.send("INIT");
    }
Ejemplo n.º 2
0
    private void _onConnection(Socket _socket)
    {
        TcpConnection connection = new TcpConnection(NETWORK_SESSION_ID, _socket);

        NETWORK_SESSION_ID++;
        Console.WriteLine(string.Format("[SERVER] [{0}] connection++", connection.networkId));

        connection.OnRecieveTcpPackage += (string _data) => {
            string[] formatData = _data.Split(';');
            Console.WriteLine(string.Format("[SERVER] UNKNOWN[{0}] recieved data: {1}", connection.networkId, _data));

            if (formatData[0] == "IAMTREE\r\n")   // --------------------=================----------------------================---------------
            {
                Console.WriteLine("[TREE] connected");
                treeConnection = connection;
                //TreeLightController.set(new Setting_RGB() { r = 255, g = 16, b = 7 }, false);
                //TreeLightController.blink(new Setting_RGB() { r = 255, g = 16, b = 7 }, 12);
                TreeLightController.party();
                return;
            }

            if (formatData[0] == "AUTHDEV")
            {
                if (formatData[1] == AUTH_KEY)   // auth key correct ?? init device
                {
                    this.initDeviceConnection(connection, formatData);
                    Console.WriteLine(string.Format("[SERVER] [{0}] new device connection", connection.networkId));
                }
                else
                {
                    Console.WriteLine(string.Format("[SERVER] [{0}] device connection got stoped, auth_key : {1}", connection.networkId, formatData[1]));
                }
            }
            else if (formatData[0] == "USERJOIN")
            {
                this.initUserConnection(connection, formatData);
                Console.WriteLine(string.Format("[SERVER] [{0}] new user connection", connection.networkId));
            }
        };
        connection.OnConnectionClosed += () => {
            Console.WriteLine(string.Format("[SERVER] [{0}] connection--", connection.networkId));
        };
        _disabledConnections.Add(connection);
    }