Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. namespace redis_parkhaus
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. using StackExchange.Redis;
  7.  
  8. class Program
  9. {
  10. static async Task Main(string[] args)
  11. {
  12. var redis = ConnectionMultiplexer.Connect("xxxxxx.redis.cache.windows.net:6380,password=XXXXXX,ssl=True,abortConnect=False");
  13. IDatabase cache = redis.GetDatabase();
  14.  
  15. var bytes = new byte[10];
  16.  
  17. var sensors = new Dictionary<string, int>
  18. {
  19. { "Sensor 001", 0 },
  20. { "Sensor 002", 1 },
  21. { "Sensor 003", 10 },
  22. };
  23.  
  24.  
  25. const string parkhaus2 = "Parkhaus 2";
  26. const string parkhaus3 = "Parkhaus 3";
  27.  
  28. await cache.StringSetAsync(parkhaus2, new byte[10]);
  29. await cache.StringSetAsync(parkhaus3, new byte[10]);
  30. await cache.StringSetBitAsync(parkhaus2, sensors["Sensor 001"], true);
  31. await cache.StringSetBitAsync(parkhaus2, sensors["Sensor 002"], true);
  32.  
  33.  
  34. const string timeSlot1 = "2019-04-12 12:00-13:00";
  35.  
  36. await cache.StringSetAsync(timeSlot1, bytes);
  37. await cache.ParkhouseUsed(sensors, "Sensor 001", timeSlot1);
  38. await cache.ParkhouseUsed(sensors, "Sensor 002", timeSlot1);
  39.  
  40. var count = await cache.GetParkhouseUtilization(parkhaus2, timeSlot1);
  41.  
  42. await Console.Out.WriteLineAsync($"{parkhaus2} in {timeSlot1}: {count}");
  43. }
  44. }
  45.  
  46. public static class Utils
  47. {
  48. public static Task ParkhouseUsed(this IDatabase cache, Dictionary<string, int> sensorsIDs, string sensorName, string timeslot)
  49. {
  50. var sensorIndex = sensorsIDs[sensorName];
  51. return cache.StringSetBitAsync(timeslot, sensorIndex, true);
  52. }
  53.  
  54. public static async Task<long> GetParkhouseUtilization(this IDatabase cache, string parkhouse, string timeslot)
  55. {
  56. var query = $"{parkhouse} {timeslot}";
  57. await cache.StringBitOperationAsync(Bitwise.And, query, timeslot, parkhouse);
  58. var count = await cache.StringBitCountAsync(query);
  59. await cache.KeyDeleteAsync(query);
  60. return count;
  61. }
  62.  
  63. public static string AsHexString(this byte[] bytes) => BitConverter.ToString(bytes).Replace("-", "");
  64.  
  65. public static byte[] HexStringToByteArray(this string hex)
  66. {
  67. int NumberChars = hex.Length;
  68. byte[] bytes = new byte[NumberChars / 2];
  69. for (int i = 0; i < NumberChars; i += 2)
  70. bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  71. return bytes;
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement