Skip to content

Instantly share code, notes, and snippets.

View ramonsmits's full-sized avatar

Ramon Smits ramonsmits

View GitHub Profile
@ramonsmits
ramonsmits / LogExtensions.cs
Created February 19, 2025 12:21
NServiceBus logger extension method with excension support for format methods
namespace NServiceBus.Logging;
using System;
public static class LogExtensions
{
public static void ErrorFormat(this ILog log, Exception exception, string format, params object[] args)
{
if (log.IsErrorEnabled)
{
@ramonsmits
ramonsmits / PublishMsgPack.cs
Created February 9, 2025 21:21
MQTTnet MessagePack helper that supports mqtt 5 topic alias
using MessagePack;
using MQTTnet;
using MQTTnet.Client;
public static class MqttExtensions
{
static readonly HashSet<ushort> topicAliases = new();
/// <summary>
/// Publish a message pack serialized message with optional topic alias.
@ramonsmits
ramonsmits / hexdump.cs
Created January 8, 2025 20:14
c# hex dump that formats bytes like many common hex viewers
static void DumpHex(byte[] data, int bytesPerLine = 16)
{
if (data == null) throw new ArgumentNullException(nameof(data));
var sb = new StringBuilder();
for (int offset = 0; offset < data.Length; offset += bytesPerLine)
{
sb.Clear();
sb.AppendFormat("{0:X8} ", offset);
@ramonsmits
ramonsmits / howto.md
Created December 11, 2024 16:17
Install .NET 9 on Raspberry pi
@ramonsmits
ramonsmits / update_git_repos.sh
Last active November 8, 2024 10:26 — forked from douglas/update_git_repos.sh
Update all git repositories under a base directory
#!/bin/bash
# store the current dir
CUR_DIR=$(pwd)
# Let the person running the script know what's going on.
printf "\n\033[1mPulling in latest changes for all repositories...\033[0m\n"
# Find all git repositories and update it to the master latest revision
for i in $(find . -name ".git" | cut -c 3-); do
@ramonsmits
ramonsmits / flex-cycle-calculate.cs
Created September 11, 2024 09:14
Motorola FLEX pager protocol calculate hourly offset based on cycles/frames
// FLEX|2024-09-05 15:22:37|1600/2/K/A|05.105|002029569 000123126 000126999|ALN|A2 11126 Rit 126665 VWS Wormerveer Industrieweg Wormerveer
const int framesPerHour = 15 * 128;
const int millisecondsInFrame = 3600_000 / framesPerHour;
var segments = l.Split('|');
var values = segments[3].Split(".");
var cycle = int.Parse(values[0]);
var frame = int.Parse(values[1]);
var frames = cycle * 128 + frame;
@ramonsmits
ramonsmits / DateTimeUnixTimeExtensions.cs
Last active September 7, 2024 08:32
Port DateTimeOffset.ToUnixTimeMilliseconds to DateTime for much faster and efficent handling of Unix Time
// DateTimeOffset first converts internally to DateTime, if you only want to get Unix Time you would need to do:
// DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() which allocated more memory and is significantly slower due to
// embedding offset data based on the active timezone.
public static class DateTimeUnixTimeExtensions
{
static readonly long UnixEpochTicks = DateTime.UnixEpoch.Ticks;
static readonly long UnixEpochMilliseconds = UnixEpochTicks / TimeSpan.TicksPerMillisecond; // 62,135,596,800,000
public static long ToUnixTimeMilliseconds(this DateTime instance)
{
@ramonsmits
ramonsmits / solution.md
Last active January 22, 2024 11:47
1152: Error extracting to the temporary location

I had issue running the Yamaha USB driver installer part of um3141x64.zip. Running the setup.exe showed dialog with the message:

1152: Error extracting to the temporary location

I used Sysintermals Procmon to check what failed but that didn't reveal the problem. My current TEMP folder points to the folder S:\.tmp on a ReFS partition. After temporarily creating a c:\tmp, set TEMP and TMP environment variables to that value the setup.exe ran without any issues. That means the installer could:

  1. Required C: drive
  2. Didn't like the period character in the path
  3. Isn't working with ReFS partitions
@ramonsmits
ramonsmits / howto.md
Created December 30, 2023 22:15
Backup restore Postgres between major versions via docker
  1. Ensure that a shared folder exists between the containers. For example, I have the following volume mapping on both instances: - ./backups:/backups
  2. Create a backup via the command pg_dump. For example, the following docker container myapp_db_1:
  • docker exec myapp_db_1 bash -c "pg_dump --username=myuser --no-password --format=c mydatabase > /backups/1.dump"
    
  1. Restore via the command pg_restore on another (newer major) of postgres:
  • docker exec myapp_db_2 bash -c  "pg_restore --username=myuser --no-password --dbname=mydatabase --verbose /backups/1.dump"
    
@ramonsmits
ramonsmits / FileIsInUse.cs
Created November 30, 2023 12:19
Check if a file is in use by trying to open it exclusively
static class FileHelper
{
public static bool FileInUse(string path)
{
try
{
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
return false;
}
catch (IOException)