-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleFunctions.cs
More file actions
59 lines (54 loc) · 2.61 KB
/
ExampleFunctions.cs
File metadata and controls
59 lines (54 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using Microsoft.Azure.WebJobs;
using System.Text;
using Microsoft.Extensions.Logging;
using ExampleFunction.AdvancedConfig;
using CaseOnline.Azure.WebJobs.Extensions.Mqtt;
using CaseOnline.Azure.WebJobs.Extensions.Mqtt.Messaging;
namespace ExampleFunctions
{
public static class ExampleFunctions
{
/// <summary>
/// This sample functions shows how to subscribe to a topic using the default connectionstring ("MqttConnection")
/// After logging the contents of the incoming message it also publishes a message to the "testtopic/out" topic using the same connection
/// </summary>
[FunctionName("SimpleFunction")]
public static void SimpleFunction(
[MqttTrigger("test/#")] IMqttMessage message,
[Mqtt] out IMqttMessage outMessage,
ILogger logger)
{
var body = message.GetMessage();
var bodyString = Encoding.UTF8.GetString(body);
logger.LogInformation($"Message for topic {message.Topic}: {bodyString}");
outMessage = new MqttMessage("testtopic/out", Encoding.UTF8.GetBytes("Hi!"), MqttQualityOfServiceLevel.AtLeastOnce, true);
}
/// <summary>
/// This sample function shows how to trigger a function based on a custom configurated Mqtt Connection
/// </summary>
[FunctionName("AdvancedFunction")]
public static void AdvancedFunction(
[MqttTrigger(typeof(ExampleMqttConfigProvider), "testtopic/#")]IMqttMessage message,
ILogger log)
{
var body = Encoding.UTF8.GetString(message.GetMessage());
log.LogInformation($"Advanced: message from topic {message.Topic} body: {body}");
}
/// <summary>
/// This sample function shows how to run a function based on a timer trigger and then publishing to a MQTT topic
/// In this case the ICollector construct is used to output multiple results.
/// The Mqtt attribute is configured to use another connectionstring provided in the settings
/// </summary>
[FunctionName("TimerFunction")]
public static void TimerFunction(
[TimerTrigger("0 * * * * *")]TimerInfo timerInfo,
[Mqtt] ICollector<IMqttMessage> outMessages,
ILogger logger)
{
var body = Encoding.UTF8.GetBytes($"It is currently {DateTime.UtcNow:g}");
outMessages.Add(new MqttMessage("test/one", body, MqttQualityOfServiceLevel.AtLeastOnce, true));
outMessages.Add(new MqttMessage("topic/two", body, MqttQualityOfServiceLevel.AtLeastOnce, true));
}
}
}