Compare commits
	
		
			6 Commits
		
	
	
		
			feature/pr
			...
			feature/do
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 966d8bd79e | |||
| 5b42ce9f43 | |||
| 85320d3cf3 | |||
| 9d55c39e33 | |||
| d75e9d378d | |||
| 3cdaa2dc35 | 
							
								
								
									
										25
									
								
								.dockerignore
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								.dockerignore
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
**/.classpath
 | 
			
		||||
**/.dockerignore
 | 
			
		||||
**/.env
 | 
			
		||||
**/.git
 | 
			
		||||
**/.gitignore
 | 
			
		||||
**/.project
 | 
			
		||||
**/.settings
 | 
			
		||||
**/.toolstarget
 | 
			
		||||
**/.vs
 | 
			
		||||
**/.vscode
 | 
			
		||||
**/*.*proj.user
 | 
			
		||||
**/*.dbmdl
 | 
			
		||||
**/*.jfm
 | 
			
		||||
**/azds.yaml
 | 
			
		||||
**/docs
 | 
			
		||||
**/bin
 | 
			
		||||
**/docker-compose*
 | 
			
		||||
**/Dockerfile*
 | 
			
		||||
**/node_modules
 | 
			
		||||
**/npm-debug.log
 | 
			
		||||
**/obj
 | 
			
		||||
**/secrets.dev.yaml
 | 
			
		||||
**/values.dev.yaml
 | 
			
		||||
LICENSE
 | 
			
		||||
README.md
 | 
			
		||||
@@ -8,6 +8,9 @@
 | 
			
		||||
    <SpaRoot>ClientApp\</SpaRoot>
 | 
			
		||||
    <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
 | 
			
		||||
    <AssemblyName>Birdmap.API</AssemblyName>
 | 
			
		||||
    <UserSecretsId>a919c854-b332-49ee-8e38-96549f828836</UserSecretsId>
 | 
			
		||||
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
 | 
			
		||||
    <DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
 | 
			
		||||
  </PropertyGroup>
 | 
			
		||||
 | 
			
		||||
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
 | 
			
		||||
@@ -31,6 +34,7 @@
 | 
			
		||||
      <PrivateAssets>all</PrivateAssets>
 | 
			
		||||
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
 | 
			
		||||
    </PackageReference>
 | 
			
		||||
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.9" />
 | 
			
		||||
    <PackageReference Include="MQTTnet" Version="3.0.13" />
 | 
			
		||||
    <PackageReference Include="MQTTnet.AspNetCore" Version="3.0.13" />
 | 
			
		||||
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
export default {
 | 
			
		||||
    probability_method_name: 'NotifyDeviceAsync',
 | 
			
		||||
    probability_method_name: 'NotifyMessagesAsync',
 | 
			
		||||
    update_method_name: 'NotifyDeviceUpdatedAsync',
 | 
			
		||||
    update_all_method_name: 'NotifyAllUpdatedAsync',
 | 
			
		||||
};
 | 
			
		||||
@@ -57,6 +57,9 @@ class Dashboard extends Component {
 | 
			
		||||
    componentWillUnmount() {
 | 
			
		||||
        this.context.removeHandler(C.update_all_method_name, this.updateSeries);
 | 
			
		||||
        this.context.removeHandler(C.update_method_name, this.updateSeries);
 | 
			
		||||
        if (this.updateTimer) {
 | 
			
		||||
            clearTimeout(this.updateTimer);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    getItemsWithStatus(iterate, status) {
 | 
			
		||||
@@ -111,8 +114,8 @@ class Dashboard extends Component {
 | 
			
		||||
    updateDynamic = () => {
 | 
			
		||||
        const secondAgo = new Date();
 | 
			
		||||
        secondAgo.setMilliseconds(0);
 | 
			
		||||
        const minuteAgo = new Date( Date.now() - 1000 * 60 );
 | 
			
		||||
        const hourAgo = new Date( Date.now() - 1000 * 60 * 60 );
 | 
			
		||||
        const minuteAgo = new Date(Date.now() - 1000 * 60);
 | 
			
		||||
        const hourAgo = new Date(Date.now() - 1000 * 60 * 60);
 | 
			
		||||
 | 
			
		||||
        const minuteDevicePoints = {};
 | 
			
		||||
        const hourDevicePoints = {};
 | 
			
		||||
@@ -125,7 +128,7 @@ class Dashboard extends Component {
 | 
			
		||||
            barDevicePoints[d.id] = Array(3).fill(0);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const processMethod = (items, index) => {
 | 
			
		||||
        const processHeatmapItem = (items, index) => {
 | 
			
		||||
            const p = items[index];
 | 
			
		||||
            if (p.date > minuteAgo) {
 | 
			
		||||
                var seconds = Math.floor((p.date.getTime() - minuteAgo.getTime()) / 1000);
 | 
			
		||||
@@ -164,7 +167,7 @@ class Dashboard extends Component {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const finishMethod = () => {
 | 
			
		||||
        const onFinished = () => {
 | 
			
		||||
            const minuteHeatmapSeries = [];
 | 
			
		||||
 | 
			
		||||
            var i = 0;
 | 
			
		||||
@@ -172,7 +175,7 @@ class Dashboard extends Component {
 | 
			
		||||
                minuteHeatmapSeries.push({
 | 
			
		||||
                    name: "Device " + i,
 | 
			
		||||
                    data: minuteDevicePoints[p].map((value, index) => ({
 | 
			
		||||
                        x: new Date( Date.now() - (60 - index) * 1000 ).toLocaleTimeString('hu-HU'),
 | 
			
		||||
                        x: new Date(Date.now() - (60 - index) * 1000).toLocaleTimeString('hu-HU'),
 | 
			
		||||
                        y: value
 | 
			
		||||
                    })),
 | 
			
		||||
                });
 | 
			
		||||
@@ -186,7 +189,7 @@ class Dashboard extends Component {
 | 
			
		||||
                hourHeatmapSeries.push({
 | 
			
		||||
                    name: "Device " + i,
 | 
			
		||||
                    data: hourDevicePoints[p].map((value, index) => ({
 | 
			
		||||
                        x: new Date( Date.now() - (60 - index) * 1000 * 60 ).toLocaleTimeString('hu-HU').substring(0, 5),
 | 
			
		||||
                        x: new Date(Date.now() - (60 - index) * 1000 * 60).toLocaleTimeString('hu-HU').substring(0, 5),
 | 
			
		||||
                        y: value
 | 
			
		||||
                    })),
 | 
			
		||||
                });
 | 
			
		||||
@@ -218,7 +221,7 @@ class Dashboard extends Component {
 | 
			
		||||
                data: getCount(2),
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            const lineSeries = [{name: "message/sec", data: []}];
 | 
			
		||||
            const lineSeries = [{ name: "message/sec", data: [] }];
 | 
			
		||||
            for (var m in linePoints) {
 | 
			
		||||
                lineSeries[0].data.push({
 | 
			
		||||
                    x: new Date(m).getTime(),
 | 
			
		||||
@@ -236,21 +239,24 @@ class Dashboard extends Component {
 | 
			
		||||
                return categories;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            this.setState({
 | 
			
		||||
                heatmapSecondsSeries: minuteHeatmapSeries,
 | 
			
		||||
                heatmapMinutesSeries: hourHeatmapSeries,
 | 
			
		||||
                barSeries: barSeries,
 | 
			
		||||
                barCategories: getBarCategories(),
 | 
			
		||||
                lineSeries: lineSeries,
 | 
			
		||||
            });
 | 
			
		||||
            const toUpdate = [
 | 
			
		||||
                { heatmapSecondsSeries: minuteHeatmapSeries },
 | 
			
		||||
                { heatmapMinutesSeries: hourHeatmapSeries },
 | 
			
		||||
                { barSeries: barSeries },
 | 
			
		||||
                { barCategories: getBarCategories() },
 | 
			
		||||
                { lineSeries: lineSeries }
 | 
			
		||||
            ];
 | 
			
		||||
 | 
			
		||||
            setTimeout(this.updateDynamic, 1000);
 | 
			
		||||
            //Set states must be done separately otherwise ApexChart's UI update freezes the page.
 | 
			
		||||
            this.performTask(toUpdate, 2, 300, (list, index) => {
 | 
			
		||||
                this.setState(list[index]);
 | 
			
		||||
            },
 | 
			
		||||
            () => {
 | 
			
		||||
                this.updateTimer = setTimeout(this.updateDynamic, 1000);
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const processHeatmapItem = processMethod.bind(this);
 | 
			
		||||
        const onFinished = finishMethod.bind(this)
 | 
			
		||||
 | 
			
		||||
        this.performTask(this.context.heatmapPoints, Math.ceil(this.context.heatmapPoints.length / 100), 10,
 | 
			
		||||
        this.performTask(this.context.heatmapPoints, Math.ceil(this.context.heatmapPoints.length / 50), 20,
 | 
			
		||||
            processHeatmapItem, onFinished);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -281,36 +287,36 @@ class Dashboard extends Component {
 | 
			
		||||
            <Box className={classes.root}>
 | 
			
		||||
                <Grid container spacing={3}>
 | 
			
		||||
                    <Grid item xs={12}>
 | 
			
		||||
                        <Services isAdmin={this.props.isAdmin}/>
 | 
			
		||||
                        <Services isAdmin={this.props.isAdmin} />
 | 
			
		||||
                    </Grid>
 | 
			
		||||
                    <Grid item xs={6}>
 | 
			
		||||
                        <Paper className={classes.paper}>
 | 
			
		||||
                            <DonutChart totalLabel="Devices" series={this.state.deviceSeries}/>
 | 
			
		||||
                            <DonutChart totalLabel="Devices" series={this.state.deviceSeries} />
 | 
			
		||||
                        </Paper>
 | 
			
		||||
                    </Grid>
 | 
			
		||||
                    <Grid item xs={6}>
 | 
			
		||||
                        <Paper className={classes.paper}>
 | 
			
		||||
                            <DonutChart totalLabel="Sensors" series={this.state.sensorSeries}/>
 | 
			
		||||
                            <DonutChart totalLabel="Sensors" series={this.state.sensorSeries} />
 | 
			
		||||
                        </Paper>
 | 
			
		||||
                    </Grid>
 | 
			
		||||
                    <Grid item xs={12}>
 | 
			
		||||
                        <Paper className={classes.paper}>
 | 
			
		||||
                            <HeatmapChart label="Highest probability per second by devices" series={this.state.heatmapSecondsSeries}/>
 | 
			
		||||
                            <HeatmapChart label="Highest probability per second by devices" series={this.state.heatmapSecondsSeries} />
 | 
			
		||||
                        </Paper>
 | 
			
		||||
                    </Grid>
 | 
			
		||||
                    <Grid item xs={12}>
 | 
			
		||||
                        <Paper className={classes.paper}>
 | 
			
		||||
                            <HeatmapChart label="Highest probability per minute by devices" series={this.state.heatmapMinutesSeries}/>
 | 
			
		||||
                            <HeatmapChart label="Highest probability per minute by devices" series={this.state.heatmapMinutesSeries} />
 | 
			
		||||
                        </Paper>
 | 
			
		||||
                    </Grid>
 | 
			
		||||
                    <Grid item xs={6}>
 | 
			
		||||
                        <Paper className={classes.paper}>
 | 
			
		||||
                            <BarChart label="# of messages by devices" series={this.state.barSeries} categories={this.state.barCategories}/>
 | 
			
		||||
                            <BarChart label="# of messages by devices" series={this.state.barSeries} categories={this.state.barCategories} />
 | 
			
		||||
                        </Paper>
 | 
			
		||||
                    </Grid>
 | 
			
		||||
                    <Grid item xs={6}>
 | 
			
		||||
                        <Paper className={classes.paper}>
 | 
			
		||||
                            <LineChart label="# of messages per second" series={this.state.lineSeries}/>
 | 
			
		||||
                            <LineChart label="# of messages per second" series={this.state.lineSeries} />
 | 
			
		||||
                        </Paper>
 | 
			
		||||
                    </Grid>
 | 
			
		||||
                </Grid>
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,21 @@ export class HeatmapChart extends Component {
 | 
			
		||||
    
 | 
			
		||||
        this.state = {
 | 
			
		||||
            options: {
 | 
			
		||||
              chart: {
 | 
			
		||||
                animations: {
 | 
			
		||||
                  enabled: true,
 | 
			
		||||
                  easing: 'linear',
 | 
			
		||||
                  speed: 250,
 | 
			
		||||
                  animateGradually: {
 | 
			
		||||
                      enabled: false,
 | 
			
		||||
                      speed: 250,
 | 
			
		||||
                  },
 | 
			
		||||
                  dynamicAnimation: {
 | 
			
		||||
                      enabled: true,
 | 
			
		||||
                      speed: 250
 | 
			
		||||
                  }
 | 
			
		||||
                }
 | 
			
		||||
              },
 | 
			
		||||
              dataLabels: {
 | 
			
		||||
                enabled: false
 | 
			
		||||
              },
 | 
			
		||||
 
 | 
			
		||||
@@ -20,11 +20,18 @@ export default class MapContainer extends Component {
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        this.probabilityHandler = this.probabilityHandler.bind(this);
 | 
			
		||||
        this.handlePoint = this.handlePoint.bind(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static contextType = DevicesContext;
 | 
			
		||||
 | 
			
		||||
    probabilityHandler(point) {
 | 
			
		||||
    probabilityHandler(points) {
 | 
			
		||||
        for (var point of points) {
 | 
			
		||||
            this.handlePoint(point);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    handlePoint(point) {
 | 
			
		||||
        if (point.prob > 0.5) {
 | 
			
		||||
 | 
			
		||||
            this.setState({
 | 
			
		||||
 
 | 
			
		||||
@@ -99,15 +99,19 @@ export default class DevicesContextProvider extends Component {
 | 
			
		||||
            .then(_ => {
 | 
			
		||||
                console.log('Devices hub Connected!');
 | 
			
		||||
 | 
			
		||||
                newConnection.on(C.probability_method_name, (id, date, prob) => {
 | 
			
		||||
                newConnection.on(C.probability_method_name, (messages) => {
 | 
			
		||||
                    //console.log(method_name + " recieved: [id: " + id + ", date: " + date + ", prob: " + prob + "]");
 | 
			
		||||
                    var device = this.state.devices.filter(function (x) { return x.id === id })[0]
 | 
			
		||||
                    var newPoint = { deviceId: device.id, lat: device.coordinates.latitude, lng: device.coordinates.longitude, prob: prob, date: new Date(date) };
 | 
			
		||||
                    const newPoints = [];
 | 
			
		||||
                    for (var message of messages) {
 | 
			
		||||
                        var device = this.state.devices.filter(function (x) { return x.id === message.deviceId })[0]
 | 
			
		||||
                        var newPoint = { deviceId: device.id, lat: device.coordinates.latitude, lng: device.coordinates.longitude, prob: message.probability, date: new Date(message.date) };
 | 
			
		||||
                        newPoints.push(newPoint);
 | 
			
		||||
                    }
 | 
			
		||||
                    this.setState({
 | 
			
		||||
                        heatmapPoints: [...this.state.heatmapPoints, newPoint]
 | 
			
		||||
                        heatmapPoints: this.state.heatmapPoints.concat(newPoints)
 | 
			
		||||
                    });
 | 
			
		||||
 | 
			
		||||
                    this.invokeHandlers(C.probability_method_name, newPoint);
 | 
			
		||||
                    this.invokeHandlers(C.probability_method_name, newPoints);
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
                newConnection.on(C.update_all_method_name, () => {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										27
									
								
								Birdmap.API/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								Birdmap.API/Dockerfile
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
 | 
			
		||||
WORKDIR /app
 | 
			
		||||
RUN apt-get update && apt-get install -y curl
 | 
			
		||||
RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash -
 | 
			
		||||
RUN apt-get update && apt-get install -y nodejs
 | 
			
		||||
 | 
			
		||||
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
 | 
			
		||||
RUN apt-get update && apt-get install -y curl
 | 
			
		||||
RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash -
 | 
			
		||||
RUN apt-get update && apt-get install -y nodejs
 | 
			
		||||
WORKDIR /src
 | 
			
		||||
COPY ["Birdmap.API/Birdmap.API.csproj", "Birdmap.API/"]
 | 
			
		||||
COPY ["Birdmap.BLL/Birdmap.BLL.csproj", "Birdmap.BLL/"]
 | 
			
		||||
COPY ["Birdmap.Common/Birdmap.Common.csproj", "Birdmap.Common/"]
 | 
			
		||||
COPY ["Birdmap.DAL/Birdmap.DAL.csproj", "Birdmap.DAL/"]
 | 
			
		||||
RUN dotnet restore "Birdmap.API/Birdmap.API.csproj"
 | 
			
		||||
COPY . .
 | 
			
		||||
WORKDIR "/src/Birdmap.API"
 | 
			
		||||
RUN dotnet build "Birdmap.API.csproj" -c Release -o /app/build
 | 
			
		||||
 | 
			
		||||
FROM build AS publish
 | 
			
		||||
RUN dotnet publish "Birdmap.API.csproj" -c Release -o /app/publish
 | 
			
		||||
 | 
			
		||||
FROM base AS final
 | 
			
		||||
WORKDIR /app
 | 
			
		||||
COPY --from=publish /app/publish .
 | 
			
		||||
ENTRYPOINT ["dotnet", "Birdmap.API.dll"]
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
using Birdmap.DAL;
 | 
			
		||||
using Microsoft.AspNetCore.Hosting;
 | 
			
		||||
using Microsoft.Extensions.Configuration;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
using Microsoft.Extensions.Hosting;
 | 
			
		||||
using Microsoft.Extensions.Logging;
 | 
			
		||||
@@ -39,6 +40,10 @@ namespace Birdmap.API
 | 
			
		||||
 | 
			
		||||
        public static IHostBuilder CreateHostBuilder(string[] args) =>
 | 
			
		||||
            Host.CreateDefaultBuilder(args)
 | 
			
		||||
                .ConfigureAppConfiguration((hostingContext, config) =>
 | 
			
		||||
                {
 | 
			
		||||
                    config.AddEnvironmentVariables(prefix: "Birdmap_");
 | 
			
		||||
                })
 | 
			
		||||
                .ConfigureWebHostDefaults(webBuilder =>
 | 
			
		||||
                {
 | 
			
		||||
                    webBuilder.UseStartup<Startup>();
 | 
			
		||||
@@ -53,8 +58,8 @@ namespace Birdmap.API
 | 
			
		||||
        private static void SeedDatabase(IHost host)
 | 
			
		||||
        {
 | 
			
		||||
            using var scope = host.Services.CreateScope();
 | 
			
		||||
            var dbInitializer = scope.ServiceProvider.GetRequiredService<DbInitializer>();
 | 
			
		||||
 | 
			
		||||
            var dbInitializer = scope.ServiceProvider.GetRequiredService<DbInitializer>();
 | 
			
		||||
            dbInitializer.Initialize();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,11 @@
 | 
			
		||||
{
 | 
			
		||||
{
 | 
			
		||||
  "iisSettings": {
 | 
			
		||||
    "windowsAuthentication": false,
 | 
			
		||||
    "anonymousAuthentication": true,
 | 
			
		||||
    "iis": {
 | 
			
		||||
      "applicationUrl": "http://localhost/Birdmap.API",
 | 
			
		||||
      "sslPort": 0
 | 
			
		||||
    },
 | 
			
		||||
    "iisExpress": {
 | 
			
		||||
      "applicationUrl": "http://localhost:63288",
 | 
			
		||||
      "sslPort": 44331
 | 
			
		||||
@@ -12,16 +16,24 @@
 | 
			
		||||
      "commandName": "IISExpress",
 | 
			
		||||
      "launchBrowser": true,
 | 
			
		||||
      "environmentVariables": {
 | 
			
		||||
        "Birdmap_LocalDbConnectionString": "Data Source=DESKTOP-3600;Initial Catalog=birdmap2;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
 | 
			
		||||
        "ASPNETCORE_ENVIRONMENT": "Development"
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    "Birdmap": {
 | 
			
		||||
      "commandName": "Project",
 | 
			
		||||
      "launchBrowser": true,
 | 
			
		||||
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
 | 
			
		||||
      "environmentVariables": {
 | 
			
		||||
        "ASPNETCORE_ENVIRONMENT": "Development"
 | 
			
		||||
      }
 | 
			
		||||
      },
 | 
			
		||||
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
 | 
			
		||||
    },
 | 
			
		||||
    "Docker": {
 | 
			
		||||
      "commandName": "Docker",
 | 
			
		||||
      "launchBrowser": true,
 | 
			
		||||
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
 | 
			
		||||
      "publishAllPorts": true,
 | 
			
		||||
      "useSSL": true
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,11 +1,14 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace Birdmap.API.Services
 | 
			
		||||
{
 | 
			
		||||
    public record Message(Guid DeviceId, DateTime Date, double Probability);
 | 
			
		||||
 | 
			
		||||
    public interface IDevicesHubClient
 | 
			
		||||
    {
 | 
			
		||||
        Task NotifyDeviceAsync(Guid deviceId, DateTime date, double probability);
 | 
			
		||||
        Task NotifyMessagesAsync(IEnumerable<Message> messages);
 | 
			
		||||
        Task NotifyDeviceUpdatedAsync(Guid deviceId);
 | 
			
		||||
        Task NotifyAllUpdatedAsync();
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -9,9 +9,11 @@ using MQTTnet.Client.Disconnecting;
 | 
			
		||||
using MQTTnet.Client.Options;
 | 
			
		||||
using Newtonsoft.Json;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Timer = System.Timers.Timer;
 | 
			
		||||
 | 
			
		||||
namespace Birdmap.API.Services.Mqtt
 | 
			
		||||
{
 | 
			
		||||
@@ -22,6 +24,9 @@ namespace Birdmap.API.Services.Mqtt
 | 
			
		||||
        private readonly ILogger<MqttClientService> _logger;
 | 
			
		||||
        private readonly IInputService _inputService;
 | 
			
		||||
        private readonly IHubContext<DevicesHub, IDevicesHubClient> _hubContext;
 | 
			
		||||
        private readonly Timer _hubTimer;
 | 
			
		||||
        private readonly List<Message> _messages = new();
 | 
			
		||||
        private readonly object _messageLock = new();
 | 
			
		||||
 | 
			
		||||
        public bool IsConnected => _mqttClient.IsConnected;
 | 
			
		||||
 | 
			
		||||
@@ -31,10 +36,30 @@ namespace Birdmap.API.Services.Mqtt
 | 
			
		||||
            _logger = logger;
 | 
			
		||||
            _inputService = inputService;
 | 
			
		||||
            _hubContext = hubContext;
 | 
			
		||||
            _hubTimer = new Timer()
 | 
			
		||||
            {
 | 
			
		||||
                AutoReset = true,
 | 
			
		||||
                Interval = 1000,
 | 
			
		||||
            };
 | 
			
		||||
            _hubTimer.Elapsed += SendMqttMessagesWithSignalR;
 | 
			
		||||
 | 
			
		||||
            _mqttClient = new MqttFactory().CreateMqttClient();
 | 
			
		||||
            ConfigureMqttClient();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void SendMqttMessagesWithSignalR(object sender, System.Timers.ElapsedEventArgs e)
 | 
			
		||||
        {
 | 
			
		||||
            lock (_messageLock)
 | 
			
		||||
            {
 | 
			
		||||
                if (_messages.Any())
 | 
			
		||||
                {
 | 
			
		||||
                    _logger.LogInformation($"Sending ({_messages.Count}) messages: {string.Join(" | ", _messages)}");
 | 
			
		||||
                    _hubContext.Clients.All.NotifyMessagesAsync(_messages);
 | 
			
		||||
                    _messages.Clear();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ConfigureMqttClient()
 | 
			
		||||
        {
 | 
			
		||||
            _mqttClient.ConnectedHandler = this;
 | 
			
		||||
@@ -55,7 +80,7 @@ namespace Birdmap.API.Services.Mqtt
 | 
			
		||||
        {
 | 
			
		||||
            var message = eventArgs.ApplicationMessage.ConvertPayloadToString();
 | 
			
		||||
 | 
			
		||||
            _logger.LogInformation($"Recieved [{eventArgs.ClientId}] " +
 | 
			
		||||
            _logger.LogDebug($"Recieved [{eventArgs.ClientId}] " +
 | 
			
		||||
                $"Topic: {eventArgs.ApplicationMessage.Topic} | Payload: {message} | QoS: {eventArgs.ApplicationMessage.QualityOfServiceLevel} | Retain: {eventArgs.ApplicationMessage.Retain}");
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
@@ -63,7 +88,10 @@ namespace Birdmap.API.Services.Mqtt
 | 
			
		||||
                var payload = JsonConvert.DeserializeObject<Payload>(message);
 | 
			
		||||
                var inputResponse = await _inputService.GetInputAsync(payload.TagID);
 | 
			
		||||
 | 
			
		||||
                await _hubContext.Clients.All.NotifyDeviceAsync(inputResponse.Message.Device_id, inputResponse.Message.Date.UtcDateTime, payload.Probability);
 | 
			
		||||
                lock (_messageLock)
 | 
			
		||||
                {
 | 
			
		||||
                    _messages.Add(new Message(inputResponse.Message.Device_id, inputResponse.Message.Date.UtcDateTime, payload.Probability));
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception ex)
 | 
			
		||||
            {
 | 
			
		||||
@@ -79,6 +107,7 @@ namespace Birdmap.API.Services.Mqtt
 | 
			
		||||
                _logger.LogInformation($"Connected. Auth result: {eventArgs.AuthenticateResult}. Subscribing to topic: {topic}");
 | 
			
		||||
 | 
			
		||||
                await _mqttClient.SubscribeAsync(topic);
 | 
			
		||||
                _hubTimer.Start();
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception ex)
 | 
			
		||||
            {
 | 
			
		||||
@@ -88,17 +117,18 @@ namespace Birdmap.API.Services.Mqtt
 | 
			
		||||
 | 
			
		||||
        public async Task HandleDisconnectedAsync(MqttClientDisconnectedEventArgs eventArgs)
 | 
			
		||||
        {
 | 
			
		||||
            _logger.LogWarning(eventArgs.Exception, $"Disconnected. Reason {eventArgs.ReasonCode}. Auth result: {eventArgs.AuthenticateResult}. Reconnecting...");
 | 
			
		||||
            _logger.LogDebug(eventArgs.Exception, $"Disconnected. Reason {eventArgs.ReasonCode}. Auth result: {eventArgs.AuthenticateResult}. Reconnecting...");
 | 
			
		||||
 | 
			
		||||
            await Task.Delay(TimeSpan.FromSeconds(5));
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                _hubTimer.Stop();
 | 
			
		||||
                await _mqttClient.ConnectAsync(_options, CancellationToken.None);
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception ex)
 | 
			
		||||
            {
 | 
			
		||||
                _logger.LogError(ex, $"Reconnect failed...");
 | 
			
		||||
                _logger.LogDebug(ex, $"Reconnect failed...");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,8 @@
 | 
			
		||||
  "AllowedHosts": "*",
 | 
			
		||||
  "Secret": "7vj.3KW.hYE!}4u6",
 | 
			
		||||
  // "LocalDbConnectionString": "Data Source=DESKTOP-3600\\SQLEXPRESS;Initial Catalog=birdmap;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
 | 
			
		||||
  "LocalDbConnectionString": "Data Source=DESKTOP-3600;Initial Catalog=birdmap;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
 | 
			
		||||
  //"LocalDbConnectionString": "Data Source=DESKTOP-3600;Initial Catalog=birdmap;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
 | 
			
		||||
  "LocalDbConnectionString": "Data Source=db;Initial Catalog=birdmap;User=sa;Password=RPSsql12345",
 | 
			
		||||
  "Defaults": {
 | 
			
		||||
    "Services": {
 | 
			
		||||
      "Local Database": "https://localhost:44331/health",
 | 
			
		||||
@@ -29,6 +30,7 @@
 | 
			
		||||
    ]
 | 
			
		||||
  },
 | 
			
		||||
  "UseDummyServices": true,
 | 
			
		||||
  "ServicesBaseUrl": "https://birb.k8s.kmlabz.com/",
 | 
			
		||||
  "Mqtt": {
 | 
			
		||||
    "BrokerHostSettings": {
 | 
			
		||||
      "Host": "localhost",
 | 
			
		||||
 
 | 
			
		||||
@@ -23,8 +23,9 @@ namespace Birdmap.BLL.Services
 | 
			
		||||
        private System.Net.Http.HttpClient _httpClient;
 | 
			
		||||
        private System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings;
 | 
			
		||||
 | 
			
		||||
        public LiveDummyService(System.Net.Http.HttpClient httpClient)
 | 
			
		||||
        public LiveDummyService(string baseUrl, System.Net.Http.HttpClient httpClient)
 | 
			
		||||
        {
 | 
			
		||||
            _baseUrl = baseUrl;
 | 
			
		||||
            _httpClient = httpClient;
 | 
			
		||||
            _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -23,8 +23,9 @@ namespace Birdmap.BLL.Services
 | 
			
		||||
        private System.Net.Http.HttpClient _httpClient;
 | 
			
		||||
        private System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings;
 | 
			
		||||
 | 
			
		||||
        public LiveInputService(System.Net.Http.HttpClient httpClient)
 | 
			
		||||
        public LiveInputService(string baseUrl, System.Net.Http.HttpClient httpClient)
 | 
			
		||||
        {
 | 
			
		||||
            _baseUrl = baseUrl;
 | 
			
		||||
            _httpClient = httpClient;
 | 
			
		||||
            _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@
 | 
			
		||||
using Birdmap.BLL.Services;
 | 
			
		||||
using Microsoft.Extensions.Configuration;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
 | 
			
		||||
namespace Birdmap.BLL
 | 
			
		||||
{
 | 
			
		||||
@@ -20,8 +21,20 @@ namespace Birdmap.BLL
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                services.AddTransient<IInputService, LiveInputService>();
 | 
			
		||||
                services.AddTransient<IDeviceService, LiveDummyService>();
 | 
			
		||||
                var baseUrl = configuration.GetValue<string>("ServicesBaseUrl");
 | 
			
		||||
 | 
			
		||||
                services.AddTransient<IInputService, LiveInputService>(serviceProvider =>
 | 
			
		||||
                {
 | 
			
		||||
                    var httpClient = serviceProvider.GetService<HttpClient>();
 | 
			
		||||
                    var service = new LiveInputService(baseUrl, httpClient);
 | 
			
		||||
                    return service;
 | 
			
		||||
                });
 | 
			
		||||
                services.AddTransient<IDeviceService, LiveDummyService>(serviceProvider =>
 | 
			
		||||
                {
 | 
			
		||||
                    var httpClient = serviceProvider.GetService<HttpClient>();
 | 
			
		||||
                    var service = new LiveDummyService(baseUrl, httpClient);
 | 
			
		||||
                    return service;
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return services;
 | 
			
		||||
 
 | 
			
		||||
@@ -22,10 +22,17 @@ namespace Birdmap.DAL
 | 
			
		||||
 | 
			
		||||
        public void Initialize()
 | 
			
		||||
        {
 | 
			
		||||
            EnsureCreated();
 | 
			
		||||
            AddDefaultUsers();
 | 
			
		||||
            AddDefaultServices();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void EnsureCreated()
 | 
			
		||||
        {
 | 
			
		||||
            _logger.LogInformation("Ensuring database is created...");
 | 
			
		||||
            _context.Database.EnsureCreated();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void AddDefaultServices()
 | 
			
		||||
        {
 | 
			
		||||
            _logger.LogInformation("Removing previously added default services...");
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Birdmap.Common", "Birdmap.C
 | 
			
		||||
EndProject
 | 
			
		||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.TestApp.WinForm", "MQTTnet.TestApp.WinForm\MQTTnet.TestApp.WinForm.csproj", "{E1707FE7-4A65-42AC-B71C-6CC1A55FC42A}"
 | 
			
		||||
EndProject
 | 
			
		||||
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{9443433B-1D13-41F0-B345-B36ACD15EF81}"
 | 
			
		||||
EndProject
 | 
			
		||||
Global
 | 
			
		||||
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 | 
			
		||||
		Debug|Any CPU = Debug|Any CPU
 | 
			
		||||
@@ -39,6 +41,10 @@ Global
 | 
			
		||||
		{E1707FE7-4A65-42AC-B71C-6CC1A55FC42A}.Debug|Any CPU.Build.0 = Debug|Any CPU
 | 
			
		||||
		{E1707FE7-4A65-42AC-B71C-6CC1A55FC42A}.Release|Any CPU.ActiveCfg = Release|Any CPU
 | 
			
		||||
		{E1707FE7-4A65-42AC-B71C-6CC1A55FC42A}.Release|Any CPU.Build.0 = Release|Any CPU
 | 
			
		||||
		{9443433B-1D13-41F0-B345-B36ACD15EF81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 | 
			
		||||
		{9443433B-1D13-41F0-B345-B36ACD15EF81}.Debug|Any CPU.Build.0 = Debug|Any CPU
 | 
			
		||||
		{9443433B-1D13-41F0-B345-B36ACD15EF81}.Release|Any CPU.ActiveCfg = Release|Any CPU
 | 
			
		||||
		{9443433B-1D13-41F0-B345-B36ACD15EF81}.Release|Any CPU.Build.0 = Release|Any CPU
 | 
			
		||||
	EndGlobalSection
 | 
			
		||||
	GlobalSection(SolutionProperties) = preSolution
 | 
			
		||||
		HideSolutionNode = FALSE
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								docker-compose.dcproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								docker-compose.dcproj
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
<?xml version="1.0" encoding="utf-8"?>
 | 
			
		||||
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
 | 
			
		||||
  <PropertyGroup Label="Globals">
 | 
			
		||||
    <ProjectVersion>2.1</ProjectVersion>
 | 
			
		||||
    <DockerTargetOS>Linux</DockerTargetOS>
 | 
			
		||||
    <ProjectGuid>9443433b-1d13-41f0-b345-b36acd15ef81</ProjectGuid>
 | 
			
		||||
    <DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
 | 
			
		||||
    <DockerServiceUrl>{Scheme}://localhost:{ServicePort}</DockerServiceUrl>
 | 
			
		||||
    <DockerServiceName>birdmap.api</DockerServiceName>
 | 
			
		||||
  </PropertyGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <None Include="docker-compose.yml" />
 | 
			
		||||
    <None Include=".dockerignore" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
</Project>
 | 
			
		||||
							
								
								
									
										44
									
								
								docker-compose.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								docker-compose.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
version: '3.4'
 | 
			
		||||
 | 
			
		||||
services:
 | 
			
		||||
    db:
 | 
			
		||||
        image: "mcr.microsoft.com/mssql/server:2019-latest"
 | 
			
		||||
        environment:
 | 
			
		||||
          - ACCEPT_EULA=Y
 | 
			
		||||
          - SA_PASSWORD=RPSsql12345
 | 
			
		||||
 | 
			
		||||
    birdmap.api:
 | 
			
		||||
        image: ${DOCKER_REGISTRY-}birdmapapi
 | 
			
		||||
        ports:
 | 
			
		||||
            - "8000:80"
 | 
			
		||||
            - "8001:443"
 | 
			
		||||
        volumes:
 | 
			
		||||
            - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
 | 
			
		||||
            - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
 | 
			
		||||
        build:
 | 
			
		||||
          context: .
 | 
			
		||||
          dockerfile: Birdmap.API/Dockerfile
 | 
			
		||||
        depends_on:
 | 
			
		||||
            - db
 | 
			
		||||
        environment:
 | 
			
		||||
            - ASPNETCORE_ENVIRONMENT=Development
 | 
			
		||||
            - ASPNETCORE_URLS=https://+;http://+
 | 
			
		||||
            - ASPNETCORE_HTTPS_PORT=8001
 | 
			
		||||
            - ASPNETCORE_Kestrel__Certificates__Default__Password=certpass123
 | 
			
		||||
            - ASPNETCORE_Kestrel__Certificates__Default__Path=/root/.aspnet/https/aspnetapp.pfx
 | 
			
		||||
            - Birdmap_LocalDbConnectionString=Data Source=db;Initial Catalog=birdmap;User=sa;Password=RPSsql12345
 | 
			
		||||
            - Birdmap_Defaults__Users__0__Name=admin
 | 
			
		||||
            - Birdmap_Defaults__Users__0__Password=pass
 | 
			
		||||
            - Birdmap_Defaults__Users__0__Role=Admin
 | 
			
		||||
            - Birdmap_Defaults__Users__1__Name=user
 | 
			
		||||
            - Birdmap_Defaults__Users__1__Password=pass
 | 
			
		||||
            - Birdmap_Defaults__Users__1__Role=User
 | 
			
		||||
            - Birdmap_Defaults__Services__Local-Database=https://localhost/health
 | 
			
		||||
            - Birdmap_UseDummyServices=true
 | 
			
		||||
            - Birdmap_ServicesBaseUrl=https://birb.k8s.kmlabz.com/
 | 
			
		||||
            - Birdmap_Mqtt__BrokerHostSettings__Host=localhost
 | 
			
		||||
            - Birdmap_Mqtt__BrokerHostSettings__Port=1883
 | 
			
		||||
            - Birdmap_Mqtt__ClientSettings__Id=ASP.NET Core client
 | 
			
		||||
            - Birdmap_Mqtt__ClientSettings__Username=username
 | 
			
		||||
            - Birdmap_Mqtt__ClientSettings__Password=password
 | 
			
		||||
            - Birdmap_Mqtt__ClientSettings__Topic=devices/output
 | 
			
		||||
		Reference in New Issue
	
	Block a user