-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new typescript device samples Moved existing javascript samples to a javascript directory modified the readme files
- Loading branch information
Showing
41 changed files
with
2,021 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,4 @@ | |
"lint": "jshint --show-non-errors .", | ||
"ci": "npm run lint" | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,4 @@ | |
"npmlockrefresh": "npm i --package-lock-only", | ||
"lint": "jshint --show-non-errors ." | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Javascript Samples for the Azure IoT device SDK for Node.js | ||
|
||
This folder contains simple samples showing how to use the various features of the Microsoft Azure IoT Hub service from a device written using JavaScript. | ||
|
||
For instructions on running the JavaScript samples, see the [readme.md](../../samples) in the root samples folder. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
import { Client } from 'azure-iot-device'; | ||
import { Mqtt as Protocol } from 'azure-iot-device-mqtt'; | ||
|
||
let client: Client = null; | ||
|
||
function main(): void { | ||
// open a connection to the device | ||
const deviceConnectionString = process.env.DEVICE_CONNECTION_STRING || ''; | ||
|
||
if (deviceConnectionString === '') { | ||
console.log('device connection string has not been set'); | ||
process.exit(-1); | ||
} | ||
|
||
client = Client.fromConnectionString(deviceConnectionString, Protocol); | ||
client.open(onConnect); | ||
} | ||
|
||
function onConnect(err: Error): void { | ||
if (!!err) { | ||
console.error('Could not connect: ' + err.message); | ||
} else { | ||
console.log('Connected to device. Registering handlers for methods.'); | ||
|
||
// register handlers for all the method names we are interested in | ||
client.onDeviceMethod('getDeviceLog', onGetDeviceLog); | ||
client.onDeviceMethod('lockDoor', onLockDoor); | ||
} | ||
} | ||
|
||
function onGetDeviceLog(request: any, response: any): void { | ||
printDeviceMethodRequest(request); | ||
|
||
// Implement actual logic here. | ||
|
||
// complete the response | ||
response.send(200, 'example payload', function (err: Error): void { | ||
if (!!err) { | ||
console.error('An error ocurred when sending a method response:\n' + err.toString()); | ||
} else { | ||
console.log('Response to method "%s" sent successfully.', request.methodName); | ||
} | ||
}); | ||
} | ||
|
||
function onLockDoor(request: any, response: any): void { | ||
printDeviceMethodRequest(request); | ||
|
||
// Implement actual logic here. | ||
|
||
// complete the response | ||
response.send(200, function (err: Error): void { | ||
if (!!err) { | ||
console.error('An error ocurred when sending a method response:\n' + err.toString()); | ||
} else { | ||
console.log('Response to method "%s" sent successfully.', request.methodName); | ||
} | ||
}); | ||
} | ||
|
||
function printDeviceMethodRequest(request: any): void { | ||
// print method name | ||
console.log('Received method call for method "%s"', request.methodName); | ||
|
||
// if there's a payload just do a default console log on it | ||
if (!!request.payload) { | ||
console.log('Payload:\n' + request.payload); | ||
} | ||
} | ||
|
||
// get the app rolling | ||
main(); |
Oops, something went wrong.