-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cs
359 lines (335 loc) · 11.5 KB
/
App.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace espjs
{
class App
{
public Uart uart = new Uart();
public string port = "";
public string workDir;
public string execDir;
public string code = "";
public string[] args;
public Config config;
public UserConfig userConfig;
public bool hasUserConfig = false;
public bool runOnce;
public Dictionary<string, string> fileHashMap = new Dictionary<string, string>();
public App(bool runOnce = false)
{
if (Uart.HasPort())
{
port = Uart.GetPort();
}
this.runOnce = runOnce;
if (!runOnce)
{
Console.WriteLine("输入help获取帮助信息");
}
// 设置工作目录
this.workDir = Directory.GetCurrentDirectory();
string execFile = System.Reflection.Assembly.GetExecutingAssembly().Location;
this.execDir = Directory.GetParent(execFile).FullName;
// 加载基础配置
this.config = Config.Load();
uart.sp.BaudRate = this.config.BaudRate;
// 加载用户配置
if (UserConfig.Exists())
{
this.hasUserConfig = true;
this.userConfig = UserConfig.Load();
// 设置串口参数
this.uart.sp.BaudRate = this.userConfig.BaudRate;
}
else
{
// 设置串口参数
this.uart.sp.BaudRate = config.BaudRate;
}
}
/// <summary>
/// 获取指定位置的参数, 如果没有就返回默认值
/// </summary>
/// <param name="num">参数位置</param>
/// <param name="def">默认值</param>
/// <returns></returns>
public string GetParam(int num, string def = "")
{
if (args.Length <= num)
{
return def;
}
return args[num];
}
/// <summary>
/// 获取指定位置参数, 如果没有就让用户输入一个
/// </summary>
/// <param name="num">参数位置</param>
/// <param name="msg">提示用户输入的文本</param>
/// <returns></returns>
public string GetParamOrReadLine(int num, string msg = "")
{
if (args.Length <= num)
{
Console.WriteLine(msg);
return Console.ReadLine();
}
return args[num];
}
/// <summary>
/// 让程序进入下一次循环, 如果指定了程序只运行一次, 则程序会自动退出
/// </summary>
public void Next()
{
if (this.runOnce)
{
Environment.Exit(0);
}
Console.Write("> ");
args = Console.ReadLine().Trim().Split(' ');
Run(args);
}
/// <summary>
/// 程序入口
/// </summary>
/// <param name="args">用户输入的参数</param>
public void Run(string[] args)
{
this.args = args;
if (IsBaseCommand())
{
Next();
}
else if (!Uart.HasPort())
{
Console.WriteLine("没有可用端口");
Next();
}
else
{
UartCommand();
Next();
}
}
/// <summary>
/// 检测并运行基础命令
/// </summary>
/// <returns></returns>
public bool IsBaseCommand()
{
string cmd = GetParam(0, "");
switch (cmd)
{
case "":
break;
case "init":
Init.Run(workDir, execDir);
break;
case "version":
Console.WriteLine("当前版本: " + this.config.Version);
break;
case "debug":
string debug = GetParam(1, "true");
if (debug == "true")
{
uart.showRunCode = true;
Console.WriteLine("开启调试");
}
else
{
uart.showRunCode = false;
Console.WriteLine("关闭调试");
}
break;
case "clear":
uart.codeHistory.Clear();
Console.Clear();
break;
case "atob":
string atobStr = GetParamOrReadLine(1, "请输入要解密的字符串: ");
Console.WriteLine(uart.Atob(atobStr));
break;
case "btoa":
string btoaStr = GetParamOrReadLine(1, "请输入要加密的字符串: ");
Console.WriteLine(uart.Btoa(btoaStr));
break;
case "help":
Console.Write(File.ReadAllText(execDir + @"\help.txt"));
break;
case "exit":
case "quit":
Environment.Exit(0);
break;
case "port":
Port();
break;
case "baud":
if (args.Length == 1)
{
Console.WriteLine("当前波特率: " + uart.sp.BaudRate);
}
else
{
uart.sp.BaudRate = Convert.ToInt32(GetParam(1, "115200"));
if (UserConfig.Exists())
{
userConfig.BaudRate = uart.sp.BaudRate;
userConfig.Save();
}
}
break;
case "module":
case "modules":
ModuleManage();
break;
case "run":
if (Script.TryGetValue(GetParamOrReadLine(1, "请输入要执行的脚本名称: "), out string value))
{
Run(value.Split(' '));
}
break;
default:
return false;
}
return true;
}
/// <summary>
/// 运行串口相关命令
/// </summary>
public void UartCommand()
{
string cmd = GetParam(0, "");
switch (cmd)
{
case "restart":
case "reboot":
uart.SendCode(port, "E.reboot();");
break;
case "reset":
uart.SendCode(port, "reset(true);");
break;
case "blink":
new Upload(workDir, uart, port).WriteBlinkCode(GetParam(1, "NodeMCU.D4"));
break;
case "flash":
Flash.Write(port, GetParam(1, ""));
break;
case "rm":
case "del":
uart.SendCode(port, "require('Storage').erase('" + GetParamOrReadLine(1, "请输入Storage的名称: ") + "')");
break;
case "ll":
case "ls":
case "dir":
case "free":
case "storage":
Storage.Run(uart, port, args);
break;
case "get":
case "cat":
uart.SendCode(port, "console.log(require('Storage').read('" + GetParamOrReadLine(1, "请输入文件名: ") + "'))");
break;
case "exec":
case "run":
uart.SendCode(port, GetParamOrReadLine(1, "请输入代码"));
break;
case "load":
new Upload(workDir, uart, port).LoadFile(GetParamOrReadLine(1, "请输入要加载的文件名: "));
break;
case "upload":
new Upload(workDir, uart, port).Path(GetParam(1, ""));
break;
case "dev":
new DevMode(workDir, uart, port).Run();
break;
case "boot":
new Upload(workDir, uart, port).SendBootCodeFromFile(GetParamOrReadLine(1, "请输入要启动的文件名: "));
break;
case "connect":
new Upload(workDir, uart, port).ConnectWifi(GetParamOrReadLine(1, "请输入wifi名称: "), GetParamOrReadLine(2, "请输入wifi密码: "));
break;
case "status":
new Upload(workDir, uart, port).GetStatus();
break;
case "shell":
ShellMode.Run(uart, port);
break;
case "<<<":
InputMode.Run(uart, port);
break;
case "hex":
uart.SendHex(port, String.Join(" ", args).Replace("hex ", ""));
break;
default:
Console.WriteLine("命令不存在: " + cmd);
break;
}
}
/// <summary>
/// 模块管理
/// </summary>
public void ModuleManage()
{
string cmd = GetParam(1, "list");
string name;
switch (cmd)
{
case "install":
Module.Install();
break;
case "ls":
case "list":
Module.Ls();
break;
case "add":
name = GetParamOrReadLine(2, "请输入模块名称: ");
string url = GetParam(3, "");
Module.Add(name, url);
break;
case "remove":
case "delete":
name = GetParamOrReadLine(2, "请输入模块名称: ");
Module.Remove(name);
break;
}
}
/// <summary>
/// 端口管理
/// </summary>
public void Port()
{
string cmd = GetParam(1, "list");
switch (cmd)
{
case "list":
string[] ports = Uart.GetPorts();
if (ports.Length == 0)
{
Console.WriteLine("没有可用端口");
}
foreach (string port in ports)
{
if (port == this.port)
{
Console.WriteLine(port + " 当前选择");
}
else
{
Console.WriteLine(port);
}
}
break;
case "current":
Console.WriteLine(port);
break;
default:
this.port = GetParamOrReadLine(1, "请输入要设置的端口: ");
this.port = this.port.ToUpper();
break;
}
}
}
}