131 lines
2.5 KiB
TypeScript
131 lines
2.5 KiB
TypeScript
import { createApp } from 'petite-vue'
|
|
|
|
import { api, polling } from './settings'
|
|
import { pageid } from '@params'
|
|
|
|
type confdata = {
|
|
apiurl: string
|
|
pollurl: string
|
|
itemid: string
|
|
sid: string
|
|
}
|
|
|
|
export function config (api, polling): confdata {
|
|
const pu = polling ? `${api.path}/${polling.msgbase.join('/')}` : ''
|
|
const urlparams = new URL(location.href).searchParams
|
|
return {
|
|
apiurl: api.path,
|
|
pollurl: pu,
|
|
itemid: urlparams.get('id'),
|
|
sid: getSid()
|
|
}
|
|
}
|
|
|
|
export const pvapp = {
|
|
run(conf: confdata) {
|
|
appdata.conf = conf
|
|
createApp(appdata).mount()
|
|
if (appdata.conf.pollurl) {
|
|
appdata.poll()
|
|
}
|
|
}
|
|
}
|
|
|
|
const appdata = {
|
|
$delimiters: ['{|', '|}'],
|
|
conf: {} as confdata,
|
|
components: {},
|
|
output: '',
|
|
handle,
|
|
poll,
|
|
mounted(name: string) {
|
|
console.log('app mounted: ', name)
|
|
},
|
|
Data
|
|
}
|
|
|
|
|
|
// components
|
|
|
|
function Data(name: string): object {
|
|
const comp = {
|
|
name: name,
|
|
data: {},
|
|
save
|
|
}
|
|
appdata.components[name] = comp
|
|
return comp
|
|
}
|
|
|
|
// appdata and component method definitions
|
|
|
|
function save() {
|
|
let value = ''
|
|
for (const k of Object.keys(this.data)) {
|
|
value += `${k}: ${this.data[k]}, `
|
|
}
|
|
this.output += '\n' + value
|
|
console.log('save:', value)
|
|
}
|
|
|
|
function poll() {
|
|
dopoll(this)
|
|
}
|
|
|
|
function handle(msg) {
|
|
const data = JSON.parse(msg.payload)
|
|
const [ domain, action, class_, item ] = msg.path.split('/')
|
|
//console.log('msgbase: ', domain, action, class_, item)
|
|
// TODO: check message parts (using default values), select handler fct
|
|
Object.assign(this.components[class_].data, data)
|
|
}
|
|
|
|
// basic functions - move to api.ts
|
|
|
|
function createSid(): string {
|
|
const arr = new Uint32Array(2)
|
|
crypto.getRandomValues(arr)
|
|
const result: string[] = []
|
|
arr.forEach((x) => result.push(x.toString(36)))
|
|
return result.join('')
|
|
}
|
|
|
|
function getSid(): string {
|
|
const sid_key = 'api.sessionid'
|
|
let sid = localStorage.getItem(sid_key)
|
|
if (!sid) {
|
|
sid = createSid()
|
|
localStorage.setItem(sid_key, sid)
|
|
}
|
|
return sid
|
|
}
|
|
|
|
//console.log("sid: ", getSid())
|
|
// TODO: clear sid - when?
|
|
//localStorage.setItem('api.sessionid', '')
|
|
|
|
async function dopoll(app: typeof appdata) {
|
|
const wait_time = 10000
|
|
while (true) {
|
|
try {
|
|
let res = await fetch(app.conf.pollurl)
|
|
let msg = await res.json()
|
|
console.log(msg)
|
|
switch (msg.status) {
|
|
case 'idle':
|
|
break
|
|
case 'data':
|
|
app.handle(msg)
|
|
break
|
|
default:
|
|
console.log('poll response: ', msg)
|
|
await sleep(wait_time)
|
|
}
|
|
} catch (error) {
|
|
console.log(error)
|
|
await sleep(wait_time)
|
|
}
|
|
}
|
|
}
|
|
|
|
const sleep = (delay: number) => new Promise(r => setTimeout(r, delay))
|