在遠端監視解決方案加速器 Web UI 中新增自訂服務
本文說明如何在遠端監視解決方案加速器 Web UI 中新增服務。 本文章說明:
- 如何準備本機開發環境。
- 如何將服務新增至 Web UI。
本文中的服務範例會提供格線資料,在遠端監視解決方案加速器 Web UI 中新增自訂格線操作方式文章會說明如何新增。
在 React 應用程式中,服務通常會與後端服務互動。 遠端監視解決方案加速器中的範例包括會與 IoT 中樞管理員和組態微服務互動的服務。
必要條件
若要完成本操作指南中的部署,您必須在本機開發機器上安裝下列軟體:
在您開始使用 Intune 之前
請先完成在遠端監視解決方案加速器 Web UI 中新增自訂頁面操作方式文章中的步驟再繼續。
新增服務
若要在 Web UI 中新增服務,您必須新增可定義此服務的原始程式檔,然後修改一些現有檔案,讓 Web UI 知道新的服務。
新增可定義服務的檔案
為了讓您能夠開始,src/walkthrough/services 資料夾包含可定義簡單服務的檔案:
exampleService.js
import { Observable } from 'rxjs';
import { toExampleItemModel, toExampleItemsModel } from './models';
/** Normally, you'll need to define the endpoint URL.
* See app.config.js to add a new service URL.
*
* For this example, we'll just hardcode sample data to be returned instead
* of making an actual service call. See the other service files for examples.
*/
//const ENDPOINT = Config.serviceUrls.example;
/** Contains methods for calling the example service */
export class ExampleService {
/** Returns an example item */
static getExampleItem(id) {
return Observable.of(
{ ID: id, Description: "This is an example item." },
)
.map(toExampleItemModel);
}
/** Returns a list of example items */
static getExampleItems() {
return Observable.of(
{
items: [
{ ID: "123", Description: "This is item 123." },
{ ID: "188", Description: "This is item ONE-DOUBLE-EIGHT." },
{ ID: "210", Description: "This is item TWO-TEN." },
{ ID: "277", Description: "This is item 277." },
{ ID: "413", Description: "This is item FOUR-THIRTEEN." },
{ ID: "789", Description: "This is item 789." },
]
}
).map(toExampleItemsModel);
}
/** Mimics a server call by adding a delay */
static updateExampleItems() {
return this.getExampleItems().delay(2000);
}
}
若要深入了解服務的實作方式,請參閱您所遺失的回應式程式設計簡介。
model/exampleModels.js
import { camelCaseReshape, getItems } from 'utilities';
/**
* Reshape the server side model to match what the UI wants.
*
* Left side is the name on the client side.
* Right side is the name as it comes from the server (dot notation is supported).
*/
export const toExampleItemModel = (data = {}) => camelCaseReshape(data, {
'id': 'id',
'description': 'descr'
});
export const toExampleItemsModel = (response = {}) => getItems(response)
.map(toExampleItemModel);
將 exampleService.js 複製到 src/services 資料夾,並將 exampleModels.js 複製到 src/services/models 資料夾。
更新 src/services 資料夾中的 index.js 檔案,以匯出新的服務:
export * from './exampleService';
更新 src/services/models 資料夾中的 index.js 檔案,以匯出新的模型:
export * from './exampleModels';
設定從存放區對服務的呼叫
為了讓您能夠開始,src/walkthrough/store/reducers 資料夾包含歸納器範例:
exampleReducer.js
import 'rxjs';
import { Observable } from 'rxjs';
import moment from 'moment';
import { schema, normalize } from 'normalizr';
import update from 'immutability-helper';
import { createSelector } from 'reselect';
import { ExampleService } from 'walkthrough/services';
import {
createReducerScenario,
createEpicScenario,
errorPendingInitialState,
pendingReducer,
errorReducer,
setPending,
toActionCreator,
getPending,
getError
} from 'store/utilities';
// ========================= Epics - START
const handleError = fromAction => error =>
Observable.of(redux.actions.registerError(fromAction.type, { error, fromAction }));
export const epics = createEpicScenario({
/** Loads the example items */
fetchExamples: {
type: 'EXAMPLES_FETCH',
epic: fromAction =>
ExampleService.getExampleItems()
.map(toActionCreator(redux.actions.updateExamples, fromAction))
.catch(handleError(fromAction))
}
});
// ========================= Epics - END
// ========================= Schemas - START
const itemSchema = new schema.Entity('examples');
const itemListSchema = new schema.Array(itemSchema);
// ========================= Schemas - END
// ========================= Reducers - START
const initialState = { ...errorPendingInitialState, entities: {}, items: [], lastUpdated: '' };
const updateExamplesReducer = (state, { payload, fromAction }) => {
const { entities: { examples }, result } = normalize(payload, itemListSchema);
return update(state, {
entities: { $set: examples },
items: { $set: result },
lastUpdated: { $set: moment() },
...setPending(fromAction.type, false)
});
};
/* Action types that cause a pending flag */
const fetchableTypes = [
epics.actionTypes.fetchExamples
];
export const redux = createReducerScenario({
updateExamples: { type: 'EXAMPLES_UPDATE', reducer: updateExamplesReducer },
registerError: { type: 'EXAMPLE_REDUCER_ERROR', reducer: errorReducer },
isFetching: { multiType: fetchableTypes, reducer: pendingReducer }
});
export const reducer = { examples: redux.getReducer(initialState) };
// ========================= Reducers - END
// ========================= Selectors - START
export const getExamplesReducer = state => state.examples;
export const getEntities = state => getExamplesReducer(state).entities || {};
export const getItems = state => getExamplesReducer(state).items || [];
export const getExamplesLastUpdated = state => getExamplesReducer(state).lastUpdated;
export const getExamplesError = state =>
getError(getExamplesReducer(state), epics.actionTypes.fetchExamples);
export const getExamplesPendingStatus = state =>
getPending(getExamplesReducer(state), epics.actionTypes.fetchExamples);
export const getExamples = createSelector(
getEntities, getItems,
(entities, items) => items.map(id => entities[id])
);
// ========================= Selectors - END
將 exampleReducer.js 複製到 src/store/reducers 資料夾。
若要深入了解歸納器和 Epics,請參閱 redux-observable。
設定中介軟體
若要設定中介軟體,請將歸納器新增至 src/store 資料夾中的 rootReducer.js 檔案:
import { reducer as exampleReducer } from './reducers/exampleReducer';
const rootReducer = combineReducers({
...appReducer,
...devicesReducer,
...rulesReducer,
...simulationReducer,
...exampleReducer
});
將 Epics 新增至 src/store 資料夾中的 rootEpics.js 檔案:
import { epics as exampleEpics } from './reducers/exampleReducer';
// Extract the epic function from each property object
const epics = [
...appEpics.getEpics(),
...devicesEpics.getEpics(),
...rulesEpics.getEpics(),
...simulationEpics.getEpics(),
...exampleEpics.getEpics()
];
後續步驟
在本文中,您已了解可用來協助您在遠端監視解決方案加速器 Web UI 中新增或自訂服務的資源。
您現在已定義了一個服務,下一個步驟是在遠端監視解決方案加速器 Web UI 中新增自訂格線,以顯示服務所傳回的資料。
如需遠端監視解決方案加速器的詳細資訊,請參閱 遠端監視架構。