-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifier.cs
106 lines (93 loc) · 3.16 KB
/
Notifier.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
103
104
105
106
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Outlook = Microsoft.Office.Interop.Outlook;
/*
* Jeremy Weatherford
* no rights reserved
*/
/* TODO
* GUI for settings and feature selection
* RGB picker for background color, maybe select "color walk" keyframes
* network reachability notifier
* AWS status
* GMail xidus.net and carrotsaver
* music beat detection
* sunrise/sunset simulation
*/
namespace Notifier {
class Program {
static void Main(string[] args) {
Program p = new Program();
}
TcpClient sockNotify;
StreamWriter notifySW;
Program() {
System.Timers.Timer t = new System.Timers.Timer(5000);
t.AutoReset = true;
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Start();
Thread connect = new Thread(new ThreadStart(connectThread));
connect.IsBackground = true;
connect.Start();
TcpListener server = new TcpListener(IPAddress.Loopback, 8003);
server.Start();
d("listening on :8003");
while (true) {
TcpClient cli = server.AcceptTcpClient();
string input = new StreamReader(cli.GetStream()).ReadLine();
d("RX: " + input);
if (input.StartsWith("flash ")) {
sendNotify("flash on");
}
cli.Close();
}
}
void connectThread() {
while (true) {
try {
sockNotify = new TcpClient();
sockNotify.Connect("192.168.1.10", 23);
if (sockNotify.Connected) {
d("connected");
notifySW = new StreamWriter(sockNotify.GetStream());
notifySW.AutoFlush = true;
while (sockNotify.Connected) {
notifySW.WriteLine("ping");
Thread.Sleep(5000);
}
}
} catch { }
try { sockNotify.Close(); } catch { }
Thread.Sleep(5000);
d("Reconnecting");
}
}
void sendNotify(string msg) {
try {
notifySW.WriteLine(msg);
} catch { }
}
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
if (checkOutlook()) {
sendNotify("outlook on");
} else {
sendNotify("outlook off");
}
}
private bool checkOutlook() {
try {
Outlook._Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder f = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
return f.UnReadItemCount > 0;
} catch (Exception e) {
d(e.Message + e.StackTrace);
return true;
}
}
private void d(string msg) { Console.WriteLine(msg); }
}
}