This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
170 lines (149 loc) · 5.75 KB
/
MainForm.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http.Headers;
using System.Net.Http;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace XqLineNotifier
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private string lineToken = "";
private static string AppJsonFilePath = "./lineToken.json";
private static string XqMonitorPath = "./";
private static string XqLogFileExtension = "*.xqlog";
public void CreateFileWatcher(string path)
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Add event handlers.
// Only watch *.xqlog files
watcher.Filter = XqLogFileExtension;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnCreatedAsync);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
// Define the event handlers.
private async void OnCreatedAsync(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
try
{
if (this.lineToken.Trim() != "")
{
string message = File.ReadAllText(e.FullPath, Encoding.GetEncoding("Big5"));
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.lineToken);
var form = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("message", message)
});
HttpResponseMessage result = await client.PostAsync(new Uri("https://notify-api.line.me/api/notify"), form);
}
File.Delete(e.FullPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
public class AppSettings
{
public string LineToken { get; set; }
}
private AppSettings readAppSettings()
{
var jsonString = System.IO.File.ReadAllText(AppJsonFilePath);
AppSettings appSettings = JsonConvert.DeserializeObject<AppSettings>(jsonString);
return appSettings;
}
private void checkXqLineNotifyPathExists()
{
if (!Directory.Exists(XqMonitorPath))
{
Directory.CreateDirectory(XqMonitorPath);
}
}
private void createAppSettingsFile()
{
var appJsonObj = new AppSettings() { LineToken = "" };
System.IO.File.WriteAllText(AppJsonFilePath, JsonConvert.SerializeObject(appJsonObj));
}
private void initAppSettings()
{
try
{
if (!System.IO.File.Exists(AppJsonFilePath))
{
this.createAppSettingsFile();
}
AppSettings appSettings = this.readAppSettings();
lineToken = appSettings.LineToken;
txtLineToken.Text = lineToken;
}
catch (Exception e)
{
this.createAppSettingsFile();
}
}
private void MainForm_Load(object sender, EventArgs e)
{
checkXqLineNotifyPathExists();
initAppSettings();
createXqMonitorPath();
deleteAllXqLog();
CreateFileWatcher(XqMonitorPath);
}
private void createXqMonitorPath() {
if (!System.IO.Directory.Exists(XqMonitorPath)) {
System.IO.Directory.CreateDirectory(XqMonitorPath);
}
}
private void deleteAllXqLog() {
foreach (string sFile in System.IO.Directory.GetFiles(XqMonitorPath, XqLogFileExtension))
{
System.IO.File.Delete(sFile);
}
}
private void btnSave_Click(object sender, EventArgs e)
{
txtLineToken.Text = txtLineToken.Text.Trim();
AppSettings appSettings = this.readAppSettings();
appSettings.LineToken = txtLineToken.Text;
string jsonString = JsonConvert.SerializeObject(appSettings);
System.IO.File.WriteAllText(AppJsonFilePath, jsonString);
this.lineToken = txtLineToken.Text;
}
}
}