VitrualRedisPLC.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using FreeRedis;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace WCS.Core
  8. {
  9. public class VitrualRedisPLC : IPLCAccessor
  10. {
  11. RedisClient redis;
  12. PLCInfo plcInfo;
  13. int dbLen = 50000;
  14. public VitrualRedisPLC(PLCInfo plcInfo, string redisConnStr)
  15. {
  16. this.plcInfo = plcInfo;
  17. redis = new RedisClient(redisConnStr);
  18. }
  19. public byte[] ReadBytes(ushort db, ushort address, ushort length)
  20. {
  21. var key = $"{plcInfo.IP}-{db}";
  22. if (!redis.Exists(key))
  23. {
  24. redis.SetRange(key, 0, new byte[dbLen]);
  25. }
  26. var res = redis.GetRange<byte[]>(key, address, address + length - 1);
  27. return res;
  28. }
  29. public void WriteBytes(ushort db, ushort address, byte[] data)
  30. {
  31. var key = $"{plcInfo.IP}-{db}";
  32. if (!redis.Exists(key))
  33. {
  34. redis.SetRange(key, 0, new byte[dbLen]);
  35. }
  36. redis.SetRange(key, address, data);
  37. }
  38. }
  39. }