Microsoft streaminsight builds Iot

Source: Internet
Author: User

Original article: http://msdn.microsoft.com/zh-cn/magazine/hh852591.aspx

There have been a lot of recent debates about the Internet of Things (IOT), and there are plenty of reasons. Eric sson's CEO, Hans vestberg, predicted that 2020 devices would be connected to the Web by 50 billion (bit. ly/ycis7r [PDF download]). At present, there are about 1.5 billion PCs and less than 1 billion phones connected to the Web-50 billion, which is equivalent to about 7 devices for everyone in the world. This helps you understand this number intuitively! Market research institute IDC predicts that more than 2015 devices will be connected to the Internet by 16 billion (seeFigure 1). It is undeniable that there are some conservative predictions, but with the numbers provided by everyone, we can see that the role of the Internet is changing dramatically-from providing information and entertainment content to connecting services to emerging applications that support devices.


Figure 1 IDC predicts "embedded Internet"

The seemingly credible reason for these huge numbers is that the powerful drivers (business opportunities and inevitability) are driving the rapid growth of such solutions. According to a recent issue in The Economist (economist.com/node/17388368), The tide of the economy is not only rolling forward to the interests of technical companies and ambitious politicians. This development trend has gained momentum because such systems are really needed ."Figure 2The growth trend of such solutions is shown in the field of application. For example, forcing intelligent energy systems in Europe is an inevitable result. If we cannot manage energy consumption, we cannot build the required energy generation functions. In terms of business opportunities, simple and ubiquitous vending machines are a good example. After connecting to the device, you can assign service personnel as needed rather than on an unsatisfactory plan. You can change the price dynamically if your local demand increases or the product is near the expiration date. Report power outages to urge immediate replacement of perishable goods. In other words, the connection achieves a more efficient new business model.


Figure 2 Application growth by industry

By referencing the number of connected devices to describe this change, it will certainly be very good, but it will also lead to some misunderstandings-This change does not mean that some 100,000 of traditional embedded programmers will be fully employed. These devices are only the endpoints of complex solutions that integrate other devices into all aspects of the Internet, including analysis, cloud, web applications, PCs, and mobile interfaces.

Therefore, the method to look at this issue is that everyone who is currently building a web-based application will need to integrate the device and help develop new businesses and new business models. In other words, even if you are not an embedded developer, you are not working in a store that builds embedded devices. This is also a very attractive business opportunity worth your evaluation. Your current Microsoft skills will allow you to succeed in Iot.

Analyze device data

"Data has become a new currency," Kevin Dallas, managing director of the Windows Embedded team, said in a recent interview (bit. ly/wb1td8 ). Compared with the current Internet application, Iot involves information generation, management, and access. Let's compare the data features of typical Internet and IOT applications today. You may use a popular Mechanism shared by multiple financial institutions to pay bills online with millions of other users. You log on multiple times every month, view some pages, and submit payment information. All the data is extracted from the traditional database using the query that you run when you start interacting with the system. The number of pages you download may be large, but there are very few interactions, even if the valuable information they generate (payment information, personal information updates, etc.) needs to be saved for a long time.

Comparing this application with the energy management system, the system may have 50 million buildings (commercial buildings and residential buildings) Providing input. The input is generated by multiple internal local endpoints (for example, houses) using a single aggregated view published to the backend. This data includes real-time usage information and mandatory controls that are returned to the building as the basis for pricing and accounting. This system will frequently interact with data with relatively low value, which is not necessarily meaningful when the current status of your computing system and the trend data of this endpoint. However, the system needs to be able to immediately respond to situations that may threaten its operation (for example, grid overload and power failure caused by a sharp increase in demand. In this case, broadcast information can immediately reduce energy consumption. Such systems need to continuously analyze incoming data and compare development trends to identify models that indicate high risks of power outages.

Figure 3Demonstrate the typical architecture of Iot applications. The bottom of the stack shows various assets or devices that are equipped with different types of sensors based on different application fields. These sensors usually generate continuous data sources required for fast processing and analysis in the application field. Depending on the features of the device, the device may be able to perform some processing locally. This is called local analysis, and tools such as. Net micro framework can help you perform local processing before the device passes data. Iot applications use the Internet Protocol to transmit device data for global analysis. The results of global analysis (for example, the overall operation status of the power grid) are the contents of the management and operation end users or business decision makers. Analysis can also drive a closed system that automatically performs operations based on the situation presented in the incoming data. These methods are useful if assets can receive feedback from global analysis (for example, impact behavior changes or improvement operations. Continuous computing is required to promote global analysis of these processes and provide results as soon as possible. In addition, the analysis frequently references the time and timestamp provided along with the sensor data. Therefore, it is not appropriate to only put such data into the database and run regular queries on it. Fortunately, Microsoft streaminsight supports different methods.


Figure 3 typical architecture of Iot applications

Microsoft streaminsight

Microsoft streaminsight is designed to respond to continuously arriving data in a timely manner without writing data to the disk for analysis and query. Many Iot application fields need to analyze incoming data in almost real time after from the source. Considering the smart grid application we mentioned, it needs to quickly respond to the surging power demand to rebalance the power grid's operational capabilities. Many Iot applications have the same requirements: data processing that requires continuous analysis and high latency. The analysis must be continuous because the data source continuously generates new data. Many solutions need to identify and quickly respond to situations that can only be presented by analyzing incoming data, so they need low-latency analysis and almost immediate results. These requirements make it impractical to store data in a relational database before performing an analysis.

We call these applications an event-driven application, and Iot is a solution where such functions play a role. Streaminsight is a powerful platform for building highly scalable and Low-latency event-driven applications. It is part of Microsoft SQL Server after version 2008 R2 is released. Streaminsight supplements SQL Server in event-driven processing and analysis based on rich presentation time. Streaminsight provides business insights at the speed of data generation, rather than the speed at which traditional database reports are processed.

Analysis results that can be used immediately or enable applications to automatically respond to events can help enterprises better understand their operations in a timely manner and even automate part of their operations. They can also respond more quickly to important situations, opportunities, or trends in sensor or device data.

To write a streaminsight application, developers can use familiar tools such as Microsoft. NET Framework, LINQ, and Microsoft Visual Studio.Figure 4Describes the developer and runtime experience of the streaminsight application and introduces some key concepts.


Figure 4 Development and runtime of the streaminsight Application

Simple Iot applications

Let us have a better understanding of possible Iot application solutions, and then we will build it. In our end-to-end example, we will focus on a simple solution that uses Motion sensors to monitor rotating devices, such as turbines or windmills. This is important because an emergency occurs due to vibration. In this case, the device may fail and serious damage may occur if it is not immediately stopped. To reliably detect this situation, each device is equipped with multiple motion tracking sensors. The surge in motion in a single sensor may only indicate that the sensor's Data Reading is unreliable, but the exceptional and violent movements in multiple sensors at the same time indicate an emergency. For example, for a large turbine, you may want to trigger an alarm or even disable the device automatically. In addition to continuous checks for such situations, we also want to provide the operator with a dashboard that provides a near real-time View of the device status.

To build this solution, we need to meet the following requirements and solve the following technical difficulties:

  • What data does the device need to capture?
  • Which sensors are used to measure data?
  • How does a device send its sensor readings to the Internet?
  • How can we collect device data to a single location for analysis?
  • How can we continuously analyze incoming data and quickly respond to emergencies?
  • How can we associate sensor readings across multiple devices in a timely manner to check global conditions?

Let's take a look at how to meet these requirements and implement an end-to-end solution.

Iot applications: Implementation highlights

The following are some key steps to implement the IOT application described in the previous section. We will first discuss the devices, then go to the visualization of the output, and then go to the Cross-device analysis that fills the dashboard.

Device. To build a sensor device, we first started with netdeskplus, a popular small Development Board that runs the. NET micro framework and has 128 k sram. We added a wi-fi radio, a common fan named wifly gsx breakout, and installed actual sensors, including a three-axis accelerator, on a custom PCB Board. We program devices to send updates per second of sensor readings to Web Services, which act as the center for collecting and processing data from all devices.

We use restful connection for Web Services-it only contains http post with comma-separated name-value pairs. Of course, you can perform this operation from any type of devices that support HTTP. We choose to use. net micro framework, so that the entire application (including devices, web services, streaminsight adapter, Silverlight dashboard, etc.) can all use a single programming model (. and tool chain (Visual Studio. Obviously, if you have. Net skills, you do not need to hire new employees or outsource part of your Iot project to an external embedded store; you have the skills to fully execute it. For example, you can access the analoginput class with just a few lines of code to set the acceleration time and call the read method:


 
  1.           this.analogInputX = new AnalogInput(pinX);
  2. this.analogInputY = new AnalogInput(pinY);
  3. this.analogInputZ = new AnalogInput(pinZ);
  4. ...
  5.           rawZ = analogInputZ.Read();
  6. rawY = analogInputY.Read();
  7. rawX = analogInputX.Read();
  8.        

After reading the sensor input and setting the HTTP message content format, everything required to send data is included inFigure 5.

Figure 5 submit sensor data


 
  1.           protected void submitSensorData(string uri, string payload)
  2. {
  3.   // Message format
  4.   StringBuilder sb = new StringBuilder(256);
  5.   sb.Append(
  6.     "POST /Website/Services/DataService.aspx?method=SaveDeviceData HTTP/1.1\n");
  7.   sb.Append("User-Agent: NetduinoPlus\n");
  8.   sb.Append("Host: 192.168.1.101\n");
  9.   sb.Append("Connection: Keep-Alive\n");
  10.   sb.Append("Content-Length: ");
  11.   sb.Append(payload.Length.ToString());
  12.   sb.Append("\n");
  13.   sb.Append(payload);
  14.   sb.Append("\n");
  15.   try
  16.   {
  17.     HttpResponse response = webServer.SendRequest(uri, 80, request);
  18.   }
  19.   catch
  20.   {
  21.     ...
  22.           }
  23. }
  24.        

On the server side, we implement the savedevicedata method. The device needs to publish its messages to this method. We split the message string and analyzed the MAC address, timestamp, and load data, such as the motion readings from the accelerator. We use all this information to fill in the devicedata object passed to streaminsight for subsequent analysis (seeFigure 6).

Figure 6 fill in the devicedata object


 
  1.           private int SaveDeviceData()
  2. {
  3. ...
  4.           List<string> data = record.Split(',').ToList();
  5.   DeviceData deviceData = new DeviceData();
  6.   deviceData.MAC = NormalizeMAC(data[0].Trim());
  7.   deviceData.DateTime = DateTime.UtcNow;
  8. ...
  9.           deviceData.Motion = Convert.ToDecimal(data[2].Substring(data[2].IndexOf(":") + 1));
  10. ...
  11.           // Communicate each new device data record to StreamInsight          
  12.   DeviceDataStreaming streaming = (DeviceDataStreaming)
  13.     HttpContext.Current.Application[Global.StreamingIdentifier];
  14.     streaming.TrackDeviceData(deviceData);
  15. ...
  16.           }
  17.        

Dashboard. Now we want to build a dashboard that allows device operators to view the current status of sensors on the device. For ease of demonstration, we will focus on only one device.Figure 7An example of this type of dashboard is displayed. Let's start from the Left to view different views of sensor data.


Figure 7 dashboard for device monitoring

Moving Average view: the data grid in the lower left corner shows the sensor readings of the device, including light, temperature, and motion values, device ID, and timestamp. As you can see from the timestamp, these values are updated once per second. However, the dashboard does not display the original sensor value, but displays the moving average of sensor data within 10 seconds. This means that the value is updated every second using the average number of data in the last 10 seconds. Moving Average is a common simple technique that prevents occasional outliers and undesirable data when low-cost sensors are used.

Trend line view: in the lower right corner, the dashboard displays the trend line of the sensor. The trend of the trend line view is determined by the moving average displayed in the left-side data grid.

Alarm view: the data grid of alarms is displayed in the view in the upper-right corner. If critical conditions are detected, an alarm is triggered that displays time and other information (such as severity and status.

Analysis. Now let's take a look at the behind-the-scenes operations and discuss the analysis of processing incoming sensor data and calculating the visualized results of the dashboard. We use streaminsight for analysis. The following types indicate device data, including the MAC address, timestamp, and sensor value:


 
  1.           public class DeviceData
  2. {
  3.   public string MAC { get; set; }
  4.   public DateTime DateTime { get; set; }
  5.   public decimal?
  6.           Light { get; set; }
  7.   public decimal?
  8.           Temperature { get; set; }
  9.   public decimal?
  10.           Motion { get; set; }
  11. }
  12.        

This class defines the shape of a single event, but we want to start to discuss many events. To this end, we have defined the observable data source for streaminsight. This is only the data source that implements the system. iobservable interface:


 
  1.           public class DeviceDataObservable : IObservable<DeviceData>
  2.   {
  3.     ...
  4.           }
  5.        

After defining the. NET Framework sequence (such as enumerable or similar observable), you can write streaminsight queries for these sets. Let's take a quick look at some of the key queries. The first query obtains the observable as the input and generates the streaminsight point event stream. The "datetime" field in the device data is used as the time stamp of the streaminsight event. In the next LINQ statement, we obtain the stream as the input and group the data according to the MAC address. For each group, we then apply the hop window with a window size of 10 seconds (a part of the time-based events) and re-calculate the window every second. In each window, we calculate the average temperature, light, and motion. This allows us to recalculate the moving average of each device every second.Figure 8Shows the code used to return results in streaminsight event stream form.

Figure 8 obtain the moving average


 
  1.           public static CepStream<AverageSensorValues> GroupedAverages(
  2.               Application application,
  3.               DeviceDataObservable source)
  4.   {
  5.     var q1 = from e1 in source.ToPointStream(application,
  6.       e => PointEvent.CreateInsert(
  7.         new DateTimeOffset(
  8.           e.DateTime.ToUniversalTime()),e),
  9.       AdvanceTimeSettings.StrictlyIncreasingStartTime,
  10.       "Device Data Input Stream")
  11.              select e1;
  12.     var q2 = from e2 in q1
  13.              group e2 by e2.MAC into groups
  14.              from w in groups.HoppingWindow(
  15.                TimeSpan.FromSeconds(10),
  16.                TimeSpan.FromSeconds(1))
  17.              select new AverageSensorValues
  18.              {
  19.                DeviceId = groups.Key,
  20.                Timestamp = null,
  21.                AvgTemperature = w.Avg(t => t.Temperature),
  22.                AvgLight = w.Avg(t => t.Light),
  23.                AvgMotion = w.Avg(t => t.Motion)
  24.              };
  25.     return q2;
  26.   }
  27.        

This is the optimal location for alarm query. Remember that an alarm is triggered when the readings of multiple motion sensors are higher than the motion threshold value at the same time. You only need to use several streaminsight LINQ statements for the group average that you just calculated to solve this problem. By expressing changes to the alert threshold as an event stream named alarmthresholdsignal, the first query Q3 applies an excellent technique. This query connects the threshold value to the average stream from the previous query, and then only filters events that exceed the threshold value:


 
  1.           var q3 = from sensor in GroupedAverages(application, source)
  2.          from refdata in AlarmThresholdSignal(application, alarmsthresholds)
  3.          where (sensor.AvgMotion !=
  4.            null && (double) sensor.AvgMotion > refdata.Threshold)
  5.          select new
  6.          {
  7.            AlarmDevice = sensor.DeviceId,
  8.            AlarmInfo = "This is a test alarm for a single device",
  9.          };
  10.        

The next query uses the streaminsight snapshot window to identify the time point when the event status changes. If a new event is generated in the previous filter query, this is a new snapshot, and the snapshot operation generates a new window, which contains all events consistent or overlapping with the events that trigger the snapshot window. The following code counts events that are higher than the alarm threshold when a snapshot window is created:


 
  1.           var alarmcount = from win in q3.SnapshotWindow()
  2.                  select new
  3.                  {
  4.                    AlarmCount = win.Count()
  5.                  };
  6.        

The last step is to check whether the count shows that multiple devices will issue an alarm:


 
  1.           var filteralarms = from f in alarmcount
  2.                    where f.AlarmCount >= 2
  3.                    select new AlarmEvent
  4.                    {
  5.                      AlarmTime = null,
  6.                      AlarmInfo = "Now we have an alarm across multiple devices",
  7.                      AlarmKind = 0,
  8.                      AlarmSeverity = 10,
  9.                      AlarmStatus = 1
  10.                    };
  11.        

Now, we only need to transmit the output stream containing the average sensor value and alarm from streaminsight to the UI.

Send output to UI

To use streaminsight that generates a result stream on the server side, we need a way to send the stream to the user. Users may not run in server processes and may use lightweight web applications to visualize results. If you use Silverlight, the duplex protocol is convenient because it supports continuous push-based transmission from the server to the client. HTML5 web sockets are also a compelling alternative. In any case, you want to easily add new analyses on the server side and easily connect them to the UI without splitting the client-server interface between the UI and processes that host streaminsight. If the load between the UI and the server is moderate, You Can serialize the server result into XML and deserialize it on the client. In this way, you only need to pay attention to the XML in the line and your client-server interface, and the additional cookie indicating the type of deserialization. Below are several key codes.

The first code segment is the Windows Communication Foundation protocol, which is used to transmit event data in XML serialized string form and indicates the type of guid:


 
  1.           [ServiceContract]
  2. public interface IDuplexClient
  3. {
  4.   [OperationContract(IsOneWay = true)]
  5.   void Receive(string eventData, Guid guid);
  6. }
  7.        

Now, we can use data protocols to add comments to the result event structure for serialization, as shown inFigure 9.

Figure 9 Add an annotation to the event structure


 
  1.           [DataContract]
  2. public class AverageSensorValues : BaseEvent
  3. {
  4.   [DataMember]
  5.   public new static Guid TypeGuid =
  6.     Guid.Parse("{F67ECF8B-489F-418F-A01A-43B606C623AC}");
  7.   public override Guid GetTypeGuid() { return TypeGuid; }
  8.   [DataMember]
  9.   public string DeviceId { get; set; }
  10.   [DataMember]
  11.   public DateTime?
  12.           Timestamp { get; set; }
  13.   [DataMember]
  14.   public decimal?
  15.           AvgLight { get; set; }
  16.   [DataMember]
  17.   public decimal?
  18.           AvgTemperature { get; set; }
  19.   [DataMember]
  20.   public decimal?
  21.           AvgMotion { get; set; }
  22. }
  23.        

Now, we can easily serialize the server result event and send it to the client, as shown in figureFigure 10.

Figure 10 sending result events from the server


 
  1.           static public void CallClient<T>(T eventData) where T : BaseEvent
  2.   {
  3.     if (null != client)
  4.     {
  5.       var xmlSerializer = new XmlSerializer(typeof(T));
  6.       var stringBuilder = new StringBuilder();
  7.       var stringWriter = new StringWriter(stringBuilder);
  8.       xmlSerializer.Serialize(stringWriter, eventData);
  9.       client.Receive(stringBuilder.ToString(), eventData.GetTypeGuid());
  10.     }
  11.   }
  12.        

On the client side, we deserialize the events in the callback method of the duplex service, and then branch the events to different methods based on the types of events received, suchFigure 11.

Figure 11 receive and deserialize events on the client


 
  1.           void proxy_ReceiveReceived(object sender, ReceiveReceivedEventArgs e)
  2. {
  3.   if (e.Error == null)
  4.   {
  5.     if (AverageSensorValues.TypeGuid == e.guid)
  6.     {
  7.       ProcessAverageSensorValues(Deserialize<AverageSensorValues>(e.eventData));
  8.     }
  9.     else if (AlarmEvent.TypeGuid == e.guid)
  10.     {
  11.       ProcessAlarms(Deserialize<AlarmEvent>(e.eventData));
  12.     }
  13.     else
  14.     {
  15.       ProcessUnknown();
  16.     }
  17.   }
  18. }
  19.        

With these queries and transferred to the corresponding web application, you can now select several devices and shake them until some device readings exceed the alert threshold. The UI then generates one of these red alarms, as shown in figureFigure 12.


Figure 12 device dashboard containing alerts

Because new data is constantly entering almost real-time dashboards, observablecollections is extremely useful for updating the UI. If you make the data network and trend line based on these observable sets, you do not have to worry about updating the part in the code. These sets will automatically perform this operation for you in the background.

Prospect

In this implementation, the device communicates with a conventional web service, which can run on a common PC connected to the Internet. However, cloud computing is an attractive alternative. You do not have to possess hardware and run software for your web servers. Services in the cloud can act as centers for your applications to collect data from all devices. This allows you to easily and flexibly scale your processing capabilities when the number of devices increases or when you deploy other analyses on device data. Microsoft plans to provide the streaminsight feature as a service in Windows azure (the streaminsight project code name "Austin. By providing predefined communication endpoints and protocols, Austin allows you to easily connect devices to the rich analysis and processing capabilities in the Microsoft cloud. If you deploy Iot applications in Windows Azure, you will automatically receive cloud benefits such as flexible scalability and pay-as-you-go, allowing you to manage device connections and perform rich analysis on device data.

Another major change involves the recent W3C standardization work. The most important plan for Iot applications is HTML5 and web sockets. HTML5 provides a platform for a wide range of web applications (such as dashboards we implement. Websocket simplifies the full-duplex TCP-based communication between the browser and the web server, especially the PUSH model for the result transfer required for continuous processing of sensor data.

Connected devices create an exciting new application field, and Microsoft now provides tools for building these Iot applications. This section describes how to use your. NET Framework skills through familiar interfaces at the device level and how to provide data for streaminsight's powerful analysis functions through Web Services. Start building your Iot application with the connected device now!

Torsten grabs Is the chief project manager of Microsoft SQL Server department. He has more than 10 years of experience in Microsoft SQL Server products and obtained a doctorate in computer science from Swiss Federal Institute of Technology in Zurich, Switzerland.

Colin Miller I have been working in the PC software field for 25 years (15 years for Microsoft). I have studied databases, desktop publishing, consumer products, word, Internet Explorer, and passport (liveid) and online services. He is the product department manager of. Net micro framework.

We sincerely thank the following technical experts for reviewing this article:Rafael Fernandez MoctezumaAndLorenz tessiore

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.