Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
Adjust code according to comment
Browse files Browse the repository at this point in the history
  • Loading branch information
matrixlice committed Feb 3, 2017
1 parent b84d4bd commit cefda08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -208,3 +208,4 @@ VendingMachineSample/packages/*
/RemoteMonitoring.VC.VC.opendb
/Simulator/Simulator.WebJob/DMSimulator.exe
/pingme.txt
*.powerbirc
30 changes: 17 additions & 13 deletions DeviceAdministration/Web/Controllers/JobController.cs
Expand Up @@ -13,6 +13,7 @@
using Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security;
using Microsoft.Azure.Devices.Shared;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Controllers
{
Expand Down Expand Up @@ -116,9 +117,7 @@ public async Task<ActionResult> CloneJob(string jobId)
{
PropertyName = p.Key,
PropertyValue = p.Value.Value.ToString(),
DataType = valuetype.HasFlag(Newtonsoft.Json.Linq.JTokenType.Float & Newtonsoft.Json.Linq.JTokenType.Integer) ? TwinDataType.Number
: valuetype.HasFlag(Newtonsoft.Json.Linq.JTokenType.Boolean) ? TwinDataType.Boolean
: TwinDataType.String
DataType = convertToTwinDataType(valuetype)
};
}).ToList(),
Tags = twin.Tags.AsEnumerableFlatten().Select(t =>
Expand All @@ -128,9 +127,7 @@ public async Task<ActionResult> CloneJob(string jobId)
{
TagName = t.Key,
TagValue = t.Value.Value.ToString(),
DataType = valuetype.HasFlag(Newtonsoft.Json.Linq.JTokenType.Float & Newtonsoft.Json.Linq.JTokenType.Integer) ? TwinDataType.Number
: valuetype.HasFlag(Newtonsoft.Json.Linq.JTokenType.Boolean) ? TwinDataType.Boolean
: TwinDataType.String
DataType = convertToTwinDataType(valuetype)
};
}).ToList(),
});
Expand Down Expand Up @@ -193,7 +190,7 @@ public async Task<ActionResult> ScheduleTwinUpdate(ScheduleTwinUpdateModel model
}
else
{
TwinExtension.Set(twin, key, getDyanmicValue(tagModel.DataType, tagModel.TagValue));
TwinExtension.Set(twin, key, getDyanmicValue(tagModel.DataType, tagModel.TagValue));
}
await _nameCacheLogic.AddNameAsync(tagModel.TagName);
}
Expand All @@ -207,7 +204,7 @@ public async Task<ActionResult> ScheduleTwinUpdate(ScheduleTwinUpdateModel model
}
else
{
TwinExtension.Set(twin, key, getDyanmicValue(propertyModel.DataType, propertyModel.PropertyValue));
TwinExtension.Set(twin, key, getDyanmicValue(propertyModel.DataType, propertyModel.PropertyValue));
}
await _nameCacheLogic.AddNameAsync(propertyModel.PropertyName);
}
Expand Down Expand Up @@ -276,7 +273,7 @@ public async Task<ActionResult> ScheduleDeviceMethod(ScheduleDeviceMethodModel m
{
string methodName = model.MethodName.Split('(').First();

var parameters = model.Parameters?.ToDictionary(p => p.ParameterName, p => getDyanmicValue(p.Type,p.ParameterValue)) ?? new Dictionary<string, dynamic>();
var parameters = model.Parameters?.ToDictionary(p => p.ParameterName, p => getDyanmicValue(p.Type, p.ParameterValue)) ?? new Dictionary<string, dynamic>();
string payload = JsonConvert.SerializeObject(parameters);

var deviceListFilter = await GetFilterById(model.FilterId);
Expand All @@ -295,14 +292,14 @@ public async Task<ActionResult> ScheduleDeviceMethod(ScheduleDeviceMethodModel m

return RedirectToAction("Index", "Job", new { jobId = jobId });
}
private dynamic getDyanmicValue (TwinDataType type, dynamic value)

private dynamic getDyanmicValue(TwinDataType type, dynamic value)
{
switch (type)
{
case Infrastructure.Models.TwinDataType.String:
string valueString = value.ToString();
return valueString as dynamic;
return valueString as dynamic;
case Infrastructure.Models.TwinDataType.Number:
int valueInt;
float valuefloat;
Expand All @@ -317,7 +314,7 @@ private dynamic getDyanmicValue (TwinDataType type, dynamic value)
else
{
return value as string;
}
}
case Infrastructure.Models.TwinDataType.Boolean:
bool valueBool;
if (bool.TryParse(value.ToString(), out valueBool))
Expand All @@ -332,6 +329,13 @@ private dynamic getDyanmicValue (TwinDataType type, dynamic value)
}
}

private TwinDataType convertToTwinDataType(JTokenType valuetype)
{
return valuetype.HasFlag(Newtonsoft.Json.Linq.JTokenType.Float & Newtonsoft.Json.Linq.JTokenType.Integer) ? TwinDataType.Number
: valuetype.HasFlag(Newtonsoft.Json.Linq.JTokenType.Boolean) ? TwinDataType.Boolean
: TwinDataType.String;
}

private async Task AddMoreDetailsToJobAsync(DeviceJobModel job)
{
Task<JobRepositoryModel> queryJobTask = _jobRepository.QueryByJobIDAsync(job.JobId);
Expand Down

0 comments on commit cefda08

Please sign in to comment.