From 46025e0e14f21b3f0b42d59b197137d4cd3e48cc Mon Sep 17 00:00:00 2001 From: Jeronimo Vallelunga Date: Sun, 3 Dec 2017 21:10:49 -0300 Subject: [PATCH] Disconnected Action --- src/webapp/actions/actions.js | 10 ++++++++++ src/webapp/actions/types.js | 3 +++ src/webapp/central/central.js | 16 ++++++++++++++-- src/webapp/index.js | 4 ++-- src/webapp/middleware/index.js | 2 ++ src/webapp/reducers/index.js | 23 +++++++++++++++++------ src/webapp/store/index.js | 15 +++++++++++---- 7 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/webapp/actions/actions.js b/src/webapp/actions/actions.js index 0101e80..8ca85a2 100644 --- a/src/webapp/actions/actions.js +++ b/src/webapp/actions/actions.js @@ -26,8 +26,18 @@ export default function Actions(central, TYPES) { }, }); + const disconnectStore = () => ({ + type: TYPES.BLUETOOTH_DISCONNECT_REQUEST, + request: (dispatch) => { + dispatch({ type: TYPES.BLUETOOTH_DISCONNECTING }); + central.disconnect(); + dispatch({ type: TYPES.BLUETOOTH_DISCONNECTED }); + }, + }); + return { connectStore, + disconnectStore, syncState, sendAction, }; diff --git a/src/webapp/actions/types.js b/src/webapp/actions/types.js index f49941c..19ecd12 100644 --- a/src/webapp/actions/types.js +++ b/src/webapp/actions/types.js @@ -1,10 +1,13 @@ export const BLUETOOTH_CONNECTING = '@@bluetooth/CONNECTING'; export const BLUETOOTH_CONNECTED = '@@bluetooth/CONNECTED'; +export const BLUETOOTH_DISCONNECTING = '@@bluetooth/DISCONNECTING'; +export const BLUETOOTH_DISCONNECTED = '@@bluetooth/DISCONNECTED'; export const BLUETOOTH_ERROR = '@@bluetooth/ERROR'; export const BLUETOOTH_READ = '@@bluetooth/READ'; export const BLUETOOTH_SYNC = '@@bluetooth/SYNC'; export const BLUETOOTH_SEND = '@@bluetooth/SEND'; export const BLUETOOTH_CONNECT_REQUEST = '@@bluetooth/CONNECT_REQUEST'; +export const BLUETOOTH_DISCONNECT_REQUEST = '@@bluetooth/DISCONNECT_REQUEST'; export const BLUETOOTH_SEND_REQUEST = '@@bluetooth/SEND_REQUEST'; export const BLUETOOTH_SYNC_REQUEST = '@@bluetooth/SYNC_REQUEST'; diff --git a/src/webapp/central/central.js b/src/webapp/central/central.js index 742a7e1..0b9900a 100644 --- a/src/webapp/central/central.js +++ b/src/webapp/central/central.js @@ -13,11 +13,16 @@ export default function Central( id, }; + const isConnected = () => state.server && state.server.connected; + const connect = name => bluetooth .requestDevice({ filters: [{ services: [SERVICE_UUID], name }], }) - .then(device => device.gatt.connect()) + .then((device) => { + state.device = device; + return device.gatt.connect(); + }) .then((server) => { state.server = server; return server.getPrimaryService(SERVICE_UUID); @@ -27,6 +32,12 @@ export default function Central( state.characteristic = characteristic; }); + const disconnect = () => { + if (isConnected()) { + state.device.gatt.disconnect(); + } + }; + const listener = callback => (event) => { const chunk = decode(event.target.value); const message = `${state.message}${chunk}`; @@ -78,8 +89,9 @@ export default function Central( }; return { - connected: state.server && state.server.connected, + connected: isConnected(), connect, + disconnect, handler, write, listener, diff --git a/src/webapp/index.js b/src/webapp/index.js index ca0098b..8b628c5 100644 --- a/src/webapp/index.js +++ b/src/webapp/index.js @@ -5,11 +5,11 @@ import MIDDLEWARE from './middleware'; import REDUCERS from './reducers'; import STORE from './store'; -const { connectStore } = ACTIONS; +const { connectStore, disconnectStore } = ACTIONS; export const types = TYPES; export const status = STATUS; -export const actions = { connectStore }; +export const actions = { connectStore, disconnectStore }; export const reducers = REDUCERS; export const middleware = MIDDLEWARE; export const createSyncStore = STORE; diff --git a/src/webapp/middleware/index.js b/src/webapp/middleware/index.js index 1021295..229b1b8 100644 --- a/src/webapp/middleware/index.js +++ b/src/webapp/middleware/index.js @@ -5,11 +5,13 @@ const { sendAction } = ACTIONS; const { BLUETOOTH_CONNECT_REQUEST, + BLUETOOTH_DISCONNECT_REQUEST, BLUETOOTH_SEND_REQUEST, } = TYPES; const REQUESTS = [ BLUETOOTH_CONNECT_REQUEST, + BLUETOOTH_DISCONNECT_REQUEST, BLUETOOTH_SEND_REQUEST, ]; diff --git a/src/webapp/reducers/index.js b/src/webapp/reducers/index.js index 1d2b085..f2ebcb0 100644 --- a/src/webapp/reducers/index.js +++ b/src/webapp/reducers/index.js @@ -3,15 +3,26 @@ import * as STATUS from '../central/status'; import initial from './initial'; -export default (autosync = true) => (state = initial, { type, payload }) => { +export function status(state = initial.status, { type }) { switch (type) { case TYPES.BLUETOOTH_CONNECTING: - return Object.assign({}, state, { status: STATUS.CONNECTING }); + return STATUS.CONNECTING; case TYPES.BLUETOOTH_CONNECTED: - return Object.assign({}, state, { status: STATUS.CONNECTED }); - case TYPES.BLUETOOTH_SYNC: - return autosync ? Object.assign({}, state, { store: payload }) : state; + return STATUS.CONNECTED; + case TYPES.BLUETOOTH_DISCONNECTING: + return STATUS.DISCONNECTING; + case TYPES.BLUETOOTH_DISCONNECTED: + return STATUS.DISCONNECTED; default: return state; } -}; +} + +export function remote(state = null, { type, payload }) { + switch (type) { + case TYPES.BLUETOOTH_SYNC: + return payload; + default: + return state; + } +} diff --git a/src/webapp/store/index.js b/src/webapp/store/index.js index fe07081..a10c1e4 100644 --- a/src/webapp/store/index.js +++ b/src/webapp/store/index.js @@ -1,10 +1,10 @@ /* global window */ -import { createStore, applyMiddleware, compose } from 'redux'; +import { createStore, combineReducers, applyMiddleware, compose } from 'redux'; import middleware from '../middleware'; -import reducers from '../reducers'; +import { status, remote } from '../reducers'; -export default (actions) => { +export default (actions, reducer = () => true) => { const middlewares = [middleware(actions)]; const enhancers = [applyMiddleware(...middlewares)]; @@ -14,5 +14,12 @@ export default (actions) => { } /* eslint-enable */ - return createStore(reducers(), compose(...enhancers)); + return createStore( + combineReducers({ + status, + remote, + local: reducer, + }), + compose(...enhancers), + ); };