let DebugMode = false, Wines = [], ParamMap = { ListTimeOut: 60, DetailTimeOut: 120, PayTimeOut: 180, WaitCountDown: 5, WarnLine: 3000, DangerLine: 500, VolumeAdv: 5, VolumeNormal: 15 }, EventBus = {}, SockEventMap = {"pon": c => EventBus["debug"](`time: ${new Date().toLocaleString()}, pin/pon: ${c}`)}, socket = null, socketTimer = null, socketUrl = ""; // wss://wine.ifarmcloud.com/api/seller/socket, ws://192.168.1.6:3080/seller/socket const ServerPrefix = "wss://wine.ifarmcloud.com/api/seller/socket"; const Android = (func, ...args) => { let handler = window.android; if (handler === undefined) return console.warn("no android handler"); if (handler[func] === undefined) return console.warn(`there is no func named: <${func}>`); handler[func](...args); }, Register = (name, func) => window[name] = func, VmAndroid = () => { window._weight = 0; window._timer = null; window.android = { pin: window.pon, onWebMounted: () => window.startApp("EMULATOR32X1X14X0", "v2023.11.01"), openFrontGate: () => window.frontGateResult(true), openBackGate: () => window.backGateResult(true), lightOn: index => console.log(`light on: ${index}`), lightOff: index => console.log(`light off: ${index}`), isCupProperlyPlaced: index => { console.log(`is cup properly placed at ${index}`) window._weight = 34; // 0.2g setTimeout(() => window.cupProperlyPlaced(window._weight), 1500); }, openGas: () => console.log("open gas"), closeGas: () => console.log("close gas"), openValve: (index, pulse) => { console.log(`valve[${index}] opened for: ${pulse}`); let outed = 0; window._timer = setInterval(() => { outed += 30; window._weight += 48; if (outed >= pulse) { clearInterval(window._timer); window._timer = null; window.normalFinish(); } else window.volumePulse(outed); }, 200); }, closeValve: index => { console.log(`valve[${index}] closed`); if (window._timer !== null) clearInterval(window._timer); window._timer = null; }, readSteadyWeight: index => { console.log(`steady weight of ${index}`); window.steadyWeight(window._weight); }, setVolume: volume => console.log(`volume will be set to: ${volume}`), hide: () => console.log("gesture will be hide"), show: () => console.log("gesture will be show") }; }, SendWss = (data, ttl) => { if (socket !== null) return socket.send(JSON.stringify(data)); if (ttl === undefined) ttl = 3; if (ttl > 0) setTimeout(() => SendWss(data, ttl - 1), 1000); EventBus["debug"](`event: ${data.event}, ttl: ${ttl}`); }, ConnectSocket = device => { if (device !== undefined) socketUrl = `${ServerPrefix}/${device}`; socket = new WebSocket(socketUrl); socket.onclose = () => { socket = null; ConnectSocket(); } socket.onopen = () => { let count = 0; clearInterval(socketTimer); socketTimer = setInterval(() => SendWss({event: "pin", data: count++}), 50 * 1000); } socket.onmessage = event => { let body = JSON.parse(event.data); SockEventMap[body.event] && SockEventMap[body.event](body.data); } }, Volume2Weight = (v, d) => Math.floor(v * d / 50), Weight2Volume = (w, d) => (50 * w / d).toFixed(2), Weight2Pulse = (w, d, ppv) => Math.floor(50 * w / d * ppv), Pulse2Volume = (p, ppv) => (p / ppv).toFixed(2), Pulse2Weight = (p, d, ppv) => (p * d / ppv / 50).toFixed(2); export default { Wines, SockEventMap, ParamMap, EventBus, DebugMode, Android, Register, ConnectSocket, SendWss, VmAndroid, Volume2Weight, Weight2Volume, Pulse2Volume, Weight2Pulse, Pulse2Weight }