Skip to content

Commit

Permalink
Merge pull request #114 from kike-canaries/mqtt_anaire
Browse files Browse the repository at this point in the history
added Anaire MQTT integration and settings
  • Loading branch information
hpsaturn authored Nov 23, 2021
2 parents e036635 + 6cb0415 commit b4a26e8
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ public class ResponseConfig extends SensorConfig {

public int vrev = 0;

public String anaireid;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ protected void refreshUI(){
updateWifiSummary();
lastLocation = SmartLocation.with(getActivity()).location().getLastLocation();
updateLocationSummary();
updateAnaireSummary();
validateLocationSwitch();
launchWorldMapInit();
launchAnaireInit();
}

@Override
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
**********************************************************************************************/
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 19 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@
<string name="category_wifi_title">WiFi preferences</string>
<string name="category_wifi_summary">For remote updates or fixed stations</string>
<string name="key_fixed_stations_map">key_fixed_stations_map</string>
<string name="fixed_stations_map_title">View your fixed station</string>
<string name="fixed_stations_map_summary">Click to launch the CanAirIO Worldmap</string>
<string name="fixed_stations_map_title">CanAirIO Worldmap</string>
<string name="fixed_stations_map_summary">Click to launch (take some minutes)</string>
<string name="url_canairio_worldmap">http://influxdb.canair.io:8000</string>
<string name="summary_ifx_nogeohash">Please set first the location</string>
<string name="key_setting_send_co2_calibration">key_setting_send_co2_calibration</string>
Expand All @@ -163,4 +163,21 @@
<string name="title_disable_wifi">Disable WiFi</string>
<string name="msg_status_disconnected">Status: disconnected</string>
<string name="msg_status_connected">Status: connected</string>
<string name="summary_ifx_advanced">Custom cloud settings</string>
<string name="title_hass">Home Assitant Discovery</string>
<string name="key_setting_hassip">key_setting_hassip</string>
<string name="title_setting_hassip">MQTT Broker Ip</string>
<string name="key_setting_hassusr">key_setting_hassusr</string>
<string name="summary_setting_hassusr" />
<string name="title_setting_hassusr">Username</string>
<string name="key_setting_hasspsw">key_setting_hasspsw</string>
<string name="summary_setting_hasspsw" />
<string name="title_setting_hasspsw">Password</string>
<string name="key_setting_hasspt_default">1883</string>
<string name="key_setting_hasspt">key_setting_hasspt</string>
<string name="title_setting_hasspt">Port</string>
<string name="key_anaire_id">key_anaire_id</string>
<string name="summary_anaire_timeseries">Show your device on Anaire Cloud</string>
<string name="title_anaire_timeseries">CanAirIO in Anaire</string>
<string name="url_anaire_realtime">https://portal.anaire.org/admin/</string>
</resources>
32 changes: 31 additions & 1 deletion app/src/main/res/xml/settings_custom_influxdb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,37 @@
app:summary="Disconnected"
app:title="@string/title_device_status" />

<PreferenceCategory app:title="InfluxDB Settings">
<PreferenceCategory app:title="@string/title_hass">

<EditTextPreference
android:inputType="text"
app:defaultValue=""
app:icon="@android:drawable/ic_menu_upload"
app:key="@string/key_setting_hassip"
app:title="@string/title_setting_hassip"
app:summary="" />

<EditTextPreference
android:inputType="number"
app:defaultValue="@string/key_setting_hasspt_default"
app:key="@string/key_setting_hasspt"
app:summary="@string/key_setting_hasspt_default"
app:title="@string/title_setting_hasspt" />

<Preference
app:key="@string/key_setting_hassusr"
app:summary="@string/summary_setting_hassusr"
app:title="@string/title_setting_hassusr" />

<EditTextPreference
android:inputType="textPassword"
app:key="@string/key_setting_hasspsw"
app:summary="@string/summary_setting_hasspsw"
app:title="@string/title_setting_hasspsw" />

</PreferenceCategory>

<PreferenceCategory app:title="Custom InfluxDB Instance">

<EditTextPreference
android:inputType="text"
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/res/xml/settings_fixed_station.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
app:title="@string/category_wifi_title">

<Preference
app:key="@string/key_setting_ssid"
app:icon="@android:drawable/ic_menu_send"
app:key="@string/key_setting_ssid"
app:summary="@string/summary_wifi_ssid"
app:title="@string/key_ssid_title" />

Expand Down Expand Up @@ -49,16 +49,24 @@
app:title="@string/key_enable_location_title" />

<Preference
android:fragment="hpsaturn.pollutionreporter.view.SettingsCustomInfluxDB"
android:fragment="hpsaturn.pollutionreporter.view.SettingsAdvancedCloud"
app:key="@string/key_setting_ifx_advanced"
app:summary="Custom InfluxDB settings"
app:summary="@string/summary_ifx_advanced"
app:title="Advanced Settings" />

<SwitchPreference
app:key="@string/key_setting_enable_ifx"
app:summary="@string/key_enable_ifx_summary_ready"
app:title="@string/key_enable_ifx_title" />

<Preference
app:dependency="@string/key_setting_enable_wifi"
app:icon="@android:drawable/ic_menu_recent_history"
app:key="@string/key_anaire_id"
app:summary="@string/summary_anaire_timeseries"
app:title="@string/title_anaire_timeseries" />


<Preference
app:dependency="@string/key_setting_enable_ifx"
app:icon="@android:drawable/ic_menu_mapmode"
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
mCompileSdkVersion=31
mMinSdkVersion=21
mTargetSdkVersion=31
mVersionCode=587
mVersionName=0.7.2
mVersionCode=589
mVersionName=0.7.3
android.useAndroidX=true
android.enableJetifier=true

Expand Down

0 comments on commit b4a26e8

Please sign in to comment.