Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core';
  2. import { AppState } from '../../../setup/store.config';
  3. import { Store } from '@ngrx/store';
  4. import { InputsModel, Room, SendSettingPayload } from '../../building.models';
  5. import { Observable } from 'rxjs/Observable';
  6. import { getRoom, getRoomIdFromParam, getSettingsMessages } from '../../store/building.selectors';
  7. import { joinToRoomChannel, leaveRoomChannel, sendSetting } from '../../store/building.actions';
  8. import { isNotNil } from '../../../core/ramda.helpers';
  9. import { debounceTime, filter, switchMap, takeUntil } from 'rxjs/operators';
  10. import { interval } from 'rxjs/observable/interval';
  11. import { SocketsService } from '../../../core/sockets/sockets.service';
  12. import { Subject } from 'rxjs/Subject';
  13. import { assoc, values , pipe, head, keys, assocPath, evolve, T, F, always, whereEq } from 'ramda';
  14.  
  15.  
  16. function reduceInputs(acc, value) {
  17. const inputType = pipe(
  18. keys,
  19. head,
  20. )(value);
  21. return evolve({
  22. [inputType]: {
  23. isSuccess: T,
  24. isPending: F,
  25. value: always(value[inputType]),
  26. },
  27. })(acc);
  28. }
  29.  
  30. @Component({
  31. selector: 'building-room',
  32. templateUrl: './room.component.html',
  33. styleUrls: ['./room.component.scss'],
  34. changeDetection: ChangeDetectionStrategy.OnPush,
  35. })
  36. export class RoomComponent implements OnInit, OnDestroy {
  37. public room: Observable<Room>;
  38. private roomId: Observable<string>;
  39. private changeSetting: Subject<any> = new Subject();
  40. public inputs: InputsModel;
  41. private killer: Subject<any> = new Subject;
  42.  
  43. constructor(private store: Store<AppState>, private sockets: SocketsService) {
  44. this.resetInputs();
  45. }
  46.  
  47. ngOnInit() {
  48. this.roomId = this.store.select(getRoomIdFromParam);
  49.  
  50. this.room = this.roomId.pipe(
  51. switchMap(id => this.store.select(getRoom(id))),
  52. );
  53.  
  54. this.connectToRoomChannel();
  55.  
  56. this.store.select(getSettingsMessages)
  57. .pipe(takeUntil(this.killer))
  58. .subscribe((messages) => {
  59. this.inputs = messages.reduce(reduceInputs, this.inputs);
  60. });
  61.  
  62. this.changeSetting.pipe(
  63. debounceTime(1000),
  64. ).subscribe((data: SendSettingPayload) => {
  65. this.store.dispatch(sendSetting(data));
  66. this.inputs = evolve({
  67. [data.name]: {
  68. isSuccess: F,
  69. isPending: T,
  70. },
  71. })(this.inputs);
  72. });
  73. }
  74.  
  75. private connectToRoomChannel() {
  76. this.roomId.pipe(
  77. takeUntil(this.killer),
  78. filter(isNotNil),
  79. )
  80. .subscribe((roomId: string) => {
  81. this.resetInputs();
  82. this.store.dispatch(leaveRoomChannel());
  83. this.store.dispatch(joinToRoomChannel(roomId));
  84. // temp solution
  85. interval(5000).subscribe(() => {
  86. this.sockets.send(`room:${roomId}`, 'room_update', {});
  87. });
  88. });
  89. }
  90.  
  91. private resetInputs() {
  92. this.inputs = ['nominal_temperature', 'eco_temperature'].reduce(
  93. (acc, value) => assoc(value, {
  94. isPending: false,
  95. isSuccess: false,
  96. value: 0,
  97. })(acc),
  98. {});
  99. }
  100.  
  101. public sendSetting(name, newValue) {
  102. this.changeSetting.next({ name, newValue });
  103. }
  104.  
  105. ngOnDestroy() {
  106. this.store.dispatch(leaveRoomChannel());
  107. this.killer.next();
  108. }
  109.  
  110. public isPending(name): boolean {
  111. return this.inputs[name].isPending;
  112. }
  113.  
  114. public isSuccess(name, value): boolean {
  115. return whereEq({
  116. [name]: {
  117. value,
  118. isSuccess: true,
  119. isPending: false,
  120. },
  121. })(this.inputs);
  122. }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement