View Javadoc
1   package net.sf.mbus4j.master.console;
2   
3   /*
4    * #%L
5    * mbus4j-master-ui
6    * %%
7    * Copyright (C) 2009 - 2014 MBus4J
8    * %%
9    * mbus4j - Drivers for the M-Bus protocol - http://mbus4j.sourceforge.net/
10   * Copyright (C) 2009-2014, mbus4j.sf.net, and individual contributors as indicated
11   * by the @authors tag. See the copyright.txt in the distribution for a
12   * full listing of individual contributors.
13   * 
14   * This is free software; you can redistribute it and/or modify it
15   * under the terms of the GNU General Public License as
16   * published by the Free Software Foundation; either version 3 of
17   * the License, or (at your option) any later version.
18   * 
19   * This software is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22   * Lesser General Public License for more details.
23   * 
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this software; if not, write to the Free
26   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
28   * #L%
29   */
30  
31  import java.io.Closeable;
32  import java.io.FileNotFoundException;
33  import java.io.IOException;
34  import java.util.logging.Level;
35  import java.util.logging.Logger;
36  import net.sf.atmodem4j.spsw.SerialPortSocket;
37  import net.sf.mbus4j.MBusUtils;
38  
39  import org.apache.commons.cli.CommandLine;
40  import org.apache.commons.cli.CommandLineParser;
41  import org.apache.commons.cli.HelpFormatter;
42  import org.apache.commons.cli.Option;
43  import org.apache.commons.cli.OptionGroup;
44  import org.apache.commons.cli.Options;
45  import org.apache.commons.cli.ParseException;
46  import org.apache.commons.cli.PosixParser;
47  
48  import net.sf.mbus4j.TcpIpConnection;
49  import net.sf.mbus4j.SerialPortConnection;
50  import net.sf.mbus4j.dataframes.Frame;
51  import net.sf.mbus4j.dataframes.MBusMedium;
52  import net.sf.mbus4j.dataframes.UserDataResponse;
53  import net.sf.mbus4j.json.JsonSerializeType;
54  import net.sf.mbus4j.log.LogUtils;
55  import net.sf.mbus4j.master.MBusMaster;
56  
57  /**
58   * DOCUMENT ME!
59   *
60   * @author aploese
61   */
62  public class ConsoleApp {
63  
64      private static Logger LOG = LogUtils.getMasterLogger();
65  
66      /**
67       * Creates a new Main object.
68       */
69      public ConsoleApp() {
70          super();
71          
72      }
73  
74      /**
75       * DOCUMENT ME!
76       *
77       * @param args the command line arguments
78       *
79       * @throws FileNotFoundException DOCUMENT ME!
80       * @throws IOException DOCUMENT ME!
81       */
82      public static void main(String[] args) throws Exception {
83          Options options = new Options();
84          Option opt;
85          OptionGroup optg;
86  
87          opt = new Option("h", "help", false, "print this help message");
88          options.addOption(opt);
89  
90          optg = new OptionGroup();
91          optg.setRequired(true);
92  
93          opt = new Option("p", "port", true, "serial port to use (Serial Mode)");
94          opt.setArgName("serial port");
95          opt.setType(String.class);
96          optg.addOption(opt);
97  
98          opt = new Option("i", "tcpip", true, "TCP-IP Hostname:Port or IP-Address:Port to use (IP Mode)");
99          opt.setArgName("TCP/IP address");
100         opt.setType(String.class);
101         optg.addOption(opt);
102 
103         options.addOptionGroup(optg);
104 
105         opt = new Option("t", "timeout", true, "Response timeout offset in ms");
106         opt.setArgName("offset");
107         opt.setType(Integer.class);
108         options.addOption(opt);
109 
110         optg = new OptionGroup();
111         optg.setRequired(true);
112 
113         opt = new Option("s", "search", false, "search for m-bus devices");
114         optg.addOption(opt);
115 
116         opt = new Option("u", "read", false, "read User Data response drom device");
117         optg.addOption(opt);
118 
119         options.addOptionGroup(optg);
120 
121         opt = new Option("j", "json", false, "output to stdout as JSON");
122         options.addOption(opt);
123 
124         optg = new OptionGroup();
125         optg.setRequired(true);
126 
127         opt = new Option("r", "paddr", false, "use primary adressing");
128         optg.addOption(opt);
129 
130         opt = new Option("e", "saddr", false, "use secondary Addressing");
131         optg.addOption(opt);
132 
133         options.addOptionGroup(optg);
134 
135         opt = new Option(null, "manufacturer", true, "device manufacturer (for selecting device)");
136         opt.setArgName("man");
137         opt.setType(String.class);
138         options.addOption(opt);
139 
140         opt = new Option(null, "version", true, "device version hex (for selecting device)");
141         opt.setArgName("ver");
142         opt.setType(String.class);
143         options.addOption(opt);
144 
145         opt = new Option(null, "id", true, "<device id BCD encoded integer with 8 digits Wildcart is \"F\" (for selecting device)");
146         opt.setArgName("id");
147         opt.setType(String.class);
148         options.addOption(opt);
149 
150         opt = new Option(null, "medium", true, "device medium (for selecting device)");
151         opt.setArgName("medium");
152         opt.setType(String.class);
153         options.addOption(opt);
154 
155         opt = new Option(null, "address", true, "device primary address (hex)");
156         opt.setArgName("primary address");
157         opt.setType(String.class);
158         options.addOption(opt);
159 
160         CommandLineParser parser = new PosixParser();
161         CommandLine cmd = null;
162 
163         try {
164             for (String arg : args) {
165                 if ("-h".equals(arg) || "--help".equals(arg)) {
166                     printHelp(options);
167                     return;
168                 }
169             }
170             cmd = parser.parse(options, args);
171         } catch (ParseException ex) {
172             printHelp(options);
173             LOG.log(Level.SEVERE, "Exception during parsing", ex);
174             return;
175         }
176 
177         if (cmd.hasOption("help")) {
178             printHelp(options);
179 
180             return;
181         }
182 
183         MBusMaster master = new MBusMaster();
184 
185         int bitPerSecond = SerialPortConnection.DEFAULT_BAUDRATE;
186 
187         int responseTimeoutOffset = -1;
188 
189         if (cmd.hasOption("timeout")) {
190             responseTimeoutOffset = Integer.parseInt(cmd.getOptionValue("timeout"));
191         }
192 
193         if (cmd.hasOption("tcpip")) {
194             final String tcpAddr = cmd.getOptionValue("tcpip").split(":")[0];
195             final int tcpPort = Integer.parseInt(cmd.getOptionValue("tcpip").split(":")[1]);
196             if (responseTimeoutOffset == -1) {
197                 responseTimeoutOffset = TcpIpConnection.DEFAULT_RESPONSE_TIMEOUT_OFFSET;
198             }
199             TcpIpConnection conn = new TcpIpConnection(tcpAddr, tcpPort, bitPerSecond, responseTimeoutOffset);
200             master.setConnection(conn);
201         } else if (cmd.hasOption("port")) {
202             if (responseTimeoutOffset == -1) {
203                 responseTimeoutOffset = SerialPortConnection.DEFAULT_RESPONSE_TIMEOUT_OFFSET;
204             }
205             SerialPortConnection conn = new SerialPortConnection(cmd.getOptionValue("port"), bitPerSecond, responseTimeoutOffset);
206             master.setConnection(conn);
207         }
208 
209         try (Closeable c = master.open()) {
210             if (cmd.hasOption("search")) {
211                 if (cmd.hasOption("paddr")) {
212                     master.searchDevicesByPrimaryAddress();
213                 } else if (cmd.hasOption("saddr")) {
214                     int bcdId = cmd.hasOption("id") ? (int) MBusUtils.String2Bcd(cmd.getOptionValue("id")) : 0xFFFFFFFF;
215                     byte version = (byte) Short.parseShort(cmd.getOptionValue("version", "FF"), 16);
216                     byte medium = cmd.hasOption("medium") ? (byte) MBusMedium.fromLabel(cmd.getOptionValue("medium")).getId() : (byte) 0xFF;
217                     short manufacturer = cmd.hasOption("manufacturer") ? MBusUtils.man2Short(cmd.getOptionValue("manufacturer")) : (short) 0xFFFF;
218 //                    System.out.println("FOUND SLAVES: " + master.sendSlaveSelect(bcdId, manufacturer, version, medium, 1));
219                     master.widcardSearch(bcdId, manufacturer, version, medium, 3);
220                     Thread.sleep(1000 * 5); // 20 sec
221                     LOG.info("Reading done - Closing down");
222                 }
223                 if (cmd.hasOption("json")) {
224                     System.out.print(master.toJSON(JsonSerializeType.ALL).toString(2));
225                 } else {
226                     System.out.print(master.toString());
227                 }
228             } else if (cmd.hasOption("read")) {
229                 UserDataResponse udr = null;
230                 if (cmd.hasOption("paddr")) {
231                     final byte slaveAddress = (byte) Short.parseShort(cmd.getOptionValue("address"));
232                     //    Frame f = master.sendInitSlave(slaveAddress);
233                     udr = master.readResponse(slaveAddress);
234                 } else if (cmd.hasOption("saddr")) {
235                     int bcdId = (int) MBusUtils.String2Bcd(cmd.getOptionValue("id"));
236                     Byte version = cmd.hasOption("version") ? (byte) Short.parseShort(cmd.getOptionValue("version")) : null;
237                     MBusMedium medium = cmd.hasOption("medium") ? MBusMedium.fromLabel(cmd.getOptionValue("medium")) : null;
238                     String manufacturer = cmd.hasOption("manufacturer") ? cmd.getOptionValue("manufacturer") : null;
239                     udr = master.readResponseBySecondary(bcdId, manufacturer, version, medium, bcdId);
240                 }
241                 if (cmd.hasOption("json")) {
242                     System.out.print(udr.toJSON(JsonSerializeType.ALL).toString(2));
243                 } else {
244                     System.out.print(udr.toString());
245                 }
246             }
247 
248         } catch (Exception e) {
249             LOG.log(Level.SEVERE, "Error", e);
250         }
251             LOG.log(Level.INFO, "Finished!");
252     }
253 
254     private static void printHelp(Options opts) {
255         HelpFormatter formatter = new HelpFormatter();
256         formatter.setWidth(300);
257         formatter.printHelp("master-console [Options]", opts);
258     }
259 }