Ejemplo n.º 1
0
        private void Workroom_RequestDone(object sender, OnServiceEventArgs e)
        {
            var request = WorkRequestList
                          .Where(x => x.Code.Equals(e.Code)).Single();

            request.IsDone();
            //
            Console.WriteLine($"СТО: {e.WorkType}, заявка {e.Code} обработана!");
            //
            RequestDone?.Invoke(this, new OnServiceEventArgs(e.WorkType, e.Code));

            //Проверка на выполнение всех работ
            if (!WorkRequestList
                .Any(x => x.RequestStatus == RequestStatus.IsActive && x.Vehicle.Equals(request.Vehicle)))
            {
                WorkDone?.Invoke(request.Vehicle);
            }
        }
Ejemplo n.º 2
0
        public static void GetResponse(string url, string contentType, string method, RequestDone callback, Dictionary <string, dynamic> parameters = null)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));

            Debug.WriteLine("start request " + contentType + " - " + method + " - " + url);
            request.ContentType = String.Format("{0}; boundary={1}", contentType, boundary);
            request.Method      = method;

            Parameters = parameters;

            _callback = callback;

            if (Parameters != null && (request.Method == "POST" || request.Method == "PUT"))
            {
                request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
            }
            else
            {
                request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
            }
        }
Ejemplo n.º 3
0
        public static void Request(string url, RequestDone callback = null)
        {
            _callback = callback;

            WebRequester.GetResponse(url, "application/json", Method, GetResponse);
        }
Ejemplo n.º 4
0
 public void CarServe(Guid guid)
 {
     Console.WriteLine($"Мастерская: {WorkType}, заявка {guid} обработана!");
     //
     RequestDone?.Invoke(this, new OnServiceEventArgs(WorkType, guid));
 }
Ejemplo n.º 5
0
        private JObject ApiRequest(string changeId, bool redownload = false)
        {
            byte[] decompFile     = null;
            string cachedFilePath = CachedFilePath(_directoryPath, changeId);

            if (!File.Exists(cachedFilePath))
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_apiEndpoint + changeId);
                webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");

                transactionLog.Info("Requesting: " + changeId);

                Stopwatch watch = new Stopwatch();
                watch.Start();

                if (_requestDelay.Elapsed.Milliseconds < 500)
                {
                    System.Threading.Thread.Sleep(500 - _requestDelay.Elapsed.Milliseconds);
                }
                _requestDelay.Restart();
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                TimeSpan ts          = watch.Elapsed;
                string   elapsedTime = String.Format("{0:00}s {1:00}ms", ts.Seconds, ts.Milliseconds);
                transactionLog.Info("Done in: " + elapsedTime);

                using (Stream stream = webResponse.GetResponseStream())
                    using (FileStream file = File.Create(cachedFilePath)) {
                        byte[] tmpArr = ReadFully(stream);

                        file.Write(tmpArr, 0, tmpArr.Length);

                        Stream stream1 = new MemoryStream(tmpArr);
                        using (GZipStream decompStream = new GZipStream(stream1, CompressionMode.Decompress)) {
                            decompFile = ReadFully(decompStream);
                        }
                    }

                RequestDone.Invoke(this, new ApiRequestDoneArgs(_nextChangeId, ts.Seconds * 1000 + ts.Milliseconds, false, new FileInfo(cachedFilePath).Length));
            }
            else
            {
                transactionLog.Info(changeId + " already exists");

                RequestDone.Invoke(this, new ApiRequestDoneArgs(_nextChangeId, 0, true, new FileInfo(cachedFilePath).Length));

                using (FileStream file = new FileStream(cachedFilePath, FileMode.Open, FileAccess.Read))
                    using (GZipStream decompStream = new GZipStream(file, CompressionMode.Decompress)) {
                        try {
                            decompFile = ReadFully(decompStream);
                        }
                        catch {
                            if (redownload)
                            {
                                errorLog.Info("Redownload of " + changeId + " failed. Terminating program.");
                                Environment.Exit(0);
                            }

                            file.Close();
                            errorLog.Info("Decompression of " + changeId + " failed, redownloading");
                            File.Delete(cachedFilePath);

                            return(ApiRequest(changeId, redownload = true));
                        }
                    }
            }

            string jsonTxt = System.Text.Encoding.Default.GetString(decompFile);

            return(JObject.Parse(jsonTxt));
        }