-
Notifications
You must be signed in to change notification settings - Fork 0
/
Upload.cs
185 lines (162 loc) · 5.3 KB
/
Upload.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
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace espjs
{
/// <summary>
/// 上传代码相关操作
/// </summary>
public class Upload
{
public string workDir = "";
public Uart uart;
public string port;
private readonly UserConfig config;
public Upload(string workDir, Uart uart, string port)
{
this.workDir = workDir;
this.uart = uart;
this.port = port;
if (UserConfig.Exists())
{
this.config = UserConfig.Load();
}
}
public void Path(string path)
{
path = (path == "" ? this.workDir : path);
if (Directory.Exists(path))
{
// 文件夹
string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
foreach (string file in files)
{
string name = file.Replace(path + "\\", "").Replace("\\", "/");
if (InIgnore(name))
{
continue;
}
if (name == "index.js" || name == "main.js")
{
name = ".bootcde";
}
string code = File.ReadAllText(file);
uart.SendFile(port, name, code);
Console.WriteLine(name + " 写入完成");
// 这里需要暂停一下, 每个文件之间需要间隔一段时间, 否则容易造成单片机死机
Thread.Sleep(1000);
}
// uart.SendCode(port, "E.reboot();");
Console.WriteLine("全部文件已写入完成.");
}
else if (File.Exists(path))
{
// 文件
string code = File.ReadAllText(path);
uart.SendFile(port, path, code);
return;
}
else
{
Console.WriteLine("文件或文件夹不存在: " + path);
}
}
public void LoadFile(string filename)
{
if (!File.Exists(filename))
{
filename = workDir + @"\" + filename;
}
if (!File.Exists(filename))
{
Console.WriteLine("文件不存在: " + filename);
return;
}
string code = File.ReadAllText(filename);
uart.SendCode(port, code);
}
public void SendFile(string filename)
{
if (!File.Exists(filename))
{
filename = workDir + @"\" + filename;
}
if (!File.Exists(filename))
{
Console.WriteLine("文件不存在: " + filename);
return;
}
string code = File.ReadAllText(filename);
uart.SendCode(port, code);
}
public void SendBootCodeFromFile(string filename)
{
if (!File.Exists(filename))
{
filename = workDir + @"\" + filename;
}
if (!File.Exists(filename))
{
Console.WriteLine("文件不存在: " + filename);
return;
}
string code = File.ReadAllText(filename);
uart.SetBootCode(port, code);
}
public void WriteBlinkCode(string gpio)
{
string code = @"var val = false;setInterval(function(){digitalWrite(" + gpio + ",val);val=!val;},1000);";
uart.SetBootCode(port, code);
}
public bool InIgnore(string path)
{
if (!UserConfig.Exists())
{
return false;
}
foreach (string value in config.Ignore)
{
string rule = value.Replace("*", "");
if (value.EndsWith("*"))
{
if (path.Length >= rule.Length && rule == path.Substring(0, rule.Length))
{
return true;
}
}
else if (rule == path)
{
return true;
}
}
return false;
}
public void ConnectWifi(string ssid, string password)
{
var code = "require('Wifi').connect('" + ssid + "',{password:'" + password + "'}," +
" error =>{" +
" if (error) {" +
" console.log('error: ', error);" +
" } else {" +
" require('Wifi').save();" +
" require('Wifi').getIP((err, ipinfo) => {" +
" console.log('connected: ', ipinfo)" +
" });" +
" }" +
" });";
uart.SendCode(port, code);
}
public void GetStatus()
{
var code = "" +
"(_=>{" +
" require('Wifi').getIP((err, ipinfo) => {" +
" console.log('err: ', err);" +
" console.log('IP info: ', ipinfo);" +
" });" +
"})();";
uart.SendCode(port, code);
}
}
}