public ResultRecordChunk Fetch(int maxRecords) { var encoding = new UTF8Encoding(false); var records = new ResultRecordChunk(); var js = new Newtonsoft.Json.JsonSerializer(); foreach (var file in Directory.GetFiles(_resultsPath)) { if (records.Count >= maxRecords) break; var guid = Guid.Parse(Path.GetFileNameWithoutExtension(file)); try { using (var fs = File.OpenRead(file)) { var sr = new StreamReader(fs, encoding); var jtr = new JsonTextReader(sr); JToken token = JObject.Load(jtr); var functiontype = (ModuleFunctionType)((int)token.SelectToken("FunctionType")); var jr = token.CreateReader(); ModuleResultRecord record = null; if (functiontype == ModuleFunctionType.Check) record = js.Deserialize<CheckResultRecord>(jr); if (functiontype == ModuleFunctionType.Info) record = js.Deserialize<InfoResultRecord>(jr); if (functiontype == ModuleFunctionType.Action) record = js.Deserialize<ActionResultRecord>(jr); records.Add(guid, record); } } catch (Exception x) { _log.Warn(x, $"Could not open file \"{file}\": {x.Message}"); } } return records; }
public Guid[] UploadResults(ResultRecordChunk records) { var req = new JsonRpcRequest() { Method = "uploadResults", Params = new object[] { records } }; try { _log.Info($"Uploading {records.Count} result records to Core..."); var serialized = JsonRpcDefaults.Encoding.GetString(req.Serialize()); var respTask = node.SendCommand(serialized); var response = JsonRpcResponse.FromJsonString(respTask.Result); // Blocking! if (response.Error != null) { _log.Warn($"Error uploading records: {response.Error.Message} (0x{response.Error.Code.ToString("x")})"); return new Guid[0]; } else { var result = ((JToken)response.Result).ToObject<Guid[]>(); _log.Info($"Uploaded {result.Length} result records successfully."); return result; } } catch (Exception x) { _log.Warn($"Error uploading records: {x.Message}"); return new Guid[0]; } }