Client now connects to the broker

This commit is contained in:
2019-04-24 23:14:35 +02:00
parent ab85e78f4c
commit b4bf7aad20
8 changed files with 97 additions and 42 deletions

View File

@@ -14,7 +14,7 @@ namespace PlantBox.Client.Forms.Controls.StatusBar
public partial class StatusBar : ContentView public partial class StatusBar : ContentView
{ {
// Progress // Progress
public static readonly BindableProperty ProgressProperty = BindableProperty.Create(nameof(Progress), typeof(double), typeof(StatusBar)); public static readonly BindableProperty ProgressProperty = BindableProperty.Create(nameof(Progress), typeof(double), typeof(StatusBar), 0.5);
public double Progress public double Progress
{ {
get => (double)GetValue(ProgressProperty); get => (double)GetValue(ProgressProperty);

View File

@@ -34,7 +34,7 @@
BoundColor="DarkSlateGray" BoundColor="DarkSlateGray"
BoundHeight="2" BoundHeight="2"
LowerBound="{Binding LuminosityCaptor.Min, Converter={x:StaticResource CaptorConverter}, ConverterParameter={x:Static models:CaptorType.Luminosity}}" LowerBound="{Binding LuminosityCaptor.Min, Converter={x:StaticResource CaptorConverter}, ConverterParameter={x:Static models:CaptorType.Luminosity}}"
UpperBound="-1" UpperBound="{Binding LuminosityCaptor.Max, Converter={x:StaticResource CaptorConverter}, ConverterParameter={x:Static models:CaptorType.Luminosity}}"
Image="{extensions:ImageResource Luminosity_Icon.png}" Image="{extensions:ImageResource Luminosity_Icon.png}"
FlexLayout.Basis="{StaticResource BarWidth}" /> FlexLayout.Basis="{StaticResource BarWidth}" />
<statusbar:StatusBar Progress="{Binding TemperatureCaptor.Value, Converter={x:StaticResource CaptorConverter}, ConverterParameter={x:Static models:CaptorType.Temperature}}" <statusbar:StatusBar Progress="{Binding TemperatureCaptor.Value, Converter={x:StaticResource CaptorConverter}, ConverterParameter={x:Static models:CaptorType.Temperature}}"
@@ -49,8 +49,8 @@
FlexLayout.Basis="{StaticResource BarWidth}" /> FlexLayout.Basis="{StaticResource BarWidth}" />
<statusbar:StatusBar Progress="{Binding TankValue, Converter={x:StaticResource CaptorConverter}, ConverterParameter={x:Static models:CaptorType.Tank}}" <statusbar:StatusBar Progress="{Binding TankValue, Converter={x:StaticResource CaptorConverter}, ConverterParameter={x:Static models:CaptorType.Tank}}"
Label="{Binding TankValue, StringFormat='\{0:F0} %'}" Label="{Binding TankValue, StringFormat='\{0:F0} %'}"
ForegroundColor="DeepSkyBlue" ForegroundColor="{StaticResource HumidityColor}"
HintColor="SkyBlue" HintColor="{StaticResource HumidityColorSecondary}"
BoundColor="DarkSlateGray" BoundColor="DarkSlateGray"
BoundHeight="2" BoundHeight="2"
LowerBound="-1" LowerBound="-1"

View File

@@ -170,11 +170,13 @@ namespace PlantBox.Client.Forms.Plant
int index = 0; int index = 0;
for (uint i = 0; index < entries.Length; i += (uint)step) for (uint i = 0; index < entries.Length; i += (uint)step)
{ {
var value = values[i]; var entry = values[i];
entries[index] = new Entry((float)value.Value) double value = entry.Value != 0 ? entry.Value : 0.0001;
entries[index] = new Entry((float)value)
{ {
Label = value.Date.ToString(labelFormat), Label = entry.Date.ToString(labelFormat),
ValueLabel = string.Format(valueFormat, value.Value), ValueLabel = string.Format(valueFormat, value),
Color = colors[index] Color = colors[index]
}; };

View File

@@ -9,19 +9,19 @@ namespace PlantBox.Client.Models
public HistoricEntries MinutelyHistoric { get; } public HistoricEntries MinutelyHistoric { get; }
public HistoricEntries HourlyHistoric { get; } public HistoricEntries HourlyHistoric { get; }
public HistoricEntries DailyHistoric { get; } public HistoricEntries DailyHistoric { get; }
public HistoricEntries WeeklyHistoric { get; }
public HistoricEntries MonthlyHistoric { get; } public HistoricEntries MonthlyHistoric { get; }
public Historic public Historic
( (
HistoricEntries minutelyHistoric, HistoricEntries hourlyHistoric, HistoricEntries dailyHistoric, HistoricEntries minutelyHistoric,
HistoricEntries weeklyHistoric, HistoricEntries monthlyHistoric HistoricEntries hourlyHistoric,
HistoricEntries dailyHistoric,
HistoricEntries monthlyHistoric
) )
{ {
MinutelyHistoric = minutelyHistoric; MinutelyHistoric = minutelyHistoric;
HourlyHistoric = hourlyHistoric; HourlyHistoric = hourlyHistoric;
DailyHistoric = dailyHistoric; DailyHistoric = dailyHistoric;
WeeklyHistoric = weeklyHistoric;
MonthlyHistoric = monthlyHistoric; MonthlyHistoric = monthlyHistoric;
} }
} }

View File

@@ -1,4 +1,5 @@
using PlantBox.Client.Extensions; using PlantBox.Client.Extensions;
using PlantBox.Shared.Communication.Commands;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -26,6 +27,10 @@ namespace PlantBox.Client.Models
Luminosities = ValuesToEntries(luminosities); Luminosities = ValuesToEntries(luminosities);
Temperatures = ValuesToEntries(temperatures); Temperatures = ValuesToEntries(temperatures);
} }
public HistoricEntries(HistoricResponse response, TimeSpan time) : this(DateTime.Now - response.Time, time, response.Humidities, response.Luminosities, response.Temperatures)
{
}
private HistoricEntry[] ValuesToEntries(double[] values) private HistoricEntry[] ValuesToEntries(double[] values)
{ {

View File

@@ -11,13 +11,25 @@ namespace PlantBox.Client.Models
public ulong ID { get; } public ulong ID { get; }
public PlantType Type { get; } public PlantType Type { get; }
public PlantState State { get; } public PlantState State { get; }
public double HumidityMin { get; }
public double HumidityMax { get; }
public double LuminosityMin { get; }
public double LuminosityMax { get; }
public double TemperatureMin { get; }
public double TemperatureMax { get; }
public PlantInfo(string name, ulong iD, PlantType type, PlantState state) public PlantInfo(string name, ulong id, PlantType type, PlantState state, double humidityMin, double humidityMax, double luminosityMin, double luminosityMax, double temperatureMin, double temperatureMax)
{ {
Name = name; Name = name;
ID = iD; ID = id;
Type = type; Type = type;
State = state; State = state;
HumidityMin = humidityMin;
HumidityMax = humidityMax;
LuminosityMin = luminosityMin;
LuminosityMax = luminosityMax;
TemperatureMin = temperatureMin;
TemperatureMax = temperatureMax;
} }
} }
} }

View File

@@ -1,10 +1,12 @@
using PlantBox.Client.Models; using PlantBox.Client.Models;
using PlantBox.Shared.Communication;
using PlantBox.Shared.Communication.Commands; using PlantBox.Shared.Communication.Commands;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Net.Sockets;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -24,17 +26,38 @@ namespace PlantBox.Client.ViewModels
public ObservableCollection<PlantInfo> LoadPlants(IEnumerable<ulong> ids) public ObservableCollection<PlantInfo> LoadPlants(IEnumerable<ulong> ids)
{ {
// Mock IEnumerable<PlantInfo> infos = GetPlantInfos(ids);
return new ObservableCollection<PlantInfo>
return new ObservableCollection<PlantInfo>(infos);
}
private IEnumerable<PlantInfo> GetPlantInfos(IEnumerable<ulong> ids)
{
using (var client = new TcpClient(App.Settings.BrokerIP, Connection.TCP_CLIENT_PORT))
{
var stream = new CommandStream(client.GetStream());
foreach (ulong id in ids)
{
stream.Send(new InfoRequest().ToCommandPacket(id));
(_, var responseInfo) = stream.Receive<InfoResponse>();
yield return new PlantInfo
( (
new[] { responseInfo.Name,
new PlantInfo("Salon", 0, PlantType.Ficus, PlantState.Good), id,
new PlantInfo("Chambre", 1, PlantType.Bamboo, PlantState.Warning), responseInfo.Type,
new PlantInfo("Hellx", 2, PlantType.Cactus, PlantState.Bad), responseInfo.State,
new PlantInfo("Hello", 3, PlantType.Cactus, PlantState.Bad) responseInfo.HumidityMin,
}.OrderBy(x => x.State).ThenBy(x => x.Name) responseInfo.HumidityMax,
responseInfo.LuminosityMin,
responseInfo.LuminosityMax,
responseInfo.TemperatureMin,
responseInfo.TemperatureMax
); );
} }
}
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null) protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{ {

View File

@@ -1,8 +1,10 @@
using PlantBox.Client.Models; using PlantBox.Client.Models;
using PlantBox.Shared.Communication;
using PlantBox.Shared.Communication.Commands; using PlantBox.Shared.Communication.Commands;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Net.Sockets;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
@@ -119,27 +121,38 @@ namespace PlantBox.Client.ViewModels
private void LoadValues() private void LoadValues()
{ {
// TODO using (var client = new TcpClient(App.Settings.BrokerIP, Connection.TCP_CLIENT_PORT))
//throw new NotImplementedException(); {
var stream = new CommandStream(client.GetStream());
// Temp // Captors info
HumidityCaptor = new CaptorValue(20, 80, 21); stream.Send(new CaptorsRequest().ToCommandPacket(PlantInfo.ID));
LuminosityCaptor = new CaptorValue(50, 1000, 375.6); (_, var captorsReponse) = stream.Receive<CaptorsResponse>();
TemperatureCaptor = new CaptorValue(20, 35, 27);
TankValue = 78.45;
var humidities = new[] { 45.5, 45, 89, 78, 42, 56, 45, 23, 14, 89, 10, 5 }; HumidityCaptor = new CaptorValue(PlantInfo.HumidityMin, PlantInfo.HumidityMax, captorsReponse.Humidity);
var luminosities = new[] { 45.5, 45, 89, 78, 42, 56, 45, 23, 14, 89, 10, 5 }; LuminosityCaptor = new CaptorValue(PlantInfo.LuminosityMin, PlantInfo.LuminosityMax, captorsReponse.Luminosity);
var temperatures = new[] { 45.5, 45, 89, 78, 42, 56, 45, 23, 14, 89, 10, 5 }; TemperatureCaptor = new CaptorValue(PlantInfo.TemperatureMin, PlantInfo.TemperatureMax, captorsReponse.Temperature);
var humidities2 = new[] { 45.5, 45, 89, 78, 42, 56, 45, 23, 14, 89, 10, 5, 45, 89, 78, 42, 56, 45, 23, 14, 89, 10, 5, 9 }; TankValue = captorsReponse.Tank;
var luminosities2 = new[] { 45.5, 45, 89, 78, 42, 56, 45, 23, 14, 89, 10, 5, 45, 89, 78, 42, 56, 45, 23, 14, 89, 10, 5, 9 };
var temperatures2 = new[] { 45.5, 45, 89, 78, 42, 56, 45, 23, 14, 89, 10, 5, 45, 89, 78, 42, 56, 45, 23, 14, 89, 10, 5, 9 };
Historic = new Historic(new HistoricEntries(DateTime.Now, TimeSpan.FromMinutes(5), humidities, luminosities, temperatures), // Historic
new HistoricEntries(DateTime.Now, TimeSpan.FromHours(1), humidities2, luminosities2, temperatures2), stream.Send(new HistoricRequest(HistoricInterval.Minutely, 12).ToCommandPacket(PlantInfo.ID));
new HistoricEntries(DateTime.Now, TimeSpan.FromDays(1), humidities2, luminosities2, temperatures2), (_, var minutelyHistoric) = stream.Receive<HistoricResponse>();
new HistoricEntries(DateTime.Now, TimeSpan.FromDays(7), humidities2, luminosities2, temperatures2), stream.Send(new HistoricRequest(HistoricInterval.Hourly, 48).ToCommandPacket(PlantInfo.ID));
new HistoricEntries(DateTime.Now, TimeSpan.FromDays(31), humidities2, luminosities2, temperatures2)); (_, var hourlyHistoric) = stream.Receive<HistoricResponse>();
stream.Send(new HistoricRequest(HistoricInterval.Daily, 186).ToCommandPacket(PlantInfo.ID));
(_, var dailyHistoric) = stream.Receive<HistoricResponse>();
stream.Send(new HistoricRequest(HistoricInterval.Monthly, 36).ToCommandPacket(PlantInfo.ID));
(_, var monthlyHistoric) = stream.Receive<HistoricResponse>();
var historic = new Historic
(
new HistoricEntries(minutelyHistoric, TimeSpan.FromMinutes(5)),
new HistoricEntries(hourlyHistoric, TimeSpan.FromHours(1)),
new HistoricEntries(dailyHistoric, TimeSpan.FromDays(1)),
new HistoricEntries(monthlyHistoric, TimeSpan.FromDays(31))
);
Historic = historic;
}
} }
protected void OnPropertyChanged([CallerMemberName] string propertyName = null) protected void OnPropertyChanged([CallerMemberName] string propertyName = null)