forked from kvonehr/ARP-Windows-Service
/
wifi_doorlock_service.cs
102 lines (87 loc) · 2.39 KB
/
wifi_doorlock_service.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.ServiceProcess;
using System.ComponentModel;
using System.Configuration.Install;
using System.Threading;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.IO.Ports;
public class CronService : ServiceBase
{
private CronJob job;
private Timer stateTimer;
private TimerCallback timerDelegate;
public CronService()
{
this.ServiceName = "WIFI_DOOR_LOCK";
this.CanStop = true;
this.CanPauseAndContinue = false;
this.AutoLog = true;
}
protected override void OnStart(string [] args)
{
job = new CronJob();
timerDelegate = new TimerCallback(job.checkConnection);
stateTimer = new Timer(timerDelegate, null, 15000, 15000);
}
protected override void OnStop()
{
stateTimer.Dispose();
}
public static void Main()
{
System.ServiceProcess.ServiceBase.Run(new CronService());
}
}
[RunInstaller(true)]
public class CronInstaller : Installer
{
private ServiceProcessInstaller processInstaller;
private ServiceInstaller serviceInstaller;
public CronInstaller()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "WIFI_DOOR_LOCK";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
public class CronJob
{
// The state object is necessary for a TimerCallback.
public void checkConnection(object stateObject)
{
Process p = new Process();
Ping pingSender = new Ping ();
p.StartInfo.FileName = "arp";
p.StartInfo.Arguments = "-a";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string data = "a";
byte[] buffer = Encoding.ASCII.GetBytes (data);
for(int i = 0; i < 25 ; i++){
pingSender.Send ("10.0.0."+i.ToString(),10,buffer);
}
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
string MAC = "xx-xx-xx-xx-xx-xx";
if(output.Contains(MAC)){
SerialPort port = new SerialPort("COM5", 9600);
port.Open();
port.Write("u");
port.Close();
}
else{
SerialPort port = new SerialPort("COM5", 9600);
port.Open();
port.Write("l");
port.Close();
}
}
}