Intel Edison. Облако Intel IoT Analytics: отправка SMS и RESTful клиент на ASP.NET

Intel IoT Analytics
Intel IoT Analytics. SMS HTTP RESTful ASP.NET. .

SMS HTTP
Intel Edison. Intel IoT Analytics: : e-mail . SMS , .
:
  • Higth_temp_PowerOnRelay_and_send_to_web@devdotnet.org
  • Low_temp_PowerOffRelay_and_send_to_web@devdotnet.org

SMS smsc.ru. API .

SMS GET :

smsc.ru/sys/send.php?login=&psw=&phones=&mes=, :
login —
psw — MD5- .
phones — "+"
mes — .
Head , .

:
smsc.ru/sys/send.php?login=tele****&psw=d46d70eb6******&phones=79232******&mes=Relay_power_ON
smsc.ru/sys/send.php?login=tele****&psw=d46d70eb6******&phones=79232******&mes=Relay_power_OFF
Rules, Higth_temp_PowerOnRelay_and_send_to_web@devdotnet.org.

Notifications type — HTTP Endpoint URL.
Intel IoT Analytics

, Low_temp_PowerOffRelay_and_send_to_web@devdotnet.org.

SMS
Intel IoT Analytics

SMS , , .

RESTful ASP.NET Razor
: . : .  API .  ASP.NET Razor netf 4.5.   JSON  Newtonsoft.Json.  Web.config . , API.

, wiki Authorization:
string GetNewToken()
            {
                // login  pass
                string strLogin, strPass;
                strLogin = System.Configuration.ConfigurationManager.AppSettings["Login"];
                strPass = System.Configuration.ConfigurationManager.AppSettings["Pass"];
                //,  .
                //https://github.com/enableiot/iotkit-api/wiki/Authorization
                // JSON
                var obj = new
                {
                    username = strLogin,
                    password = strPass
                };
                string postData = JsonConvert.SerializeObject(obj, Formatting.Indented);
                //HttpWebRequest
                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"];
            }


wiki Data-API:
void GetStateSensors(string strToken, Dictionary<string, string> dicSensors)
            {
                // 
                //https://github.com/enableiot/iotkit-api/wiki/Data-API
                // JSON
                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)HttpWebRequest.Create("https://dashboard.us.enableiot.com/v1/api/accounts/4c8c6b2e-c54f-4df5-8bbb-a5e59df85aaa/data/search");
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/json";
                //Headers
                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());
                //add Dic
                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);
            }



Intel IoT Analytics


, . , . API , . , . : . , email, (invite). , (invite).   , . ,  SMS , . email , . , . . 28 . . , .
Intel IoT Analytics
№39 , 38. , , . , . Intel IoT Analytics ( BETA).


web.devdotnet.org.
.

web.config
<configuration>
  <appSettings>
    <add key="Login" value="***"/>
    <add key="Pass" value="***"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>


index.cshtml
@using System.Net.Http
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq

@{
    //   Session
    string strToken;
    strToken = (string)Session["Token"];
    if (strToken == null)
    {
        strToken = GetNewToken();
        Session["Token"] = strToken;
    }
    //Action
    if (Request["command"] != null)
    {
        string strCommand = (string)Request["command"];
        SetSateActuation(strToken,strCommand);
    }
    //  Sensors
    Dictionary<string, string> dicSensors = new Dictionary<string, string>();
    GetStateSensors(strToken, dicSensors);
    //  Actuation
    bool boolValueLED, boolValueRelay;
    GetStateActuations(strToken, out boolValueLED, out boolValueRelay);
}
        @functions {
            // Pass a user name to this method.
            string GetNewToken()
            {
                // login  pass
                string strLogin, strPass;
                strLogin = System.Configuration.ConfigurationManager.AppSettings["Login"];
                strPass = System.Configuration.ConfigurationManager.AppSettings["Pass"];
                //,  .
                //https://github.com/enableiot/iotkit-api/wiki/Authorization
                // JSON
                var obj = new
                {
                    username = strLogin,
                    password = strPass
                };
                string postData = JsonConvert.SerializeObject(obj, Formatting.Indented);
                //HttpWebRequest
                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)
            {
                // 
                //https://github.com/enableiot/iotkit-api/wiki/Data-API
                // JSON
                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)HttpWebRequest.Create("https://dashboard.us.enableiot.com/v1/api/accounts/4c8c6b2e-c54f-4df5-8bbb-a5e59df85aaa/data/search");
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/json";
                //Headers
                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());
                //add Dic
                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)
            {
                //  Actuation
                //https://github.com/enableiot/iotkit-api/wiki/Control-Device-API
                //HttpWebRequest
                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";
                //Headers
                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());
                //Parse data
                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"]);
                    }
                }
                //find last 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)
            {
                //  Actuation
                //https://github.com/enableiot/iotkit-api/wiki/Control-Device-API
                // 
                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:
                        // You can use the default case.
                        return;
                }
                // JSON
                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)HttpWebRequest.Create("https://dashboard.us.enableiot.com/v1/api/accounts/4c8c6b2e-c54f-4df5-8bbb-a5e59df85aaa/control");
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/json";
                //Headers
                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>



  1. Intel Edison.
  2. Intel Edison. Intel IoT Analytics:
  3. Intel Edison. Intel IoT Analytics:
  4. Intel Edison. Intel IoT Analytics: SMS RESTful ASP.NET

Source: https://habr.com/ru/post/hi384929/


All Articles