added sensor store

This commit is contained in:
snoop 2018-03-13 15:53:57 +09:00
parent 419a8fab9f
commit 4ae0bb9cb4
14 changed files with 278 additions and 2 deletions

View File

@ -2,7 +2,7 @@ import { TestBed, inject } from '@angular/core/testing';
import { NotificationService } from './notification.service'; import { NotificationService } from './notification.service';
describe('MemberService', () => { describe('NotificationService', () => {
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
providers: [NotificationService] providers: [NotificationService]

View File

@ -0,0 +1,24 @@
import { NgModule } from '@angular/core';
import { StoreModule } from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import {
StoreRouterConnectingModule,
RouterStateSerializer,
} from '@ngrx/router-store';
import { EffectsModule } from '@ngrx/effects';
import { combineReducers, ActionReducer, ActionReducerMap, MetaReducer } from '@ngrx/store';
import {
REDUCERS,
EFFECTS,
} from './store';
import { MODULE } from './sensor.constant';
@NgModule({
imports: [
StoreModule.forFeature(MODULE.name, REDUCERS),
EffectsModule.forFeature(EFFECTS),
],
})
export class SensorStoreModule { }

View File

@ -0,0 +1,3 @@
export const MODULE = {
name: 'Sensor'
};

View File

@ -5,12 +5,15 @@ import {FormsModule} from '@angular/forms';
import { MaterialModule } from 'app/commons/ui/material/material.module'; import { MaterialModule } from 'app/commons/ui/material/material.module';
import { COMPONENTS } from './component'; import { COMPONENTS } from './component';
import { SERVICES } from './service';
import { SensorStoreModule } from './sensor-store.module';
@NgModule({ @NgModule({
imports: [ imports: [
CommonModule, CommonModule,
MaterialModule, MaterialModule,
FormsModule FormsModule,
SensorStoreModule
], ],
declarations: [ declarations: [
COMPONENTS, COMPONENTS,
@ -18,5 +21,8 @@ import { COMPONENTS } from './component';
exports: [ exports: [
COMPONENTS, COMPONENTS,
], ],
providers: [
SERVICES,
],
}) })
export class SensorModule { } export class SensorModule { }

View File

@ -0,0 +1,5 @@
import { SensorService } from './sensor.service';
export const SERVICES = [
SensorService,
];

View File

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { SensorService } from './sensor.service';
describe('SensorService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [SensorService]
});
});
it('should be created', inject([SensorService], (service: SensorService) => {
expect(service).toBeTruthy();
}));
});

View File

@ -0,0 +1,30 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { RPCClient } from 'packages/core/rpc/client/RPCClient';
import { Sensor } from '../model';
import { Domain } from '../../domain/model';
@Injectable()
export class SensorService {
public constructor(
private rpcClient: RPCClient,
) {
}
public readAllByDomain(domain: Domain): Observable<Sensor[]> {
const body = {
domain: domain,
};
return this.rpcClient.call('SensorService.readAllByDomain', domain);
}
}

View File

@ -0,0 +1,30 @@
import {
createSelector,
createFeatureSelector,
ActionReducerMap,
} from '@ngrx/store';
import { StateSelector } from 'packages/commons/util/ngrx/store';
import { MODULE } from '../sensor.constant';
import * as ReadAllByDomainStore from './readallbydomain';
export interface State {
readallbydomain: ReadAllByDomainStore.State;
}
export const REDUCERS = {
readallbymember: ReadAllByDomainStore.reducer,
};
export const EFFECTS = [
ReadAllByDomainStore.Effects,
];
export const selectNotificationState = createFeatureSelector<State>(MODULE.name);
export const ReadAllByMemberSelector = new StateSelector<ReadAllByDomainStore.State>(createSelector(
selectNotificationState,
(state: State) => state.readallbydomain
));

View File

@ -0,0 +1,4 @@
export * from './readallbydomain.action';
export * from './readallbydomain.effect';
export * from './readallbydomain.reducer';
export * from './readallbydomain.state';

View File

@ -0,0 +1,36 @@
import { Action } from '@ngrx/store';
import { RPCError } from 'packages/core/rpc/error';
import { Sensor } from '../../model';
import { Domain } from '../../../domain/model';
export enum ActionType {
ReadAllByDomain = '[Sensor.ReadAllByDomain] ReadAllByDomain',
ReadAllByDomainSuccess = '[Sensor.ReadAllByDomainSuccess] ReadAllByDomainSuccess',
ReadAllByDomainFailure = '[Sensor.ReadAllByDomainFailure] ReadAllByDomainFailure',
}
export class ReadAllByDomain implements Action {
readonly type = ActionType.ReadAllByDomain;
constructor(public payload: Domain) {}
}
export class ReadAllByDomainSuccess implements Action {
readonly type = ActionType.ReadAllByDomainSuccess;
constructor(public payload: Sensor[]) {}
}
export class ReadAllByDomainFailure implements Action {
readonly type = ActionType.ReadAllByDomainFailure;
constructor(public payload: RPCError) {}
}
export type Actions =
| ReadAllByDomain
| ReadAllByDomainSuccess
| ReadAllByDomainFailure
;

View File

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { Effects } from './readallbydomain.effect';
describe('ReadAllByDomain.Effects', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [Effects]
});
});
it('should be created', inject([Effects], (effects: Effects) => {
expect(effects).toBeTruthy();
}));
});

View File

@ -0,0 +1,48 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Effect, Actions, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/exhaustMap';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';
import { RPCError } from 'packages/core/rpc/error';
import { Sensor } from '../../model';
import { SensorService } from '../../service/sensor.service';
import {
ReadAllByDomain,
ReadAllByDomainSuccess,
ReadAllByDomainFailure,
ActionType,
} from './readallbydomain.action';
@Injectable()
export class Effects {
constructor(
private actions$: Actions,
private sensorService: SensorService,
private router: Router
) { }
@Effect()
readAllByMember$: Observable<Action> = this.actions$
.ofType(ActionType.ReadAllByDomain)
.map((action: ReadAllByDomain) => action.payload)
.exhaustMap(domain =>
this.sensorService
.readAllByDomain(domain)
.map(sensorList => new ReadAllByDomainSuccess(sensorList))
.catch(error => of(new ReadAllByDomainFailure(error)))
);
}

View File

@ -0,0 +1,45 @@
import {
Actions,
ActionType,
} from './readallbydomain.action';
import {
State,
initialState,
} from './readallbydomain.state';
import { Sensor } from '../../model';
export function reducer(state = initialState, action: Actions): State {
switch (action.type) {
case ActionType.ReadAllByDomain: {
return {
...state,
error: null,
pending: true,
};
}
case ActionType.ReadAllByDomainSuccess: {
return {
...state,
error: null,
pending: false,
sensorList: action.payload
};
}
case ActionType.ReadAllByDomainFailure: {
return {
...state,
error: action.payload,
pending: false,
sensorList: null,
};
}
default: {
return state;
}
}
}

View File

@ -0,0 +1,15 @@
import { RPCError } from 'packages/core/rpc/error';
import { Sensor } from '../../model';
export interface State {
error: RPCError | null;
pending: boolean;
sensorList: Sensor[] | null;
}
export const initialState: State = {
error: null,
pending: false,
sensorList: null,
};