Skip to content

Commit

Permalink
permite reiniciar print manager
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslopezj committed Jul 19, 2023
1 parent de24036 commit 8ac06b5
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/App/offline/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import {
setIsCentral
} from './ws'
import {setCertificates} from './certs'
import {restartPrintManager} from '../print/restartPrintManager'

export interface IntraSyncAPI {
getDeviceIP: () => Promise<string>
restartPrintManager: () => Promise<{success: boolean; message: string}>
respondToRequest: (token: string, status: 'accepted' | 'rejected') => void
onNewAuthorizationRequest: (callback: (request: AuthorizationRequest) => void) => void

Expand All @@ -36,6 +38,7 @@ export interface IntraSyncAPI {

export function registerIntraSync() {
handleEvent('getDeviceIP', getDeviceIP)
handleEvent('restartPrintManager', restartPrintManager)
handleEvent('respondToRequest', respondToRequest)
handleEvent('acceptConnection', acceptConnection)
handleEvent('rejectConnection', rejectConnection)
Expand Down
1 change: 1 addition & 0 deletions app/App/offline/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {contextBridge, ipcRenderer} from 'electron'
export const methods = [
'respondToRequest',
'getDeviceIP',
'restartPrintManager',
'acceptConnection',
'rejectConnection',
'getConnectedClientTokens',
Expand Down
70 changes: 70 additions & 0 deletions app/App/print/restartPrintManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import find from 'find-process'
import {exec} from 'child_process'
import tcpPortUsed from 'tcp-port-used'

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

export async function restartPrintManager(): Promise<{success: boolean; message: string}> {
try {
let processes = [...(await find('name', 'jspm')), ...(await find('port', 25443))]
// uniq by pid
processes = processes.filter(
(proc, index, self) => self.findIndex(p => p.pid === proc.pid) === index
)

console.log('Found processes', processes)
const messages: string[] = []

for (const proc of processes) {
try {
// force quit the process
const signal = 'SIGKILL'
process.kill(proc.pid, signal)
messages.push(`Se ha cerrado el proceso`)
await tcpPortUsed.waitUntilFree(25443, 500, 30000)
await sleep(20000)
} catch (error) {
messages.push(`No se pudo cerrar el proceso ${proc.pid}`)
}
}

// open the same app again
const isMac = process.platform === 'darwin'
if (isMac) {
exec('open -a "/Applications/JSPrintManager5.app"')
messages.push(`Se abrió la aplicación JSPrintManager5.app`)
} else {
exec('start "" "C:\\Program Files (x86)\\Neodynamic\\jspm for Windows\\v5.0\\jspm5.exe"')
messages.push(`Se abrió la aplicación JSPrintManager5.exe`)
}

await sleep(10000)

processes = [...(await find('name', 'jspm')), ...(await find('port', 25443))]

if (!processes) {
messages.push(`No se puso iniciar print manager. Intente reiniciar la computadora`)
return {
success: false,
message: messages.join('\n')
}
}

console.log('Found processes after restart', processes)

if (!(await tcpPortUsed.check(25443))) {
messages.push(`Print manager no se abrió en el puerto correcto. Reintentando...`)
return restartPrintManager()
}

return {
success: true,
message: messages.join('\n')
}
} catch (error) {
return {
success: false,
message: error.message
}
}
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "justo-crisp",
"version": "0.3.3",
"version": "0.4.2",
"license": "MIT",
"description": "Crisp POS",
"author": "nicolaslopezj",
Expand All @@ -23,6 +23,8 @@
"electron-store": "^8.1.0",
"electron-updater": "^5.3.0",
"express": "^4.18.2",
"find-process": "^1.4.7",
"tcp-port-used": "^1.0.2",
"utf-8-validate": "^6.0.3",
"uuid": "^9.0.0",
"ws": "^8.13.0"
Expand All @@ -33,6 +35,7 @@
"@types/cors": "^2.8.13",
"@types/electron": "^1.6.10",
"@types/express": "^4.17.17",
"@types/tcp-port-used": "^1.0.1",
"@types/uuid": "^9.0.1",
"@types/ws": "^8.5.4",
"@typescript-eslint/eslint-plugin": "^5.57.0",
Expand Down
50 changes: 49 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@
"@types/mime" "*"
"@types/node" "*"

"@types/tcp-port-used@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/tcp-port-used/-/tcp-port-used-1.0.1.tgz#4e15eada6d9f63a5e6c5ef73348d6023f1e91157"
integrity sha512-6pwWTx8oUtWvsiZUCrhrK/53MzKVLnuNSSaZILPy3uMes9QnTrLMar9BDlJArbMOjDcjb3QXFk6Rz8qmmuySZw==

"@types/uuid@^9.0.1":
version "9.0.1"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.1.tgz#98586dc36aee8dacc98cc396dbca8d0429647aa6"
Expand Down Expand Up @@ -998,7 +1003,7 @@ [email protected]:
dependencies:
graceful-readlink ">= 1.0.0"

commander@^5.0.0:
commander@^5.0.0, commander@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-5.1.0.tgz#46abbd1652f8e059bddaef99bbdcb2ad9cf179ae"
integrity sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==
Expand Down Expand Up @@ -1101,6 +1106,13 @@ debug@4, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
dependencies:
ms "2.1.2"

[email protected]:
version "4.3.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee"
integrity sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==
dependencies:
ms "2.1.2"

debug@^3.2.7:
version "3.2.7"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a"
Expand Down Expand Up @@ -1764,6 +1776,15 @@ [email protected]:
statuses "2.0.1"
unpipe "~1.0.0"

find-process@^1.4.7:
version "1.4.7"
resolved "https://registry.yarnpkg.com/find-process/-/find-process-1.4.7.tgz#8c76962259216c381ef1099371465b5b439ea121"
integrity sha512-/U4CYp1214Xrp3u3Fqr9yNynUrr5Le4y0SsJh2lMDDSbpwYSz3M2SMWQC+wqcx79cN8PQtHQIL8KnuY9M66fdg==
dependencies:
chalk "^4.0.0"
commander "^5.1.0"
debug "^4.1.1"

find-up@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73"
Expand Down Expand Up @@ -2184,6 +2205,11 @@ internal-slot@^1.0.5:
has "^1.0.3"
side-channel "^1.0.4"

ip-regex@^4.1.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-4.3.0.tgz#687275ab0f57fa76978ff8f4dddc8a23d5990db5"
integrity sha512-B9ZWJxHHOHUhUjCPrMpLD4xEq35bUTClHM1S6CBU5ixQnkZmwipwgc96vAd7AAGM9TGHvJR+Uss+/Ak6UphK+Q==

[email protected]:
version "1.9.1"
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.9.1.tgz#bff38543eeb8984825079ff3a2a8e6cbd46781b3"
Expand Down Expand Up @@ -2328,13 +2354,27 @@ is-typed-array@^1.1.10, is-typed-array@^1.1.9:
gopd "^1.0.1"
has-tostringtag "^1.0.0"

is-url@^1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/is-url/-/is-url-1.2.4.tgz#04a4df46d28c4cff3d73d01ff06abeb318a1aa52"
integrity sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww==

is-weakref@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-weakref/-/is-weakref-1.0.2.tgz#9529f383a9338205e89765e0392efc2f100f06f2"
integrity sha512-qctsuLZmIQ0+vSSMfoVvyFe2+GSEvnmZ2ezTup1SBse9+twCCeial6EEi3Nc2KFcf6+qz2FBPnjXsk8xhKSaPQ==
dependencies:
call-bind "^1.0.2"

is2@^2.0.6:
version "2.0.9"
resolved "https://registry.yarnpkg.com/is2/-/is2-2.0.9.tgz#ff63b441f90de343fa8fac2125ee170da8e8240d"
integrity sha512-rZkHeBn9Zzq52sd9IUIV3a5mfwBY+o2HePMh0wkGBM4z4qjvy2GwVxQ6nNXSfw6MmVP6gf1QIlWjiOavhM3x5g==
dependencies:
deep-is "^0.1.3"
ip-regex "^4.1.0"
is-url "^1.2.4"

isbinaryfile@^3.0.2:
version "3.0.3"
resolved "https://registry.yarnpkg.com/isbinaryfile/-/isbinaryfile-3.0.3.tgz#5d6def3edebf6e8ca8cae9c30183a804b5f8be80"
Expand Down Expand Up @@ -3388,6 +3428,14 @@ tar@^6.1.11:
mkdirp "^1.0.3"
yallist "^4.0.0"

tcp-port-used@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/tcp-port-used/-/tcp-port-used-1.0.2.tgz#9652b7436eb1f4cfae111c79b558a25769f6faea"
integrity sha512-l7ar8lLUD3XS1V2lfoJlCBaeoaWo/2xfYt81hM7VlvR4RrMVFqfmzfhLVk40hAb368uitje5gPtBRL1m/DGvLA==
dependencies:
debug "4.3.1"
is2 "^2.0.6"

temp-file@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/temp-file/-/temp-file-3.4.0.tgz#766ea28911c683996c248ef1a20eea04d51652c7"
Expand Down

0 comments on commit 8ac06b5

Please sign in to comment.