Commit b02c6cc82579d62995fc894e1587465f2ab9dd7d
1 parent
c4e931730a
Exists in
master
rename vessel to message
Showing 6 changed files with 46 additions and 46 deletions Inline Diff
src/app/component/import-vessels/import-vessels.component.ts
View file @
b02c6cc
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; | 1 | 1 | import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; | |
import {VesselsService} from '../../service/vessels.service'; | 2 | 2 | import {VesselsService} from '../../service/vessels.service'; | |
import {Vessel} from '../../model/vessel'; | 3 | 3 | import {Message} from '../../model/message'; | |
4 | 4 | |||
@Component({ | 5 | 5 | @Component({ | |
selector: 'app-import-vessels', | 6 | 6 | selector: 'app-import-vessels', | |
templateUrl: './import-vessels.component.html', | 7 | 7 | templateUrl: './import-vessels.component.html', | |
styleUrls: ['./import-vessels.component.scss'] | 8 | 8 | styleUrls: ['./import-vessels.component.scss'] | |
}) | 9 | 9 | }) | |
export class ImportVesselsComponent implements OnInit { | 10 | 10 | export class ImportVesselsComponent implements OnInit { | |
vessels: Map<number, Vessel>; | 11 | 11 | vessels: Map<number, Message>; | |
12 | 12 | |||
13 | 13 | |||
constructor(private vesselsService: VesselsService) { | 14 | 14 | constructor(private vesselsService: VesselsService) { | |
} | 15 | 15 | } | |
16 | 16 | |||
@ViewChild('fileDropRef', {static: false}) fileDropEl: ElementRef; | 17 | 17 | @ViewChild('fileDropRef', {static: false}) fileDropEl: ElementRef; | |
files: any[] = []; | 18 | 18 | files: any[] = []; | |
19 | 19 | |||
ngOnInit(): void { | 20 | 20 | ngOnInit(): void { | |
this.vesselsService.currentVessels.subscribe(vessels => this.vessels = vessels); | 21 | 21 | this.vesselsService.currentVessels.subscribe(vessels => this.vessels = vessels); | |
} | 22 | 22 | } | |
23 | 23 | |||
/** | 24 | 24 | /** | |
* on file drop handler | 25 | 25 | * on file drop handler | |
*/ | 26 | 26 | */ | |
onFileDropped($event): void { | 27 | 27 | onFileDropped($event): void { | |
this.prepareFilesList($event); | 28 | 28 | this.prepareFilesList($event); | |
} | 29 | 29 | } | |
30 | 30 | |||
/** | 31 | 31 | /** | |
* handle file from browsing | 32 | 32 | * handle file from browsing | |
*/ | 33 | 33 | */ | |
fileBrowseHandler(files): void { | 34 | 34 | fileBrowseHandler(files): void { | |
this.prepareFilesList(files); | 35 | 35 | this.prepareFilesList(files); | |
} | 36 | 36 | } | |
37 | 37 | |||
/** | 38 | 38 | /** | |
* Delete file from files list | 39 | 39 | * Delete file from files list | |
* @param index (File index) | 40 | 40 | * @param index (File index) | |
*/ | 41 | 41 | */ | |
deleteFile(index: number): void { | 42 | 42 | deleteFile(index: number): void { | |
if (this.files[index].progress < 100) { | 43 | 43 | if (this.files[index].progress < 100) { | |
return; | 44 | 44 | return; | |
} | 45 | 45 | } | |
this.files.splice(index, 1); | 46 | 46 | this.files.splice(index, 1); | |
} | 47 | 47 | } | |
48 | 48 | |||
49 | 49 | |||
uploadFilesSimulator(index: number): void { | 50 | 50 | uploadFilesSimulator(index: number): void { | |
const fileReader = new FileReader(); | 51 | 51 | const fileReader = new FileReader(); | |
let nbLine: number; | 52 | 52 | let nbLine: number; | |
53 | 53 | |||
fileReader.onload = (e) => { | 54 | 54 | fileReader.onload = (e) => { | |
const lines: string[] = (fileReader.result as string).split('\n'); | 55 | 55 | const lines: string[] = (fileReader.result as string).split('\n'); | |
nbLine = lines.length; | 56 | 56 | nbLine = lines.length; | |
for (const line of lines) { | 57 | 57 | for (const line of lines) { | |
const splitLine = line.split(','); | 58 | 58 | const splitLine = line.split(','); | |
const newVessel = new Vessel(splitLine); | 59 | 59 | const newVessel = new Message(splitLine); | |
this.vessels.set(Number(newVessel.mmsi), newVessel); | 60 | 60 | this.vessels.set(Number(newVessel.mmsi), newVessel); | |
} | 61 | 61 | } | |
this.vesselsService.changeVesselsSet(this.vessels); | 62 | 62 | this.vesselsService.changeVesselsSet(this.vessels); | |
}; | 63 | 63 | }; | |
64 | 64 | |||
fileReader.onprogress = (e ) => { | 65 | 65 | fileReader.onprogress = (e ) => { | |
if (e.lengthComputable) { | 66 | 66 | if (e.lengthComputable) { | |
this.files[index].progress = Math.round(((e.loaded / e.total) * 100)); | 67 | 67 | this.files[index].progress = Math.round(((e.loaded / e.total) * 100)); | |
} | 68 | 68 | } | |
}; | 69 | 69 | }; | |
70 | 70 | |||
fileReader.readAsText(this.files[index]); | 71 | 71 | fileReader.readAsText(this.files[index]); | |
} | 72 | 72 | } | |
73 | 73 | |||
prepareFilesList(files: Array<any>): void { | 74 | 74 | prepareFilesList(files: Array<any>): void { | |
for (const item of files) { | 75 | 75 | for (const item of files) { | |
item.progress = 0; | 76 | 76 | item.progress = 0; | |
this.files.push(item); | 77 | 77 | this.files.push(item); | |
} | 78 | 78 | } | |
this.fileDropEl.nativeElement.value = ''; | 79 | 79 | this.fileDropEl.nativeElement.value = ''; | |
this.uploadFilesSimulator(0); | 80 | 80 | this.uploadFilesSimulator(0); | |
} | 81 | 81 | } |
src/app/component/list-vessel/list-vessel.component.ts
View file @
b02c6cc
import {Component, OnInit} from '@angular/core'; | 1 | 1 | import {Component, OnInit} from '@angular/core'; | |
import {Vessel} from '../../model/vessel'; | 2 | 2 | import {Message} from '../../model/message'; | |
import {VesselsService} from '../../service/vessels.service'; | 3 | 3 | import {VesselsService} from '../../service/vessels.service'; | |
4 | 4 | |||
@Component({ | 5 | 5 | @Component({ | |
selector: 'app-list-vessel', | 6 | 6 | selector: 'app-list-vessel', | |
templateUrl: './list-vessel.component.html', | 7 | 7 | templateUrl: './list-vessel.component.html', | |
styleUrls: ['./list-vessel.component.scss'] | 8 | 8 | styleUrls: ['./list-vessel.component.scss'] | |
}) | 9 | 9 | }) | |
export class ListVesselComponent implements OnInit { | 10 | 10 | export class ListVesselComponent implements OnInit { | |
vessels: Map<number, Vessel>; | 11 | 11 | vessels: Map<number, Message>; | |
12 | 12 | |||
13 | 13 | |||
constructor(private vesselsService: VesselsService) { | 14 | 14 | constructor(private vesselsService: VesselsService) { | |
} | 15 | 15 | } | |
16 | 16 | |||
ngOnInit(): void { | 17 | 17 | ngOnInit(): void { | |
this.vesselsService.currentVessels.subscribe(vessels => { | 18 | 18 | this.vesselsService.currentVessels.subscribe(vessels => { | |
this.vessels = vessels; | 19 | 19 | this.vessels = vessels; | |
console.log(this.vessels); | 20 | 20 | console.log(this.vessels); | |
}); | 21 | 21 | }); |
src/app/model/message.ts
View file @
b02c6cc
File was created | 1 | export class Message { | ||
2 | mmsi: string; | |||
3 | time: string; | |||
4 | latitude: string; | |||
5 | longitude: string; | |||
6 | speedOverGround: string; | |||
7 | courseOverGround: string; | |||
8 | heading: string; | |||
9 | vesselName: string; | |||
10 | imo: string; | |||
11 | callSign: string; | |||
12 | vesselType: string; | |||
13 | status: string; | |||
14 | length: string; | |||
15 | width: string; | |||
16 | draft: string; | |||
17 | cargo: string; | |||
18 | ||||
19 | ||||
20 | constructor(splitLine: string[]) { | |||
21 | this.mmsi = splitLine[0]; | |||
22 | this.time = splitLine[1]; | |||
23 | this.latitude = splitLine[2]; | |||
24 | this.longitude = splitLine[3]; | |||
25 | this.speedOverGround = splitLine[4]; | |||
26 | this.courseOverGround = splitLine[5]; | |||
27 | this.heading = splitLine[6]; | |||
28 | this.vesselName = splitLine[7]; | |||
29 | this.imo = splitLine[8]; | |||
30 | this.callSign = splitLine[9]; | |||
31 | this.vesselType = splitLine[10]; | |||
32 | this.status = splitLine[11]; | |||
33 | this.length = splitLine[12]; | |||
34 | this.width = splitLine[13]; | |||
35 | this.draft = splitLine[14]; | |||
36 | this.cargo = splitLine[15]; |
src/app/model/vessel.spec.ts
View file @
b02c6cc
import { Vessel } from './vessel'; | 1 | 1 | import { Message } from './message'; | |
2 | 2 | |||
describe('Vessel', () => { | 3 | 3 | describe('Vessel', () => { | |
// it('should create an instance', () => { | 4 | 4 | // it('should create an instance', () => { | |
// expect(new Vessel()).toBeTruthy(); | 5 | 5 | // expect(new Vessel()).toBeTruthy(); | |
// }); | 6 | 6 | // }); | |
}); | 7 | 7 | }); |
src/app/model/vessel.ts
View file @
b02c6cc
export class Vessel { | 1 | File was deleted | ||
mmsi: string; | 2 | |||
time: string; | 3 | |||
latitude: string; | 4 | |||
longitude: string; | 5 | |||
speedOverGround: string; | 6 | |||
courseOverGround: string; | 7 | |||
heading: string; | 8 | |||
vesselName: string; | 9 | |||
imo: string; | 10 | |||
callSign: string; | 11 | |||
vesselType: string; | 12 | |||
status: string; | 13 | |||
length: string; | 14 | |||
width: string; | 15 | |||
draft: string; | 16 | |||
cargo: string; | 17 | |||
18 | ||||
19 | ||||
constructor(splitLine: string[]) { | 20 | |||
this.mmsi = splitLine[0]; | 21 | |||
this.time = splitLine[1]; | 22 | |||
this.latitude = splitLine[2]; | 23 | |||
this.longitude = splitLine[3]; | 24 | |||
this.speedOverGround = splitLine[4]; | 25 | |||
this.courseOverGround = splitLine[5]; | 26 | |||
this.heading = splitLine[6]; | 27 | |||
this.vesselName = splitLine[7]; | 28 | |||
this.imo = splitLine[8]; | 29 | |||
this.callSign = splitLine[9]; | 30 | |||
this.vesselType = splitLine[10]; | 31 | |||
this.status = splitLine[11]; | 32 | |||
this.length = splitLine[12]; | 33 | |||
this.width = splitLine[13]; | 34 | |||
this.draft = splitLine[14]; | 35 | |||
this.cargo = splitLine[15]; | 36 |
src/app/service/vessels.service.ts
View file @
b02c6cc
import {Injectable} from '@angular/core'; | 1 | 1 | import {Injectable} from '@angular/core'; | |
import {BehaviorSubject, Observable} from 'rxjs'; | 2 | 2 | import {BehaviorSubject, Observable} from 'rxjs'; | |
import {Vessel} from '../model/vessel'; | 3 | 3 | import {Message} from '../model/message'; | |
4 | 4 | |||
@Injectable({ | 5 | 5 | @Injectable({ | |
providedIn: 'root' | 6 | 6 | providedIn: 'root' | |
}) | 7 | 7 | }) | |
export class VesselsService { | 8 | 8 | export class VesselsService { | |
private vessels = new BehaviorSubject(new Map()); | 9 | 9 | private vessels = new BehaviorSubject(new Map()); | |
currentVessels = this.vessels.asObservable(); | 10 | 10 | currentVessels = this.vessels.asObservable(); | |
11 | 11 | |||
constructor() { | 12 | 12 | constructor() { | |
} | 13 | 13 | } | |
14 | 14 | |||
changeVesselsSet(newVessels: Map<number, Vessel>): void { | 15 | 15 | changeVesselsSet(newVessels: Map<number, Message>): void { | |
this.vessels.next(newVessels); | 16 | 16 | this.vessels.next(newVessels); | |
} | 17 | 17 | } |