Skip to content

Commit

Permalink
refactor: add datawedge profile creation management in Scanner (#882)
Browse files Browse the repository at this point in the history
* RM#87363
  • Loading branch information
lme-axelor committed Feb 12, 2025
1 parent 692bece commit a24bb7a
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 88 deletions.
6 changes: 6 additions & 0 deletions changelogs/unreleased/87363.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"title": "Scanner: add Datawedge profile creation management",
"type": "refactor",
"packages": "core",
"description": "Based on the application name and bundle id, the Scanner component is now dynamically creating the Datawegde profile with the correct configuration. Caution, if you modified the bundle id compared to the standard application, then your action intent was also modified to match the bundle id. To correctly get this functionnality, please delete your current Datawedge profile and let the application recreate a new one with the right configutation."
}
88 changes: 0 additions & 88 deletions packages/core/src/components/external/Scanner/Scanner.js

This file was deleted.

156 changes: 156 additions & 0 deletions packages/core/src/components/external/Scanner/Scanner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2025 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import {useCallback, useEffect, useMemo} from 'react';
import {DeviceEventEmitter, Platform} from 'react-native';
import DataWedgeIntents from 'react-native-datawedge-intents';
import DeviceInfo from 'react-native-device-info';
import {useDispatch} from '../../../redux/hooks';
import {scanValue, useScannerSelector} from '../../../features/scannerSlice';
import {DATAWEDGE_API} from './path.helpers';
import {
castScanIntent,
castVersionIntent,
formatBarcode,
isScanIntent,
isVersionIntent,
} from './intent.helpers';

const Scanner = () => {
const {isEnabled} = useScannerSelector();
const dispatch = useDispatch();

const appConfig = useMemo(
() => ({
profileName: `${DeviceInfo.getApplicationName().replaceAll(' ', '_')}${
__DEV__ ? '_demo' : ''
}`,
intentAction: `${DeviceInfo.getBundleId()}.zebra.ACTION`,
packageName: `${DeviceInfo.getBundleId()}`,
}),
[],
);

const registerBroadcastReceiver = useCallback(() => {
DataWedgeIntents.registerBroadcastReceiver({
filterActions: [appConfig.intentAction, DATAWEDGE_API.RESULT_ACTION],
filterCategories: ['android.intent.category.DEFAULT'],
});
}, [appConfig.intentAction]);

const sendCommand = useCallback((extraName: string, extraValue: any = '') => {
DataWedgeIntents.sendBroadcastWithExtras({
action: DATAWEDGE_API.ACTION,
extras: {[extraName]: extraValue, SEND_RESULT: 'true'},
});
}, []);

const determineVersion = useCallback(() => {
sendCommand(DATAWEDGE_API.GET_VERSION_INFO);
}, [sendCommand]);

const createProfile = useCallback(() => {
sendCommand(DATAWEDGE_API.CREATE_PROFILE, appConfig.profileName);
sendCommand(DATAWEDGE_API.SET_CONFIG, {
PROFILE_NAME: appConfig.profileName,
PROFILE_ENABLED: 'true',
CONFIG_MODE: 'UPDATE',
PLUGIN_CONFIG: {
PLUGIN_NAME: 'BARCODE',
RESET_CONFIG: 'true',
PARAM_LIST: {},
},
APP_LIST: [
{
PACKAGE_NAME: appConfig.packageName,
ACTIVITY_LIST: ['*'],
},
],
});

sendCommand(DATAWEDGE_API.SET_CONFIG, {
PROFILE_NAME: appConfig.profileName,
PROFILE_ENABLED: 'true',
CONFIG_MODE: 'UPDATE',
PLUGIN_CONFIG: {
PLUGIN_NAME: 'INTENT',
RESET_CONFIG: 'true',
PARAM_LIST: {
intent_output_enabled: 'true',
intent_action: appConfig.intentAction,
intent_delivery: '2',
},
},
});
}, [
appConfig.intentAction,
appConfig.packageName,
appConfig.profileName,
sendCommand,
]);

const broadcastReceiver = useCallback(
(intent: any) => {
if (isScanIntent(intent)) {
const {labelType, value} = castScanIntent(intent);
const formattedValue = formatBarcode(value, labelType);
dispatch(scanValue(formattedValue));
}

if (isVersionIntent(intent)) {
const {version} = castVersionIntent(intent);

if (version >= '06.4') {
createProfile();
}
}
},
[createProfile, dispatch],
);

useEffect(() => {
if (Platform.OS !== 'ios') {
registerBroadcastReceiver();
determineVersion();
}
}, [determineVersion, registerBroadcastReceiver]);

useEffect(() => {
if (Platform.OS !== 'ios') {
if (isEnabled) {
const listener = DeviceEventEmitter.addListener(
'datawedge_broadcast_intent',
broadcastReceiver,
);

return () => {
listener.remove();
};
}
}
}, [
broadcastReceiver,
determineVersion,
isEnabled,
registerBroadcastReceiver,
]);

return null;
};

export default Scanner;
51 changes: 51 additions & 0 deletions packages/core/src/components/external/Scanner/intent.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2025 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import {DATAWEDGE_API, DATAWEDGE_RESULT} from './path.helpers';

export function isScanIntent(intent: any): boolean {
return (
intent.hasOwnProperty(DATAWEDGE_RESULT.LABEL_TYPE) &&
intent.hasOwnProperty(DATAWEDGE_RESULT.DATA_STRING)
);
}

export function castScanIntent(intent: any) {
return {
labelType: intent[DATAWEDGE_RESULT.LABEL_TYPE],
time: new Date(intent[DATAWEDGE_RESULT.DATA_DISPATCH_TIME]),
value: intent[DATAWEDGE_RESULT.DATA_STRING],
};
}

const EAN13_LABEL_TYPE = 'LABEL-TYPE-EAN13';

export function formatBarcode(value: string, type: string) {
return type === EAN13_LABEL_TYPE ? value.slice(0, -1) : value;
}

export function isVersionIntent(intent: any): boolean {
return intent.hasOwnProperty(DATAWEDGE_API.RESULT_GET_VERSION_INFO);
}

export function castVersionIntent(intent: any) {
const versionInfo = intent[DATAWEDGE_API.RESULT_GET_VERSION_INFO];
return {
version: versionInfo.DATAWEDGE,
};
}
37 changes: 37 additions & 0 deletions packages/core/src/components/external/Scanner/path.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2025 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

const DATAWEDGE_PATH = 'com.symbol.datawedge';
const API_PATH = `${DATAWEDGE_PATH}.api`;

export const DATAWEDGE_API = {
GET_VERSION_INFO: `${API_PATH}.GET_VERSION_INFO`,
RESULT_ACTION: `${API_PATH}.RESULT_ACTION`,
RESULT_GET_VERSION_INFO: `${API_PATH}.RESULT_GET_VERSION_INFO`,
ACTION: `${API_PATH}.ACTION`,
CREATE_PROFILE: `${API_PATH}.CREATE_PROFILE`,
GET_ACTIVE_PROFILE: `${API_PATH}.GET_ACTIVE_PROFILE`,
SET_CONFIG: `${API_PATH}.SET_CONFIG`,
ENUMERATE_SCANNERS: `${API_PATH}.ENUMERATE_SCANNERS`,
};

export const DATAWEDGE_RESULT = {
LABEL_TYPE: `${DATAWEDGE_PATH}.label_type`,
DATA_DISPATCH_TIME: `${DATAWEDGE_PATH}.data_dispatch_time`,
DATA_STRING: `${DATAWEDGE_PATH}.data_string`,
};

0 comments on commit a24bb7a

Please sign in to comment.