/************************************************************** * fs-live .NET-API * version: 1.0 * date: 2018-09-28 * contact: api@fs-live.org * * This API is a C# wrapper for the fs-live web-API to push * timing data to the fs-live databases. * * The source code is provided as is, without any warrantys. * Feel free to use and modify the code. Keep in mind that * development of the whole fs-live project is still ongoing. * Future updates to the web-API will also result in changes in * the .NET-API and you will have to merge the updated API with * your customizations (if any). **************************************************************/ using System.Globalization; using System.IO; using System.Net; using System.Text; namespace FSlive { public static class PushToFSlive { private static string passkey; private static string competition; private static string SendForm(string content, string action) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(action); request.Method = "POST"; byte[] byteArray = Encoding.UTF8.GetBytes(content); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } public static void Init(string key, string comp) { passkey = key; competition = comp; } public static string Skidpad(string carNr, float timeRight, float timeLeft) { string formAction = "http://fs-live.org/" + competition + "/admin/process-skid.php"; string formContent = "car_nr=" + carNr + "&right=" + timeRight.ToString("F3", CultureInfo.CreateSpecificCulture("en-en")) + "&left=" + timeLeft.ToString("F3", CultureInfo.CreateSpecificCulture("en-en")) + "&passkey=" + passkey; return SendForm(formContent, formAction); } public static string SkidpadState(string carNr, string state) { string formAction = "http://fs-live.org/" + competition + "/admin/process-skid.php"; switch (state) { case "RERUN": state = "999"; break; case "DNF": state = "888"; break; default: break; } string formContent = "car_nr=" + carNr + "&right=" + state + "&left=" + state + "&passkey=" + passkey; return SendForm(formContent, formAction); } public static string SkidpadState(string carNr, string stateRight, string stateLeft) { string formAction = "http://fs-live.org/" + competition + "/admin/process-skid.php"; switch (stateRight) { case "RERUN": stateRight = "999"; break; case "DNF": stateRight = "888"; break; default: break; } switch (stateLeft) { case "RERUN": stateLeft = "999"; break; case "DNF": stateLeft = "888"; break; default: break; } string formContent = "car_nr=" + carNr + "&right=" + stateRight + "&left=" + stateLeft + "&passkey=" + passkey; return SendForm(formContent, formAction); } public static string Acceleration(string carNr, float time) { string formAction = "http://fs-live.org/" + competition + "/admin/process-acc.php"; string formContent = "car_nr=" + carNr + "&time=" + time.ToString("F3", CultureInfo.CreateSpecificCulture("en-en")) + "&passkey=" + passkey; return SendForm(formContent, formAction); } public static string AccelerationState(string carNr, string state) { string formAction = "http://fs-live.org/" + competition + "/admin/process-acc.php"; switch (state) { case "RERUN": state = "999"; break; case "DNF": state = "888"; break; default: break; } string formContent = "car_nr=" + carNr + "&time=" + state + "&passkey=" + passkey; return SendForm(formContent, formAction); } public static string Autocross(string carNr, float time) { string formAction = "http://fs-live.org/" + competition + "/admin/process-autox.php"; string formContent = "car_nr=" + carNr + "&time=" + time.ToString("F3", CultureInfo.CreateSpecificCulture("en-en")) + "&passkey=" + passkey; return SendForm(formContent, formAction); } public static string AutocrossState(string carNr, string state) { string formAction = "http://fs-live.org/" + competition + "/admin/process-autox.php"; switch (state) { case "RERUN": state = "999"; break; case "DNF": state = "888"; break; default: break; } string formContent = "car_nr=" + carNr + "&time=" + state + "&passkey=" + passkey; return SendForm(formContent, formAction); } public static string Trackdrive(string carNr, int attempt, float time) { string formAction = "http://fs-live.org/" + competition + "/admin/process-track.php"; string formContent = "car_nr=" + carNr + "&try=" + attempt + "&mode=update" + "&time=" + time.ToString("F3", CultureInfo.CreateSpecificCulture("en-en")) + "&passkey=" + passkey; return SendForm(formContent, formAction); } public static string TrackdriveState(string carNr, int attempt, string state) { string formAction = "http://fs-live.org/" + competition + "/admin/process-track.php"; string formContent = "car_nr=" + carNr + "&try=" + attempt + "&mode=" + state + "&passkey=" + passkey; return SendForm(formContent, formAction); } public static string Endurance(string carNr, float time) { string formAction = "http://fs-live.org/" + competition + "/admin/process-endu.php"; string formContent = "car_nr=" + carNr + "&mode=update" + "&time=" + time.ToString("F3", CultureInfo.CreateSpecificCulture("en-en")) + "&passkey=" + passkey; return SendForm(formContent, formAction); } public static string EnduranceState(string carNr, string state) { string formAction = "http://fs-live.org/" + competition + "/admin/process-endu.php"; string formContent = "car_nr=" + carNr + "&mode=" + state + "&passkey=" + passkey; return SendForm(formContent, formAction); } }//public static class PushToFSlive public static class FSliveConvert { public static string ToCarNr(string cl, int nr) { return cl + nr.ToString("D3"); } }//public static class Conversion }//namespace FSlive