The MIT License (MIT)
Copyright (c) 2017 Michael Watson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This solution requires Visual Studio for PC Version 15.3 or later. Earlier versions of VS don't support Azure Functions.
- In the Azure portal, click on New -> Enter
Function Appinto the Search Bar -> SelectedFunction Appfrom the search results -> Click Create - Name the Function App
- Select Consumption for the Hosting Plan
- Select the your Resource Group
- Select the Location closest to you
- Click Create
- Open using Visual Studio for PC (Version 15.3 or later)
- Ensure the following Extensions are installed
- Visual Studio 2017 Tools for Azure Functions
- Azure Functions and Web Jobs Tools
-
Download the code from this Github code repository
-
Open the project on Visual Studio
-
Open the file Constants in the folder Constants - you'll need to fill in the details here from various Azure services; we'll go step-by-step through these including 1) Azure Blob Storage, 2) Azure Cosmos DB, 3) Azure AD Application registration, 4) Azure Media Services, and 5) a URL for a Azure WebHook associated with your Azure Media Services.
- Create an Azure Blob Storage instance in the Azure portal
-
Give it a unique name (it will only lowercase letter and numbers)
-
Once you've created the account take note of the account keys. Look on the left menu bar for Settings > Access Keys. Make a note of the KEY and CONNECTION STRING for either key1 or key2
- Create an Azure Cosmos DB instance in the Azure Portal.
-
When entering your information, make sure you select SQL (DocumentDB)
-
Navigate to your Cosmos DB instance after it has been created
-
Select Data Explorer (Preview) in the menu options
-
Click New Collection in the top left corner
-
Name the Database Id Xamarin for this example and name the Collection Id XamCamAccount3. You can change these but you must make the corresponding changes in your code under XamCamFunctions > CosmosDB > CosmosDBService.cs in the DatabaseId and CollectionId fields.
-
You will now see that the left menu is populated with our new database and collection. Select XamCamAccount3 or whatever you called your collection and click on Documents -- it's empty now but this is where you'll see the videos you've taken from your IoT Device.
-
Once you've created the account take note of the account keys. Look on the left menu bar for Settings > Keys. Make a note of the URI, PRIMARY KEY, and PRIMARY CONNECTION STRING
-
Click to your Azure Active Directory. On the left menu panel click to Manage > App Registrations. Then in the resulting panel click, on the upper-left, + New application registration
-
Enter in a name, select Native as the Application Type, and type in a Redirect URI
-
Once you've registered the app take note of the account keys. On the overview panel look for the application ID - we'll call this the Client ID in our application. Click on the Settings, and click on Keys. Create a Key and take a note of it as it will be called Client Secret in our application.
4)Also depending on how we choose to do our Authentication - we will need the Directory Id of our Azure Active Directory Account. You can find this by clicking back into the general Azure Active Directory section within the Azure portal. On the left menu bar to go Manage > Properties > DirectoryID. Take note of the DirectoryID - this will be called tenantID in our application.
-
Give it a unique name (it will only lowercase letter and numbers) and connect it with the Storage Account in step 4
-
Click down to Streaming endpoints > take note of the hostname (you'll need to reference this in the application and add a slash at the end of the URL)
-
Click into the streaming endpoint, to press Play. Click Settings and click on Premium we've set our Streaming Units to 2 for up to 400Mbs egress.
-
Return to the final step in Section 3: Download the source from this Github code repository - fill in the constants as appropriate from the above steps. Optionally, you can store these values as Key/Value pairs under your Function > Application settings preventing the need to check in sensitive values in your source control.
-
For the Webhook constants, you'll need to check back into the Azure portal under your Function after you published your function. Once you complete the below steps, go to the Azure Portal and click on the function called "NewXamCamWebHookThree".
Notice on the upper-right you see </> Get function URL. That URL will be represented in the Constants class in your code as WebHookEndpoint. Also, the WebHookSigningKey will be the portion of that URL between the "code=" and the "&clientId=default"
-
In Visual Studio, right-click on XamCamFunctions and select Publish
-
After you are logged in, you'll see the Function App you created earlier. Select that Functions App App and click OK
-
Visual Studio is now publishing the XamCamFunctions code to your Azure Functions App
The Following will show you how to create an IoT Hub in Azure and create Azure functions to create/register/delete iot devices registrations from your Hub.
The IOTManager project contains a DeviceManager class which communicates with our Azure IoT Hub. Through this class we can provision new IOT Devices and get their connectionstrings, which we then use (from the client side, or the IoT device) to interact with the "Device Twin" - a digital representation of the various states of the IoT Device.
- In the azure portal, navigate to New. Search for IoT Hub and click Create
- Name your IoT Hub, fill out the resource group (either new or existing). Leave all other fields untouched
- Click Create
The file Constants.cs in the IOTManager project contains a few properties that we need to get from our newly created IotHub.
- Navigate to your ResourceGroup that contains the newly created IoT Hub, and click on the IoT Hub
- Under "Overview" - copy and paste the value for "HostName" to the property in Constants.cs
- Under "Shared access policies" - click on "iothubowner" and copy the value of "Connection string—primary key" into the property in Constants.cs
What is the device twin?
Device twins are JSON documents that store device state information (metadata,configurations, and conditions). IoT Hub persists a device twin for each device that you connect to IoT Hub.
For more information on device twins, visit this article on understanding device twins.
There are 3 api endpoints defined within the functions project:
GET or POST: /api/AddDevice/id/{uniqueDeviceIdAsString}
-
Takes in a string parameter for the ID of the IoT device. This string has to be unique.
-
If device doesn't exist it creates a new one, and returns the connection string to IoTHub. If it does exist it simply returns the connection string
PUT or DELETE: /api/DeleteDevice/id/{DeviceId}
- This endpoint deletes the device specified by the id if it exists
GET: /api/GetDevices/{Optional ID parameter}
- Returns a List of devices.
- If the deviceID parameter supplied - it returns that device
- If no parameter supplied - it returns ALL devices