Skip to content

Commit

Permalink
using angular2-logger with fix to print position (code-chunks/angular…
Browse files Browse the repository at this point in the history
  • Loading branch information
camueller committed Jan 3, 2018
1 parent 30cd7ae commit ac61e24
Show file tree
Hide file tree
Showing 24 changed files with 418 additions and 143 deletions.
4 changes: 4 additions & 0 deletions src/main/angular/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import {DialogService} from './shared/dialog.service';
import {CanDeactivateGuard} from './shared/can-deactivate-guard.service';
import {ErrorInterceptor} from './shared/http-error-interceptor';
import {StatusService} from './status/status.service';
import {Logger, Options} from './log/logger';
import {Level} from './log/level';

@NgModule({
declarations: [
Expand Down Expand Up @@ -90,6 +92,8 @@ import {StatusService} from './status/status.service';
ControlResolver,
ControlDefaultsResolver,
DialogService,
Logger,
{provide: Options, useValue: {level: Level.DEBUG}},
MeterService,
MeterResolver,
MeterDefaultsResolver,
Expand Down
21 changes: 12 additions & 9 deletions src/main/angular/src/app/appliance/appliance-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,31 @@ with this program; if not, write to the Free Software Foundation, Inc.,

import {Appliance} from './appliance';
import {ApplianceHeader} from './appliance-header';
import {Status} from '../status/status';
import {Logger} from '../log/logger';

export class ApplianceFactory {

static createEmptyAppliance(): Appliance {
constructor(private logger: Logger) {
}

createEmptyAppliance(): Appliance {
return new Appliance();
}

static toApplianceHeaderFromJSON(rawApplianceHeader: any): ApplianceHeader {
console.log('ApplianceHeader (JSON)' + JSON.stringify(rawApplianceHeader));
toApplianceHeaderFromJSON(rawApplianceHeader: any): ApplianceHeader {
this.logger.debug('ApplianceHeader (JSON)' + JSON.stringify(rawApplianceHeader));
const applianceHeader = new ApplianceHeader();
applianceHeader.id = rawApplianceHeader.id;
applianceHeader.name = rawApplianceHeader.name;
applianceHeader.vendor = rawApplianceHeader.vendor;
applianceHeader.type = rawApplianceHeader.type;
applianceHeader.controllable = rawApplianceHeader.controllable;
console.log('ApplianceHeader (TYPE)' + JSON.stringify(applianceHeader));
this.logger.debug('ApplianceHeader (TYPE)' + JSON.stringify(applianceHeader));
return applianceHeader;
}

static toApplianceFromJSON(applianceInfo: any): Appliance {
console.log('Appliance (JSON)' + JSON.stringify(applianceInfo));
toApplianceFromJSON(applianceInfo: any): Appliance {
this.logger.debug('Appliance (JSON)' + JSON.stringify(applianceInfo));
const appliance = new Appliance();
appliance.id = applianceInfo.id;
appliance.name = applianceInfo.name;
Expand All @@ -51,11 +54,11 @@ export class ApplianceFactory {
appliance.currentPowerMethod = applianceInfo.currentPowerMethod;
appliance.interruptionsAllowed = applianceInfo.interruptionsAllowed;

console.log('Appliance (TYPE)' + JSON.stringify(appliance));
this.logger.debug('Appliance (TYPE)' + JSON.stringify(appliance));
return appliance;
}

static toJSONfromApplianceInfo(appliance: Appliance): String {
toJSONfromApplianceInfo(appliance: Appliance): String {
return JSON.stringify(appliance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {ApplianceTestdata} from './appliance-testdata';
import {RouterTestingModule} from '@angular/router/testing';
import {Appliance} from './appliance';
import {By} from '@angular/platform-browser';
import {Level} from '../log/level';
import {Logger, Options} from '../log/logger';

const translations: any = {
'dialog.candeactivate': 'Änderungen verwerfen?',
Expand Down Expand Up @@ -63,7 +65,9 @@ describe('ApplianceComponent', () => {
{provide: Location, useValue: location},
{provide: ApplianceService, useValue: applianceService},
{provide: DialogService, useValue: dialogService},
AppliancesReloadService
AppliancesReloadService,
Logger,
{provide: Options, useValue: {level: Level.DEBUG}},
]
});

Expand Down
14 changes: 10 additions & 4 deletions src/main/angular/src/app/appliance/appliance.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {ErrorMessages} from '../shared/error-messages';
import {Appliance} from './appliance';
import {Observable} from 'rxjs/Observable';
import {DialogService} from '../shared/dialog.service';
import {Logger} from '../log/logger';

@Component({
selector: 'app-appliance-details',
Expand All @@ -40,25 +41,30 @@ import {DialogService} from '../shared/dialog.service';
})
export class ApplianceComponent implements OnInit, CanDeactivate<ApplianceComponent> {
@ViewChild('detailsForm') detailsForm: NgForm;
appliance = ApplianceFactory.createEmptyAppliance();
appliance: Appliance;
errors: { [key: string]: string } = {};
errorMessages: ErrorMessages;
errorMessageHandler: ErrorMessageHandler;
VALIDATOR_PATTERN_INTEGER = InputValidatorPatterns.INTEGER;
VALIDATOR_PATTERN_ID = InputValidatorPatterns.APPLIANCE_ID;
isNew = false;
discardChangesMessage: string;
confirmDeletionMessage: string;

constructor(private applianceService: ApplianceService,
constructor(private logger: Logger,
private applianceService: ApplianceService,
private appliancesReloadService: AppliancesReloadService,
private route: ActivatedRoute,
private translate: TranslateService,
private dialogService: DialogService,
private location: Location) {
const applianceFactory = new ApplianceFactory(logger);
this.appliance = applianceFactory.createEmptyAppliance();
this.errorMessageHandler = new ErrorMessageHandler(logger);
}

ngOnInit() {
console.log('ApplianceComponent.ngOnInit()');
this.logger.debug('ApplianceComponent.ngOnInit()');
this.errorMessages = new ApplianceErrorMessages(this.translate);
this.translate.get('dialog.candeactivate').subscribe(translated => this.discardChangesMessage = translated);
this.translate.get('ApplianceComponent.confirmDeletion').subscribe(translated => this.confirmDeletionMessage = translated);
Expand All @@ -70,7 +76,7 @@ export class ApplianceComponent implements OnInit, CanDeactivate<ApplianceCompon
}
});
this.detailsForm.statusChanges.subscribe(() =>
this.errors = ErrorMessageHandler.applyErrorMessages4TemplateDrivenForm(this.detailsForm, this.errorMessages));
this.errors = this.errorMessageHandler.applyErrorMessages4TemplateDrivenForm(this.detailsForm, this.errorMessages));
}

canDeactivate(): Observable<boolean> | boolean {
Expand Down
17 changes: 11 additions & 6 deletions src/main/angular/src/app/appliance/appliance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,43 @@ import {ApplianceFactory} from './appliance-factory';
import {ApplianceHeader} from './appliance-header';
import {SaeService} from '../shared/sae-service';
import {HttpClient} from '@angular/common/http';
import {Logger} from '../log/logger';

@Injectable()
export class ApplianceService extends SaeService {

constructor(protected http: HttpClient) {
applianceFactory: ApplianceFactory;

constructor(private logger: Logger,
protected http: HttpClient) {
super(http);
this.applianceFactory = new ApplianceFactory(logger);
}

getApplianceHeaders(): Observable<Array<ApplianceHeader>> {
return this.http.get(`${SaeService.API}/appliances`)
.map((applianceHeaders: Array<ApplianceHeader>) => {
return applianceHeaders.map(
applianceHeader => ApplianceFactory.toApplianceHeaderFromJSON(applianceHeader));
applianceHeader => this.applianceFactory.toApplianceHeaderFromJSON(applianceHeader));
});
}

getAppliance(id: string): Observable<Appliance> {
return this.http.get(`${SaeService.API}/appliance?id=${id}`)
.map(applianceInfo => ApplianceFactory.toApplianceFromJSON(applianceInfo));
.map(applianceInfo => this.applianceFactory.toApplianceFromJSON(applianceInfo));
}

updateAppliance(appliance: Appliance, create: boolean): Observable<any> {
const url = `${SaeService.API}/appliance?id=${appliance.id}&create=${create}`;
const content = ApplianceFactory.toJSONfromApplianceInfo(appliance);
console.log('Updating appliance using ' + url);
const content = this.applianceFactory.toJSONfromApplianceInfo(appliance);
this.logger.debug('Updating appliance using ' + url);
return this.http.put(url, content,
{headers: this.headersContentTypeJson, responseType: 'text'});
}

deleteAppliance(id: string): Observable<any> {
const url = `${SaeService.API}/appliance?id=${id}`;
console.log('Delete appliance using ' + url);
this.logger.debug('Delete appliance using ' + url);
return this.http.delete(url, {responseType: 'text'});
}
}
56 changes: 30 additions & 26 deletions src/main/angular/src/app/control/control-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ import {HttpSwitch} from './http-switch';
import {AlwaysOnSwitch} from './always-on-switch';
import {ControlDefaults} from './control-defaults';
import {MockSwitch} from './mock-switch';
import {Logger} from '../log/logger';

export class ControlFactory {

static defaultsFromJSON(rawControlDefaults: any): ControlDefaults {
console.log('ControlDefaults (JSON): ' + JSON.stringify(rawControlDefaults));
constructor(private logger: Logger) {
}

defaultsFromJSON(rawControlDefaults: any): ControlDefaults {
this.logger.debug('ControlDefaults (JSON): ' + JSON.stringify(rawControlDefaults));
const controlDefaults = new ControlDefaults();
controlDefaults.startingCurrentSwitchDefaults_powerThreshold
= rawControlDefaults.startingCurrentSwitchDefaults.powerThreshold;
Expand All @@ -38,30 +42,30 @@ export class ControlFactory {
= rawControlDefaults.startingCurrentSwitchDefaults.finishedCurrentDetectionDuration;
controlDefaults.startingCurrentSwitchDefaults_minRunningTime
= rawControlDefaults.startingCurrentSwitchDefaults.minRunningTime;
console.log('ControlDefaults (TYPE): ' + JSON.stringify(controlDefaults));
this.logger.debug('ControlDefaults (TYPE): ' + JSON.stringify(controlDefaults));
return controlDefaults;
}

static createEmptyControl(): Control {
createEmptyControl(): Control {
return new Control();
}

static fromJSON(rawControl: any): Control {
console.log('Control (JSON): ' + JSON.stringify(rawControl));
fromJSON(rawControl: any): Control {
this.logger.debug('Control (JSON): ' + JSON.stringify(rawControl));
const control = new Control();
if (rawControl['@class'] === StartingCurrentSwitch.TYPE) {
control.startingCurrentDetection = true;
control.startingCurrentSwitch = ControlFactory.createStartingCurrentSwitch(rawControl);
ControlFactory.fromJSONbyType(control, rawControl.control);
control.startingCurrentSwitch = this.createStartingCurrentSwitch(rawControl);
this.fromJSONbyType(control, rawControl.control);
} else {
ControlFactory.fromJSONbyType(control, rawControl);
this.fromJSONbyType(control, rawControl);
}
console.log('Control (TYPE): ' + JSON.stringify(control));
this.logger.debug('Control (TYPE): ' + JSON.stringify(control));
return control;
}

static toJSON(control: Control): string {
console.log('Control (TYPE): ' + JSON.stringify(control));
toJSON(control: Control): string {
this.logger.debug('Control (TYPE): ' + JSON.stringify(control));
let controlUsed: any;
if (control.startingCurrentSwitch != null) {
control.startingCurrentSwitch['control'] = this.getControlByType(control);
Expand All @@ -85,28 +89,28 @@ export class ControlFactory {
if (controlUsed != null) {
rawControl = JSON.stringify(controlUsed);
}
console.log('Control (JSON): ' + rawControl);
this.logger.debug('Control (JSON): ' + rawControl);
return rawControl;
}

static fromJSONbyType(control: Control, rawControl: any) {
fromJSONbyType(control: Control, rawControl: any) {
if (rawControl != null) {
control.type = rawControl['@class'];
if (control.type === AlwaysOnSwitch.TYPE) {
control.alwaysOnSwitch = ControlFactory.createAlwaysOnSwitch(rawControl);
control.alwaysOnSwitch = this.createAlwaysOnSwitch(rawControl);
} else if (control.type === MockSwitch.TYPE) {
control.mockSwitch = ControlFactory.createMockSwitch(rawControl);
control.mockSwitch = this.createMockSwitch(rawControl);
} else if (control.type === Switch.TYPE) {
control.switch_ = ControlFactory.createSwitch(rawControl);
control.switch_ = this.createSwitch(rawControl);
} else if (control.type === ModbusSwitch.TYPE) {
control.modbusSwitch = ControlFactory.createModbusSwitch(rawControl);
control.modbusSwitch = this.createModbusSwitch(rawControl);
} else if (control.type === HttpSwitch.TYPE) {
control.httpSwitch = ControlFactory.createHttpSwitch(rawControl);
control.httpSwitch = this.createHttpSwitch(rawControl);
}
}
}

static getControlByType(control: Control): any {
getControlByType(control: Control): any {
if (control.type === AlwaysOnSwitch.TYPE) {
return control.alwaysOnSwitch;
} else if (control.type === MockSwitch.TYPE) {
Expand All @@ -121,15 +125,15 @@ export class ControlFactory {
return null;
}

static createAlwaysOnSwitch(rawAlwaysOnSwitch: any): AlwaysOnSwitch {
createAlwaysOnSwitch(rawAlwaysOnSwitch: any): AlwaysOnSwitch {
return new AlwaysOnSwitch();
}

static createMockSwitch(rawMockSwitch: any): MockSwitch {
createMockSwitch(rawMockSwitch: any): MockSwitch {
return new MockSwitch();
}

static createStartingCurrentSwitch(rawStartingCurrentSwitch: any): StartingCurrentSwitch {
createStartingCurrentSwitch(rawStartingCurrentSwitch: any): StartingCurrentSwitch {
const startingCurrentSwitch = new StartingCurrentSwitch();
startingCurrentSwitch.powerThreshold = rawStartingCurrentSwitch.powerThreshold;
startingCurrentSwitch.startingCurrentDetectionDuration = rawStartingCurrentSwitch.startingCurrentDetectionDuration;
Expand All @@ -138,21 +142,21 @@ export class ControlFactory {
return startingCurrentSwitch;
}

static createSwitch(rawSwitch: any): Switch {
createSwitch(rawSwitch: any): Switch {
const switch_ = new Switch();
switch_.gpio = rawSwitch.gpio;
switch_.reverseStates = rawSwitch.reverseStates;
return switch_;
}

static createModbusSwitch(rawModbusSwitch: any): ModbusSwitch {
createModbusSwitch(rawModbusSwitch: any): ModbusSwitch {
const modbusSwitch = new ModbusSwitch();
modbusSwitch.slaveAddress = rawModbusSwitch.slaveAddress;
modbusSwitch.registerAddress = rawModbusSwitch.registerAddress;
return modbusSwitch;
}

static createHttpSwitch(rawHttpSwitch: any): HttpSwitch {
createHttpSwitch(rawHttpSwitch: any): HttpSwitch {
const httpSwitch = new HttpSwitch();
httpSwitch.onUrl = rawHttpSwitch.onUrl;
httpSwitch.offUrl = rawHttpSwitch.offUrl;
Expand Down
17 changes: 11 additions & 6 deletions src/main/angular/src/app/control/control-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,38 @@ import {Control} from './control';
import {ControlDefaults} from './control-defaults';
import {SaeService} from '../shared/sae-service';
import {HttpClient} from '@angular/common/http';
import {Logger} from '../log/logger';

@Injectable()
export class ControlService extends SaeService {

constructor(protected http: HttpClient) {
controlFactory: ControlFactory;

constructor(private logger: Logger,
protected http: HttpClient) {
super(http);
this.controlFactory = new ControlFactory(logger);
}

getControlDefaults(): Observable<ControlDefaults> {
return this.http.get(`${SaeService.API}/controldefaults`)
.map(response => ControlFactory.defaultsFromJSON(response));
.map(response => this.controlFactory.defaultsFromJSON(response));
}

getControl(id: string): Observable<Control> {
return this.http.get(`${SaeService.API}/control?id=${id}`)
.map(response => {
if (response == null) {
return ControlFactory.createEmptyControl();
return this.controlFactory.createEmptyControl();
}
return ControlFactory.fromJSON(response);
return this.controlFactory.fromJSON(response);
});
}

updateControl(control: Control, id: string): Observable<any> {
const url = `${SaeService.API}/control?id=${id}`;
const content = ControlFactory.toJSON(control);
console.log('Update control using ' + url);
const content = this.controlFactory.toJSON(control);
this.logger.debug('Update control using ' + url);
if (content != null) {
return this.http.put(url, content, {headers: this.headersContentTypeJson, responseType: 'text'});
} else {
Expand Down
Loading

0 comments on commit ac61e24

Please sign in to comment.