Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace redis_parkhaus
- {
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using StackExchange.Redis;
- class Program
- {
- static async Task Main(string[] args)
- {
- var redis = ConnectionMultiplexer.Connect("xxxxxx.redis.cache.windows.net:6380,password=XXXXXX,ssl=True,abortConnect=False");
- IDatabase cache = redis.GetDatabase();
- var bytes = new byte[10];
- var sensors = new Dictionary<string, int>
- {
- { "Sensor 001", 0 },
- { "Sensor 002", 1 },
- { "Sensor 003", 10 },
- };
- const string parkhaus2 = "Parkhaus 2";
- const string parkhaus3 = "Parkhaus 3";
- await cache.StringSetAsync(parkhaus2, new byte[10]);
- await cache.StringSetAsync(parkhaus3, new byte[10]);
- await cache.StringSetBitAsync(parkhaus2, sensors["Sensor 001"], true);
- await cache.StringSetBitAsync(parkhaus2, sensors["Sensor 002"], true);
- const string timeSlot1 = "2019-04-12 12:00-13:00";
- await cache.StringSetAsync(timeSlot1, bytes);
- await cache.ParkhouseUsed(sensors, "Sensor 001", timeSlot1);
- await cache.ParkhouseUsed(sensors, "Sensor 002", timeSlot1);
- var count = await cache.GetParkhouseUtilization(parkhaus2, timeSlot1);
- await Console.Out.WriteLineAsync($"{parkhaus2} in {timeSlot1}: {count}");
- }
- }
- public static class Utils
- {
- public static Task ParkhouseUsed(this IDatabase cache, Dictionary<string, int> sensorsIDs, string sensorName, string timeslot)
- {
- var sensorIndex = sensorsIDs[sensorName];
- return cache.StringSetBitAsync(timeslot, sensorIndex, true);
- }
- public static async Task<long> GetParkhouseUtilization(this IDatabase cache, string parkhouse, string timeslot)
- {
- var query = $"{parkhouse} {timeslot}";
- await cache.StringBitOperationAsync(Bitwise.And, query, timeslot, parkhouse);
- var count = await cache.StringBitCountAsync(query);
- await cache.KeyDeleteAsync(query);
- return count;
- }
- public static string AsHexString(this byte[] bytes) => BitConverter.ToString(bytes).Replace("-", "");
- public static byte[] HexStringToByteArray(this string hex)
- {
- int NumberChars = hex.Length;
- byte[] bytes = new byte[NumberChars / 2];
- for (int i = 0; i < NumberChars; i += 2)
- bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
- return bytes;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement