Team Updates

Team Stream Item
N
Naiara Silva Santos
fromsocketimport*
fromev3dev2.motorimportLargeMotor, OUTPUT_A, OUTPUT_B, SpeedPercent, MoveTank
fromev3dev2.sensorimportINPUT_1
fromev3dev2.sensor.legoimportTouchSensor
importthreading
importstruct
serverPort=12008
serverSocket=socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
motorX=LargeMotor(OUTPUT_A)
motorY=LargeMotor(OUTPUT_B)
classserver(threading.Thread):
defrun(self):
print("Aguardando conexao")
while1:
connectionSocket, addr=serverSocket.accept()
sentence=connectionSocket.recv(1024)
try:
(azimuth, elevation) =struct.unpack('ff', sentence)
print('pointing to: ('+str(azimuth) +'; '+str(elevation) +')')
# Azimuth
azimuth= (azimuth*3) % (360*3)
motorX.run_to_abs_pos(position_sp=azimuth, speed_sp=200, stop_action="hold")
# Elevation
ifelevation<-90:
elevation=-90
elifelevation>90:
elevation=90
motorY.run_to_abs_pos(position_sp=elevation, speed_sp=100, stop_action="hold")
connectionSocket.close()
except:
print('something wrong happened')
# Define as threads
server_thread=server()
# Inicia as threads
server_thread.start()
# Resetar posicao dos robos
motorX.run_to_abs_pos(position_sp=0, speed_sp=200, stop_action="hold")
motorY.run_to_abs_pos(position_sp=0, speed_sp=100, stop_action="hold")
# Aguarda as threads serem finalizadas
server_thread.join()
view raw __init__.py hosted with ❤ by GitHub
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
}
}
}
}
N
Naiara Silva Santos