public override byte[] EncodeValue(ScalarValue value) { if (value == null || value.IsNull) { throw new ArgumentNullException("value", "Only nullable strings can represent null values."); } string str = value.ToString(); #warning Bug? ToString should always have a non-null value if (str != null) { if (str.Length == 0) { return(NullValueEncoding); } if (str.StartsWith(ZeroTerminator)) { return(ZeroPreamble); } return(Encoding.UTF8.GetBytes(str)); } return(NullValueEncoding); }
public override ScalarValue DecodeValue(ScalarValue newValue, ScalarValue previousValue, Scalar field) { if ((newValue == null) || newValue.Null) { return(null); } var diffValue = (TwinValue)newValue; ScalarValue base_Renamed = (previousValue.Undefined)?field.BaseValue:previousValue; if (diffValue.first.ToInt() > base_Renamed.ToString().Length) { Global.HandleError(Error.FastConstants.D7_SUBTRCTN_LEN_LONG, "The string diff <" + diffValue + "> cannot be applied to the base value \"" + base_Renamed + "\" because the subtraction length is too long."); } return(Util.ApplyDifference((StringValue)base_Renamed, diffValue)); }
/// <summary> /// Convert the object to a simple string, this only works for single /// values or char arrays. /// </summary> /// <returns> /// The string representation of the object if it is simple enough, /// null otherwise. /// </returns> string ToSimpleString() { //TODO: Allow for numeric arrays of less than 10 elements like matlab //TODO: Allow for complex number if (IsNumeric && (NumberOfElements == 1) && !IsComplex) { return(ScalarValue.ToString()); } else if (IsChar) { return(StringValue); } else { return(null); } }
public override ScalarValue DecodeValue(ScalarValue newValue, ScalarValue priorValue, Scalar field) { if (newValue == null || newValue.IsNull) { return(null); } var diffValue = (TwinValue)newValue; ScalarValue v = (priorValue.IsUndefined) ? field.BaseValue : priorValue; if (diffValue.First.ToInt() > v.ToString().Length) { Global.ErrorHandler.OnError(null, DynError.SubtrctnLenLong, "The string diff <{0}> cannot be applied to the base value '{1}' because the subtraction length is too long.", diffValue, v); } byte[] bytes = Util.ApplyDifference(v, diffValue); return(field.FastType.GetValue(bytes)); //return Util.ApplyDifference((StringValue) v, diffValue); }
public override byte[] EncodeValue(ScalarValue value_Renamed) { if ((value_Renamed == null) || value_Renamed.Null) { throw new SystemException("Only nullable strings can represent null values."); } string string_Renamed = value_Renamed.ToString(); if (string_Renamed != null) { if (string_Renamed.Length == 0) { return(NULL_VALUE_ENCODING); } if (string_Renamed.StartsWith(ZERO_TERMINATOR)) { return(ZERO_PREAMBLE); } return(System.Text.Encoding.UTF8.GetBytes(string_Renamed)); } return(NULL_VALUE_ENCODING); }
private StringValue Observe(StringValue body, ListValue instrumentPlusObserveType, ScalarValue exposure) { string url = ""; string filename = body + "_" + instrumentPlusObserveType[0] + "_" + instrumentPlusObserveType[1] + ".txt"; string opts = ""; var lines = File.ReadAllLines("config.txt", Encoding.UTF8); foreach (string line in lines) { string[] parameters = line.Split('|'); if (parameters[0] == "url") { url = parameters[1]; } if (parameters[0] == "params") { opts = parameters[1]; } } if (File.Exists(filename)) { var configPsg = File.ReadAllText(filename); var planetsMap = new Dictionary <string, string> { ["PS4892b"] = "PS4892b", ["PS4892c"] = "TauCetif", ["PS4892d"] = "Teegardenb", ["PS4892e"] = "TRAPPIST1b", ["PS4892f"] = "TRAPPIST1c", ["PS4892g"] = "TRAPPIST1e", ["PS4892h"] = "TRAPPIST1f", ["PS4892i"] = "TRAPPIST1d", ["PS4892j"] = "TRAPPIST1g", ["PS4892k"] = "TRAPPIST1h", }; List <CelestialBody> bodies = FlightGlobals.Bodies; Boolean bodyExist = false; CelestialBody targetBody = bodies[0]; foreach (var currentBody in bodies) { if (currentBody.GetName() == planetsMap[body]) { bodyExist = true; targetBody = currentBody; } } if (bodyExist) { Vector3 heading = targetBody.GetTransform().position - FlightGlobals.ActiveVessel.GetTransform().position; double distance = Math.Round(heading.magnitude / 1000.0); double distanceAu = distance / 149600000000; if (distance < 100000000) { configPsg = configPsg.Replace("${DISTANCE}", distance.ToString()); configPsg = configPsg.Replace("${ALTITUDEUNIT}", "km"); } else { configPsg = configPsg.Replace("${DISTANCE}", distanceAu.ToString()); configPsg = configPsg.Replace("${ALTITUDEUNIT}", "AU"); } configPsg = configPsg.Replace("${EXPOSURE}", exposure.ToString()); configPsg = configPsg.Replace("\r\n", "\n"); File.WriteAllText("psg_config_debug.txt", configPsg); var request = WebRequest.Create(url); request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { string req = opts + WebUtility.UrlEncode(configPsg); streamWriter.Write(req); } _logger.Log($"posting to url: {url}"); _logger.Log($"post body:\n{WebUtility.UrlEncode(configPsg)}"); var httpResponse = request.GetResponse() as HttpWebResponse; using (Stream responseStream = httpResponse.GetResponseStream()) using (var reader = new StreamReader(responseStream, Encoding.UTF8)) { _logger.Log("got response from PSG"); _logger.Log($"PSG response size: {responseStream.Length}"); var payloadRows = 0; var output = new List <(string, string)>(); while (!reader.EndOfStream) { var currentRow = reader.ReadLine(); if (currentRow.StartsWith(@"#", StringComparison.InvariantCulture)) { continue; } payloadRows++; var values = currentRow.Split(' '); output.Add((values[0], values[2])); } _logger.Log($"PSG response payload size: {payloadRows} rows"); if (payloadRows == 0) { _logger.LogError("No response from PSG"); return("No response from PSG"); } var csvOutput = new StringBuilder(); var psgOutput = new StringBuilder(); foreach (var(first, second) in output) { csvOutput.AppendLine($"{first};{second}"); psgOutput.AppendLine($"{first} {second}"); } var currentTime = DateTime.Now; var outFile = $"psg_out_{currentTime:O}"; outFile = Regex.Replace(outFile, @"\+|:", "_"); var csvFilename = $"{outFile}.csv"; var psgFilename = $"{outFile}.txt"; _logger.Log($"writing PSG response to '{csvFilename}'"); File.WriteAllText(csvFilename, csvOutput.ToString()); _logger.Log($"writing PSG response to '{psgFilename}'"); File.WriteAllText(psgFilename, psgOutput.ToString()); } return("Observe complete"); } else { return("Celestial body does not exist"); } } else { return("Config file does not exist"); } //return "No result"; }
public virtual string Serialize(ScalarValue value_Renamed) { return value_Renamed.ToString(); }
[Obsolete("need?")] // BUG? Do we need this? public virtual string Serialize(ScalarValue value) { return(value.ToString()); }
public virtual string Serialize(ScalarValue value_Renamed) { return(value_Renamed.ToString()); }
public override byte[] EncodeValue(ScalarValue value_Renamed) { if ((value_Renamed == null) || value_Renamed.Null) { throw new SystemException("Only nullable strings can represent null values."); } string string_Renamed = value_Renamed.ToString(); if (string_Renamed != null) { if (string_Renamed.Length == 0) { return NULL_VALUE_ENCODING; } if (string_Renamed.StartsWith(ZERO_TERMINATOR)) { return ZERO_PREAMBLE; } return System.Text.Encoding.UTF8.GetBytes(string_Renamed); } return NULL_VALUE_ENCODING; }
public virtual string Serialize(ScalarValue value) { return value.ToString(); }
public override byte[] EncodeValue(ScalarValue value) { if (value == null || value.IsNull) throw new ArgumentNullException("value", "Only nullable strings can represent null values."); string str = value.ToString(); #warning Bug? ToString should always have a non-null value if (str != null) { if (str.Length == 0) { return NullValueEncoding; } if (str.StartsWith(ZeroTerminator)) { return ZeroPreamble; } return Encoding.UTF8.GetBytes(str); } return NullValueEncoding; }
public void TestToString() { ScalarValue value = String("1234abcd"); Assert.AreEqual("1234abcd", value.ToString()); }