Skip to content

Commit

Permalink
v1.4.0-beta.26
Browse files Browse the repository at this point in the history
- fixed memory leak in ValueStatistics: lower overall memory usage and faster events processing
- added statistic data resampling
- improved statistics graph
- implemented ServiceWorker to run HG as a system service (see README)
- updated README.md
  • Loading branch information
genemars committed Mar 8, 2023
1 parent e5be9d2 commit 72766b2
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 129 deletions.
95 changes: 77 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,91 @@ Download the `.zip` archive corresponding to the hosting operating system:

https://github.com/genielabs/HomeGenie/releases

Unzip the archive file and run the `./HomeGenie` command to start the service:
Unzip the archive file. A new `homegenie` folder will be created.

```shell

### Running in a terminal

Set the current directory to `homegenie` and run the `./HomeGenie` command:

```bash
cd homegenie
./HomeGenie
```

HomeGenie UI is now accessible with a web browser:
To stop the application press `CTRL + C`


### Running as a system service

HomeGenie can be installed as a service. The procedure is different depending on the
hosting operating system.

#### Recommended procedure for Linux

1) Add a specific user for the service and copy the content of `homegenie` folder
to the new user home directory:

```bash
sudo useradd homegenie
sudo cp -ar ./path-to-extracted-folder/homegenie/* /home/homegenie/
sudo chown -R homegenie:homegenie /home/homegenie
```

2) Create the file `/etc/systemd/system/homegenie.service` with the following content:
```bash
[Unit]
Description=HomeGenie

`http://<server_address>:<port>/`
[Service]
Type=notify
User=homegenie
WorkingDirectory=/home/homegenie/
ExecStart=/home/homegenie/HomeGenie
Restart=on-failure

where `<server_address>` is the name or ip of the host where *HomeGenie* was installed and `<port>`
is the port on which is listening for web requests (default port is `8080`).
[Install]
WantedBy=multi-user.target
```

3) Refresh `SystemD` configuration
```bash
sudo systemctl daemon-reload
```

---
4) Start the service and enable <em>HomeGenie</em> to auto-start on next system boot:
```bash
sudo systemctl start homegenie.service
sudo systemctl enable homegenie.service
```

Other possible commands are `stop` and `disable`.


See also:
- [Create Linux Service](https://devblogs.microsoft.com/dotnet/net-core-and-systemd/#create-unit-files) (SystemD)
- [Create Windows Service](https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service#create-the-windows-service)


### Accessing the UI

HomeGenie user interface can be accessed from any web browser entering the url

&nbsp;&nbsp;&nbsp;&nbsp; `http://<server_ip>:<port>/`

Where `server_ip` is the IP address of the machine where HomeGenie is running and `port` can be *80*
or the first available port starting from *8080*.
<small>(ex. *http://192.168.1.150:8080/*)</small>

The `port` settings can be changed either from the maintenance page
or editing the `systemconfig.xml` file located in the application folder.


### Optional post-installation steps

Depending on the hosting operating system, it might be required to run additional steps
in order to allow the service to access the **Serial port**, **USB** devices and **GPIO** hardware.

### Common additional steps

To enable **audio playack** and **voice synthesis**:
```shell
# Audio playback utilities
Expand All @@ -75,16 +139,10 @@ To use **X10 Home Automation** hardware:
sudo apt-get install libusb-1.0-0 libusb-1.0-0-dev
```

To grant access to the **Serial port** and/or **GPIO** to the current user:
To grant access to the **Serial port** and/or **GPIO** to the `homegenie` user:
```shell
sudo gpasswd -a $USER dialout
sudo gpasswd -a $USER gpio
```

It's recommended that a dedicated user is added for running a service, but as a last resort, if you are still getting `access denied`
error while trying to access connected hardware, run `./HomeGenie` service using `sudo`:
```
sudo ./HomeGenie
sudo gpasswd -a homegenie dialout
sudo gpasswd -a homegenie gpio
```


Expand Down Expand Up @@ -132,6 +190,7 @@ Windows specific solution for deploying HomeGenie as a Windows service (deprecat
- https://github.com/genielabs/HomeGenie-WindowsPhone
- https://github.com/genielabs/homegenie-mini
- https://github.com/genielabs/yot
- https://github.com/zuixjs/zuix

------

Expand Down
2 changes: 1 addition & 1 deletion assets/build/all/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@

</div>

<script src="runtime.b92e2ca178ebbf4a.js" type="module"></script><script src="polyfills.710b144f4805f4b0.js" type="module"></script><script src="main.e66db3b2ab088749.js" type="module"></script>
<script src="runtime.b92e2ca178ebbf4a.js" type="module"></script><script src="polyfills.710b144f4805f4b0.js" type="module"></script><script src="main.6e0cec9b0905a17b.js" type="module"></script>

</body></html>

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions assets/build/all/app/ngsw.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"configVersion": 1,
"timestamp": 1678109215629,
"timestamp": 1678284933122,
"index": "/app/index.html",
"assetGroups": [
{
Expand All @@ -15,7 +15,7 @@
"/app/613.16b9c52c0ae74833.js",
"/app/859.0f150be9c99a6b5a.js",
"/app/index.html",
"/app/main.e66db3b2ab088749.js",
"/app/main.6e0cec9b0905a17b.js",
"/app/manifest.webmanifest",
"/app/polyfills.710b144f4805f4b0.js",
"/app/runtime.b92e2ca178ebbf4a.js",
Expand Down Expand Up @@ -724,8 +724,8 @@
"/app/assets/widgets/weather/images_alt/weather.svg": "c719c09048977fd40688272316b35ddf1bdf89e8",
"/app/assets/widgets/weather/style.css": "1b70730e73885cc0ee83566eaf584008d34e2255",
"/app/assets/workers/typings-worker.js": "8df10d867c12d36cdebfb7e16caacd0020db301f",
"/app/index.html": "a2ef3f6bb3f7a135ed5730f20d57b90a3f300000",
"/app/main.e66db3b2ab088749.js": "705ff59b858b7fb0bbb0122d4deefb53ec77d231",
"/app/index.html": "12279b6f7ed37e147591c867e01990e599a54c55",
"/app/main.6e0cec9b0905a17b.js": "de4175fd3b0f16ba00f7bc3fc0d1e766e0667ec6",
"/app/manifest.webmanifest": "d47bb07a495e27ddf072f8702df373f79b4eb183",
"/app/polyfills.710b144f4805f4b0.js": "594a5ea1b9d3f9cbc4f363eca84eda2c8bb8a901",
"/app/runtime.b92e2ca178ebbf4a.js": "b50d841a4c50d466c34aab3267d3fb4afe87dede",
Expand Down
51 changes: 5 additions & 46 deletions src/HomeGenie/Data/ValueStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ You should have received a copy of the GNU General Public License

using System;
using System.Collections.Generic;

using HomeGenie.Service;
using HomeGenie.Service.Logging;

namespace HomeGenie.Data
{
Expand All @@ -33,13 +31,6 @@ namespace HomeGenie.Data
/// </summary>
public class ValueStatistics
{
private static readonly List<string> StatisticsFields = new List<string>() {
"Sensor.",
"Meter.",
"PowerMonitor.",
"Statistics."
};

/// <summary>
/// Stat value.
/// </summary>
Expand Down Expand Up @@ -76,7 +67,6 @@ public StatValue(double value, DateTime timestamp)
}
}

private List<StatValue> statValues;
private TsList<StatValue> historyValues;
// historyLimit is expressed in minutes
private int historyLimit = 60 * 24;
Expand All @@ -85,12 +75,10 @@ public StatValue(double value, DateTime timestamp)

public ValueStatistics()
{
LastProcessedTimestamp = DateTime.UtcNow;
statValues = new List<StatValue>();
statValues.Add(new StatValue(0, LastProcessedTimestamp));
lastEvent = lastOn = lastOff = new StatValue(0, LastProcessedTimestamp);
var initValue = new StatValue(0, DateTime.UtcNow);
lastEvent = lastOn = lastOff = initValue;
historyValues = new TsList<StatValue>();
historyValues.Add(lastEvent);
historyValues.Add(initValue);
}

/// <summary>
Expand Down Expand Up @@ -129,7 +117,7 @@ public TsList<StatValue> History
/// <value>The current.</value>
public StatValue Current
{
get { return historyValues[0]; }
get { return historyValues.Count > 0 ? historyValues[0] : null; }
}

/// <summary>
Expand Down Expand Up @@ -161,12 +149,6 @@ public StatValue LastOff

internal void AddValue(string fieldName, double value, DateTime timestamp)
{

if (IsValidField(fieldName))
{
// add value for StatisticsLogger use
statValues.Add(new StatValue(value, timestamp));
}
// "value" is the occurring event in this very moment,
// so "Current" is holding previous value right now
if (Current.Value != value)
Expand All @@ -183,7 +165,7 @@ internal void AddValue(string fieldName, double value, DateTime timestamp)
lastOn = new StatValue(value, timestamp);
}
}
// keeep size within historyLimit (minutes)
// keep size within historyLimit (minutes)
try
{
if (historyValues.Count > historyLimitSize)
Expand All @@ -200,20 +182,6 @@ internal void AddValue(string fieldName, double value, DateTime timestamp)
historyValues.Insert(0, new StatValue(value, timestamp));
}

private static bool IsValidField(string field)
{
bool isValid = false;
foreach (string f in StatisticsFields)
{
if (field.StartsWith(f))
{
isValid = true;
break;
}
}
return isValid;
}

/// <summary>
/// Get resampled statistic values by averaging values for a given time range increment (eg 60 minutes)
/// </summary>
Expand All @@ -223,14 +191,5 @@ internal List<StatValue> GetResampledValues(int sampleWidth) // in minutes
// TODO: to be implemented
return null;
}

// These fields are used by StatisticsLogger
internal DateTime LastProcessedTimestamp;
internal List<StatValue> Values
{
get { return statValues; }
set { statValues = value; }
}

}
}
5 changes: 5 additions & 0 deletions src/HomeGenie/HomeGenie.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageReference Include="IronPython" Version="3.4.0" />
<PackageReference Include="IronPython.StdLib" Version="3.4.0" />
<PackageReference Include="KNX.net" Version="1.1.10" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.1" />
<PackageReference Include="Microsoft.NETCore.Platforms" Version="6.0.7" />
<PackageReference Include="MIG" Version="1.2.9" />
<PackageReference Include="MIG.HomeAutomation" Version="1.2.19" />
Expand Down Expand Up @@ -68,6 +69,10 @@
<Files Include="$(ProjectDir)../../assets/build/all/**" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="6.0.0" />
</ItemGroup>

<Target Name="CopyFiles" AfterTargets="Build">
<Copy SourceFiles="@(Files)" DestinationFolder="$(TargetDir)/%(RecursiveDir)" />
</Target>
Expand Down
Loading

0 comments on commit 72766b2

Please sign in to comment.