| namespaceStarSniper.Services.Impl{ |
| publicclassObservatoryLocationProvider:IObservatoryLocationProvider{ |
| publicstaticreadonlyObservatoryLocationProviderInstance=newObservatoryLocationProvider(); |
| |
| publicstaticreadonlystringApiUrl="https://sscweb.sci.gsfc.nasa.gov/WS/sscr/2/locations/"; |
| |
| privateDateTime_lastUpdate=DateTime.MinValue; |
| privateDateTime_nextUpdate=DateTime.MinValue; |
| |
| publicDateTimeLastUpdate{ |
| get=>_lastUpdate; |
| set{ |
| _lastUpdate=value; |
| _nextUpdate=value.AddSeconds(Settings.UpdateTime); |
| } |
| } |
| publicDateTimeNextUpdate=>_nextUpdate; |
| |
| publicObservatoryLocationProvider(){} |
| |
| publicDictionary<string,ObservatoryLocation[]>RequestPosition(paramsIObservatoryIdProvider[]observatoryIds){ |
| if(observatoryIds==null) |
| thrownewArgumentNullException(nameof(observatoryIds)); |
| |
| Dictionary<string,List<ObservatoryLocation>>locations=newDictionary<string,List<ObservatoryLocation>>(); |
| foreach(IObservatoryIdProviderobservatoryinobservatoryIds) |
| locations.Add(observatory.Id,newList<ObservatoryLocation>()); |
| |
| List<Task>tasks=newList<Task>(); |
| intbuckets=(int)Math.Ceiling(locations.Count/10.0); |
| for(inti=0;i<buckets;i++){ |
| List<IObservatoryIdProvider>bucket=newList<IObservatoryIdProvider>(); |
| for(intj=0;j<10;j++){ |
| if((i*10)+j>=observatoryIds.Length) |
| break; |
| bucket.Add(observatoryIds[(i*10)+j]); |
| } |
| tasks.Add(Task.Run(async()=>awaitAcquire(bucket,locations))); |
| } |
| Task.WaitAll(tasks.ToArray()); |
| |
| LastUpdate=newDateTime(DateTime.Now.Ticks); |
| returnnewDictionary<string,ObservatoryLocation[]>(locations.Select(kvp =>KeyValuePair.Create(kvp.Key,kvp.Value.ToArray()))); |
| } |
| |
| privateasyncTaskAcquire(IEnumerable<IObservatoryIdProvider>observatoryIds,Dictionary<string,List<ObservatoryLocation>>locations){ |
| HttpClientclient=newHttpClient(); |
| client.DefaultRequestHeaders.Add("Accept","application/xml"); |
| |
| LocationRequestrequest=newLocationRequest(observatoryIds); |
| |
| stringbody=request.ToString(); |
| |
| HttpResponseMessageresponse=awaitclient.PostAsync(ApiUrl,newStringContent(body,Encoding.UTF8,"application/xml")); |
| |
| if(!response.IsSuccessStatusCode) |
| // TODO: Log warning |
| return; |
| |
| intlatIndex=0, |
| lonIndex=0, |
| xIndex=0, |
| yIndex=0, |
| zIndex=0, |
| timeIndex=0; |
| stringcurrent=null; |
| |
| body=awaitresponse.Content.ReadAsStringAsync(); |
| if(observatoryIds.Count()!=10) |
| Debug.WriteLine(body); |
| |
| try{ |
| using(StringReaderstringReader=newStringReader(body)) |
| using(XmlReaderreader=XmlReader.Create(stringReader)){ |
| while(reader.Read()){ |
| if(reader.NodeType!=XmlNodeType.Element) |
| continue; |
| switch(reader.Name){ |
| case"Id": |
| reader.Read(); |
| current=reader.ReadContentAsString(); |
| latIndex=0; |
| lonIndex=0; |
| xIndex=0; |
| yIndex=0; |
| zIndex=0; |
| timeIndex=0; |
| break; |
| case"Latitude": |
| if(latIndex>=locations[current].Count) |
| locations[current].Add(newObservatoryLocation()); |
| reader.Read(); |
| locations[current][latIndex].Latitude=reader.ReadContentAsDouble(); |
| latIndex++; |
| break; |
| case"Longitude": |
| if(lonIndex>=locations[current].Count) |
| locations[current].Add(newObservatoryLocation()); |
| reader.Read(); |
| locations[current][lonIndex].Longitude=reader.ReadContentAsDouble(); |
| lonIndex++; |
| break; |
| case"X": |
| if(xIndex>=locations[current].Count) |
| locations[current].Add(newObservatoryLocation()); |
| reader.Read(); |
| locations[current][xIndex].X=reader.ReadContentAsDouble(); |
| xIndex++; |
| break; |
| case"Y": |
| if(yIndex>=locations[current].Count) |
| locations[current].Add(newObservatoryLocation()); |
| reader.Read(); |
| locations[current][yIndex].Y=reader.ReadContentAsDouble(); |
| yIndex++; |
| break; |
| case"Z": |
| if(zIndex>=locations[current].Count) |
| locations[current].Add(newObservatoryLocation()); |
| reader.Read(); |
| locations[current][zIndex].Z=reader.ReadContentAsDouble(); |
| zIndex++; |
| break; |
| case"Time": |
| if(timeIndex>=locations[current].Count) |
| locations[current].Add(newObservatoryLocation()); |
| reader.Read(); |
| locations[current][timeIndex].Time=DateTime.Parse(reader.ReadContentAsString()); |
| timeIndex++; |
| break; |
| } |
| } |
| } |
| } |
| catch(Exceptione){ |
| Debug.WriteLine(e); |
| // TODO: Log error |
| } |
| } |
| } |
| } |