Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Graham
committed
Jan 6, 2022
1 parent
1c3a500
commit 59d9be9
Showing
20 changed files
with
231 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/MultiPlug.Ext.RasPi.Config/Components/Localisation/SyncWifiCountryService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Timers; | ||
using MultiPlug.Base.Exchange; | ||
using MultiPlug.Ext.RasPi.Config.Utils.Swan; | ||
using MultiPlug.Ext.RasPi.Config.Diagnostics; | ||
|
||
namespace MultiPlug.Ext.RasPi.Config.Components.Localisation | ||
{ | ||
internal class SyncWifiCountryService | ||
{ | ||
IMultiPlugAPI m_MultiPlugAPI; | ||
|
||
private Timer m_Timer = new Timer(); | ||
|
||
internal event Action Synced; | ||
internal event Action<EventLogEntryCodes, string[]> Log; | ||
|
||
private string m_LastSyncMultiPlugValue = null; | ||
|
||
internal SyncWifiCountryService(IMultiPlugAPI theMultiPlugAPI) | ||
{ | ||
m_MultiPlugAPI = theMultiPlugAPI; | ||
m_Timer.Interval = 30000; | ||
m_Timer.AutoReset = false; | ||
m_Timer.Elapsed += OnSleepMatured; | ||
} | ||
|
||
internal void Reset() | ||
{ | ||
m_LastSyncMultiPlugValue = null; | ||
} | ||
|
||
internal void Begin() | ||
{ | ||
if( ! Utils.Hardware.isRunningRaspberryPi ) | ||
{ | ||
return; | ||
} | ||
|
||
if (m_LastSyncMultiPlugValue != null && m_LastSyncMultiPlugValue.Equals(m_MultiPlugAPI.Configuration.Localisation.Country)) | ||
{ | ||
// MultiPlug Country hasn't changed since last Sync | ||
return; | ||
} | ||
|
||
if (!m_Timer.Enabled) | ||
{ | ||
Task SyncTask = new Task(() => OnSleepMatured(null, null)); | ||
SyncTask.Start(); | ||
} | ||
} | ||
|
||
private void OnSleepMatured(object sender, ElapsedEventArgs e) | ||
{ | ||
if( m_MultiPlugAPI.Configuration.Localisation.Country.Equals(string.Empty) ) | ||
{ | ||
m_Timer.Enabled = true; | ||
} | ||
else | ||
{ | ||
m_LastSyncMultiPlugValue = m_MultiPlugAPI.Configuration.Localisation.Country; | ||
|
||
Task<ProcessResult> CurrentWifiCode = ProcessRunner.GetProcessResultAsync("wpa_cli", "-i wlan0 get country"); | ||
|
||
CurrentWifiCode.Wait(); | ||
|
||
string WifiCountry = CurrentWifiCode.Result.Okay() ? CurrentWifiCode.Result.GetOutput() : string.Empty; | ||
|
||
if ( ! WifiCountry.Equals(m_LastSyncMultiPlugValue)) | ||
{ | ||
Log?.Invoke(EventLogEntryCodes.WifiCountrySetting, new string[] { m_LastSyncMultiPlugValue }); | ||
Task<ProcessResult> SetWifiCountry = ProcessRunner.GetProcessResultAsync("raspi-config", "nonint do_wifi_country " + m_LastSyncMultiPlugValue); | ||
SetWifiCountry.Wait(); | ||
LoggingActions.LogTaskResult(Log, SetWifiCountry, EventLogEntryCodes.WifiCountrySet, EventLogEntryCodes.WifiCountrySettingError); | ||
Synced?.Invoke(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/MultiPlug.Ext.RasPi.Config/Models/Load/LocalisationLoad.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace MultiPlug.Ext.RasPi.Config.Models.Load | ||
{ | ||
public class LocalisationLoad | ||
{ | ||
[DataMember] | ||
public bool WiFiCountrySyncEnabled { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace MultiPlug.Ext.RasPi.Config.Models.Load | ||
{ | ||
public class Root | ||
{ | ||
[DataMember] | ||
public LocalisationLoad Localisation { get; set; } | ||
} | ||
} |
Oops, something went wrong.