-
Notifications
You must be signed in to change notification settings - Fork 1
/
TpLink8970Reader.java
121 lines (99 loc) · 3.8 KB
/
TpLink8970Reader.java
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
package it.albertus.routerlogger.reader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import it.albertus.routerlogger.resources.Messages;
import it.albertus.util.IOUtils;
import it.albertus.util.logging.LoggerFactory;
/**
* <b>TP-Link TD-W8970 V1</b>. Comandi Telnet disponibili (case sensitive):
* <ul>
* <li><tt><b>adsl show info</b></tt></li>
* <li><tt><b>wan show connection info</b></tt> (sconsigliato, verboso)</li>
* <li><tt><b>wan show connection info <i>name</i></b></tt></li>
* </ul>
*/
public class TpLink8970Reader extends Reader {
private static final Logger logger = LoggerFactory.getLogger(TpLink8970Reader.class);
public static class Defaults {
public static final String COMMAND_INFO_ADSL = "adsl show info";
private Defaults() {
throw new IllegalAccessError("Constants class");
}
}
public static final String DEVICE_MODEL_KEY = "lbl.device.model.tplink.8970";
protected static final String COMMAND_PROMPT = "#";
protected static final String LOGIN_PROMPT = ":";
@Override
public boolean login(final String username, final char[] password) throws IOException {
final StringBuilder received = new StringBuilder();
// Username...
received.append(readFromTelnet(LOGIN_PROMPT, true).trim());
writeToTelnet(username);
// Password...
received.append(readFromTelnet(LOGIN_PROMPT, true).trim());
writeToTelnet(password);
logger.log(Level.INFO, LOG_MASK_TELNET, received);
received.setLength(0);
// Welcome! (salto caratteri speciali (clear screen, ecc.)...
final String welcome = readFromTelnet("-", true);
received.append(readFromTelnet(COMMAND_PROMPT, true).trim());
if (logger.isLoggable(Level.INFO)) {
logger.log(Level.INFO, LOG_MASK_TELNET, welcome.charAt(welcome.length() - 1) + received.toString());
}
return true;
}
@Override
public LinkedHashMap<String, String> readInfo() throws IOException {
// Informazioni sulla portante ADSL...
writeToTelnet(configuration.getString("tplink.8970.command.info.adsl", Defaults.COMMAND_INFO_ADSL));
readFromTelnet("{", true); // Avanzamento del reader fino all'inizio dei dati di interesse.
final LinkedHashMap<String, String> info = new LinkedHashMap<String, String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new StringReader(readFromTelnet("}", false).trim()));
String line;
while ((line = reader.readLine()) != null) {
info.put(line.substring(0, line.indexOf('=')).trim(), line.substring(line.indexOf('=') + 1).trim());
}
}
finally {
IOUtils.closeQuietly(reader);
}
readFromTelnet(COMMAND_PROMPT, true); // Avanzamento del reader fino al prompt dei comandi.
// Informazioni sulla connessione ad Internet...
final String commandInfoWan = configuration.getString("tplink.8970.command.info.wan");
if (commandInfoWan != null && commandInfoWan.trim().length() != 0) {
writeToTelnet(commandInfoWan);
readFromTelnet("{", true);
try {
reader = new BufferedReader(new StringReader(readFromTelnet("}", false).trim()));
String line;
while ((line = reader.readLine()) != null) {
info.put(line.substring(0, line.indexOf('=')).trim(), line.substring(line.indexOf('=') + 1).trim());
}
}
finally {
IOUtils.closeQuietly(reader);
}
readFromTelnet(COMMAND_PROMPT, true);
}
return info;
}
@Override
public void logout() throws IOException {
logger.info(Messages.get("msg.logging.out"));
writeToTelnet("logout");
}
@Override
public String getDeviceModel() {
return Messages.get(DEVICE_MODEL_KEY);
}
@Override
public String getImageFileName() {
return "tplink_td_w8970v1.png";
}
}