private static void GetResponse(string response) { Response = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(response) ?? Activator.CreateInstance(typeof(T)) as T; _callback?.Invoke(Response); }
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); } }
public void CarServe(Guid guid) { Console.WriteLine($"Мастерская: {WorkType}, заявка {guid} обработана!"); // RequestDone?.Invoke(this, new OnServiceEventArgs(WorkType, guid)); }
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)); }