Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core';
- import { AppState } from '../../../setup/store.config';
- import { Store } from '@ngrx/store';
- import { InputsModel, Room, SendSettingPayload } from '../../building.models';
- import { Observable } from 'rxjs/Observable';
- import { getRoom, getRoomIdFromParam, getSettingsMessages } from '../../store/building.selectors';
- import { joinToRoomChannel, leaveRoomChannel, sendSetting } from '../../store/building.actions';
- import { isNotNil } from '../../../core/ramda.helpers';
- import { debounceTime, filter, switchMap, takeUntil } from 'rxjs/operators';
- import { interval } from 'rxjs/observable/interval';
- import { SocketsService } from '../../../core/sockets/sockets.service';
- import { Subject } from 'rxjs/Subject';
- import { assoc, values , pipe, head, keys, assocPath, evolve, T, F, always, whereEq } from 'ramda';
- function reduceInputs(acc, value) {
- const inputType = pipe(
- keys,
- head,
- )(value);
- return evolve({
- [inputType]: {
- isSuccess: T,
- isPending: F,
- value: always(value[inputType]),
- },
- })(acc);
- }
- @Component({
- selector: 'building-room',
- templateUrl: './room.component.html',
- styleUrls: ['./room.component.scss'],
- changeDetection: ChangeDetectionStrategy.OnPush,
- })
- export class RoomComponent implements OnInit, OnDestroy {
- public room: Observable<Room>;
- private roomId: Observable<string>;
- private changeSetting: Subject<any> = new Subject();
- public inputs: InputsModel;
- private killer: Subject<any> = new Subject;
- constructor(private store: Store<AppState>, private sockets: SocketsService) {
- this.resetInputs();
- }
- ngOnInit() {
- this.roomId = this.store.select(getRoomIdFromParam);
- this.room = this.roomId.pipe(
- switchMap(id => this.store.select(getRoom(id))),
- );
- this.connectToRoomChannel();
- this.store.select(getSettingsMessages)
- .pipe(takeUntil(this.killer))
- .subscribe((messages) => {
- this.inputs = messages.reduce(reduceInputs, this.inputs);
- });
- this.changeSetting.pipe(
- debounceTime(1000),
- ).subscribe((data: SendSettingPayload) => {
- this.store.dispatch(sendSetting(data));
- this.inputs = evolve({
- [data.name]: {
- isSuccess: F,
- isPending: T,
- },
- })(this.inputs);
- });
- }
- private connectToRoomChannel() {
- this.roomId.pipe(
- takeUntil(this.killer),
- filter(isNotNil),
- )
- .subscribe((roomId: string) => {
- this.resetInputs();
- this.store.dispatch(leaveRoomChannel());
- this.store.dispatch(joinToRoomChannel(roomId));
- // temp solution
- interval(5000).subscribe(() => {
- this.sockets.send(`room:${roomId}`, 'room_update', {});
- });
- });
- }
- private resetInputs() {
- this.inputs = ['nominal_temperature', 'eco_temperature'].reduce(
- (acc, value) => assoc(value, {
- isPending: false,
- isSuccess: false,
- value: 0,
- })(acc),
- {});
- }
- public sendSetting(name, newValue) {
- this.changeSetting.next({ name, newValue });
- }
- ngOnDestroy() {
- this.store.dispatch(leaveRoomChannel());
- this.killer.next();
- }
- public isPending(name): boolean {
- return this.inputs[name].isPending;
- }
- public isSuccess(name, value): boolean {
- return whereEq({
- [name]: {
- value,
- isSuccess: true,
- isPending: false,
- },
- })(this.inputs);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement