/** Sends cached records and closes the channel */ @Override public void close() { super.close(); try { channel.close(); } catch (IOException e) { System.err.println( "WARNING: Thermostat error closing socket channel: [" + socket.getAbsolutePath() + "]"); e.printStackTrace(); } }
private void writeBatch(ArrayList<ThermostatRecord> records) throws IOException { StringBuilder sb = new StringBuilder(); sb.append("[\n"); boolean first = true; for (ThermostatRecord rec : records) { if (!first) { sb.append(",\n"); } else { first = false; } sb.append(rec.toJson()); } sb.append("\n]"); ByteBuffer envelope = ByteBuffer.wrap(sb.toString().getBytes(Charset.forName("UTF-8"))); channel.write(envelope); }
/** * Constructor * * @param sendThreshold min number of records to cache before sending * @param loseThreshold max number of packages to cache * @param socket path to socket * @param batchSize number of records to send at once * @param attempts number of send attempts to repeat in case of error * @param breakIntervalMillis number of milliseconds to wait between the attempts */ protected ThermostatUnixSocketTransport( int sendThreshold, int loseThreshold, File socket, int batchSize, int attempts, long breakIntervalMillis) { super(sendThreshold, loseThreshold); this.batchSize = batchSize; this.attempts = attempts; this.breakIntervalMillis = breakIntervalMillis; if (!socket.exists()) { throw new ThermostatException("Specified Thermostat socket: [" + socket + "] doesn't exist"); } this.socket = socket; try { this.channel = ThermostatLocalSocketChannel.open(socket); } catch (Exception e) { throw new ThermostatException( "Error opening Thermostat socket: [" + socket.getAbsolutePath() + "]", e); } }