/// <summary> /// Example of how a json from the server should look with an incoming operation. /// [ /// { /// "cpu_throttle": "normal", /// "restart": "none", /// "agent_id": "a3521251-4308-402c-9f4c-4fe357a445aa", /// "plugin": "rv", /// "operation_id": "6daa65599-499f-4d33-8edf-ba5653584180", /// "operation": "install_agent_update", /// "data": [ /// { /// "name": "TopPatch Agent", /// "uri": "https://toppatch.com/downloads/RVAgentUpdate_02_00_12.exe", /// "uris" :[ /// "file_uris": "https://toppatch.com/downloads/RVAgentUpdate_02_00_12.exe", /// "file_uris": "https://toppatch.com/downloads/RVAgentUpdate_02_00_12.exe" /// ] /// "cli_options": "/qn UPDATE=\"true\" ", /// "id": "06676211e6b5ebd53df167cb837eefe53c75fa7127946b115bd32cc6c84f5e06" /// } /// ], /// "net_throttle": "0" /// "server_queue_ttl": "123456789", /// "agent_queue_ttl" : "123456789" /// } ///] /// </summary> /// <param name="operationJson">Message from the server.</param> private static void Serialize(string operationJson) { var parsed = JObject.Parse(operationJson); var deserialized = JsonConvert.DeserializeObject <IncomingData>(parsed.ToString()); var directoryname = OpDirectory; foreach (var data in deserialized.file_data) { var opdata = new SavedOpData(); var filename = Path.Combine(directoryname, data.app_id) + ".data"; if (File.Exists(filename)) { Logger.Log("Operation {0} already exists on disk, will not overwrite.", LogLevel.Info, data.app_name); continue; } Logger.Log("Saving {0} operation to disk.", LogLevel.Info, data.app_name); opdata.app_id = (String.IsNullOrEmpty(data.app_id)) ? String.Empty : data.app_id; opdata.cpu_throttle = (String.IsNullOrEmpty(deserialized.cpu_throttle)) ? String.Empty : deserialized.cpu_throttle; opdata.agent_id = (String.IsNullOrEmpty(deserialized.agent_id)) ? String.Empty : deserialized.agent_id; opdata.plugin = (String.IsNullOrEmpty(deserialized.plugin)) ? String.Empty : deserialized.plugin; opdata.operation_id = (String.IsNullOrEmpty(deserialized.operation_id)) ? String.Empty : deserialized.operation_id; opdata.operation = (String.IsNullOrEmpty(deserialized.operation)) ? String.Empty : deserialized.operation; opdata.restart = (String.IsNullOrEmpty(deserialized.restart)) ? String.Empty : deserialized.restart; opdata.net_throttle = deserialized.net_throttle.ToString(CultureInfo.InvariantCulture); opdata.filedata_app_id = (String.IsNullOrEmpty(data.app_id)) ? String.Empty : data.app_id; opdata.filedata_app_name = (String.IsNullOrEmpty(data.app_name)) ? String.Empty : data.app_name; opdata.filedata_app_clioptions = (String.IsNullOrEmpty(data.cli_options)) ? String.Empty : data.cli_options; opdata.error = string.Empty; opdata.reboot_required = false.ToString().ToLower(); opdata.success = false.ToString().ToLower(); opdata.operation_status = OperationStatus.Pending; //ttl opdata.server_queue_ttl = (String.IsNullOrEmpty(deserialized.server_queue_ttl)) ? String.Empty : deserialized.server_queue_ttl; opdata.agent_queue_ttl = (String.IsNullOrEmpty(deserialized.agent_queue_ttl)) ? String.Empty : deserialized.agent_queue_ttl; foreach (var uridata in data.app_uris) { var appUri = new SavedOpData.AppUri(); appUri.file_name = (String.IsNullOrEmpty(uridata.file_name)) ? String.Empty : uridata.file_name; appUri.file_size = uridata.file_size; appUri.file_uri = (String.IsNullOrEmpty(uridata.file_uri)) ? String.Empty : uridata.file_uri; foreach (var link in uridata.file_uris) { appUri.file_uris.Add(link); } appUri.file_hash = (String.IsNullOrEmpty(uridata.file_hash)) ? String.Empty : uridata.file_hash; opdata.filedata_app_uris.Add(appUri); } var serialized = JsonConvert.SerializeObject(opdata); File.WriteAllText(filename, serialized); } }
/// <summary> /// Example of how a json from the server should look with an incoming operation. /// [ /// { /// "cpu_throttle": "normal", /// "restart": "none", /// "agent_id": "a3521251-4308-402c-9f4c-4fe357a445aa", /// "plugin": "rv", /// "operation_id": "6daa65599-499f-4d33-8edf-ba5653584180", /// "operation": "install_agent_update", /// "data": [ /// { /// "name": "TopPatch Agent", /// "uri": "https://toppatch.com/downloads/RVAgentUpdate_02_00_12.exe", /// "uris" :[ /// "file_uris": "https://toppatch.com/downloads/RVAgentUpdate_02_00_12.exe", /// "file_uris": "https://toppatch.com/downloads/RVAgentUpdate_02_00_12.exe" /// ] /// "cli_options": "/qn UPDATE=\"true\" ", /// "id": "06676211e6b5ebd53df167cb837eefe53c75fa7127946b115bd32cc6c84f5e06" /// } /// ], /// "net_throttle": "0" /// "server_queue_ttl": "123456789", /// "agent_queue_ttl" : "123456789" /// } ///] /// </summary> /// <param name="operationJson">Message from the server.</param> private static void Serialize(string operationJson) { var parsed = JObject.Parse(operationJson); var deserialized = JsonConvert.DeserializeObject<IncomingData>(parsed.ToString()); var directoryname = OpDirectory; foreach (var data in deserialized.file_data) { var opdata = new SavedOpData(); var filename = Path.Combine(directoryname, data.app_id) + ".data"; if (File.Exists(filename)) { Logger.Log("Operation {0} already exists on disk, will not overwrite.", LogLevel.Info, data.app_name); continue; } Logger.Log("Saving {0} operation to disk.", LogLevel.Info, data.app_name); opdata.app_id = (String.IsNullOrEmpty(data.app_id)) ? String.Empty : data.app_id; opdata.cpu_throttle = (String.IsNullOrEmpty(deserialized.cpu_throttle)) ? String.Empty : deserialized.cpu_throttle; opdata.agent_id = (String.IsNullOrEmpty(deserialized.agent_id)) ? String.Empty : deserialized.agent_id; opdata.plugin = (String.IsNullOrEmpty(deserialized.plugin)) ? String.Empty : deserialized.plugin; opdata.operation_id = (String.IsNullOrEmpty(deserialized.operation_id)) ? String.Empty : deserialized.operation_id; opdata.operation = (String.IsNullOrEmpty(deserialized.operation)) ? String.Empty : deserialized.operation; opdata.restart = (String.IsNullOrEmpty(deserialized.restart)) ? String.Empty : deserialized.restart; opdata.net_throttle = deserialized.net_throttle.ToString(CultureInfo.InvariantCulture); opdata.filedata_app_id = (String.IsNullOrEmpty(data.app_id)) ? String.Empty : data.app_id; opdata.filedata_app_name = (String.IsNullOrEmpty(data.app_name)) ? String.Empty : data.app_name; opdata.filedata_app_clioptions = (String.IsNullOrEmpty(data.cli_options)) ? String.Empty : data.cli_options; opdata.error = string.Empty; opdata.reboot_required = false.ToString().ToLower(); opdata.success = false.ToString().ToLower(); opdata.operation_status = OperationStatus.Pending; //ttl opdata.server_queue_ttl = (String.IsNullOrEmpty(deserialized.server_queue_ttl)) ? String.Empty : deserialized.server_queue_ttl; opdata.agent_queue_ttl = (String.IsNullOrEmpty(deserialized.agent_queue_ttl)) ? String.Empty : deserialized.agent_queue_ttl; foreach (var uridata in data.app_uris) { var appUri = new SavedOpData.AppUri(); appUri.file_name = (String.IsNullOrEmpty(uridata.file_name)) ? String.Empty : uridata.file_name; appUri.file_size = uridata.file_size; appUri.file_uri = (String.IsNullOrEmpty(uridata.file_uri)) ? String.Empty : uridata.file_uri; foreach (var link in uridata.file_uris) { appUri.file_uris.Add(link); } appUri.file_hash = (String.IsNullOrEmpty(uridata.file_hash)) ? String.Empty : uridata.file_hash; opdata.filedata_app_uris.Add(appUri); } var serialized = JsonConvert.SerializeObject(opdata); File.WriteAllText(filename, serialized); } }