Skip to content

Commit

Permalink
first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mlapaglia committed Jun 30, 2021
1 parent b9f174b commit 7a8b525
Show file tree
Hide file tree
Showing 21 changed files with 643 additions and 6 deletions.
12 changes: 11 additions & 1 deletion OpenAlprWebhookProcessor/Alerts/AlertsController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using OpenAlprWebhookProcessor.Alerts.Pushover;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -11,12 +12,15 @@ public class AlertsController : Controller

private readonly UpsertAlertsRequestHandler _upsertAlertsRequestHandler;

private readonly UpsertPushoverRequestHandler _upsertPushoverRequestHandler;
public AlertsController(
GetAlertsRequestHandler getAlertsRequestHandler,
UpsertAlertsRequestHandler upsertAlertsRequestHandler)
UpsertAlertsRequestHandler upsertAlertsRequestHandler,
UpsertPushoverRequestHandler upsertPushoverRequestHandler)
{
_getAlertsRequestHandler = getAlertsRequestHandler;
_upsertAlertsRequestHandler = upsertAlertsRequestHandler;
_upsertPushoverRequestHandler = upsertPushoverRequestHandler;
}

[HttpPost("alerts/add")]
Expand All @@ -36,5 +40,11 @@ public async Task<List<Alert>> GetAlerts(CancellationToken cancellationToken)
{
return await _getAlertsRequestHandler.HandleAsync(cancellationToken);
}

[HttpPost("pushover")]
public async Task UpsertPushover([FromBody] UpsertPushoverRequest request)
{
await _upsertPushoverRequestHandler.HandleAsync(request);
}
}
}
9 changes: 9 additions & 0 deletions OpenAlprWebhookProcessor/Alerts/IAlertClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace OpenAlprWebhookProcessor.Alerts
{
public interface IAlertClient
{
Task SendAlertAsync(Alert alert);
}
}
26 changes: 26 additions & 0 deletions OpenAlprWebhookProcessor/Alerts/Pushover/PushoverClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Net.Http;
using System.Threading.Tasks;

namespace OpenAlprWebhookProcessor.Alerts.Pushover
{
public class PushoverClient : IAlertClient
{
private const string PushOverApiUrl = "https://api.pushover.net/1/messages.json";

private readonly HttpClient _httpClient;

private readonly PushoverConfiguration _pushoverConfiguration;

public PushoverClient(PushoverConfiguration pushoverConfiguration)
{
_pushoverConfiguration = pushoverConfiguration;
_httpClient = new HttpClient();
}
public async Task SendAlertAsync(Alert alert)
{
var pushUrl = PushOverApiUrl + $"?token={_pushoverConfiguration.AppToken}&user={_pushoverConfiguration.UserKey}&message={alert.PlateNumber}";

await _httpClient.PostAsync(pushUrl, null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace OpenAlprWebhookProcessor.Alerts.Pushover
{
public class PushoverConfiguration
{
public string AppToken { get; set; }

public string UserKey { get; set; }
}
}
13 changes: 13 additions & 0 deletions OpenAlprWebhookProcessor/Alerts/Pushover/UpsertPushoverRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OpenAlprWebhookProcessor.Alerts.Pushover
{
public class UpsertPushoverRequest
{
public bool IsEnabled { get; set; }

public string UserKey { get; set; }

public string ApiToken { get; set; }

public bool SendPlatePreview { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.EntityFrameworkCore;
using OpenAlprWebhookProcessor.Data;
using System.Threading.Tasks;

namespace OpenAlprWebhookProcessor.Alerts.Pushover
{
public class UpsertPushoverRequestHandler
{
private readonly ProcessorContext _processorContext;

public UpsertPushoverRequestHandler(ProcessorContext processorContext)
{
_processorContext = processorContext;
}

public async Task HandleAsync(UpsertPushoverRequest request)
{
var pushoverClient = await _processorContext.PushoverAlertClients.FirstOrDefaultAsync();

bool isAdding = false;

if (pushoverClient == null)
{
isAdding = true;

pushoverClient = new Data.Pushover()
{
ApiToken = request.ApiToken,
IsEnabled = request.IsEnabled,
SendPlatePreview = request.SendPlatePreview,
UserKey = request.UserKey,
};
}
else
{
pushoverClient.ApiToken = request.ApiToken;
pushoverClient.IsEnabled = request.IsEnabled;
pushoverClient.SendPlatePreview = request.SendPlatePreview;
pushoverClient.UserKey = request.UserKey;
}

if (isAdding)
{
_processorContext.PushoverAlertClients.Add(pushoverClient);
}

await _processorContext.SaveChangesAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
margin: 0px;
}

.item {
flex-grow: 1;
margin-top: 15px;
}

.alert {
background-color: #FFEB3B;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<app-pushover></app-pushover>

<div style="margin: 15px;">
<table mat-table [dataSource]="alerts" multiTemplateDataRows class="mat-elevation-z8" style="width:50%">
<ng-container matColumnDef="plateNumber">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div style="margin:16px;" fxLayout="row wrap" fxLayout="row wrap" fxLayoutGap=20px>
<mat-card>
<mat-card-header style="justify-content: space-between;">
<mat-card-title>Pushover</mat-card-title>
<mat-card-subtitle>https://pushover.net/</mat-card-subtitle>
<mat-slide-toggle
style="padding-top: 10px;"
[(ngModel)]="isEnabled"></mat-slide-toggle>
</mat-card-header>
<mat-card-content *ngIf="isEnabled" [@inOutAnimation]>
<div style="margin:16px;" fxLayout="row wrap" fxLayout="row wrap" fxLayoutGap=20px>
<mat-form-field fxFlex="80">
<mat-label>User Key</mat-label>
<input matInput [(ngModel)]="userKey">
<mat-icon matTooltip="The pushover user key, found on the profile page" style="cursor:default" matSuffix>help_center</mat-icon>
</mat-form-field>
<mat-form-field fxFlex="80">
<mat-label>API Token</mat-label>
<input matInput [(ngModel)]="apiToken">
<mat-icon matTooltip="The pushover api token, found under 'Your Applications' on the profile page" style="cursor:default" matSuffix>help_center</mat-icon>
</mat-form-field>
<div>
<mat-checkbox [(ngModel)]="sendPlatePreviewEnabled">Send Plate Preview Image</mat-checkbox>
</div>
</div>
</mat-card-content>
<mat-card-actions align="end" *ngIf="isEnabled" [@inOutAnimation]>
<button mat-button>Save</button>
</mat-card-actions>
</mat-card>
</div>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { PushoverComponent } from './pushover.component';

describe('PushoverComponent', () => {
let component: PushoverComponent;
let fixture: ComponentFixture<PushoverComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ PushoverComponent ]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(PushoverComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { animate, style, transition, trigger } from '@angular/animations';
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-pushover',
templateUrl: './pushover.component.html',
styleUrls: ['./pushover.component.less'],
animations: [
trigger(
'inOutAnimation',
[
transition(
':enter', [
style({ height: 0, opacity: 0 }),
animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)',
style({ height: '*', opacity: 1 }))]),
transition(
':leave', [
style({ height: '*', opacity: 1 }),
animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)',
style({ height: 0, opacity: 0 }))])
])]
})
export class PushoverComponent implements OnInit {
public isEnabled: string;
public userKey: string;
public apiToken: string;
public sendPlatePreviewEnabled: boolean;

constructor() { }

ngOnInit(): void {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { PushoverService } from './pushover.service';

describe('PushoverService', () => {
let service: PushoverService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(PushoverService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Forward } from '@app/settings/forwards/forward';
import { Pushover } from './pushover';

@Injectable({
providedIn: 'root'
})
export class PushoverService {

constructor(private http: HttpClient) { }

public upsertPushover(pushover: Pushover) {
return this.http.post('/alerts/pushover', pushover);
}

public getPushover() {
return this.http.get('/alerts/pushover');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class Pushover {
apiToken: string;
userKey: string;
isEnabled: boolean;
sendPlatePreview: boolean;

constructor(init?:Partial<Pushover>) {
Object.assign(this, init);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ForwardsComponent } from './forwards/forwards.component';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { SystemLogsComponent } from './systemLogs/systemLogs.component';
import { HighlightModule } from 'ngx-highlightjs';
import { PushoverComponent } from './alerts/pushover/pushover.component';

@NgModule({
declarations: [
Expand All @@ -46,6 +47,7 @@ import { HighlightModule } from 'ngx-highlightjs';
IgnoreComponent,
ForwardsComponent,
SystemLogsComponent,
PushoverComponent,
],
imports: [
CommonModule,
Expand Down
Loading

0 comments on commit 7a8b525

Please sign in to comment.