From 6cb0415b5fede8e7a1fd813d4278ef5332e5d74b Mon Sep 17 00:00:00 2001 From: Hpsaturn Date: Sat, 20 Nov 2021 13:39:22 +0100 Subject: [PATCH] added Anaire MQTT integration and settings --- .../models/ResponseConfig.java | 1 + ...fluxDB.java => SettingsAdvancedCloud.java} | 17 ++++----- .../view/SettingsBaseFragment.java | 10 ++++++ .../view/SettingsFixedStation.java | 36 ++++++++++++++++--- app/src/main/res/values/strings.xml | 21 +++++++++-- .../main/res/xml/settings_custom_influxdb.xml | 32 ++++++++++++++++- .../main/res/xml/settings_fixed_station.xml | 14 ++++++-- gradle.properties | 4 +-- 8 files changed, 114 insertions(+), 21 deletions(-) rename app/src/main/java/hpsaturn/pollutionreporter/view/{SettingsCustomInfluxDB.java => SettingsAdvancedCloud.java} (93%) diff --git a/app/src/main/java/hpsaturn/pollutionreporter/models/ResponseConfig.java b/app/src/main/java/hpsaturn/pollutionreporter/models/ResponseConfig.java index 8e61ac7e..6d68e8d5 100644 --- a/app/src/main/java/hpsaturn/pollutionreporter/models/ResponseConfig.java +++ b/app/src/main/java/hpsaturn/pollutionreporter/models/ResponseConfig.java @@ -43,4 +43,5 @@ public class ResponseConfig extends SensorConfig { public int vrev = 0; + public String anaireid; } diff --git a/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsCustomInfluxDB.java b/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsAdvancedCloud.java similarity index 93% rename from app/src/main/java/hpsaturn/pollutionreporter/view/SettingsCustomInfluxDB.java rename to app/src/main/java/hpsaturn/pollutionreporter/view/SettingsAdvancedCloud.java index 0aa42417..8514b899 100644 --- a/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsCustomInfluxDB.java +++ b/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsAdvancedCloud.java @@ -15,7 +15,13 @@ /** * Created by Antonio Vanegas @hpsaturn on 4/8/21. */ -public class SettingsCustomInfluxDB extends SettingsBaseFragment { +public class SettingsAdvancedCloud extends SettingsBaseFragment { + + @Override + public void onCreatePreferencesFix(@Nullable Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.settings_custom_influxdb, rootKey); + } + @Override protected void refreshUI() { updateInfluxSummary(); @@ -62,11 +68,6 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin Logger.i(TAG,"skip onSharedPreferenceChanged because is in reading mode!"); } - @Override - public void onCreatePreferencesFix(@Nullable Bundle savedInstanceState, String rootKey) { - setPreferencesFromResource(R.xml.settings_custom_influxdb, rootKey); - - } private void saveAllPreferences(ResponseConfig config) { saveSharedPreference(R.string.key_setting_ifxdb, config.ifxdb); @@ -75,11 +76,11 @@ private void saveAllPreferences(ResponseConfig config) { } private String getInfluxDbDname() { - return getSharedPreference(getString(R.string.key_setting_ifxdb)); + return getSharedPreference(R.string.key_setting_ifxdb); } private String getInfluxDbIP() { - return getSharedPreference(getString(R.string.key_setting_ifxip)); + return getSharedPreference(R.string.key_setting_ifxip); } private int getInfluxDbPort() { diff --git a/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsBaseFragment.java b/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsBaseFragment.java index a8a4b91d..a656de23 100644 --- a/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsBaseFragment.java +++ b/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsBaseFragment.java @@ -179,6 +179,11 @@ private void saveSharedPreference(int key, boolean enable) { editor.apply(); } + String getSharedPreference(int key) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getMain()); + return preferences.getString(getString(key), ""); + } + String getSharedPreference(String key) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getMain()); return preferences.getString(key, ""); @@ -189,6 +194,11 @@ String getSharedPreference(String key, String defvalue) { return preferences.getString(key, defvalue); } + String getSharedPreference(int key, String defvalue) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getMain()); + return preferences.getString(getString(key), defvalue); + } + void clearSharedPreferences(){ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getMain()); preferences.edit().clear().apply(); diff --git a/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsFixedStation.java b/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsFixedStation.java index a0e962fa..571d62ee 100644 --- a/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsFixedStation.java +++ b/app/src/main/java/hpsaturn/pollutionreporter/view/SettingsFixedStation.java @@ -55,8 +55,10 @@ protected void refreshUI(){ updateWifiSummary(); lastLocation = SmartLocation.with(getActivity()).location().getLastLocation(); updateLocationSummary(); + updateAnaireSummary(); validateLocationSwitch(); launchWorldMapInit(); + launchAnaireInit(); } @Override @@ -77,12 +79,16 @@ protected void onConfigRead(ResponseConfig config) { if (config.geo != null && config.geo.length() == 0 ) { enableSwitch(R.string.key_setting_enable_ifx,false); } + if (config.anaireid != null && !config.anaireid.equals(getSharedPreference(R.string.key_anaire_id))){ + notify_sync = true; + } else if (config.geo != null) { enableSwitch(R.string.key_setting_enable_ifx, true); currentGeoHash = config.geo; } updateLocationSummary(); + updateAnaireSummary(); validateLocationSwitch(); updateWifiSummary(); updateWifiSummary(config.wsta); @@ -94,7 +100,6 @@ else if (config.geo != null) { updateStatusSummary(true); updatePreferencesSummmary(config); Logger.v(TAG, "[Config] notify device sync complete"); -// getMain().showSnackMessage(R.string.msg_sync_complete); } } @@ -201,8 +206,8 @@ private void saveInfluxDbConfig() { if (getInfluxDbSwitch().isChecked() && isInfluxDbSwitchFieldsValid()) { InfluxdbConfig config = new InfluxdbConfig(); - config.ifxdb = getSharedPreference(getString(R.string.key_setting_ifxdb),getString(R.string.key_ifxdb_default));; - config.ifxip = getSharedPreference(getString(R.string.key_setting_ifxip),getString(R.string.key_ifxip_default)); + config.ifxdb = getSharedPreference(R.string.key_setting_ifxdb,getString(R.string.key_ifxdb_default));; + config.ifxip = getSharedPreference(R.string.key_setting_ifxip,getString(R.string.key_ifxip_default)); config.ifxpt = NumberUtils.toInt(getSharedPreference(getString(R.string.key_setting_ifxpt)),8086); config.ienb = true; sendSensorConfig(config); @@ -215,8 +220,8 @@ private void saveInfluxDbConfig() { private boolean isInfluxDbSwitchFieldsValid(){ Logger.v(TAG, "[Config] validating->" + getString(R.string.key_setting_ifxdb)); - String ifxdb = getSharedPreference(getString(R.string.key_setting_ifxdb),getString(R.string.key_ifxdb_default)); - String ifxip = getSharedPreference(getString(R.string.key_setting_ifxip),getString(R.string.key_ifxip_default)); + String ifxdb = getSharedPreference(R.string.key_setting_ifxdb,getString(R.string.key_ifxdb_default)); + String ifxip = getSharedPreference(R.string.key_setting_ifxip,getString(R.string.key_ifxip_default)); Logger.v(TAG, "[Config] values -> " + ifxdb + " " + ifxip); return !(ifxdb.length() == 0 || ifxip.length() == 0); } @@ -255,6 +260,26 @@ private void launchWorldMapInit() { }); } + private void launchAnaireInit() { + Preference anaireLaunch = findPreference(getString(R.string.key_anaire_id)); + + assert anaireLaunch != null; + anaireLaunch.setOnPreferenceClickListener(preference -> { + String anaireId = getSharedPreference(R.string.key_anaire_id); + String url = getString(R.string.url_anaire_realtime)+anaireId+"/"; + Logger.v(TAG, "[Config] Anaire time serie: " + url); + + UITools.viewLink(getActivity(),url); + return true; + }); + } + + private void updateAnaireSummary(){ + String anaireId = getSharedPreference(R.string.key_anaire_id); + String summary = getString(R.string.summary_anaire_timeseries)+"\nYour station: "+anaireId; + updateSummary(R.string.key_anaire_id,summary); + } + /*********************************************************************************************** * InfluxDb Geohash parameter **********************************************************************************************/ @@ -346,6 +371,7 @@ private void saveAllPreferences(ResponseConfig config) { saveSharedPreference(R.string.key_setting_ssid, config.ssid); saveSharedPreference(R.string.key_setting_ifxdb, config.ifxdb); saveSharedPreference(R.string.key_setting_ifxip, config.ifxip); + saveSharedPreference(R.string.key_anaire_id, config.anaireid); } class UpdateTimeTask extends TimerTask { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 669cdd92..c9986679 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -142,8 +142,8 @@ WiFi preferences For remote updates or fixed stations key_fixed_stations_map - View your fixed station - Click to launch the CanAirIO Worldmap + CanAirIO Worldmap + Click to launch (take some minutes) http://influxdb.canair.io:8000 Please set first the location key_setting_send_co2_calibration @@ -163,4 +163,21 @@ Disable WiFi Status: disconnected Status: connected + Custom cloud settings + Home Assitant Discovery + key_setting_hassip + MQTT Broker Ip + key_setting_hassusr + + Username + key_setting_hasspsw + + Password + 1883 + key_setting_hasspt + Port + key_anaire_id + Show your device on Anaire Cloud + CanAirIO in Anaire + https://portal.anaire.org/admin/ diff --git a/app/src/main/res/xml/settings_custom_influxdb.xml b/app/src/main/res/xml/settings_custom_influxdb.xml index b6b64afb..ddd4c269 100644 --- a/app/src/main/res/xml/settings_custom_influxdb.xml +++ b/app/src/main/res/xml/settings_custom_influxdb.xml @@ -9,7 +9,37 @@ app:summary="Disconnected" app:title="@string/title_device_status" /> - + + + + + + + + + + + + + @@ -49,9 +49,9 @@ app:title="@string/key_enable_location_title" /> + + +