Skip to content

Commit

Permalink
Merge branch 'fix#7_MQTTReconnect' into dev_and_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Volker Hänsel committed Oct 8, 2023
2 parents bc6f421 + a1d54aa commit b46d765
Show file tree
Hide file tree
Showing 24 changed files with 93 additions and 66 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Docker Image CI
env:
IMAGE_NAME: volkerhaensel/${{ github.event.repository.name }}
REGISTRY: docker.io
on:
push:
workflow_dispatch:

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Git describe
id: ghd
uses: proudust/gh-describe@v1

# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
with:
context: ./tuya_mqtt.net
push: false
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
build-args: |
GIT_COMMIT=${{ steps.ghd.outputs.describe }}
41 changes: 0 additions & 41 deletions .github/workflows/docker-image.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64,linux/arm/v7
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
build-args: |
GIT_COMMIT=${{ steps.ghd.outputs.describe }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ In general all TUYA devices shall be supported.
The fundamental intention has been using the devices through the localTuya library.
Battery driven sensors are not supported as those do not offer a local port. To use those devices in the same way, there is a implementation to poll those data points over the cloud access.

So Tuya cloud AccessKey is nice, but not a must.
Having a Tuya cloud AccessKey is nice, but not a must.

## used other github projects
implements a MQTT Client (based on [MQTTnet](https://github.com/dotnet/MQTTnet))
Expand Down
Binary file added doc/wiki/cloudMode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/cloudModeAdd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/localMode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/mqttStatus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/save.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/scanned_empty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/tuya1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/tuya2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/wiki/tuya3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ services:
- 6667:6667/udp #needs to be published to receive encryped TUYA broadcasts
- 6666:6666/udp #needs to be published to receive unencryped TUYA broadcasts
- 8889:80/tcp #connect to 8889 to get to the web UI
# network_mode: host

restart: on-failure
healthcheck:
test: curl --fail http://localhost:80 || exit 1
Expand Down
2 changes: 1 addition & 1 deletion tuya_mqtt.net/Data/GlobalOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class GlobalOptions
{
public bool MtqqReconnect { get; set; } = true;
public bool MqttReconnect { get; set; } = true;
public int ExceptionsHealthDegraded { get; set; } = 10; //>10 Exceptions per minute = degraded
public int ExceptionsUnhealthy { get; set; } = 40; //>40 Exceptions per minute = unhealthy

Expand Down
4 changes: 4 additions & 0 deletions tuya_mqtt.net/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \
RID=linux-arm64 ; \
elif [ "$TARGETPLATFORM" = "linux/arm/v7" ]; then \
RID=linux-arm ; \
elif [ "$TARGETPLATFORM" = "linux/arm/v6" ]; then \
RID=linux-arm ; \
fi \
&& echo "target $TARGETPLATFORM build $BUILDPLATFORM RID $RID" \
&& date > "/src/Resources/BuildDate.txt" \
Expand All @@ -45,6 +47,8 @@ RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \
RID=linux-arm64 ; \
elif [ "$TARGETPLATFORM" = "linux/arm/v7" ]; then \
RID=linux-arm ; \
elif [ "$TARGETPLATFORM" = "linux/arm/v6" ]; then \
RID=linux-arm ; \
fi \
&& echo "target $TARGETPLATFORM build $BUILDPLATFORM RID $RID" \
&& dotnet publish "tuya_mqtt.net.csproj" -r $RID --self-contained false -c Release -o /app/publish /p:UseAppHost=false
Expand Down
24 changes: 20 additions & 4 deletions tuya_mqtt.net/Pages/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@inject ILogger<Settings> Logger
@inject IWritableOptions<MqttClientOptions> Mqttdata
@inject IWritableOptions<TuyaCommunicatorOptions> Tuyadata
@inject MqttClientService Client;
@inject MqttClientService _mqttClient;
@inject TuyaCommunicatorService TuyaService;
@inject ISnackbar Snackbar

Expand Down Expand Up @@ -78,7 +78,7 @@
<MudSwitch @bind-Checked="_mqttV5" Label="MQTT v5" onH Color="Color.Primary"/>
</MudTooltip>
<MudTooltip Text="allow packet fragmentation">
<MudSwitch @bind-Checked="_mqttNoFragmentation" Label="Packet fagmentation" Color="Color.Primary"/>
<MudSwitch @bind-Checked="_mqttNoFragmentation" Label="Packet fragmentation" Color="Color.Primary"/>
</MudTooltip>
<MudTextField @bind-Value="_mqttTopic" Label="Topic" HelperText="MQTT Main Topic" Variant="Variant.Text"></MudTextField>
</MudPaper>
Expand Down Expand Up @@ -222,7 +222,7 @@

_isInWait = true;
StateHasChanged();
var result = await Client.TestAsync(options);
var result = await _mqttClient.TestAsync(options);
if (result)
{
Snackbar.Add("Connected to MQTT broker", Severity.Success);
Expand Down Expand Up @@ -278,7 +278,23 @@
opt.TuyaAPIRegion = TuyaApiRegion;
});

Snackbar.Add("Settings saved", Severity.Success);
if (!_mqttClient.IsConnected) //try reconnect if not connected
{
Task.Run(async () =>
{
try
{
await _mqttClient.ReconnectMqttBrokerAsync();
}
catch
{
//suppress errors
}
}).AndForget();
;
}

Snackbar.Add("Settings saved", Severity.Success);
}


Expand Down
10 changes: 5 additions & 5 deletions tuya_mqtt.net/Pages/TuyaDevices.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<MudPaper>

<MudToggleIconButton @bind-Toggled="@CloudMode" Disabled="@ApiDisabled"
Icon="@Icons.Material.Filled.Cloud" Color="@Color.Primary" Title="Cloud"
ToggledIcon="@Icons.Material.Filled.Lan" ToggledColor="@Color.Primary" ToggledTitle="Local"/>
Icon="@Icons.Material.Filled.Lan" Color="@Color.Primary" Title="Switch to Cloud"
ToggledIcon="@Icons.Material.Filled.Cloud" ToggledColor="@Color.Primary" ToggledTitle="Switch to Local"/>
<span>@(CloudMode ? "Cloud Mode" : "Local Mode")</span>

<MudStack Row="true" Class="align-center">
Expand Down Expand Up @@ -109,7 +109,7 @@
<MudTooltip Text="Save device to monitored list">
<MudFab Color="Color.Primary" StartIcon="@Icons.Material.Filled.Save" OnClick="SaveAddTuyaDeviceAsync"/>
</MudTooltip>
<MudTooltip Text="delete device from monitored list">
<MudTooltip Text="Delete device from monitored list">
<MudFab Disabled="@(!_enableDelete)" Class="ml-4" Color="Color.Primary" StartIcon="@Icons.Material.Filled.DeleteSweep" OnClick="DeleteTuyaDevice" />
</MudTooltip>
</MudPaper>
Expand All @@ -129,7 +129,7 @@
private MudExpansionPanel? _expansionList;
private MudExpansionPanel? _expansionAdd;

private readonly double[] _timeList = { 500,1,2,5,10,30,60 };
private readonly double[] _timeList = { 0.5,1,2,5,10,30,60 };

private IEnumerable<KeyValuePair<string, TuyaDeviceScanInfo>>? _tuyaScannedDevices;
private IReadOnlyDictionary<string, TuyaExtendedDeviceInformation>? _tuyaMonitoredDevices;
Expand Down Expand Up @@ -289,7 +289,7 @@
_tuyaLocalKey = localKey;
_tuyaName = name;
_tuyaProtocolVersion = pv;
_tuyaPolltimeValue = 5; //5 seconds default
_tuyaPolltimeValue = 10; //10 seconds default
_enableDelete = false;
_tuyaDps = String.Empty;
_tuyaDeviceType = TuyaDeviceType.LocalTuya0A; //this shall work in most of the cases
Expand Down
4 changes: 2 additions & 2 deletions tuya_mqtt.net/Services/MqttClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void MqttHeartbeat(object? state)
}
}

private async Task ReconnectMqttBrokerAsync()
public async Task ReconnectMqttBrokerAsync()
{
Dispose();

Expand Down Expand Up @@ -121,7 +121,7 @@ private async Task ReconnectMqttBrokerAsync()

if (_mqttClient?.IsConnected == true)
{
_autoReconnect = GlobalOptions.MtqqReconnect;
_autoReconnect = GlobalOptions.MqttReconnect;

if (Options.MqttPort > 0)
_logger.LogInformation($"MQTT client connected to {Options.MqttHost}:{Options.MqttPort}");
Expand Down
13 changes: 5 additions & 8 deletions tuya_mqtt.net/Shared/TuyaMonitoredDevices.razor
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,22 @@
<div style="display: grid; grid-template-columns: 1fr auto; align-items: center; width: 100%">


@if (context.Value.SubItems is {Count: > 0 })
@if (context.Value.Value == null)
{
<MudTooltip Text="@context.Value.Address">
<MudText Style="justify-self: start;">@context.Value.Title</MudText>
</MudTooltip>

<MudIconButton Style="justify-self: end;" Icon="@Icons.Material.Filled.Edit" OnClick="@(() => { ClickChangeButton(item.ID).AndForget(); })" OnSize="Size.Medium" Color="Color.Inherit" />

<MudTooltip Text="change device settings">
<MudFab IconSize="Size.Medium" Style="justify-self: end;" StartIcon="@Icons.Material.Outlined.ChangeCircle" Size="Size.Small" Tag="1" OnClick="@(() => { ClickChangeButton(item.ID).AndForget(); })" />
</MudTooltip>
}
else
{
<MudTooltip Text="@context.Value.Address">
<MudText Style="justify-self: start;">@context.Value.Title</MudText>
</MudTooltip>
<MudStack Row="true">
<MudText Style="">@context.Value.Value</MudText>

<MudIconButton Style="justify-self: end;" Icon="@Icons.Material.Filled.Edit" OnClick="@(() => { ClickChangeButton(item.ID).AndForget(); })" OnSize="Size.Medium" Color="Color.Inherit" />
</MudStack>
<MudText Style="color:gray">@context.Value.Value</MudText>
}
</div>
</BodyContent>
Expand Down
8 changes: 6 additions & 2 deletions tuya_mqtt.net/Shared/TuyaScannedDevices.razor
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
<MudItem xs="2" Class="d-flex justify-center">
@if (ContainsMonitoredDevices(d.Value.GwId))
{
<MudFab StartIcon="@Icons.Material.Outlined.ChangeCircle" Size="Size.Small" Tag="1" OnClick="() => { ClickAddChangeButton(d.Value).AndForget(); }"/>
<MudTooltip Text="change device settings">
<MudFab StartIcon="@Icons.Material.Outlined.ChangeCircle" Size="Size.Small" Tag="1" OnClick="() => { ClickAddChangeButton(d.Value).AndForget(); }"/>
</MudTooltip>
}
else
{
<MudFab StartIcon="@Icons.Material.Filled.Add" Size="Size.Small" Tag="1" OnClick="() => { ClickAddChangeButton(d.Value).AndForget(); }"/>
<MudTooltip Text="connect device">
<MudFab StartIcon="@Icons.Material.Filled.Add" Size="Size.Small" Tag="1" OnClick="() => { ClickAddChangeButton(d.Value).AndForget(); }"/>
</MudTooltip>
}

</MudItem>
Expand Down

0 comments on commit b46d765

Please sign in to comment.