Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testcases passes #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions CodeChallengeV2/Services/GeocodeService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using CodeChallengeV2.Models;
using Newtonsoft.Json.Linq;

namespace CodeChallengeV2.Services
{
public class GeocodeService : IGeocodeService
{
public Task<string> FindCountryCode(GeocodePayload payload)
static readonly HttpClient client = new HttpClient();
public async Task<string> FindCountryCode(GeocodePayload payload)
{
return Task.FromResult("DK");
var requestStr = $"http://api.geonames.org/findNearbyPlaceNameJSON?lat={payload.Lat}&lng={payload.Long}&username=kk9440";

HttpResponseMessage response = (await client.GetAsync(requestStr)).EnsureSuccessStatusCode();
var parsedContent = JObject.Parse(await response.Content.ReadAsStringAsync());
return (string) parsedContent["geonames"][0]["countryCode"];
}
}
}
15 changes: 14 additions & 1 deletion CodeChallengeV2/Services/NetworkSimulatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ public class NetworkSimulatorService : INetworkSimulatorService
/// <returns></returns>
public Task<List<Node>> FindCritialGateways(NetworkGraph graph)
{
return Task.FromResult(Enumerable.Empty<Node>().ToList());
var devDict = new Dictionary<String, List<String>>();
List <Node> criticalGatewayNodes = new List<Node>();

graph.Graphs.ToList().ForEach(g => {
List<String> devices = g.Nodes.Where(x => x.Type == "Device").Select(x => x.Id).ToList();

devices.ForEach(dev => devDict[dev] = new List<string>());
g.Edges.Where(x => x.Relation == "is_connected_to").ToList().ForEach(y => devDict[y.Source].Add(y.Target));

List<String> criticalGateways = devDict.Where( x => x.Value.Count == 1).Select( y => y.Value.First()).ToList();
criticalGatewayNodes.AddRange(g.Nodes.Where(x => x.Type == "Gateway" && criticalGateways.Contains(x.Id)).ToList());
});

return Task.FromResult(criticalGatewayNodes);
}
}
}
49 changes: 47 additions & 2 deletions CodeChallengeV2/Services/SensorService.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using CodeChallengeV2.Models;

namespace CodeChallengeV2.Services
{
public class SensorService : ISensorService
{
private static byte[] StringToByteArray(string hex) {
byte[] bytes = new byte[(hex.Length >> 1)];
for (int idx = 0; idx < hex.Length; idx += 2) bytes[(idx >> 1)] = Convert.ToByte(hex.Substring(idx, 2), 16);
return bytes;
}

private static T GetData<T>(byte[] bytes, int offset, int dataSize) {
T result = default(T);
for (int i = 0; i < Marshal.SizeOf(default(T)); i++) {
result = (result * (dynamic)256) | bytes[0];
}

return result;
}

public Task<SensorPayloadDecoded> DecodePayload(SensorPayload payload)
{
//TODO: implement decoding
byte[] bytes = StringToByteArray(payload.FPort.ToString("X2") + payload.Data);
int arrayIdx = 0;
var res = new SensorPayloadDecoded()
{
DevEUI = payload.DevEUI,
Time = payload.Time
Time = payload.Time
};

while (arrayIdx < bytes.Length) {
double data = (double)((Int16)(bytes[arrayIdx+2] << 8 | bytes[arrayIdx+1]));
switch(bytes[arrayIdx]) {
case 20:
arrayIdx += 3;
break;
case 40:
res.TempInternal = data / 100.0;
arrayIdx += 3;
break;
case 41:
res.TempRed = data / 100.0;
arrayIdx += 3;
break;
case 42:
res.TempBlue = data / 100.0;
arrayIdx += 3;
break;
case 43:
res.TempHumidity = data / 100.0;
res.Humidity = (double)((SByte)(bytes[arrayIdx+3])) / 2.0;
arrayIdx += 4;
break;
default:
break;
}
}

return Task.FromResult(res);
}
}
Expand Down