220 lines
8.2 KiB
C#
220 lines
8.2 KiB
C#
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Opens the existing StreamWriter and appends the provided string to the text file
|
|
/// </summary>
|
|
/// <param name="arg">The string to append</param>
|
|
/// <param name="close">Flag signifying the last object to be written. Disposes the StreamWriter</param>
|
|
/// <param name="console">Displays output to the console</param>
|
|
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();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Automatically closes the open file write and checks the dates of the first entry.
|
|
/// Deletes the file if older than 7 days
|
|
/// </summary>
|
|
/// <param name="filePath">The path to the log file</param>
|
|
//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);
|
|
// }
|
|
// }
|
|
|
|
//}
|
|
}
|
|
}
|