Skip to content

Commit

Permalink
Add sending test results to server
Browse files Browse the repository at this point in the history
  • Loading branch information
squix78 committed Jun 13, 2024
1 parent 16844f0 commit 03aecf7
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"start": "ng serve --proxy-config proxy.conf.json",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
Expand Down
9 changes: 9 additions & 0 deletions proxy.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"/api/*":
{
"target": "https://iot-flasher.thingpulse.com/",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
<router-outlet></router-outlet>
</div>
<footer class="flex-footer">
<span>{{getCurrentYear()}} By <a href="https://www.thingpulse.com">ThingPulse</a> 1.0.8</span>
<span>{{getCurrentYear()}} By <a href="https://www.thingpulse.com">ThingPulse</a> 1.0.10</span>
</footer>
57 changes: 54 additions & 3 deletions src/app/components/testrunner/testrunner.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { DeviceConfiguration } from 'src/app/model/device-configuration';
import { DeviceConfigurationService } from 'src/app/shared/device-configuration.service';
import { EspPortService } from 'src/app/shared/esp-port.service';
import { Partition, PartitionProgress, sleep, TestState } from 'src/app/shared/utils.service';
import { TestResultService } from 'src/app/shared/test-result.service';
import { TestResult } from 'src/app/model/test-result';

@Component({
selector: 'app-testrunner',
Expand All @@ -17,8 +19,8 @@ export class TestrunnerComponent implements OnInit {

deviceId: string;
title = 'ThingPulse Hardware Test Tool';
firmwareMessages: any = [];
displayedColumns: string[] = ['heapSize', 'freeHeap', 'psramSize', 'freePsram'];
firmwareMessages: FirmwareMessage[] = [];

@ViewChild('stepper') stepper: MatStepper;


Expand All @@ -35,7 +37,8 @@ export class TestrunnerComponent implements OnInit {

constructor(private espPortService: EspPortService,
private route: ActivatedRoute,
private deviceConfigurationService: DeviceConfigurationService) {
private deviceConfigurationService: DeviceConfigurationService,
private testResultService: TestResultService) {

}

Expand Down Expand Up @@ -64,6 +67,7 @@ export class TestrunnerComponent implements OnInit {
if (this.firmwareMessages.length > 0) {
console.log("We have a message");
}
this.sendTestResults(this.firmwareMessages);
} catch(e) {
this.messageArea = this.messageArea + '\n' + message;
this.messageCount++;
Expand Down Expand Up @@ -159,6 +163,53 @@ export class TestrunnerComponent implements OnInit {
return "warn";
}

sendTestResults(firmwareMessages: FirmwareMessage[]) {
/*
[
{"name":"Mac Address","value":"DC:54:75:F0:3F:D0","result":"OK"},
{"name":"Chip Model","value":"ESP32-S3","result":"OK"},
{"name":"Chip Revision","value":"0","result":"OK"},
{"name":"Available Cores","value":"2","result":"OK"},
{"name":"Heap Size","value":"384kb","result":"OK"},
{"name":"Free Heap","value":"333kb","result":"OK"},
{"name":"PSRAM Size","value":"0kb","result":"OK"},
{"name":"Free PSRAM","value":"0kb","result":"OK"},
{"name":"Flash Chip Size","value":"4096kb","result":"OK"},
{"name":"External Flash Card Type","value":"SDSC","result":"OK"},
{"name":"External Flash Card Size","value":"120MB","result":"OK"},
{"name":"Build Date","value":"Jun 10 2024","result":"OK"},
{"name":"Build Time","value":"21:27:56","result":"OK"}
]
*/

let isOverallSuccess = true;
let macAddress : string = "";
firmwareMessages.forEach((message: FirmwareMessage) => {
if (message.result === "NOK") {
isOverallSuccess = false;
}
switch(message.name) {
case "Mac Address":
macAddress = message.value;
break;
}
});

let testResult : TestResult = {
mac_address: macAddress,
device_type: this.deviceConfiguration.name,
overall_result: isOverallSuccess ? 'OK' : 'NOK',
additional_info: firmwareMessages
};

this.testResultService.sendTestResult(testResult).subscribe(response => {
console.log('Test result sent successfully:', response);
});

}

}




2 changes: 2 additions & 0 deletions src/app/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ <h1>Device Class</h1>

<app-add-local-configuration></app-add-local-configuration>

<button (click)="sendResult()">Send Test Result</button>


20 changes: 19 additions & 1 deletion src/app/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { DeviceConfiguration } from '../model/device-configuration';
import { DeviceConfigurationService } from '../shared/device-configuration.service';
import { TestResultService } from '../shared/test-result.service';
import { TestResult } from '../model/test-result';

@Component({
selector: 'app-home',
Expand All @@ -11,7 +13,7 @@ export class HomeComponent implements OnInit {

deviceConfigurations: DeviceConfiguration[];

constructor(public deviceConfigurationService: DeviceConfigurationService) {
constructor(public deviceConfigurationService: DeviceConfigurationService, private testResultService: TestResultService) {

}

Expand All @@ -35,6 +37,22 @@ export class HomeComponent implements OnInit {
isLocalConfiguration(id: string): boolean {
return this.deviceConfigurationService.isLocalConfiguration(id);
}

sendResult() {
const testResult = {
mac_address: '00:1A:2B:3C:4D:5E',
overall_result: <const> 'OK',
device_type: 'DeviceTypeA',
additional_info: {
test1: 'pass',
test2: 'fail'
}
};

this.testResultService.sendTestResult(testResult).subscribe(response => {
console.log('Test result sent successfully:', response);
});
}


}
9 changes: 3 additions & 6 deletions src/app/model/firmware-message.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export interface FirmwareMessage {
heapSize: number;
freeHeap: number;
psramSize: number;
freePsram: number;
buildTime: string;
buildDate: string;
name: string;
value: string;
result: string;
}
6 changes: 6 additions & 0 deletions src/app/model/test-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface TestResult {
mac_address: string;
overall_result: 'OK' | 'NOK';
device_type: string;
[key: string]: any;
}
16 changes: 16 additions & 0 deletions src/app/shared/test-result.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { TestResultService } from './test-result.service';

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

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

it('should be created', () => {
expect(service).toBeTruthy();
});
});
35 changes: 35 additions & 0 deletions src/app/shared/test-result.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { TestResult } from '../model/test-result';

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

private apiUrl = '/api/rest.php'; // Update with your actual API endpoint
private apiKey = 'yourkey'; // Replace with your generated API key

constructor(private http: HttpClient) { }

sendTestResult(result: TestResult): Observable<any> {
const headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
});
return this.http.post<any>(this.apiUrl, result, { headers: headers })
.pipe(
catchError(this.handleError<any>('sendTestResult'))
);
}

private handleError<T>(operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
console.error(error); // Log the error for debugging purposes
// Let the app continue by returning an empty result.
return of(result as T);
};
}
}

0 comments on commit 03aecf7

Please sign in to comment.