/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import open from 'open'; import express from 'express'; import cors from 'cors'; import sdk from '@nsoft/seven-sdk'; import config from '../config'; import { getCurrentUser } from '../auth/keytarHandler'; import { createAccessKey } from '../auth/loginHandler'; import SevenAuthorizedUser from '../auth/SevenAuthorizedUser'; import EmulateDeviceParams from './interfaces/EmulateDeviceParams'; import SevenNotification from '../enums/SevenNotification'; import { showNotification } from '../helpers/notificationHandler'; import { emulateConnectionChecker } from '../connectionChecker'; import { emulateFiscalPrint } from '../fiscalPrint'; import { emulateLPS } from '../localPubSub'; import { emulateNpsPrint, emulateSpsPrint } from '../print'; import { emulateCardReader } from '../cardReader'; import { getCurrentTenant } from '../tenants/tenantsHandler'; import { addPrivateNetworkHeader } from '../helpers'; const devicePort = 8081; const configDevicePort = 8082; const deviceApp = express(); const configDeviceApp = express(); deviceApp.use(cors({ origin: function (domain, callback) { callback(null, true); }, credentials: true, })); deviceApp.use(addPrivateNetworkHeader()); configDeviceApp.use(cors({ origin: function (domain, callback) { callback(null, true); }, credentials: true })); configDeviceApp.use(addPrivateNetworkHeader()); const emulators: { [index: string]: () => void } = { 'sps': (): void => { emulateNpsPrint(); emulateSpsPrint(); }, 'fp': emulateFiscalPrint, '7cc': emulateConnectionChecker, 'lps': () => { emulateLPS(); }, 'crs': () => { emulateCardReader(); }, 'emulateServices': (): void => { showNotification(SevenNotification.INFO, 'Starting services...'); emulateSpsPrint(); emulateNpsPrint(); emulateLPS(); emulateConnectionChecker(); emulateFiscalPrint(); emulateCardReader(); showNotification(SevenNotification.SUCCESS, 'Services started'); }, }; const emulatorChoices = Object.keys(emulators).filter(item => item !== 'emulateServices'); const getDataCenter = () => { const currentTenant = getCurrentTenant(); const dc = currentTenant?.dataCenter?.split('_')[1].toUpperCase(); const typedDc = dc as keyof typeof sdk.Common.DataCenter; switch (dc) { case 'SERBIA': return sdk.Common.DataCenter.RS; case 'SERBIA2': return sdk.Common.DataCenter.RS2; case 'LONDON': return sdk.Common.DataCenter.UK; default: if (typedDc && sdk.Common.DataCenter[typedDc]) { return sdk.Common.DataCenter[typedDc]; } return sdk.Common.DataCenter.UK; } }; const startNarMock = ({ deviceId, tenantUuid, appName }: EmulateDeviceParams) => { let windowTitle: string; if (deviceId && !appName) { showNotification(SevenNotification.ERROR, 'Missing required parameter --appName'); return; } if (appName && !deviceId) { getCurrentUser().then((currentUser: SevenAuthorizedUser | null) => { if (currentUser) { if (typeof appName === 'string' && appName.toUpperCase() === '7SHOP') { windowTitle = '7Shop 0'; open(config.applications.shop[currentUser.environment].replace('&ar=true', '')); } else { windowTitle = '7Terminal 0'; open(config.applications.terminal[currentUser.environment].replace('ar=true', 'ar=ls')); } } }).catch((err) => { showNotification(SevenNotification.ERROR, err); }); } else { if (!tenantUuid) { showNotification(SevenNotification.ERROR, 'Please use tenants:use command to select tenant.'); return; } getCurrentUser().then((currentUser: SevenAuthorizedUser | null) => { if (currentUser) { const environment = currentUser.environment; const dataCenter = getDataCenter(); if (typeof appName === 'string' && appName.toUpperCase() === '7SHOP') { windowTitle = '7Shop 0'; open(config.applications.shop[currentUser.environment]); } else { windowTitle = '7Terminal 0'; open(config.applications.terminal[currentUser.environment]); } deviceApp.get('/', async (req, res) => { const accessKey = createAccessKey(currentUser.keyId, currentUser.keySecret); const deviceData = await sdk.Devices.Debugging.getDeviceInfo({ deviceId, accessKey, tenantUuid, environment, dataCenter }); if (deviceData.httpCode) { res.status(deviceData.httpCode); } res.json(deviceData); }); deviceApp.get('/peripherals_initialization_status', async (req, res) => { res.status(200); res.json({ initialized: true }); }); configDeviceApp.post('/get_app_config', async (req, res) => { const accessKey = createAccessKey(currentUser.keyId, currentUser.keySecret); const appConfig = await sdk.Devices.Debugging.getAppConfig({ deviceId, accessKey, tenantUuid, environment, windowTitle, dataCenter }); if (appConfig.httpCode) { res.status(appConfig.httpCode); } res.json(appConfig); }); configDeviceApp.post('/get_app_cpv_uuids', async (req, res) => { const accessKey = createAccessKey(currentUser.keyId, currentUser.keySecret); const appCpvUuids = await sdk.Devices.Debugging.getAppCpvUuids({ deviceId, accessKey, tenantUuid, environment, windowTitle, dataCenter }); if (appCpvUuids.httpCode) { res.status(appCpvUuids.httpCode); } res.json(appCpvUuids); }); deviceApp.listen(devicePort); configDeviceApp.listen(configDevicePort); } else { showNotification(SevenNotification.ERROR, 'No authorized user found.'); } }).catch((err) => { showNotification(SevenNotification.ERROR, err); }); } }; /** * Emulates services * @param {Object|null} params * @param {Array.} params.include * @param {Array.} params.exclude * @param {number|null} params.deviceId * @param {string|null} params.dc * @param {string|null} params.server */ const emulate = ({ include, exclude, deviceId, appName }: EmulateDeviceParams): void => { // if include and exclude are not defined start every service if (!include && !exclude) { emulators.emulateServices(); } else if (include && Array.isArray(include)) { include.forEach((service: string) => { showNotification(SevenNotification.SUCCESS, `Starting ${service} service...`); emulators[service](); }); } else if (exclude && Array.isArray(exclude)) { const services = emulatorChoices.filter((item: string) => !exclude.includes(item)); services.forEach((item: string) => { showNotification(SevenNotification.SUCCESS, `Starting ${item} service...`); emulators[item](); }); } const currentTenant = getCurrentTenant(); const tenantUuid = currentTenant?.uuid; const params: EmulateDeviceParams = { deviceId, tenantUuid, appName }; if (deviceId || appName) { startNarMock(params); } }; export default { emulate, emulatorChoices };