@using System.Net.Http
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@{
string strToken;
strToken = (string)Session["Token"];
if (strToken == null)
{
strToken = GetNewToken();
Session["Token"] = strToken;
}
if (Request["command"] != null)
{
string strCommand = (string)Request["command"];
SetSateActuation(strToken,strCommand);
}
Dictionary<string, string> dicSensors = new Dictionary<string, string>();
GetStateSensors(strToken, dicSensors);
bool boolValueLED, boolValueRelay;
GetStateActuations(strToken, out boolValueLED, out boolValueRelay);
}
@functions {
string GetNewToken()
{
string strLogin, strPass;
strLogin = System.Configuration.ConfigurationManager.AppSettings["Login"];
strPass = System.Configuration.ConfigurationManager.AppSettings["Pass"];
var obj = new
{
username = strLogin,
password = strPass
};
string postData = JsonConvert.SerializeObject(obj, Formatting.Indented);
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://dashboard.us.enableiot.com/v1/api/auth/token");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
byte[] byte1 = System.Text.Encoding.UTF8.GetBytes(postData);
httpWebRequest.ContentLength = byte1.Length;
httpWebRequest.GetRequestStream().Write(byte1, 0, byte1.Length);
httpWebRequest.GetRequestStream().Close();
StreamReader reader = new StreamReader(httpWebRequest.GetResponse().GetResponseStream());
JObject objResponse = JObject.Parse(reader.ReadToEnd());
return (string)objResponse["token"];
}
void GetStateSensors(string strToken, Dictionary<string, string> dicSensors)
{
var obj = new
{
from = -20,
targetFilter = new { deviceList = new[] { "9e-8d-22-0c-50-7b" } },
metrics = new[] { new {
id="99fb5b73-a9e2-4ae0-8a0d-45e6a7cc0541",
op="none"
},
new {
id="b3731f8a-4f31-414d-a072-b364b1792b5b",
op="none"
}
}
};
string postData = JsonConvert.SerializeObject(obj, Formatting.Indented);
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://dashboard.us.enableiot.com/v1/api/accounts/4c8c6b2e-c54f-4df5-8bbb-a5e59df85aaa/data/search");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("Authorization", "Bearer " + strToken);
httpWebRequest.Headers.Add("accountId", "4c8c6b2e-c54f-4df5-8bbb-a5e59df85aaa");
byte[] byte1 = System.Text.Encoding.UTF8.GetBytes(postData);
httpWebRequest.ContentLength = byte1.Length;
httpWebRequest.GetRequestStream().Write(byte1, 0, byte1.Length);
httpWebRequest.GetRequestStream().Close();
StreamReader reader = new StreamReader(httpWebRequest.GetResponse().GetResponseStream());
JObject objResponse = JObject.Parse(reader.ReadToEnd());
string strValueSensor = (string)objResponse["series"][0]["points"][0]["value"];
dicSensors.Add("pressure", strValueSensor);
strValueSensor = (string)objResponse["series"][1]["points"][0]["value"];
strValueSensor = Math.Round(decimal.Parse(strValueSensor, System.Globalization.CultureInfo.CreateSpecificCulture("en")), 2).ToString();
dicSensors.Add("temperature", strValueSensor);
}
void GetStateActuations(string strToken, out bool boolValueLED, out bool boolValueRelay)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://dashboard.us.enableiot.com/v1/api/accounts/4c8c6b2e-c54f-4df5-8bbb-a5e59df85aaa/control/devices/9e-8d-22-0c-50-7b?from=-60000");
httpWebRequest.Method = "GET";
httpWebRequest.Headers.Add("Authorization", "Bearer " + strToken);
httpWebRequest.Headers.Add("accountId", "4c8c6b2e-c54f-4df5-8bbb-a5e59df85aaa");
StreamReader reader = new StreamReader(httpWebRequest.GetResponse().GetResponseStream());
JArray jArr = (JArray)JsonConvert.DeserializeObject(reader.ReadToEnd());
Dictionary<long, string> dicSensorLED = new Dictionary<long, string>();
Dictionary<long, string> dicSensorRelay = new Dictionary<long, string>();
long longTicks;
foreach (JObject item in jArr)
{
longTicks = DateTime.Parse((string)item["created"]).Ticks;
if ((string)item["command"] == "LED.v1.0")
{
if (!dicSensorLED.ContainsKey(longTicks)) dicSensorLED.Add(longTicks, (string)item["params"][0]["value"]);
}
else
{
if (!dicSensorRelay.ContainsKey(longTicks)) dicSensorRelay.Add(longTicks, (string)item["params"][0]["value"]);
}
}
string strValueLED = dicSensorLED[dicSensorLED.Max(x => x.Key)];
string strValueRelay = dicSensorRelay[dicSensorRelay.Max(x => x.Key)];
if (strValueLED == "0") boolValueLED = false; else boolValueLED = true;
if (strValueRelay == "0") boolValueRelay = false; else boolValueRelay = true;
}
void SetSateActuation(string strToken, string strCommand)
{
string strComponentId, strName, strValue, strComplexCommands;
switch (strCommand)
{
case "led_on":
{
strComponentId="64dbdbee-e181-403f-8b51-0872e11a289e";
strName="LED";
strValue = "1";
strComplexCommands = "led1_ON";
break;
}
case "led_off":
{
strComponentId = "64dbdbee-e181-403f-8b51-0872e11a289e";
strName = "LED";
strValue = "0";
strComplexCommands = "led1_OFF";
break;
}
case "relay_on":
{
strComponentId = "5f72e39a-463a-4e57-bac7-9b32debfd865";
strName = "RELAY";
strValue = "1";
strComplexCommands = "relay1_ON";
break;
}
case "relay_off":
{
strComponentId = "5f72e39a-463a-4e57-bac7-9b32debfd865";
strName = "RELAY";
strValue = "0";
strComplexCommands = "relay1_OFF";
break;
}
default:
return;
}
var obj = new
{
commands = new[] { new {
componentId = strComponentId,
parameters= new[] { new
{
name=strName,
value=strValue
}
},
transport="ws"
}
},
complexCommands = new[] { strComplexCommands }
};
string postData = JsonConvert.SerializeObject(obj, Formatting.Indented);
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://dashboard.us.enableiot.com/v1/api/accounts/4c8c6b2e-c54f-4df5-8bbb-a5e59df85aaa/control");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("Authorization", "Bearer " + strToken);
httpWebRequest.Headers.Add("accountId", "4c8c6b2e-c54f-4df5-8bbb-a5e59df85aaa");
byte[] byte1 = System.Text.Encoding.UTF8.GetBytes(postData);
httpWebRequest.ContentLength = byte1.Length;
httpWebRequest.GetRequestStream().Write(byte1, 0, byte1.Length);
httpWebRequest.GetRequestStream().Close();
httpWebRequest.GetResponse();
}
}
}
<html>
<head>
<title> RESTful Intel IoT Analytics ASP.NET Razor</title>
</head>
<body>
<h1> RESTful Intel IoT Analytics ASP.NET Razor</h1>
<p>: @DateTime.Now.ToString()</p>
<p> <a href="https://github.com/enableiot/iotkit-api/wiki/Api-Home">api intel iot analytics.</a></p>
<p> API :</p>
<ol>
<li>, </li>
<li> (sensor)</li>
<li> (Actuation)</li>
</ol>
<h3> </h3>
<p>: @dicSensors["temperature"] *</p>
<p>: @dicSensors["pressure"] </p>
@if (boolValueLED==false)
{
<p>LED: OFF</p>
}
else
{
<p>LED: ON</p>
}
@if (boolValueRelay == false)
{
<p>: OFF</p>
}
else
{
<p>: ON</p>
}
<form name="formaction" action="~/index.cshtml" method="get">
<h3> Actuation</h3>
<p> LED:
<button name="command" type="submit" value="led_on"> ON </button>
<button name="command" type="submit" value="led_off"> OFF </button>
</p>
<p> :
<button name="command" type="submit" value="relay_on"> ON </button>
<button name="command" type="submit" value="relay_off"> OFF </button>
</p>
<p></p>
<p>
<button name="update" type="submit"> </button>
</p>
</form>
</body>
</html>