-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
162 lines (137 loc) · 5.97 KB
/
Program.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
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ConfigTool
{
class Program
{
static string m_DeviceHost = "";
static string m_DeviceMac = "";
static string m_DeviceKey = "";
static void Main(string[] args)
{
JObject reqObj = new JObject();
JObject reqPackObj = new JObject();
JObject resObj = new JObject();
JObject resPackObj = new JObject();
while (true)
{
Console.Write("AC Device IP or 'exit': ");
m_DeviceHost = Console.ReadLine().Trim();
if (m_DeviceHost == "exit")
break;
// scan Device
reqObj["t"] = "scan";
string response = sendCommand(reqObj.ToString());
if (response == "")
{
Console.WriteLine("Device didn't response properly.");
continue;
}
resObj = JObject.Parse(response);
resPackObj = JObject.Parse(Crypter.Decrypt((string) resObj["pack"], ""));
m_DeviceMac = (string) resPackObj["mac"];
// bind Device
reqPackObj = new JObject();
reqPackObj["mac"] = m_DeviceMac;
reqPackObj["t"] = "bind";
reqPackObj["uid"] = 0;
resPackObj = sendCommandPack(reqPackObj, "");
if ((string) resPackObj["t"] != "bindok")
throw new Exception("didn't get 'bindok' message from Device");
m_DeviceKey = (string) resPackObj["key"];
// print Menue
string choice = "";
while (choice != "9")
{
Console.WriteLine("-------------------------");
Console.WriteLine("1 - query Device");
Console.WriteLine("2 - set Device Name");
Console.WriteLine("3 - set Device Remotehost");
Console.WriteLine("9 - exit");
choice = Console.ReadLine().Trim();
switch (choice)
{
case "1":
reqPackObj = new JObject();
reqPackObj["cols"] = new JArray("host", "name");
reqPackObj["t"] = "status";
reqPackObj["mac"] = 0;
resPackObj = sendCommandPack(reqPackObj, m_DeviceKey);
Console.WriteLine("Device Name: " + (string) resPackObj["dat"][1]);
Console.WriteLine("Device Remotehost: " + (string) resPackObj["dat"][0]);
break;
case "2":
Console.Write("new Device Name: ");
string newDeviceName = Console.ReadLine();
reqPackObj = new JObject();
reqPackObj["opt"] = new JArray("name");
reqPackObj["p"] = new JArray(newDeviceName);
reqPackObj["t"] = "cmd";
resPackObj = sendCommandPack(reqPackObj, m_DeviceKey);
break;
case "3":
Console.Write("new Device Remotehost: ");
string newDeviceRemoteHost = Console.ReadLine();
reqPackObj = new JObject();
reqPackObj["opt"] = new JArray("host");
reqPackObj["p"] = new JArray(newDeviceRemoteHost);
reqPackObj["t"] = "cmd";
resPackObj = sendCommandPack(reqPackObj, m_DeviceKey);
break;
}
}
}
}
private static JObject sendCommandPack(JObject pack, string key)
{
JObject reqObj = new JObject();
reqObj["cid"] = "app";
reqObj["i"] = key == "" ? 1 : 0;
reqObj["pack"] = Crypter.Encrypt(pack.ToString(), key);
reqObj["t"] = "pack";
reqObj["tcid"] = m_DeviceMac;
reqObj["uid"] = 22130;
string response = sendCommand(reqObj.ToString());
JObject resObj = JObject.Parse(response);
return JObject.Parse(Crypter.Decrypt((string)resObj["pack"], key));
}
private static string sendCommand(string Command)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
string returnData = "";
UdpClient udpClient = null;
for (int i = 0; i < 3; i++)
{
try
{
udpClient = new UdpClient(RemoteIpEndPoint);
udpClient.Client.SendTimeout = 1000;
udpClient.Client.ReceiveTimeout = 1000;
udpClient.Connect(m_DeviceHost, 7000);
// Sends a message to the host to which you have connected.
Byte[] sendBytes = Encoding.ASCII.GetBytes(Command);
udpClient.Send(sendBytes, sendBytes.Length);
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
returnData = Encoding.ASCII.GetString(receiveBytes);
udpClient.Close();
break;
}
catch
{
try
{
udpClient.Close();
}
catch { }
returnData = "";
}
}
return returnData;
}
}
}