diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..2497407 --- /dev/null +++ b/Program.cs @@ -0,0 +1,219 @@ +using System.Net.NetworkInformation; +using System.Text; +using System; +using System.IO; +using PiSharp.LibGpio; +using PiSharp.LibGpio.Entities; +using System.Timers; + +namespace RaspPi_Control +{ + class Program + { + public static StreamWriter filer; + public static bool pinState = PinSetup(); + public static int tickCount = 0; + public static string filePath = "~/Logs/pilog.log"; + public static string fileDirectory = "~/Logs/"; + static void Main(string[] args) + { + Console.Title = "Automatic Modem Reset - DO NOT CLOSE (C) Brychan Dempsey"; + Console.WriteLine("Beginning application..."); + Console.WriteLine("Renaming Old Logs..."); + changeLogs(); + System.Timers.Timer aTimer = new System.Timers.Timer(); + aTimer.Elapsed += new ElapsedEventHandler(timerFunction); + aTimer.Interval = 60000; + aTimer.Enabled = true; + Console.WriteLine("Timer Started, value is " + aTimer.Enabled.ToString()); + Console.WriteLine("Waiting on keypress to end application"); + Console.ReadKey(); + } + static void timerFunction(object source, ElapsedEventArgs timeargs) + { + tickCount++; + if(tickCount > 86399) // Do not keep adding to a log for more than a day + { + changeLogs(); + } + Console.WriteLine("Timer has ticked!"); + doCheck(); + } + static void changeLogs() + { + string mainLog = "pilog.log"; + string twoLog = "pilog2.log"; + string threeLog = "pilog3.log"; + string fourLog = "pilog4.log"; + Console.WriteLine("Changing Logs..."); + if (File.Exists(fileDirectory + fourLog)) + { + Console.WriteLine("Deleting 4th log..."); + File.Delete(fileDirectory + fourLog); + } + if (File.Exists(fileDirectory + threeLog)) + { + Console.WriteLine("3rd > 4th log..."); + File.Move(fileDirectory + threeLog, fileDirectory + fourLog); + File.Delete(fileDirectory + threeLog); + } + if (File.Exists(fileDirectory + twoLog)) + { + Console.WriteLine("2nd > 3rd log..."); + File.Move(fileDirectory + twoLog, fileDirectory + threeLog); + File.Delete(fileDirectory + twoLog); + } + if (File.Exists(fileDirectory + mainLog)) + { + Console.WriteLine("1st > 2nd log..."); + File.Move(fileDirectory + mainLog, fileDirectory + twoLog); + File.Delete(fileDirectory + mainLog); + } + else + { + Console.WriteLine("Main log did not exist, continuing.."); + } + } + static void doCheck() + { + // Prepare Log Writing + Directory.CreateDirectory(fileDirectory); + Console.WriteLine("Directory Created."); + filer = File.AppendText(filePath); + Console.WriteLine("Beginning Log."); + WriteLog("NEW INSTANCE: " + DateTime.UtcNow + " "); + WriteLog("--------------------------------"); + Console.WriteLine("Beginning IP Ping."); + // Begin ipCheck + string ipAddress = "192.168.1.1"; + string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + byte[] buffer = Encoding.ASCII.GetBytes(data); + int timeout = 120; + Ping controlPing = new Ping(); + PingOptions options = new PingOptions(); + bool success = false; + PingReply reply; + try + { + reply = controlPing.Send(ipAddress, timeout, buffer, options); + if (reply.Status == IPStatus.Success) + { + success = true; + } + } + catch (Exception e) + { + WriteLog("Error: " + e.ToString()); + success = false; + } + finally + { + if (success) + { + Console.WriteLine("Ping successfull"); + if(!pinState) + { + LibGpio.Gpio.OutputValue(BroadcomPinNumber.Four, true); + pinState = true; + } + WriteLog("Ping successfull", true); + System.Threading.Thread.Sleep(1000); + } + else + { + WriteLog("Ping was not successfull: " + DateTime.UtcNow); + if (pinState) + { + LibGpio.Gpio.OutputValue(BroadcomPinNumber.Four, false); + pinState = false; + System.Threading.Thread.Sleep(1000); + LibGpio.Gpio.OutputValue(BroadcomPinNumber.Four, true); + pinState = true; + } + else + { + LibGpio.Gpio.OutputValue(BroadcomPinNumber.Four, true); + pinState = true; + } + System.Threading.Thread.Sleep(1000); + WriteLog("Rebooting", true); + } + } + return; + } + static bool PinSetup() + { + LibGpio.Gpio.TestMode = false; + LibGpio.Gpio.SetupChannel(BroadcomPinNumber.Four, Direction.Output); + LibGpio.Gpio.OutputValue(BroadcomPinNumber.Four, true); + return true; + } + + /// + /// Opens the existing StreamWriter and appends the provided string to the text file + /// + /// The string to append + /// Flag signifying the last object to be written. Disposes the StreamWriter + /// Displays output to the console + static void WriteLog(string arg, bool close=false, bool console=true) + { + filer.WriteLine(arg); + if(console) + { + Console.WriteLine("Appending " + arg + " to log file..."); + } + if (close) + { + filer.Dispose(); + } + } + /// + /// Automatically closes the open file write and checks the dates of the first entry. + /// Deletes the file if older than 7 days + /// + /// The path to the log file + //static void ClearLog( string filePath) + //{ + // char[] readChar = new char[100]; + + // StreamReader sr = new StreamReader(filePath); + // try + // { + // Console.WriteLine("Reading stream from path"); + // sr.ReadBlock(readChar, 0, 100); + // } + // catch (Exception e) + // { + // Console.WriteLine("Encountered an error {0} in checking the file: {1}",e,filePath); + // return; + // } + // finally + // { + // Console.WriteLine("Success!"); + // try + // { + // sr.Dispose(); + // } + // catch (NullReferenceException) + // { + // Console.WriteLine("File Stream already closed"); + // } + // Console.WriteLine("Successfully read {0}",filePath); + // string finalString = new string(readChar); + // int dateIndex = finalString.IndexOf("NEW INSTANCE:") + 14; + // finalString = finalString.Remove(0, dateIndex); + // dateIndex = finalString.IndexOf(" "); + // finalString = finalString.Remove(dateIndex, finalString.Length - dateIndex); + // DateTime dateTime; + // DateTime.TryParse(finalString, out dateTime); + // TimeSpan dayDiff = DateTime.UtcNow - dateTime; + // if(dayDiff.Days >= 7) + // { + // Console.WriteLine("File is older than 7 days, deleting"); + // File.Delete(filePath); + // } + // } + + //} + } +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c05d218 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("RaspPi Control")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("RaspPi Control")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("f22470e9-a58c-4335-ab43-4829d1eb8aba")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/RaspPi Control.csproj b/RaspPi Control.csproj new file mode 100644 index 0000000..64b69e8 --- /dev/null +++ b/RaspPi Control.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {F22470E9-A58C-4335-AB43-4829D1EB8ABA} + Exe + Properties + RaspPi_Control + RaspPi Control + v3.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + E:\Users\Brychan\Downloads2\PiSharp-master\PiSharp-master\src\PiSharp.LibGpio\bin\Debug\PiSharp.LibGpio.dll + + + E:\Users\Brychan\Downloads2\Raspberry-GPIO-Manager-main\Raspberry-GPIO-Manager-main\RaspberryGPIOManager\bin\Release\RaspberryGPIOManager.dll + + + + + + + + + + + + + + + \ No newline at end of file