<commit_before>import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Diagnostic } from 'ionic-native';
import { ActionsObservable } from 'redux-observable';

import { IDeviceInfo, IService } from './../plugin';
import { DeviceActions } from './../actions/device';
import { Action } from './../actions';
import { IAppState, ICharacteristicState } from './../state';
import { BLECentralService } from './../services/ble-central';

@Injectable()
export class DeviceEpics {

  requestPermission$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.PERMISSION_REQUEST)
      .mergeMap(() =>
        this.bleService.requestRuntimePermission()
          .map(res => {
            if (res === Diagnostic.permissionStatus.GRANTED) {
              return this.deviceActions.permissionGranted();
            } else {
              return this.deviceActions.permissionDenied();
            }
          })
      );

  startScan$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_SCAN)
      .mergeMap(() =>
        this.bleService.startScan()
          .map(res => this.deviceActions.addDeviceInfo(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  stopScan$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.STOP_SCAN)
      .mergeMap(() =>
        this.bleService.stopScan()
          .map(res => this.deviceActions.stoppedScan())
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  connectToDevice$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.CONNECT_TO_DEVICE)
      .map(action => action.payload)
      .mergeMap((deviceInfo) =>
        this.bleService.connectToDevice(deviceInfo)
          .map((res) => this.deviceActions.connectedToDevice(res))
          .catch(err => Observable.of(this.deviceActions.failedToConnectDevice(deviceInfo, err)))
      );

  disconnectDevice$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.DISCONNECT_DEVICE)
      .map(action => action.payload)
      .mergeMap((deviceInfo) =>
        this.bleService.disconnectDevice(deviceInfo)
          .map((res) => this.deviceActions.deviceDisconnected(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  monitorDisconnect$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.MONITOR_DEVICE_DISCONNECT)
      .mergeMap((deviceInfo) =>
        this.bleService.monitorDeviceDisconnect()
          .map((res) => this.deviceActions.deviceDisconnected(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  deviceConnected$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.CONNECTED_TO_DEVICE)
      .map(action => action.payload)
      .map(deviceInfo => this.deviceActions.discoverServices(deviceInfo));

  discoverServices$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_SERVICE_DISCOVERY)
      .map(action => action.payload)
      .mergeMap((deviceInfo) =>
        this.bleService.discoverServices(deviceInfo)
          .map(res => this.deviceActions.discoveredServices(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  discoverCharacterisitics$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_CHARACTERISTICS_DISCOVERY)
      .map(action => action.payload)
      .mergeMap((service) =>
        this.bleService.discoverCharacteristics(service)
          .map(res => this.deviceActions.discoveredCharacteristics(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  monitorCharacteristic$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_CHARACTERISTIC_MONITORING)
      .map(action => action.payload)
      .mergeMap((charState) =>
        this.bleService.monitorCharacteristic(charState.characteristic, charState.transactionId)
          .map(res => this.deviceActions.readCharacteristic(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );


  stopCharacteristicMonitoring$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.STOP_CHARACTERISTIC_MONITORING)
      .map(action => action.payload)
      .mergeMap((charState) =>
        this.bleService.stopCharacteristicMonitoring(charState.characteristic, charState.transactionId)
          .map(() => this.deviceActions.stoppedCharacteristicMonitoring(charState))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  readCharacteristic$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_READING_CHARACTERISITIC)
      .map(action => action.payload)
      .mergeMap((charState) =>
        this.bleService.readCharacteristic(charState.characteristic, charState.transactionId)
          .map((res) => this.deviceActions.readCharacteristic(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  writeCharacteristic$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_WRITING_CHARACTERISITIC)
      .map(action => action.payload)
      .mergeMap((payload) =>
        this.bleService.writeCharacteristic(payload.charState.characteristic, payload.value, payload.withResponse, payload.charState.transactionId)
          .map((res) => this.deviceActions.wroteCharacteristic(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  getState$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.GET_CURRENT_STATE)
      .mergeMap(() =>
        this.bleService.getState()<commit_msg>refactor - create toPayload() function and use generic type to control the type of payload extracted from action<commit_after>import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Diagnostic } from 'ionic-native';
import { ActionsObservable } from 'redux-observable';

import { IDeviceInfo, IService } from './../plugin';
import { DeviceActions } from './../actions/device';
import { Action } from './../actions';
import { ICharacteristicState } from './../state';
import { BLECentralService } from './../services/ble-central';

function toPayload(action: Action): any {
  return action.payload;
}

@Injectable()
export class DeviceEpics {

  requestPermission$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.PERMISSION_REQUEST)
      .mergeMap(() =>
        this.bleService.requestRuntimePermission()
          .map(res => {
            if (res === Diagnostic.permissionStatus.GRANTED) {
              return this.deviceActions.permissionGranted();
            } else {
              return this.deviceActions.permissionDenied();
            }
          })
      );

  startScan$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_SCAN)
      .mergeMap(() =>
        this.bleService.startScan()
          .map(res => this.deviceActions.addDeviceInfo(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  stopScan$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.STOP_SCAN)
      .mergeMap(() =>
        this.bleService.stopScan()
          .map(res => this.deviceActions.stoppedScan())
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  connectToDevice$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.CONNECT_TO_DEVICE)
      .map<IDeviceInfo>(toPayload)
      .mergeMap((deviceInfo) =>
        this.bleService.connectToDevice(deviceInfo)
          .map((res) => this.deviceActions.connectedToDevice(res))
          .catch(err => Observable.of(this.deviceActions.failedToConnectDevice(deviceInfo, err)))
      );

  disconnectDevice$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.DISCONNECT_DEVICE)
      .map<IDeviceInfo>(toPayload)
      .mergeMap((deviceInfo) =>
        this.bleService.disconnectDevice(deviceInfo)
          .map((res) => this.deviceActions.deviceDisconnected(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  monitorDisconnect$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.MONITOR_DEVICE_DISCONNECT)
      .mergeMap(() =>
        this.bleService.monitorDeviceDisconnect()
          .map((res) => this.deviceActions.deviceDisconnected(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  deviceConnected$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.CONNECTED_TO_DEVICE)
      .map<IDeviceInfo>(toPayload)
      .map((deviceInfo) => this.deviceActions.discoverServices(deviceInfo));

  discoverServices$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_SERVICE_DISCOVERY)
      .map<IDeviceInfo>(toPayload)
      .mergeMap((deviceInfo) =>
        this.bleService.discoverServices(deviceInfo)
          .map(res => this.deviceActions.discoveredServices(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  discoverCharacterisitics$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_CHARACTERISTICS_DISCOVERY)
      .map<IService>(toPayload)
      .mergeMap((service) =>
        this.bleService.discoverCharacteristics(service)
          .map(res => this.deviceActions.discoveredCharacteristics(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  monitorCharacteristic$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_CHARACTERISTIC_MONITORING)
      .map<ICharacteristicState>(toPayload)
      .mergeMap((charState) =>
        this.bleService.monitorCharacteristic(charState.characteristic, charState.transactionId)
          .map(res => this.deviceActions.readCharacteristic(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );


  stopCharacteristicMonitoring$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.STOP_CHARACTERISTIC_MONITORING)
      .map<ICharacteristicState>(toPayload)
      .mergeMap((charState) =>
        this.bleService.stopCharacteristicMonitoring(charState.characteristic, charState.transactionId)
          .map(() => this.deviceActions.stoppedCharacteristicMonitoring(charState))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  readCharacteristic$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_READING_CHARACTERISITIC)
      .map<ICharacteristicState>(toPayload)
      .mergeMap((charState) =>
        this.bleService.readCharacteristic(charState.characteristic, charState.transactionId)
          .map((res) => this.deviceActions.readCharacteristic(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  writeCharacteristic$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.START_WRITING_CHARACTERISITIC)
      .map<{ charState: ICharacteristicState; value: string; withResponse: boolean; }>(toPayload)
      .mergeMap((payload) =>
        this.bleService.writeCharacteristic(payload.charState.characteristic, payload.value, payload.withResponse, payload.charState.transactionId)
          .map((res) => this.deviceActions.wroteCharacteristic(res))
          .catch(err => Observable.of(this.deviceActions.bleError(err)))
      );

  getState$ = (action$: ActionsObservable<Action>) =>
    action$.ofType(DeviceActions.GET_CURRENT_STATE)
      .mergeMap(() =>
        this.bleService.getState()
{'fraction_non_alphanumeric': 0.13740781554632533, 'fraction_numerical': 0.0, 'mean_word_length': 3.700398406374502, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 151, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6675268', 'n_tokens_mistral': 3401, 'n_tokens_neox': 3342, 'n_words': 426}