Back to project page sensors2mqtt.
The source code is released under:
Eclipse Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECI...
If you think the Android project sensors2mqtt listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/******************************************************************************* * Copyright (c) 2013 Markus Alexander Kuppe and others. All rights reserved. * This program and the accompanying materials are made available under the terms * of the Eclipse Public License v1.0 which accompanies this distribution, and is * available at http://www.eclipse.org/legal/epl-v10.html */*www . java 2 s .c o m*/ * Contributors: * Markus Alexander Kuppe - initial API and implementation ******************************************************************************/ package org.kuppe.sensors2mqtt; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttPersistenceException; import android.os.AsyncTask; public class MyAsyncTask extends AsyncTask<List<Float>, Void, Void> { private static final int QoS0 = 0; // fire and forget (lowest QoS is good enough here) private final MqttClient client; private final String topic; private final Comparator<Float> comparator; public MyAsyncTask(MqttClient client, String topic) { this.client = client; this.topic = topic; this.comparator = new Comparator<Float>() { @Override public int compare(Float lhs, Float rhs) { return lhs.compareTo(rhs); } }; } @Override protected Void doInBackground(List<Float>... params) { final List<Float> list = params[0]; // Sort sensor readings Collections.sort(list, comparator); // Get median final Float[] floats = list.toArray(new Float[list.size()]); float f = floats[floats.length / 2]; // Send (as string for easier readability on the consumer end) try { client.publish(topic, Float.toString(f) .getBytes(), QoS0, false); } catch (MqttPersistenceException e) { e.printStackTrace(); } catch (MqttException e) { e.printStackTrace(); } return null; } }