Exemplo n.º 1
0
        public bool Register(Me me, IPAddress ip, int port = 5000)
        {
            if (!AmeisenDataHolder.IsConnectedToServer)
            {
                IRestResponse response = SendRequest(me, ip, port, HttpMethod.Post, true);

                if (response == null)
                {
                    return(false);
                }

                if (response.StatusCode == HttpStatusCode.Created ||
                    response.StatusCode == HttpStatusCode.OK)
                {
                    IsRegistered = true;
                    NetworkBot responseBot = JsonConvert.DeserializeObject <NetworkBot>(response.Content);
                    BotID = responseBot.id;

                    ConnectedIP   = ip;
                    ConnectedPort = port;
                    AmeisenDataHolder.IsConnectedToServer = true;

                    botUpdateTimer.Start();
                    botListUpdateTimer.Start();

                    return(true);
                }
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 2
0
        private void UpdateBot(object source, ElapsedEventArgs e)
        {
            IRestResponse response = SendRequest(AmeisenDataHolder.Me, ConnectedIP, ConnectedPort, HttpMethod.Put);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                NetworkBot responseBot = JsonConvert.DeserializeObject <NetworkBot>(response.Content);
                BotID = responseBot.id;
            }
        }
Exemplo n.º 3
0
        public void Unregister(Me me, IPAddress ip, int port = 5000)
        {
            IRestResponse response = SendRequest(me, ip, port, HttpMethod.Delete);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                IsRegistered = false;
                NetworkBot responseBot = JsonConvert.DeserializeObject <NetworkBot>(response.Content);
                BotID = responseBot.id;

                botUpdateTimer.Close();
                botListUpdateTimer.Close();
            }
        }
Exemplo n.º 4
0
        private static NetworkBot ConvertRegisterDataToBot(HttpListenerRequest request)
        {
            string         bodyContent  = ReadBody(request);
            NetworkRegData registerData = JsonConvert.DeserializeObject <NetworkRegData>(bodyContent);

            NetworkBot convertedBot = new NetworkBot
            {
                id          = botID,
                me          = registerData.Me,
                ip          = request.RemoteEndPoint.Address.ToString(),
                base64Image = registerData.Base64Image,
                lastUpdate  = DateTimeOffset.Now.ToUnixTimeMilliseconds()
            };

            convertedBot.name = convertedBot.me.Name;

            return(convertedBot);
        }
Exemplo n.º 5
0
        public void Register(Me me, IPAddress ip, int port = 16200)
        {
            IRestResponse response = SendRequest(me, ip, port, HttpMethod.Post, true);

            if (response.StatusCode == HttpStatusCode.Created ||
                response.StatusCode == HttpStatusCode.OK)
            {
                IsRegistered = true;
                NetworkBot responseBot = JsonConvert.DeserializeObject <NetworkBot>(response.Content);
                BotID = responseBot.id;

                ConnectedIP   = ip;
                ConnectedPort = port;

                botUpdateTimer.Start();
                botListUpdateTimer.Start();
            }
        }
Exemplo n.º 6
0
        private static int UpdateBot(HttpListenerRequest request)
        {
            string bodyContent = ReadBody(request);
            int    idToUpdate  = Convert.ToInt32(bodyContent.Split(']')[0].Replace("]", ""));
            string botContent  = bodyContent.Split(']')[1];

            SendableMe meSendable = JsonConvert.DeserializeObject <SendableMe>(botContent);

            NetworkBot convertedBot = new NetworkBot
            {
                id          = idToUpdate,
                me          = meSendable,
                ip          = request.RemoteEndPoint.Address.ToString(),
                base64Image = ((NetworkBot)activeBots[GetBotPositionByID(idToUpdate)]).base64Image,
                lastUpdate  = DateTimeOffset.Now.ToUnixTimeMilliseconds()
            };

            convertedBot.name = convertedBot.me.Name;
            activeBots[GetBotPositionByID(idToUpdate)] = convertedBot;

            return(idToUpdate);
        }