Upgrade dependencies and improve demos

This commit is contained in:
Devis Lucato 2017-01-25 15:52:41 -08:00
Родитель a7f5e8778d
Коммит 5519b1074d
35 изменённых файлов: 127 добавлений и 199 удалений

Просмотреть файл

@ -2,7 +2,7 @@ jdk: oraclejdk8
language: scala
scala:
- 2.11.8
- 2.12.0
- 2.12.1
cache:
directories:
- "$HOME/.ivy2"

Просмотреть файл

@ -293,8 +293,8 @@ The complete configuration reference (and default value) is available in
## Samples
The project includes multiple demos, showing some of the use cases and how IoThub React API works.
All the demos require an instance of Azure IoT hub, with some devices and messages.
The project includes multiple demos in Java and Scala, showing some of the use cases and how IoThub
React API works. All the demos require an instance of Azure IoT hub, with some devices and messages.
1. **DisplayMessages** [Java]: how to stream Azure IoT hub withing a Java application, filtering
temperature values greater than 60C
@ -320,7 +320,7 @@ All the demos require an instance of Azure IoT hub, with some devices and messag
connected devices.
We provide a [device simulator](tools/devices-simulator/README.md) in the tools section,
which will help simulating some devices sending sample telemetry.
which will help simulating some devices sending sample telemetry messages.
When ready, you should either edit the `application.conf` configuration files
([scala](samples-scala/src/main/resources/application.conf) and
@ -328,14 +328,14 @@ When ready, you should either edit the `application.conf` configuration files
with your credentials, or set the corresponding environment variables.
Follow the instructions in the previous section on how to set the correct values.
The sample folders include also some scripts showing how to setup the environment variables in
[Linux/MacOS](samples-scala/setup-env-vars.sh) and [Windows](samples-scala/setup-env-vars.bat).
The root folder includes also a script showing how to set the environment variables in
[Linux/MacOS](setup-env-vars.sh) and [Windows](setup-env-vars.bat).
* [`samples-scala`](samples-scala/src/main/scala):
You can use `sbt run` to run the demos (or the `run_samples.*` scripts)
* [`samples-java`](samples-java/src/main/java):
You can use `mvn clean compile exec:java -Dexec.mainClass="DisplayMessages.Demo"` to run the
demo app (or the `run_samples.*` scripts)
The demos can be executed using the scripts included in the root folder (`run_<language>_samples.sh`
and `run_<language>_samples.cmd`):
* [`run_scala_samples.sh`](run_scala_samples.sh): execute Scala demos
* [`run_java_samples.sh`](run_java_samples.sh): execute Java demos
## Future work (MoSCoW)

Просмотреть файл

@ -3,40 +3,54 @@
name := "iothub-react"
organization := "com.microsoft.azure.iot"
version := "0.8.0"
//version := "0.8.0-DEV.170106a"
//version := "0.9.0"
version := "0.9.0-DEV.170307a"
scalaVersion := "2.12.1"
crossScalaVersions := Seq("2.11.8", "2.12.1")
libraryDependencies <++= (scalaVersion) {
scalaVersion ⇒
val azureEventHubSDKVersion = "0.9.0"
val azureStorageSDKVersion = "4.4.0"
val iothubDeviceClientVersion = "1.0.15"
val iothubServiceClientVersion = "1.0.10"
val scalaTestVersion = "3.0.1"
val datastaxDriverVersion = "3.1.1"
val json4sVersion = "3.5.0"
val akkaStreamVersion = "2.4.16"
Seq(
// Library dependencies
"com.microsoft.azure.iothub-java-client" % "iothub-java-service-client" % iothubServiceClientVersion,
"com.microsoft.azure" % "azure-eventhubs" % azureEventHubSDKVersion,
"com.microsoft.azure" % "azure-storage" % azureStorageSDKVersion,
"com.datastax.cassandra" % "cassandra-driver-core" % datastaxDriverVersion,
"com.typesafe.akka" %% "akka-stream" % akkaStreamVersion,
"org.json4s" %% "json4s-native" % json4sVersion,
"org.json4s" %% "json4s-jackson" % json4sVersion,
// https://github.com/Azure/azure-iot-sdk-java/releases
"com.microsoft.azure.sdk.iot" % "iot-service-client" % "1.0.14",
// Tests dependencies
"org.scalatest" %% "scalatest" % scalaTestVersion % "test",
"com.microsoft.azure.iothub-java-client" % "iothub-java-device-client" % iothubDeviceClientVersion % "test"
// https://github.com/Azure/azure-event-hubs-java/releases
"com.microsoft.azure" % "azure-eventhubs" % "0.11.0",
// https://github.com/Azure/azure-storage-java/releases
"com.microsoft.azure" % "azure-storage" % "5.0.0",
// http://mvnrepository.com/artifact/com.datastax.cassandra/cassandra-driver-core
"com.datastax.cassandra" % "cassandra-driver-core" % "3.1.4",
// https://github.com/akka/akka/releases
"com.typesafe.akka" %% "akka-stream" % "2.4.17",
// https://github.com/json4s/json4s/releases
"org.json4s" %% "json4s-native" % json4sVersion,
"org.json4s" %% "json4s-jackson" % json4sVersion
)
}
lazy val root = project.in(file(".")).configs(IntegrationTest)
// Test dependencies
libraryDependencies <++= (scalaVersion) {
scalaVersion ⇒
Seq(
// https://github.com/scalatest/scalatest/releases
"org.scalatest" %% "scalatest" % "3.0.1" % "test",
// https://github.com/Azure/azure-iot-sdk-java/releases
"com.microsoft.azure.sdk.iot" % "iot-device-client" % "1.0.19" % "test"
)
}
lazy val iotHubReact = project.in(file(".")).configs(IntegrationTest)
lazy val samplesScala = project.in(file("samples-scala")).dependsOn(iotHubReact)
lazy val samplesJava = project.in(file("samples-java")).dependsOn(iotHubReact)
/* Publishing options
* see http://www.scala-sbt.org/0.13/docs/Artifacts.html
@ -70,7 +84,7 @@ pomExtra :=
/** Miscs
*/
logLevel := Level.Debug // Debug|Info|Warn|Error
logLevel := Level.Warn // Debug|Info|Warn|Error
scalacOptions ++= Seq("-deprecation", "-explaintypes", "-unchecked", "-feature")
showTiming := true
fork := true

18
run_java_samples.cmd Normal file
Просмотреть файл

@ -0,0 +1,18 @@
@ECHO OFF
REM sbt samplesJava/run
ECHO Select demo to run:
ECHO.
ECHO [1] DisplayMessages.Main
ECHO [2] SendMessageToDevice.Main
ECHO.
SET /P opt="Enter number: "
cd samples-java
if "%opt%"=="1" mvn clean compile exec:java -Dexec.mainClass="DisplayMessages.Main"
if "%opt%"=="2" mvn clean compile exec:java -Dexec.mainClass="SendMessageToDevice.Main"

20
run_java_samples.sh Executable file
Просмотреть файл

@ -0,0 +1,20 @@
#sbt samplesJava/run
echo "Select demo to run:"
echo
echo " [1] DisplayMessages.Main"
echo " [2] SendMessageToDevice.Main"
echo
read -p "Enter number: " -r opt
cd samples-java
if [ "$opt" = "1" ]; then
mvn clean compile exec:java -Dexec.mainClass="DisplayMessages.Main"
fi
if [ "$opt" = "2" ]; then
mvn clean compile exec:java -Dexec.mainClass="SendMessageToDevice.Main"
fi

1
run_scala_samples.cmd Normal file
Просмотреть файл

@ -0,0 +1 @@
sbt samplesScala/run

1
run_scala_samples.sh Executable file
Просмотреть файл

@ -0,0 +1 @@
sbt samplesScala/run

3
samples-java/build.sbt Normal file
Просмотреть файл

@ -0,0 +1,3 @@
// Copyright (c) Microsoft. All rights reserved.
scalaVersion := "2.12.1"

Просмотреть файл

@ -19,7 +19,7 @@
<dependency>
<groupId>com.microsoft.azure.iot</groupId>
<artifactId>iothub-react_2.12</artifactId>
<version>0.8.0-DEV.161101a</version>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>

Просмотреть файл

@ -0,0 +1 @@
sbt.version=0.13.13

Просмотреть файл

@ -1,2 +0,0 @@
mvn clean compile exec:java -Dexec.mainClass="DisplayMessages.Demo"

Просмотреть файл

@ -1,2 +0,0 @@
mvn clean compile exec:java -Dexec.mainClass="DisplayMessages.Demo"

Просмотреть файл

@ -8,8 +8,8 @@ import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.iot.iothubreact.MessageFromDevice;
import com.microsoft.azure.iot.iothubreact.javadsl.PartitionList;
import com.microsoft.azure.iot.iothubreact.javadsl.IoTHub;
import com.microsoft.azure.iot.iothubreact.javadsl.PartitionList;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
@ -22,7 +22,7 @@ import static java.lang.System.out;
/**
* Retrieve messages from IoT hub and display the data in the console
*/
public class Demo extends ReactiveStreamingApp
public class Main extends ReactiveStreamingApp
{
static ObjectMapper jsonParser = new ObjectMapper();
@ -56,6 +56,7 @@ public class Demo extends ReactiveStreamingApp
});
}
@SuppressWarnings("unchecked")
public static Temperature parseTemperature(MessageFromDevice m)
{
try

Просмотреть файл

@ -9,8 +9,8 @@ import akka.stream.Materializer;
/**
* Initialize reactive streaming
*/
public class ReactiveStreamingApp {
public class ReactiveStreamingApp
{
private static ActorSystem system = ActorSystem.create("Demo");
protected final static Materializer streamMaterializer = ActorMaterializer.create(system);

Просмотреть файл

@ -22,7 +22,7 @@ import static java.lang.System.out;
/**
* Retrieve messages from IoT hub and display the data in the console
*/
public class Demo extends ReactiveStreamingApp
public class Main extends ReactiveStreamingApp
{
static ObjectMapper jsonParser = new ObjectMapper();
@ -63,6 +63,7 @@ public class Demo extends ReactiveStreamingApp
});
}
@SuppressWarnings("unchecked")
public static Temperature parseTemperature(MessageFromDevice m)
{
try

Просмотреть файл

@ -11,7 +11,6 @@ import akka.stream.Materializer;
*/
public class ReactiveStreamingApp
{
private static ActorSystem system = ActorSystem.create("Demo");
protected final static Materializer streamMaterializer = ActorMaterializer.create(system);

Просмотреть файл

@ -3,7 +3,7 @@
// @see http://doc.akka.io/docs/akka/2.4.10/scala/logging.html
akka {
# Options: OFF, ERROR, WARNING, INFO, DEBUG
loglevel = "INFO"
loglevel = "DEBUG"
}
iothub-react {
@ -51,7 +51,7 @@ iothub-react {
// Checkpoints frequency (best effort), for each IoT hub partition
// Min: 1 second, Max: 1 minute
frequency = 10s
frequency = 5s
// How many messages to stream before saving the position, for each IoT hub partition.
// Since the stream position is saved in the Source, before the rest of the
@ -62,7 +62,7 @@ iothub-react {
// Store a position if its value is older than this amount of time, ignoring the threshold.
// For instance when the telemetry stops, this will force to write the last offset after some time.
// Min: 1 second, Max: 1 hour. Value is rounded to seconds.
timeThreshold = 30s
timeThreshold = 10s
storage {

Просмотреть файл

@ -1,18 +1,10 @@
// Copyright (c) Microsoft. All rights reserved.
scalaVersion := "2.12.0"
crossScalaVersions := Seq("2.11.8", "2.12.0")
scalaVersion := "2.12.1"
scalacOptions ++= Seq("-deprecation", "-explaintypes", "-unchecked", "-feature")
// This repository contains development snapshots. Production releases are in Maven Central.
resolvers += "Dev Snapshots" at "https://dl.bintray.com/microsoftazuretoketi/toketi-repo"
libraryDependencies ++= {
val prodVersion = "0.7.0"
val devVersion = "0.8.0-DEV.161101a"
Seq(
"com.microsoft.azure.iot" %% "iothub-react" % devVersion
)
libraryDependencies <++= (scalaVersion) {
scalaVersion ⇒
Seq(
"com.microsoft.azure" % "azure-eventhubs-eph" % "0.11.0"
)
}

Просмотреть файл

@ -1 +1 @@
sbt.version = 0.13.12
sbt.version=0.13.13

Просмотреть файл

@ -1 +0,0 @@
sbt run

Просмотреть файл

@ -1 +0,0 @@
sbt run

Просмотреть файл

@ -1,41 +0,0 @@
:: Populate the following environment variables, and execute this file before running
:: IoT Hub to Cassandra.
::
:: For more information about where to find these values, more information here:
::
:: * https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-create-through-portal#endpoints
:: * https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-java-java-getstarted
::
::
:: Example:
::
:: SET IOTHUB_EVENTHUB_NAME = "my-iothub-one"
::
:: SET IOTHUB_EVENTHUB_ENDPOINT = "sb://iothub-ns-myioth-75186-9fb862f912.servicebus.windows.net/"
::
:: SET IOTHUB_EVENTHUB_PARTITIONS = 4
::
:: SET IOTHUB_IOTHUB_ACCESS_POLICY = "service"
::
:: SET IOTHUB_ACCESS_KEY = "6XdRSFB9H61f+N3uOdBJiKwzeqbZUj1K//T2jFyewN4="
::
:: SET IOTHUB_ACCESS_HOSTNAME = "my-iothub-one.azure-devices.net"
::
:: see: Endpoints ⇒ Messaging ⇒ Events ⇒ "Event Hub-compatible name"
SET IOTHUB_EVENTHUB_NAME = ""
:: see: Endpoints ⇒ Messaging ⇒ Events ⇒ "Event Hub-compatible endpoint"
SET IOTHUB_EVENTHUB_ENDPOINT = ""
:: see: Endpoints ⇒ Messaging ⇒ Events ⇒ Partitions
SET IOTHUB_EVENTHUB_PARTITIONS = ""
:: see: Shared access policies, we suggest to use "service" here
SET IOTHUB_IOTHUB_ACCESS_POLICY = ""
:: see: Shared access policies ⇒ key name ⇒ Primary key
SET IOTHUB_ACCESS_KEY = ""
:: see: Shared access policies ⇒ key name ⇒ Connection string ⇒ HostName
SET IOTHUB_ACCESS_HOSTNAME = ""

Просмотреть файл

@ -1,41 +0,0 @@
# Populate the following environment variables, and execute this file before running
# IoT Hub to Cassandra.
#
# For more information about where to find these values, more information here:
#
# * https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-create-through-portal#endpoints
# * https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-java-java-getstarted
#
#
# Example:
#
# $env:IOTHUB_EVENTHUB_NAME = 'my-iothub-one'
#
# $env:IOTHUB_EVENTHUB_ENDPOINT = 'sb://iothub-ns-myioth-75186-9fb862f912.servicebus.windows.net/'
#
# $env:IOTHUB_EVENTHUB_PARTITIONS = 4
#
# $env:IOTHUB_IOTHUB_ACCESS_POLICY = 'service'
#
# $env:IOTHUB_ACCESS_KEY = '6XdRSFB9H61f+N3uOdBJiKwzeqbZUj1K//T2jFyewN4='
#
# SET IOTHUB_ACCESS_HOSTNAME = "my-iothub-one.azure-devices.net"
#
# see: Endpoints ⇒ Messaging ⇒ Events ⇒ "Event Hub-compatible name"
$env:IOTHUB_EVENTHUB_NAME = ''
# see: Endpoints ⇒ Messaging ⇒ Events ⇒ "Event Hub-compatible endpoint"
$env:IOTHUB_EVENTHUB_ENDPOINT = ''
# see: Endpoints ⇒ Messaging ⇒ Events ⇒ Partitions
$env:IOTHUB_EVENTHUB_PARTITIONS = ''
# see: Shared access policies, we suggest to use "service" here
$env:IOTHUB_IOTHUB_ACCESS_POLICY = ''
# see: Shared access policies ⇒ key name ⇒ Primary key
$env:IOTHUB_ACCESS_KEY = ''
# see: Shared access policies ⇒ key name ⇒ Connection string ⇒ HostName
$env:IOTHUB_ACCESS_HOSTNAME = ''

Просмотреть файл

@ -1,41 +0,0 @@
# Populate the following environment variables, and execute this file before running
# IoT Hub to Cassandra.
#
# For more information about where to find these values, more information here:
#
# * https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-create-through-portal#endpoints
# * https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-java-java-getstarted
#
#
# Example:
#
# export IOTHUB_EVENTHUB_NAME="my-iothub-one"
#
# export IOTHUB_EVENTHUB_ENDPOINT="sb://iothub-ns-myioth-75186-9fb862f912.servicebus.windows.net/"
#
# export IOTHUB_EVENTHUB_PARTITIONS=4
#
# export IOTHUB_IOTHUB_ACCESS_POLICY="service"
#
# export IOTHUB_ACCESS_KEY="6XdRSFB9H61f+N3uOdBJiKwzeqbZUj1K//T2jFyewN4="
#
# export IOTHUB_ACCESS_HOSTNAME="my-iothub-one.azure-devices.net"
#
# see: Endpoints ⇒ Messaging ⇒ Events ⇒ "Event Hub-compatible name"
export IOTHUB_EVENTHUB_NAME=""
# see: Endpoints ⇒ Messaging ⇒ Events ⇒ "Event Hub-compatible endpoint"
export IOTHUB_EVENTHUB_ENDPOINT=""
# see: Endpoints ⇒ Messaging ⇒ Events ⇒ Partitions
export IOTHUB_EVENTHUB_PARTITIONS=""
# see: Shared access policies, we suggest to use "service" here
export IOTHUB_IOTHUB_ACCESS_POLICY=""
# see: Shared access policies ⇒ key name ⇒ Primary key
export IOTHUB_ACCESS_KEY=""
# see: Shared access policies ⇒ key name ⇒ Connection string ⇒ HostName
export IOTHUB_ACCESS_HOSTNAME=""

Просмотреть файл

@ -22,7 +22,7 @@ iothub-react {
// see: Endpoints ⇒ Messaging ⇒ Events ⇒ Partitions
hubPartitions = ${?IOTHUB_EVENTHUB_PARTITIONS}
// see: Shared access policies
// see: "IoT Hub" ⇒ your hub ⇒ "Shared access policies"
// e.g. you should use the predefined "service" policy
accessPolicy = ${?IOTHUB_ACCESS_POLICY}

Просмотреть файл

@ -9,7 +9,7 @@ import com.microsoft.azure.iot.iothubreact.ResumeOnError._
import com.microsoft.azure.iot.iothubreact.filters._
import com.microsoft.azure.iot.iothubreact.scaladsl._
import com.microsoft.azure.iot.iothubreact.{MessageFromDevice, MessageToDevice}
import com.microsoft.azure.iot.service.sdk.DeliveryAcknowledgement
import com.microsoft.azure.sdk.iot.service.sdk.DeliveryAcknowledgement
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -8,6 +8,9 @@ import java.util
import com.microsoft.azure.eventhubs.EventData
import com.microsoft.azure.servicebus.amqp.AmqpConstants
import scala.collection.JavaConverters._
import scala.language.{implicitConversions, postfixOps}
/* MessageFromDevice factory */
private object MessageFromDevice {
@ -48,7 +51,7 @@ class MessageFromDevice(data: Option[EventData], val partition: Option[Int]) {
private[this] lazy val systemProps = data.get.getSystemProperties()
// Meta properties set by the device
lazy val properties: util.Map[String, String] = data.get.getProperties()
lazy val properties: util.Map[String, String] = data.get.getProperties().asScala.map(x (x._1, x._2.toString)).asJava
// Whether this is a keep alive message generated by the stream and not by IoT hub
val isKeepAlive: Boolean = (partition == None)

Просмотреть файл

@ -6,7 +6,7 @@ import java.nio.charset.StandardCharsets
import java.time.Instant
import java.util.Date
import com.microsoft.azure.iot.service.sdk.{DeliveryAcknowledgement, Message}
import com.microsoft.azure.sdk.iot.service.sdk.{DeliveryAcknowledgement, Message}
import scala.collection.JavaConverters._
import scala.collection.mutable.Map

Просмотреть файл

@ -5,11 +5,11 @@ package com.microsoft.azure.iot.iothubreact
import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler}
import akka.stream.{Attributes, FlowShape, Inlet, Outlet}
private[iothubreact] class StreamManager[A]
extends GraphStage[FlowShape[A, A]] {
private[iothubreact] class StreamManager
extends GraphStage[FlowShape[MessageFromDevice, MessageFromDevice]] {
private[this] val in = Inlet[A]("StreamCanceller.Flow.in")
private[this] val out = Outlet[A]("StreamCanceller.Flow.out")
private[this] val in = Inlet[MessageFromDevice]("StreamCanceller.Flow.in")
private[this] val out = Outlet[MessageFromDevice]("StreamCanceller.Flow.out")
private[this] var closeSignal = false
override val shape = FlowShape.of(in, out)
@ -20,7 +20,10 @@ private[iothubreact] class StreamManager[A]
new GraphStageLogic(shape) {
setHandler(in, new InHandler {
override def onPush(): Unit = push(out, grab(in))
override def onPush(): Unit = {
val message: MessageFromDevice = grab(in)
push(out, message)
}
})
setHandler(out, new OutHandler {

Просмотреть файл

@ -20,7 +20,7 @@ case class IoTHub() extends Logger {
// TODO: Provide ClearCheckpoints() method to clear the state
private[this] val streamManager = new StreamManager[MessageFromDevice]
private[this] val streamManager = new StreamManager
private[this] def allPartitions = Some(PartitionList(0 until Configuration.iotHubPartitions))

Просмотреть файл

@ -9,7 +9,7 @@ import akka.japi.function.Procedure
import akka.stream.javadsl.{Sink JavaSink}
import akka.stream.scaladsl.{Sink ScalaSink}
import com.microsoft.azure.iot.iothubreact.{Configuration, Logger, MessageToDevice}
import com.microsoft.azure.iot.service.sdk.{IotHubServiceClientProtocol, ServiceClient}
import com.microsoft.azure.sdk.iot.service.sdk.{IotHubServiceClientProtocol, ServiceClient}
/** Send messages from cloud to devices
*/

Просмотреть файл

@ -2,7 +2,7 @@
package it.helpers
import com.microsoft.azure.iothub._
import com.microsoft.azure.sdk.iot.device._
/* Test helper to send messages to the hub */
class Device(deviceId: String) extends Logger {