Exemplo n.º 1
0
        private void SocketOnRequestReceived(QsysSocket socket, QsysRequest request)
        {
            //CloudLog.Debug("{0}.SocketOnRequestReceived()\r\n{1}", GetType().Name, request);

            switch (request.Method)
            {
            case "EngineStatus":
                ReceivedEngineStatusUpdate(request);
                break;

            case "ChangeGroup.Poll":
                var groupId = request.Args["Id"].Value <string>();

                if (_changeGroups.ContainsKey(groupId))
                {
                    var changes = request.Args["Changes"];
                    foreach (var change in changes.Where(change => change["Component"] != null))
                    {
                        this[change["Component"].Value <string>()][change["Name"].Value <string>()].UpdateFromData
                            (change);
                    }
                }
                break;
            }
        }
Exemplo n.º 2
0
        private void ReceivedEngineStatusUpdate(QsysRequest request)
        {
            /*CloudLog.Debug("{0} received EngineStatus\r\n{1}", GetType().Name,
             *          request.Args.ToString(Formatting.Indented));*/
#if true
            Debug.WriteInfo("Q-Sys socket status received");
            Debug.WriteNormal(Debug.AnsiBlue + request.Args.ToString(Formatting.Indented) + Debug.AnsiReset);
#endif
            _platform   = request.Args["Platform"].Value <string>();
            State       = request.Args["State"].Value <string>();
            DesignName  = request.Args["DesignName"].Value <string>();
            DesignCode  = request.Args["DesignCode"].Value <string>();
            IsRedundant = request.Args["IsRedundant"].Value <bool>();
            IsEmulator  = request.Args["IsEmulator"].Value <bool>();
            Status      = new CoreStatus(request.Args["Status"]);

            if (State == "Standby")
            {
                _socket.TryAnotherAddress();
                _socket.Disconnect(true);
            }

            if (!_initialized &&
                (_initializeThread == null || _initializeThread.ThreadState != Thread.eThreadStates.ThreadRunning))
            {
                _initializeThread = new Thread(specific => InitalizeProcess(), null)
                {
                    Name     = "QsysCore Initializing Process Thread",
                    Priority = Thread.eThreadPriority.HighPriority
                };
            }
        }
Exemplo n.º 3
0
        internal QsysResponse Request(string method, object args)
        {
            if (Thread.CurrentThread.ThreadType == Thread.eThreadType.SystemThread)
            {
                throw new Exception("Cannot call QsysCore.Request synchronously in system thread!");
            }
            var request = new QsysRequest(_socket, method, args);

            //CloudLog.Debug("{0}.Request(), ID = {1}", GetType().Name, request.Id);
            _awaitingEventsLocked.Enter();
            _awaitingEvents[request.Id] = new CEvent(true, false);
            _awaitingEventsLocked.Leave();
            _socket.SendRequest(request);
            var result = _awaitingEvents[request.Id].Wait(30000);

            _awaitingEvents[request.Id].Dispose();
            _awaitingEventsLocked.Enter();
            _awaitingEvents.Remove(request.Id);
            _awaitingEventsLocked.Leave();
            if (!result)
            {
                CloudLog.Error("{0} Request Time Out, Request: {1}, ID {2}", GetType().Name, request.Method, request.Id);
                throw new TimeoutException("Request did not process a response in suitable time");
            }
            var response = _awaitingResponses[request.Id];

            _awaitingResponsesLocked.Enter();
            _awaitingResponses.Remove(request.Id);
            _awaitingResponsesLocked.Leave();
            return(response);
        }
Exemplo n.º 4
0
        protected virtual void OnRequestReceived(QsysSocket socket, QsysRequest request)
        {
#if DEBUG
            //Debug.WriteSuccess(string.Format("{0}.OnRequestReceived()", GetType().Name));
            //Debug.WriteNormal(Debug.AnsiBlue + request + Debug.AnsiReset);
#endif
            var handler = RequestReceived;
            if (handler != null)
            {
                handler(socket, request);
            }
        }
Exemplo n.º 5
0
        public void SendRequest(QsysRequest request)
        {
            if (request.Id <= 0)
            {
                return;
            }
            _requests[request.Id] = request;
            var content = request.ToString();

#if DEBUG
            Debug.WriteInfo(_name + " Tx", Debug.AnsiPurple + content + Debug.AnsiReset);
#endif
            Send(content);
        }
Exemplo n.º 6
0
        internal int RequestAsync(QsysRequestResponse callBack, string method, object args)
        {
            var request = new QsysRequest(_socket, method, args);

            _socket.SendRequest(request);
            try
            {
                _awaitingAsyncRequestsLocked.Enter();
                _awaitingAsyncRequests[request.Id] = callBack;
                _awaitingAsyncRequestsLocked.Leave();
            }
            catch (Exception e)
            {
                CloudLog.Error("Could not queue callback for QsysRequest ID {0}, {1}", request.Id, e.Message);
            }
            return(request.Id);
        }
Exemplo n.º 7
0
        internal QsysResponse(JToken data, QsysRequest request)
        {
            try
            {
                _data   = data;
                Request = request;

                Id = _data["id"].Value <int>();

                if (_data["error"] != null)
                {
                    IsError      = true;
                    ErrorMessage = _data["error"]["message"].Value <string>();
                    return;
                }

                if (_data["result"] == null)
                {
                    return;
                }

                if (_data["result"].Type != JTokenType.Boolean)
                {
                    return;
                }
                if (!_data["result"].Value <bool>())
                {
                    return;
                }
                IsAck = true;
            }
            catch (Exception e)
            {
                IsError      = true;
                ErrorMessage = "Response Parsing Error";
                CloudLog.Exception(e);
            }
        }