Blog

How to Use Snap Packages when Collecting IoT Data with Predix Time Series

Ilya Drabenia

canonical-snaps-ge-predix-time-series-demo-app

Snaps, designed by Canonical, are intended for packaging applications and their dependencies, along with the instructions for running these applications. In this article, we focus on how to write time-series data from your Linux environment to the Predix Time Series service using a snap.

 

Scenario

We will use a data generator to create a sample data stream from a device to Predix. Our previous post provides more details about this generator.

The sensor.go file:

rand.Seed(time.Now().UTC().UnixNano())    
for _ = range time.Tick(time.Second) {
    m := a.IngestMessage()
    if m != nil {
        t, _ := m.AddTag("test_tag")
        v := randInt(0, 100)
        t.AddDatapoint(measurement.Int(v), dataquality.Good)
        e := m.Send()
        if e != nil {
            fmt.Printf("Ingesting failed %s\n", e)
        } else {
            fmt.Printf("Send %d\n", v)
        }
    } else {
        fmt.Println("Creating ingest message failed")
        os.Exit(1)
    }
}

You can find more information, including the source code of the solution and a Go library for interacting with the Predix Time Series API, in this GitHub repository.

 

Packaging the generator into a snap

For creating a snap, you need to install the Snapcraft tool first. Then, declare the snap configuration that is provided below.

The snapcraft.yaml file:

name: predix-ts-sample
version: "1.0"
summary: "This is a Predix Time-Series Sample Application."
description: |
 A Predix Time-Series Sample Application using the Golang
 Predix bindings.
grade: stable
confinement: strict

apps:
 predix-ts-sample:
   command: sensor
   plugs: [network]

parts:
 predix-sensor-demo:
   source: .
   plugin: go

In this file, specify the name and the version of your snap. To build the snap, we also include the command for starting the application and its source code.

Then, run the snapcraft command, which compiles source code and packages it into a snap.

canonical-snaps-ge-predix-time-series-demo

After that, you can publish your snap to a snap repository.

 

Releasing the snap

Before releasing the snap, register an Ubuntu One account and then run the following commands:

$ snapcraft login
$ snapcraft register predix-ts-sample

Now, you should be able to push your application to a snap store.

canonical-snaps-ge-predix-time-series-pushing

Finally, you can release this snap.

canonical-snaps-ge-predix-time-series-releasing

 

Verifying the snap

You can check that the snap was really published in the uApp Explorer app store, and the predix-ts-sample application is now located there.

canonical-snaps-ge-predix-time-series-publishing

You can install this snap on your device using the following command:

$ sudo snap install predix-ts-sample

To run the snap, enter predix-ts-sample on the command line:

canonical-snaps-ge-predix-time-series-demo-running

After providing all required configuration parameters, you will see the next output:

canonical-snaps-ge-predix-time-series-running-output

 

Conclusions

In this tutorial, we tried working with the new package management system from Canonical that is focused on the reliability and isolated execution of snaps. Device engineers can install multiple snaps and create a whole system from reusable building blocks almost without programming. Judging from our experience with snaps, developing, as well as publishing, them is quite easy, and the process is supported by good enough documentation.

 

Related reading

 

About the author

Ilya Drabenia is a Technical Lead at Altoros. He has broad experience in building software architectures, including design and development of complex solutions. Ilya is passionate about microservices, domain-driven design, as well as scalable and parallel algorithms. He also holds an MS degree in Computer Science. See his profile on GitHub.


For the next parts of this series, subscribe to our blog or follow @altoros.

Get new posts right in your inbox!

No Comments

Benchmarks and Research

Subscribe to new posts

Get new posts right in your inbox!