public CommandSubscriptionTask(RestfulDeviceService restfulDeviceService, Guid deviceId, string deviceKey) { var apiInfo = restfulDeviceService.Get <ApiInfo>("/info"); var timestamp = apiInfo.ServerTimestamp; Task.Factory.StartNew(() => { try { while (true) { var сommands = restfulDeviceService.PollCommands(deviceId, deviceKey, timestamp, _cancellationTokenSource.Token); foreach (var command in сommands) { var eventArgs = new CommandEventArgs(deviceId, command); restfulDeviceService.OnCommandInserted(eventArgs); } timestamp = сommands.Max(c => c.Timestamp ?? timestamp); } } catch (OperationCanceledException) { } }); }
public CommandSubscriptionTask(RestfulDeviceService restfulDeviceService, string deviceId, string deviceKey) { var apiInfo = restfulDeviceService.Get <ApiInfo>("/info"); var timestamp = apiInfo.ServerTimestamp; Task.Factory.StartNew(() => { while (true) { try { var сommands = restfulDeviceService.PollCommands(deviceId, deviceKey, timestamp, _cancellationTokenSource.Token); foreach (var command in сommands) { var eventArgs = new CommandEventArgs(deviceId, command); restfulDeviceService.OnCommandInserted(eventArgs); } timestamp = сommands.Max(c => c.Timestamp ?? timestamp); } catch (OperationCanceledException) { return; } catch (Exception e) { LogManager.GetLogger(typeof(RestfulDeviceService)) .Error("Error on command polling. Restart polling", e); Thread.Sleep(1000); // retry with small wait } } }); }