Added solution files

This commit is contained in:
Brychan Dempsey 2021-10-06 14:18:30 +13:00
parent 51c6b08088
commit 966955ee60
3 changed files with 315 additions and 0 deletions

219
Program.cs Normal file
View File

@ -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;
}
/// <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);
// }
// }
//}
}
}

View File

@ -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")]

60
RaspPi Control.csproj Normal file
View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F22470E9-A58C-4335-AB43-4829D1EB8ABA}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RaspPi_Control</RootNamespace>
<AssemblyName>RaspPi Control</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="PiSharp.LibGpio">
<HintPath>E:\Users\Brychan\Downloads2\PiSharp-master\PiSharp-master\src\PiSharp.LibGpio\bin\Debug\PiSharp.LibGpio.dll</HintPath>
</Reference>
<Reference Include="RaspberryGPIOManager">
<HintPath>E:\Users\Brychan\Downloads2\Raspberry-GPIO-Manager-main\Raspberry-GPIO-Manager-main\RaspberryGPIOManager\bin\Release\RaspberryGPIOManager.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>