Skip to content

Commit

Permalink
Release 1.0.12 containing #6 #7 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
David Graham committed Jan 6, 2022
1 parent 1c3a500 commit 59d9be9
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 36 deletions.
13 changes: 8 additions & 5 deletions pub/Nuget/MultiPlug.Ext.RasPi.Config.nuspec
Expand Up @@ -2,16 +2,19 @@
<package >
<metadata>
<id>MultiPlug.Ext.RasPi.Config</id>
<version>1.0.11</version>
<version>1.0.12</version>
<title>Raspberry Pi Configuration Extension for MultiPlug</title>
<authors>4IR.UK</authors>
<owners>4IR.UK</owners>
<projectUrl>https://github.com/Industry4/MultiPlug.Ext.RasPi.Config</projectUrl>
<icon>images\multiplugraspberrypiico.png</icon>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Configure the Raspberry Pi with this MultiPlug Extension (https://multiplug.app)</description>
<description>Configure the Raspberry Pi with this MultiPlug Extension (https://www.multiplug.app)</description>
<license type="expression">MIT</license>
<releaseNotes>Run this extension only on a Raspberry Pi using the .Net Mono runtime.
1.0.12 Bug: https://github.com/Industry4/MultiPlug.Ext.RasPi.Config/issues/8
Bug: https://github.com/Industry4/MultiPlug.Ext.RasPi.Config/issues/7
Enhancement: https://github.com/Industry4/MultiPlug.Ext.RasPi.Config/issues/6
1.0.11 Bug: https://github.com/Industry4/MultiPlug.Ext.RasPi.Config/issues/5
Bug: https://github.com/Industry4/MultiPlug.Ext.RasPi.Config/issues/4
Enhancement: https://github.com/Industry4/MultiPlug.Ext.RasPi.Config/issues/3
Expand All @@ -26,13 +29,13 @@ Enhancement: https://github.com/Industry4/MultiPlug.Ext.RasPi.Config/issues/1
1.0.2 Bug: Changed encoder value to UTF-8
1.0.1 Enhancement: Raspberry Pi IP Address Configuration
1.0.0 Nuget name reserve.</releaseNotes>
<copyright>4IR.UK British Systems 2021</copyright>
<copyright>4IR.UK British Systems 2022</copyright>
<tags>MultiPlug Edge_Computing Raspberry Pi Raspberry_Pi IoT</tags>
<dependencies>
<group targetFramework=".NETFramework4.7.2">
<dependency id="MultiPlug.Base" version="1.0.64" />
<dependency id="MultiPlug.Base" version="1.0.65" />
<dependency id="MultiPlug.Extension.Core" version="1.0.63" />
<dependency id="MultiPlug.Core" version="2.2.76" />
<dependency id="MultiPlug.Core" version="2.2.77" />
</group>
</dependencies>
</metadata>
Expand Down
Expand Up @@ -12,6 +12,8 @@
using MultiPlug.Ext.RasPi.Config.Models.Components.Localisation;
using MultiPlug.Ext.RasPi.Config.Utils.Swan;
using MultiPlug.Ext.RasPi.Config.Diagnostics;
using MultiPlug.Base.Exchange;
using MultiPlug.Ext.RasPi.Config.Models.Load;

namespace MultiPlug.Ext.RasPi.Config.Components.Localisation
{
Expand All @@ -20,6 +22,27 @@ public class LocalisationComponent : LocalisationProperties
internal event Action<EventLogEntryCodes, string[]> Log;
internal event Action RestartDue;

private SyncWifiCountryService m_SyncService;

internal LocalisationComponent(IMultiPlugAPI theMultiPlugAPI)
{
m_SyncService = new SyncWifiCountryService(theMultiPlugAPI);
m_SyncService.Synced += OnWifiSynced;
m_SyncService.Log += OnSyncServiceLog;

WiFiCountrySyncEnabled = true; // Temp On by default
}

private void OnSyncServiceLog(EventLogEntryCodes theCode, string[] theData)
{
Log?.Invoke(theCode, theData);
}

private void OnWifiSynced()
{
RestartDue?.Invoke();
}

internal LocalisationProperties RepopulateAndGetProperties()
{
if (!Utils.Hardware.isRunningRaspberryPi) { return this; }
Expand All @@ -39,13 +62,13 @@ internal LocalisationProperties RepopulateAndGetProperties()
Task.WaitAll(Tasks);

WifiCountry = Tasks[1].Result.Okay() ? Tasks[1].Result.GetOutput() : string.Empty;
WifiCountries = Tasks[0].Result.Okay() ? Tasks[0].Result.GetOutput().Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Skip(25).Select(line => new string[] { line, line.StartsWith(WifiCountry) ? "selected=\"selected\"" : "" }).ToArray() : new string[0][];
WifiCountries = Tasks[0].Result.Okay() ? Tasks[0].Result.GetOutput().Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Skip(25).Select(line => new string[] { line.Split('\t')[0], line.Split('\t')[1], line.StartsWith(WifiCountry) ? "selected=\"selected\"" : "" }).ToArray() : new string[0][];
TimeZone = Tasks[3].Result.Okay() ? Tasks[3].Result.GetOutput().TrimEnd() : string.Empty;
TimeZones = Tasks[2].Result.Okay() ? Tasks[2].Result.GetOutput().Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Skip(25).Select(line => new string[] { line, line.StartsWith(TimeZone) ? "selected=\"selected\"" : "" }).ToArray() : new string[0][];
Date = Tasks[4].Result.Okay() ? Tasks[4].Result.GetOutput().TrimEnd() : string.Empty;
Time = Tasks[5].Result.Okay() ? Tasks[5].Result.GetOutput().TrimEnd() : string.Empty;
TimeSyncdEnabled = Tasks[6].Result.Okay() ? Tasks[6].Result.GetOutput().TrimEnd().Equals(c_Active) : false;
FakeHWClockEnabled = Tasks[7].Result.Okay() ? Tasks[7].Result.GetOutput().TrimEnd().Equals(c_Active) : false;
TimeSyncdEnabled = Tasks[6].Result.Okay(); // Will Error if inactive
FakeHWClockEnabled = Tasks[7].Result.Okay(); // Will Error if inactive
HWClockPresent = Tasks[8].Result.Okay();

// Log only if errors have occured
Expand All @@ -55,12 +78,28 @@ internal LocalisationProperties RepopulateAndGetProperties()
LoggingActions.LogTaskResult(Log, Tasks[3], EventLogEntryCodes.TimeZoneSettingGetError);
LoggingActions.LogTaskResult(Log, Tasks[4], EventLogEntryCodes.DateSettingGetError);
LoggingActions.LogTaskResult(Log, Tasks[5], EventLogEntryCodes.TimeSettingGetError);
LoggingActions.LogTaskResult(Log, Tasks[6], EventLogEntryCodes.TimeSyncdEnabledSettingGetError);
LoggingActions.LogTaskResult(Log, Tasks[7], EventLogEntryCodes.FakeHWClockEnabledSettingGetError);

return this;
}

internal void StartServices()
{
if(WiFiCountrySyncEnabled)
{
m_SyncService.Begin();
}
}

internal void UpdateProperties(LocalisationLoad theModel)
{
WiFiCountrySyncEnabled = theModel.WiFiCountrySyncEnabled;

if(WiFiCountrySyncEnabled == false)
{
m_SyncService.Reset();
}
}

internal void UpdateProperties(LocalisationProperties theModel)
{
List<Task<ProcessResult>> Tasks = new List<Task<ProcessResult>>();
Expand Down Expand Up @@ -152,12 +191,15 @@ internal void UpdateProperties(LocalisationProperties theModel)

if (WifiCountry != theModel.WifiCountry)
{
if (theModel.WifiCountry == string.Empty)
if (theModel.WifiCountry == string.Empty )
{
AskToRestart = true;
Log?.Invoke(EventLogEntryCodes.WifiCountrySetting, new string[] { "None" });
SetWifiCountry = UnsetWiFiSequence();
Tasks.Add(SetWifiCountry);
if ( ! WifiCountry.StartsWith("FAIL"))
{
AskToRestart = true;
Log?.Invoke(EventLogEntryCodes.WifiCountrySetting, new string[] { "None" });
SetWifiCountry = UnsetWiFiSequence();
Tasks.Add(SetWifiCountry);
}
}
else
{
Expand Down Expand Up @@ -189,6 +231,17 @@ internal void UpdateProperties(LocalisationProperties theModel)
Tasks.Add(SetTime);
}

if( theModel.WiFiCountrySyncEnabled && WiFiCountrySyncEnabled == false)
{
m_SyncService.Begin();
WiFiCountrySyncEnabled = true;
}
else if( theModel.WiFiCountrySyncEnabled == false)
{
WiFiCountrySyncEnabled = false;
m_SyncService.Reset();
}

if (AskToRestart) { RestartDue?.Invoke(); }

Task.WaitAll(Tasks.ToArray());
Expand Down
@@ -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();
}
}
}
}
}
Expand Up @@ -32,11 +32,15 @@

@foreach (var item in Model.Extension.Model.WifiCountries)
{
<option value="@item[0]" @Raw(item[1])>@item[0]</option>
<option value="@item[0]" @Raw(item[2])>@item[1]</option>
}
</select>
</div>
</div>
<div class="row-fluid">
<div class="span6"><b>Sync WiFi Country with MultiPlug Country</b></div>
<div class="span6"><input type="checkbox" name="WiFiCountrySyncEnabled" value="true" @Raw(Model.Extension.Model.WiFiCountrySyncEnabled ? "checked=checked" : "")></div>
</div>
</div>

<h3 class="box-header">Time</h3>
Expand Down
34 changes: 27 additions & 7 deletions src/MultiPlug.Ext.RasPi.Config/Core.cs
@@ -1,6 +1,10 @@
using System.Runtime.Serialization;

using MultiPlug.Base;
using MultiPlug.Base.Exchange;
using MultiPlug.Base.Exchange.API;
using MultiPlug.Extension.Core;

using MultiPlug.Ext.RasPi.Config.Components.Home;
using MultiPlug.Ext.RasPi.Config.Components.Network;
using MultiPlug.Ext.RasPi.Config.Components.Hat;
Expand All @@ -9,13 +13,12 @@
using MultiPlug.Ext.RasPi.Config.Components.Boot;
using MultiPlug.Ext.RasPi.Config.Components.Memory;
using MultiPlug.Ext.RasPi.Config.Components.Actions;
using MultiPlug.Extension.Core;
using MultiPlug.Ext.RasPi.Config.Components.Performance;
using MultiPlug.Ext.RasPi.Config.Components.About;

using MultiPlug.Ext.RasPi.Config.Diagnostics;
using MultiPlug.Ext.RasPi.Config.Models.Components;
using System;
using MultiPlug.Ext.RasPi.Config.Utils;
using MultiPlug.Ext.RasPi.Config.Components.About;
using MultiPlug.Ext.RasPi.Config.Components.Performance;
using MultiPlug.Ext.RasPi.Config.Models.Load;

namespace MultiPlug.Ext.RasPi.Config
{
Expand All @@ -25,6 +28,7 @@ public class Core : MultiPlugBase

private ILoggingService m_LoggingService;
private IMultiPlugActions m_MultiPlugActions;
internal IMultiPlugAPI MultiPlugAPI { get; private set; }

public static Core Instance
{
Expand All @@ -37,9 +41,12 @@ public static Core Instance
return m_Instance;
}
}
internal void Init(IMultiPlugActions theMultiPlugActions, IMultiPlugServices theMultiPlugServices)
internal void Init(IMultiPlugActions theMultiPlugActions, IMultiPlugServices theMultiPlugServices, IMultiPlugAPI theMultiPlugAPI)
{
m_MultiPlugActions = theMultiPlugActions;
MultiPlugAPI = theMultiPlugAPI;

Localisation = new LocalisationComponent(theMultiPlugAPI);

theMultiPlugServices.Logging.RegisterDefinitions(EventLogDefinitions.DefinitionsId, EventLogDefinitions.Definitions, true);

Expand Down Expand Up @@ -67,6 +74,19 @@ internal void Init(IMultiPlugActions theMultiPlugActions, IMultiPlugServices the
Actions.DoSystemRestart += OnDoSystemRestart;
}

internal void Start()
{
Localisation.StartServices();
}

internal void Load(Root config)
{
if(config.Localisation != null)
{
Localisation.UpdateProperties(config.Localisation);
}
}

private void OnDoSystemRestart()
{
m_MultiPlugActions.System.Power.Restart();
Expand All @@ -92,7 +112,7 @@ private void OnLogWriteEntry(EventLogEntryCodes theLogCode, string[] theArg)
public InterfacingComponent Interfacing { get; private set; } = new InterfacingComponent();
internal PerformanceComponent Performance { get; private set; } = new PerformanceComponent();
[DataMember]
public LocalisationComponent Localisation { get; private set; } = new LocalisationComponent();
public LocalisationComponent Localisation { get; private set; }
[DataMember]
public BootComponent Boot { get; private set; } = new BootComponent();
[DataMember]
Expand Down
Expand Up @@ -18,9 +18,10 @@ public class LocalisationProperties : SharedProperties
[DataMember]
public string TimeZone { get; set; }
public string[][] TimeZones { get; set; }
public bool SetTime { get; set; } = false;
public bool SetDate { get; set; } = false;

public bool HWClockPresent { get; set; } = false;
public bool SetTime { get; set; }
public bool SetDate { get; set; }
public bool HWClockPresent { get; set; }
[DataMember]
public bool WiFiCountrySyncEnabled { get; set; }
}
}
Expand Up @@ -9,7 +9,6 @@ public class SharedProperties : MultiPlugBase
internal const string c_Enabled = "0";
internal const string c_Disabled = "1";
internal const string c_WiFiCountryNotSet = "FAIL";
internal const string c_Active = "active";

internal const string c_LinuxRaspconfigCommand = "raspi-config";
internal const string c_WPACliCommand = "wpa_cli";
Expand Down
10 changes: 10 additions & 0 deletions src/MultiPlug.Ext.RasPi.Config/Models/Load/LocalisationLoad.cs
@@ -0,0 +1,10 @@
using System.Runtime.Serialization;

namespace MultiPlug.Ext.RasPi.Config.Models.Load
{
public class LocalisationLoad
{
[DataMember]
public bool WiFiCountrySyncEnabled { get; set; }
}
}
10 changes: 10 additions & 0 deletions src/MultiPlug.Ext.RasPi.Config/Models/Load/Root.cs
@@ -0,0 +1,10 @@
using System.Runtime.Serialization;

namespace MultiPlug.Ext.RasPi.Config.Models.Load
{
public class Root
{
[DataMember]
public LocalisationLoad Localisation { get; set; }
}
}

0 comments on commit 59d9be9

Please sign in to comment.