Skip to content

Instantly share code, notes, and snippets.

@ridomin
ridomin / dtdlom.js
Last active January 24, 2023 04:06
DtdlInterfaceInfo
// @ts-check
const filterMap = (om, dtmi, kind) => Object.entries(om).filter(e => e[1].EntityKind === kind && e[1].ChildOf === dtmi).map(e => e[1])
export class InterfaceInfo {
constructor (om, dtmi) {
/** @type import("./DtdlOM").DtdlObjectModel */
this.om = om
this.telemetries = filterMap(om, dtmi, "Telemetry")
this.properties = filterMap(om, dtmi, "Property")
this.commands = filterMap(om, dtmi, "Command")
this.components = filterMap(om, dtmi, "Component")
@ridomin
ridomin / IsPrime.cs
Created October 5, 2022 21:13
IsPrime.cs
using System;
using System.Collections.Generic;
using System.Linq;
var number = 45673;
var multiples = Multiples(number);
Console.WriteLine($"{number} is " +
$"{(multiples.Any() ? "not " : "")}prime");
@ridomin
ridomin / create-client-certificate.ps1
Created October 3, 2022 04:58
create-client-certificate.ps1
$root = gci "cert:\CurrentUser\my\<ca-thumbprint>"
Write-Host "Creating a Leaf Certificate chained to root:"
Write-Host " Root Subject:" $root.Subject.Substring(3)
Write-Host " Root Thumprint:" $root.Thumbprint
$certName = Read-Host "Device ID?"
$keyPwd = Read-Host "Key Password?" -AsSecureString
$cert = New-SelfSignedCertificate `
@ridomin
ridomin / ridoCAFY22.cer
Created August 15, 2022 22:23
ridoCAFY22
-----BEGIN CERTIFICATE-----
MIICyzCCAbOgAwIBAgIUPh/CfFW2v2IAAHPPRoOn2DTd86UwDQYJKoZIhvcNAQEL
BQAwFTETMBEGA1UEAwwKcmlkb2NhZnkyMjAeFw0yMTA5MjEwNDE0NDdaFw0yMzAy
MDMwNDE0NDdaMBUxEzARBgNVBAMMCnJpZG9jYWZ5MjIwggEiMA0GCSqGSIb3DQEB
AQUAA4IBDwAwggEKAoIBAQCrXflb9C84jYfU6VEK0gwv3ejvkv7dFkxhu1SxU71x
nabSen8zhswq1o5L2ogR97eInZXV76AXqvJ51gaF/yCIktFsIIALKWL2Lmihu79P
UAY2+5lbFji+W95yPhta8EPywjm7OIUQvj9cuHxy1FLvY1bDE07axRkALgmptZfX
EH8VSWwBQaJKw7Tlo5+27CyvraY2+Q/kOU9ibBfGhEaeK/VG9RkwXQlUmxg0IOQ3
uJj7E5K1T7nJBr/f1LR+fAqt/6g5Bv096mrYG8LpFwK6sDQPz8/mYzPe98zpK1KP
6VEYOxf+s6n3VJCVY/86UyaJvWawe59yRYmNXH8e99HJAgMBAAGjEzARMA8GA1Ud
@ridomin
ridomin / IoTHubHttpClient.cs
Created August 11, 2022 17:58
IoTHub Device Http Client in one file
namespace DeviceHttpDemo;
public class Config
{
public string host = "";
public string did = "";
public string key = "";
}
internal class SasAuth
@ridomin
ridomin / memmon.swagger.yaml
Created May 12, 2022 02:05
memmon.swagger
openapi: '3.0.2'
info:
title: MyIoTAPI
version: '1.0'
paths:
/readDeviceInfo:
get:
operationId: "readDeviceInfo"
@ridomin
ridomin / MqttNetExtensions.cs
Last active May 5, 2022 14:51
Demystify IoTHub SDKs
using MQTTnet.Client.Options;
using System.Text;
namespace demistify_iothub
{
public static class MqttNetExtensions
{
public static MqttClientOptionsBuilder WithAzureIoTHubCredentialsSas(this MqttClientOptionsBuilder builder, string hostName, string deviceId, string sasKey, string modelId = "", int sasMinutes = 60)
{
(string username, string password) = SasAuth.GenerateHubSasCredentials(hostName, deviceId, sasKey, modelId, sasMinutes);
@ridomin
ridomin / WithAzureSasCredentials.cs
Last active April 5, 2022 02:45
WithAzureSasCredentials.cs
public static MqttClientOptionsBuilder WithAzureIoTHubCredentialsSas(this MqttClientOptionsBuilder builder, string hostName, string deviceId, string sasKey, string modelId, int sasMinutes = 60)
{
(string username, string password) = SasAuth.GenerateHubSasCredentials(hostName, deviceId, sasKey, modelId, sasMinutes);
builder
.WithTcpServer(hostName, 8883)
.WithTls()
.WithClientId($"{deviceId}")
.WithCredentials(username, password);
return builder;
@ridomin
ridomin / SasAuth.cs
Created April 5, 2022 02:28
SasAuth.cs
namespace Rido.MqttCore
{
public class SasAuth
{
private const string apiversion_2020_09_30 = "2020-09-30";
public static string GetUserName(string hostName, string deviceId, string modelId = "") =>
$"{hostName}/{deviceId}/?api-version={apiversion_2020_09_30}&model-id={modelId}";
private static string Sign(string requestString, string key)
{
@ridomin
ridomin / CreateLeafCert.ps1
Created December 17, 2021 19:37
CreateLeafCert.ps1
$root = gci "cert:\CurrentUser\my\d8c5cd7502cd699783d9b2c031636ca48ae4229f"
$rootSubject = $root.Subject.Substring(3)
Write-Host "Root Certificate" $root.Thumbprint
$certName = "myCustomCert"
$cert = New-SelfSignedCertificate `
-CertStoreLocation cert:\CurrentUser\my `