-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathImportWebsocket.mjs
154 lines (142 loc) · 3.6 KB
/
ImportWebsocket.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* This script creates an ossia QML device tree from JSON.
* It tries to do some guessing on the data types when they are in strings and depending on the key names:
* - "on", "true" will convert to bool
* - "123" will convert to int
* - todo: more
*/
function importWsDevice()
{
const res = Score.prompt({
title: "Input JSON",
widgets: [
{ name:"Name", type: "lineedit", init: "Device" },
{ name:"Host", type: "lineedit", init: "ws://127.0.0.1:8080" },
{ name:"JSON", type: "textfield" },
]
});
if (res === undefined) {
return;
}
if (res[0].length === 0) {
return;
}
const name = res[0];
const host = res[1];
const obj = JSON.parse(res[2]);
let str = "";
let ind = 0;
let indent = () => " ".repeat(ind * 2);
let setup_value = (k, v) => {
const t = typeof(v);
if(t === "string")
{
const num = parseFloat(v);
// Try some special cases
if(Number.isInteger(num))
{
str += indent() + `type: Ossia.Type.Int,\n`;
str += indent() + `value: ${num},\n`;
return;
}
else if(!isNaN(num))
{
str += indent() + `type: Ossia.Type.Float,\n`;
str += indent() + `value: ${num},\n`;
return;
}
let tolow = t.toLowerCase();
if(tolow == "on" || tolow == "off" || tolow == "true" || tolow == "false" || tolow == "yes" || tolow == "no")
{
str += indent() + `type: Ossia.Type.Boolean,\n`;
str += indent() + `value: ${num ? 'true' : 'false'},\n`;
return;
}
else if(tolow == "red" || tolow == "green" || tolow == "blue" || tolow == "black" || tolow == "white")
{
str += indent() + `type: Ossia.Type.Vec4,\n`;
str += indent() + `value: "${tolow}",\n`; // FIXME
return;
}
else
{
str += indent() + `type: Ossia.Type.String,\n`;
str += indent() + `value: "${v}",\n`;
return;
}
}
else if(t === "number")
{
const num = v;
if(Number.isInteger(num))
{
str += indent() + `type: Ossia.Type.Int,\n`;
str += indent() + `value: ${num},\n`;
return;
}
else if(!isNaN(num))
{
str += indent() + `type: Ossia.Type.Float,\n`;
str += indent() + `value: ${num},\n`;
return;
}
}
else if(t === "array")
{
str += indent() + `type: Ossia.Type.List,\n`;
return;
}
};
let rec = (o) =>
{
for(let key in o)
{
let val = o[key];
str += indent() + "{\n"
ind++;
{
str += indent() + `name: "${key}",\n`
setup_value(key, val);
if(typeof(val) === "object")
{
str += indent() + 'children: [\n';
ind++;
rec(val);
ind--;
str += indent() + '],\n';
}
}
ind--;
str += indent() + "},\n";
}
}
rec(obj);
console.log(str);
// Wrap the code in something that can be turned into a device
str = `import Ossia 1.0 as Ossia
Ossia.WebSockets
{
property string host: "${host}"
processFromJson: true
function createTree() {
return [ ${str} ];
}
}`;
Score.createQMLWebSocketDevice("MyDevice", str);
// const r2 = Score.prompt({
// title: "Output code",
// widgets: [
// { name:"Result", type: "textfield", init: str }
// ]
// });
}
export function initialize() {
// This will be called when the module is loaded.
}
// This is used to register actions in the Scripts menu in score
export const actions = [
{ name: "Protocols/Websockets/Device from JSON"
, context: ActionContext.Menu
, action: importWsDevice
}
]