VitrualRedisPLC.cs 896 B

1234567891011121314151617181920212223242526272829303132
  1. using FreeRedis;
  2. namespace WCS.Core;
  3. public class VitrualRedisPLC : IPLCAccessor
  4. {
  5. private readonly int dbLen = 50000;
  6. private readonly PLCInfo plcInfo;
  7. private readonly RedisClient redis;
  8. public VitrualRedisPLC(PLCInfo plcInfo, string redisConnStr)
  9. {
  10. this.plcInfo = plcInfo;
  11. redis = new RedisClient(redisConnStr);
  12. }
  13. public byte[] ReadBytes(ushort db, ushort address, ushort length)
  14. {
  15. var key = $"{plcInfo.IP}-{db}";
  16. if (!redis.Exists(key)) redis.SetRange(key, 0, new byte[dbLen]);
  17. var res = redis.GetRange<byte[]>(key, address, address + length - 1);
  18. return res;
  19. }
  20. public void WriteBytes(ushort db, ushort address, byte[] data)
  21. {
  22. var key = $"{plcInfo.IP}-{db}";
  23. if (!redis.Exists(key)) redis.SetRange(key, 0, new byte[dbLen]);
  24. redis.SetRange(key, address, data);
  25. }
  26. }