英特尔爱迪生 英特尔物联网分析云:在ASP.NET上发送SMS和RESTful客户端

英特尔物联网分析
使用英特尔物联网分析云的最后一部分使用HTTP网关添加发送SMS通知,并在ASP.NET上创建一个简单的RESTful客户端。总结一下。

发送使用HTTP网关短信通知
在由以前的工作英特尔爱迪生。使用英特尔物联网分析云:创建规则和发送通知;已经形成的规则使您可以发送:电子邮件通知和管理中继电源。发送短信,添加到创建的这两个规则中。
规则:
  • Higth_temp_PowerOnRelay_and_send_to_web@devdotnet.org
  • Low_temp_PowerOffRelay_and_send_to_web@devdotnet.org

我们将使用smsc.ru SMS网关该API 在此处可用

要发送SMS消息,就足以形成并发送以下形式的GET请求:

smsc.ru/sys/send.php?
login=&
psw =& phones=&mes=,其中:login-客户的登录psw-客户的密码或小写的MD5密码哈希。
电话-国际格式的数字,不带“ +”符号
。短信-要发送的消息文本。
无需添加Head标头,一切都非常简单。

我们将形成两个请求:
smsc.ru/sys/send.php?login = tele ****和psw = d46d70eb6 ******和电话= 79232 ******&mes = Relay_power_ON
smsc.ru/sys/send。 php?login = tele ****和psw = d46d70eb6 ******&电话= 79232 ******&mes = Relay_power_OFF转到
规则”部分打开规则Higth_temp_PowerOnRelay_and_send_to_web@devdotnet.org。

添加通知类型-HTTP端点并输入URL
英特尔物联网分析

另外,我们将为第二条规则创建Low_temp_PowerOffRelay_and_send_to_web@devdotnet.org

通过电话收到短信
Intel IoT Analytics

从发送SMS消息的功能的缺点来看,有必要注意无法发送动态数据,例如,您不能发送当前的传感器读数。

ASP.NET Razor上的简单RESTful客户端我们将
形成一个页面,显示当前传感器的读数:温度和压力。我们将控制部件放在页面上:接通继电器和LED。Cloud API可在此处获得在ASP.NET Razor netf 4.5上实现。要使用JSON,将使用Newtonsoft.Json库。Web.config文件包含用于访问云的登录名和密码。该示例不是完整的解决方案,而是对API使用的简单演示。

与往常一样,我们获得令牌,这是Wiki授权中的一部分
编码
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);
            }


界面
英特尔物联网分析

摘要
用于数据收集的云使您无需构建基础架构,从而降低了购买硬件和软件的成本。配置设备,传感器非常简单。开发的API不仅允许接收数据和管理设备,还可以创建自己的管理面板。数据传输没有明显的延迟,请求也不会丢失。有两个角色:管理员和用户。要添加用户,管理员需要指定新的电子邮件,邀请信将发送到该地址。在注册新用户的过程中,将询问该用户有关接受访问邀请(邀请)的信息。因此,用户参与管理他的帐户,包括密码更改。的缺点不可能在SMS消息中包括传感器读数,只能是预先准备的模板。对于电子邮件,没有模板,只有带有传感器读取板的标准视图。发现了另一个功能,包括处理规则的顺序。有时在接通后立即观察到继电器断开的情况。而且,根据继电器的逻辑,应该假设只有在温度降至28 C以下时才关闭。规则逻辑的条件和设备上的代码不包含错误。通过查看警报日志已解决了该情况,新警报已添加到列表的末尾。有时在接通后立即观察到继电器断开的情况。而且,根据继电器的逻辑,应该假设只有在温度降至28 C以下时才关闭。规则逻辑的条件和设备上的代码不包含错误。通过查看警报日志已解决了该情况,新警报已添加到列表的末尾。有时在接通后立即观察到继电器断开的情况。此外,根据继电器的逻辑,仅在温度降至28 C以下时才应将其关闭。规则的逻辑条件和设备上的代码不包含错误。通过查看警报日志已解决了该情况,新警报已添加到列表的末尾。
英特尔物联网分析
事件39是最后一个要运行的事件,尽管它在时间上早于事件38。尽管温度进一步升高,继电器也会打开一秒钟,然后关闭。可能是由于服务尚未完成。当前,英特尔物联网分析云处于开发阶段(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. 英特尔爱迪生 第一次开始
  2. 英特尔爱迪生 使用英特尔物联网分析云:记录和发送数据
  3. 英特尔爱迪生 与英特尔物联网分析云一起使用:创建规则并发送通知
  4. 英特尔爱迪生 英特尔物联网分析云:在ASP.NET上发送SMS和RESTful客户端

Source: https://habr.com/ru/post/zh-CN384929/


All Articles