-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainForm.cs
241 lines (217 loc) · 7.94 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using AutoIt;
using KnownFolderPaths;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace MiJia
{
public partial class MainForm : Form
{
internal static string APPFOLDER = Path.GetDirectoryName(Application.ExecutablePath);
internal string SKYFOLDER = Path.Combine(KnownFolders.GetPath(KnownFolder.SkyDrive), @"ApplicationData\ConnectedHome");
internal string DOCFOLDER = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments), @"Elton\ConnectedHome\");
internal string SCRIPT_FILE = Path.Combine(APPFOLDER, "actions.csx");
internal string USERNAME = Environment.UserName;
private Gateway gw = new Gateway();
internal ScriptEngine engine = new ScriptEngine();
private void SetHidden(bool hide = true)
{
if (hide)
{
ShowInTaskbar = false;
WindowState = FormWindowState.Minimized;
Hide();
}
else
{
ShowInTaskbar = true;
WindowState = FormWindowState.Normal;
Show();
}
}
private Action<string, string, MessageBoxIcon> NotifyAction { get; set; } = null;
private void InitNotifyIcon()
{
notifyIcon.Icon = Icon;
notifyIcon.Text = Text;
notifyIcon.BalloonTipTitle = Text;
notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
NotifyAction = new Action<string, string, MessageBoxIcon>((content, title, icon) =>
{
if (notifyIcon is NotifyIcon)
{
if (string.IsNullOrEmpty(title)) title = Text;
ToolTipIcon ballon_icon = ToolTipIcon.None;
switch (icon)
{
case MessageBoxIcon.Information:
ballon_icon = ToolTipIcon.Info;
break;
case MessageBoxIcon.Warning:
ballon_icon = ToolTipIcon.Warning;
break;
case MessageBoxIcon.Error:
ballon_icon = ToolTipIcon.Error;
break;
default:
ballon_icon = ToolTipIcon.None;
break;
}
notifyIcon.ShowBalloonTip(5, title, content, ballon_icon);
}
});
}
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
InitNotifyIcon();
var basepath = APPFOLDER;
if (Directory.Exists(APPFOLDER))
basepath = APPFOLDER;
else if (Directory.Exists(SKYFOLDER))
basepath = SKYFOLDER;
else if (Directory.Exists(DOCFOLDER))
basepath = DOCFOLDER;
var opts = Environment.CommandLine.Split();
if (opts.Length > 1)
{
if (!string.IsNullOrEmpty(opts[1]) && File.Exists(opts[1])) SCRIPT_FILE = opts[1];
}
var configFile = Path.Combine(basepath, "config", "aqara.json");
if (engine is ScriptEngine)
{
engine.NotificationAction = NotifyAction;
engine.Init(basepath, configFile, edResult);
engine.ScriptFile = SCRIPT_FILE;
}
#if DEBUG
if (USERNAME.StartsWith("netch", StringComparison.CurrentCultureIgnoreCase)) btnTest.Visible = true;
else btnTest.Visible = false;
ContextMenuStrip = contextNotify;
#else
chkOnTop.Checked = true;
//SetHide(true);
this.WindowState = FormWindowState.Minimized;
#endif
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
#if !DEBUG
var ret = MessageBox.Show(this, "Exit?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (ret != DialogResult.Yes)
{
e.Cancel = true;
}
#endif
}
private void MainForm_Resize(object sender, EventArgs e)
{
if (sender == this)
{
switch(WindowState)
{
case FormWindowState.Normal:
SetHidden(false);
break;
case FormWindowState.Maximized:
SetHidden(false);
break;
case FormWindowState.Minimized:
SetHidden(true);
break;
default:
break;
}
}
}
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
if (e is MouseEventArgs)
{
var me = e as MouseEventArgs;
if (me.Button == MouseButtons.Left && sender == notifyIcon)
{
SetHidden(false);
}
}
}
private async void btnTest_Click(object sender, EventArgs e)
{
if (engine is ScriptEngine)
{
var ret = await engine.RunScript(true, true);
}
}
private void btnReloadScript_Click(object sender, EventArgs e)
{
//InitScriptEngine();
if (File.Exists(SCRIPT_FILE) && engine is ScriptEngine)
engine.ScriptContext = File.ReadAllText(SCRIPT_FILE);
}
private void btnEditScript_Click(object sender, EventArgs e)
{
if (File.Exists(SCRIPT_FILE))
{
#if DEBUG
var ret = AutoItX.Run($"notepad2 /s cs {SCRIPT_FILE}", APPFOLDER);
//var ret = AutoItX.RunWait($"notepad2 /s cs {sf}", APPFOLDER);
//if (ret == 0 && engine is ScriptEngine) engine.ScriptContext = File.ReadAllText(sf);
//else MessageBox.Show("notepad2 run failed!");
#else
AutoItX.Run($"notepad2 /s cs {SCRIPT_FILE}", APPFOLDER);
#endif
}
}
private void chkOnTop_CheckedChanged(object sender, EventArgs e)
{
if (sender == chkOnTop)
{
TopMost = chkOnTop.Checked;
tsmiOnTop.Checked = chkOnTop.Checked;
}
else if (sender == tsmiOnTop)
{
chkOnTop.Checked = tsmiOnTop.Checked;
}
}
private void chkPause_CheckStateChanged(object sender, EventArgs e)
{
if (sender == chkPause)
{
if (engine is ScriptEngine) engine.Pausing = chkPause.Checked;
tsmiPause.Checked = chkPause.Checked;
}
else if (sender == tsmiPause)
{
chkPause.Checked = tsmiPause.Checked;
}
}
private void tsmiShowForm_Click(object sender, EventArgs e)
{
if (!Visible) SetHidden(false);
}
private void tsmiResetGateway_Click(object sender, EventArgs e)
{
var basepath = APPFOLDER;
if (Directory.Exists(APPFOLDER))
basepath = APPFOLDER;
else if (Directory.Exists(SKYFOLDER))
basepath = SKYFOLDER;
else if (Directory.Exists(DOCFOLDER))
basepath = DOCFOLDER;
var configFile = Path.Combine(basepath, "config", "aqara.json");
engine.InitMiJiaGateway(basepath, configFile);
}
private void tsmiExit_Click(object sender, EventArgs e)
{
//Environment.Exit(0);
//Application.Exit();
Close();
}
}
}