forked from ycahome/pp-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpypluginstore.html
More file actions
533 lines (461 loc) · 17 KB
/
Copy pathpypluginstore.html
File metadata and controls
533 lines (461 loc) · 17 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<style>
:root {
--bg-color: #f4f4f9;
--text-color: #333;
--card-bg: #fff;
--border-color: #ddd;
--primary: #007bff;
--primary-hover: #0056b3;
--danger: #dc3545;
--danger-hover: #c82333;
--success: #28a745;
}
#pypluginstore-container {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
color: var(--text-color);
padding: 20px;
}
#pypluginstore-container .header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
#pypluginstore-container h1 {
margin: 0;
color: var(--primary);
}
#PyPluginStore-status {
font-weight: bold;
color: #666;
}
#pypluginstore-container .filters {
display: flex;
gap: 20px;
margin-bottom: 20px;
align-items: center;
background: var(--card-bg);
padding: 15px;
border-radius: 8px;
border: 1px solid var(--border-color);
flex-wrap: wrap;
}
#pypluginstore-container .filters input[type="text"] {
flex-grow: 1;
padding: 8px 12px;
border: 1px solid var(--border-color);
border-radius: 4px;
font-size: 1em;
min-width: 200px;
}
#pypluginstore-container .filters label {
display: flex;
align-items: center;
gap: 12px;
font-weight: bold;
cursor: pointer;
user-select: none;
}
#pypluginstore-container .sort-controls {
display: flex;
gap: 10px;
align-items: center;
}
#pypluginstore-container .sort-controls label {
margin: 0;
font-weight: normal;
font-size: 0.95em;
}
#pypluginstore-container .sort-controls select {
padding: 8px 12px;
border: 1px solid var(--border-color);
border-radius: 4px;
font-size: 1em;
background: white;
cursor: pointer;
}
/* Switch styling */
.switch {
position: relative;
display: inline-block;
width: 50px;
height: 24px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .4s;
border-radius: 24px;
}
.slider:before {
position: absolute;
content: "";
height: 18px;
width: 18px;
left: 3px;
bottom: 3px;
background-color: white;
transition: .4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: var(--success);
}
input:focus + .slider {
box-shadow: 0 0 1px var(--success);
}
input:checked + .slider:before {
transform: translateX(26px);
}
#pypluginstore-container .grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
}
#pypluginstore-container .card {
background: var(--card-bg);
border: 1px solid var(--border-color);
border-radius: 8px;
padding: 15px;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
display: flex;
flex-direction: column;
}
#pypluginstore-container .card-header {
font-size: 1.1em;
font-weight: bold;
margin-bottom: 10px;
}
#pypluginstore-container .card-desc {
font-size: 0.9em;
color: #666;
margin-bottom: 15px;
flex-grow: 1;
}
#pypluginstore-container .card-meta {
font-size: 0.8em;
color: #888;
margin-bottom: 15px;
}
#pypluginstore-container .actions {
display: flex;
gap: 10px;
align-items: stretch;
}
#pypluginstore-container button,
#pypluginstore-container .btn-link {
padding: 8px 12px;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
flex: 1;
transition: background 0.2s;
font-size: 13.3333px; /* Match default button font size */
font-family: inherit;
display: flex;
align-items: center;
justify-content: center;
text-decoration: none;
box-sizing: border-box;
min-height: 35px;
}
.btn-install { background: var(--primary); color: white; }
.btn-install:hover { background: var(--primary-hover); }
.btn-remove { background: var(--danger); color: white; }
.btn-remove:hover { background: var(--danger-hover); }
.btn-update { background: var(--success); color: white; }
.btn-info { background: #17a2b8; color: white; }
.btn-info:hover { background: #138496; }
.installed-badge {
display: inline-block;
background: var(--success);
color: white;
padding: 2px 6px;
border-radius: 10px;
font-size: 0.7em;
margin-left: 10px;
vertical-align: middle;
}
</style>
<div id="pypluginstore-container">
<div class="header">
<h1>PyPluginStore: Plugin Store</h1>
<div id="pypluginstore-status">Initializing...</div>
</div>
<div class="filters">
<input type="text" id="search-box" placeholder="Search plugins...">
<div class="sort-controls">
<label for="sort-select">Sort by:</label>
<select id="sort-select">
<option value="name">Name (A-Z)</option>
<option value="name-desc">Name (Z-A)</option>
<option value="updated">Last Updated (Newest)</option>
<option value="updated-oldest">Last Updated (Oldest)</option>
</select>
</div>
<label>
Installed
<span class="switch">
<input type="checkbox" id="installed-toggle">
<span class="slider"></span>
</span>
</label>
</div>
<div id="plugins-list" class="grid">
<!-- Plugins will be rendered here -->
</div>
</div>
<script>
(function() {
console.log("PyPluginStore script starting...");
let payloadIdx = null;
let triggerIdx = null;
let isLoading = false;
let pluginCache = null;
let installedCache = [];
let currentSort = 'name';
function setStatus(msg) {
console.log("PyPluginStore Status: " + msg);
const el = document.getElementById('pypluginstore-status');
if (el) el.innerText = msg;
}
async function init() {
if (document.getElementById('pypluginstore-status').innerText !== 'Initializing...') {
return; // Already running
}
// Attach filter listeners
document.getElementById('search-box').oninput = filterAndRender;
document.getElementById('installed-toggle').onchange = filterAndRender;
document.getElementById('sort-select').onchange = function() {
currentSort = this.value;
filterAndRender();
};
setStatus("Finding API bridge...");
try {
console.log("Fetching devices from /json.htm...");
const req = await fetch('json.htm?type=command¶m=getdevices&filter=all&used=true&t=' + Date.now());
if (!req.ok) throw new Error("HTTP error " + req.status);
const devices = await req.json();
if (devices.result) {
for (const device of devices.result) {
if (device.ID === 'PPM_API_PAYLOAD') {
payloadIdx = device.idx;
}
if (device.ID === 'PPM_API_TRIGGER') {
triggerIdx = device.idx;
}
}
}
if (!payloadIdx || !triggerIdx) {
setStatus("Error: PyPluginStore devices not found (" + (payloadIdx?'Y':'N') + "/" + (triggerIdx?'Y':'N') + "). Please ensure the plugin is added in Hardware.");
return;
}
console.log("Found bridge devices: Payload=" + payloadIdx + ", Trigger=" + triggerIdx);
loadPlugins();
} catch (e) {
console.error("Init Error:", e);
setStatus("Failed to connect to Domoticz API: " + e.message);
}
}
async function sendCommand(action, data) {
if (isLoading) {
alert("An operation is already in progress. Please wait.");
return null;
}
isLoading = true;
try {
const txId = Date.now().toString();
const payload = JSON.stringify({ action, tx_id: txId, ...data });
console.log("Sending command: " + action, payload);
await fetch(`json.htm?type=command¶m=udevice&idx=${payloadIdx}&nvalue=0&svalue=${encodeURIComponent(payload)}`);
await fetch(`json.htm?type=command¶m=switchlight&idx=${triggerIdx}&switchcmd=On`);
return await pollResponse(action, txId);
} finally {
isLoading = false;
}
}
async function pollResponse(action, txId, retries = 120) {
console.log("Polling for response to " + action + " (tx_id: " + txId + ")...");
for (let i = 0; i < retries; i++) {
await new Promise(r => setTimeout(r, 1000));
try {
const req = await fetch(`json.htm?type=command¶m=getdevices&rid=${payloadIdx}&t=` + Date.now());
const resp = await req.json();
if (resp.result && resp.result.length > 0) {
const dataStr = resp.result[0].Data;
try {
const data = JSON.parse(dataStr);
if (data.action === action && data.tx_id === txId) {
console.log("Received response:", data);
return data;
}
} catch (e) {}
}
} catch (e) {
console.error("Polling error", e);
}
}
throw new Error("Timeout waiting for plugin response.");
}
async function loadPlugins() {
setStatus("Loading plugins from registry...");
try {
const response = await sendCommand('list_plugins', {});
if (response && response.status === 'success') {
pluginCache = response.data;
installedCache = response.installed;
filterAndRender();
setStatus(`Loaded ${Object.keys(response.data).length} plugins.`);
} else {
setStatus("Error loading plugins: " + (response ? response.message : "Unknown error"));
}
} catch (e) {
setStatus(e.message);
}
}
function filterAndRender() {
if (!pluginCache) return;
const searchQuery = document.getElementById('search-box').value.toLowerCase();
const showInstalledOnly = document.getElementById('installed-toggle').checked;
const filteredKeys = Object.keys(pluginCache).filter(key => {
const info = pluginCache[key];
const description = info[2].toLowerCase();
const matchesSearch = description.includes(searchQuery) || key.toLowerCase().includes(searchQuery);
const isInstalled = installedCache.indexOf(key) !== -1;
if (showInstalledOnly && !isInstalled) return false;
return matchesSearch;
});
const filteredData = {};
filteredKeys.forEach(key => filteredData[key] = pluginCache[key]);
renderPlugins(filteredData, installedCache);
}
function sortPlugins(pluginData, sortBy) {
const keys = Object.keys(pluginData);
switch(sortBy) {
case 'name':
return keys.sort((a, b) => a.localeCompare(b));
case 'name-desc':
return keys.sort((a, b) => b.localeCompare(a));
case 'updated':
return keys.sort((a, b) => {
const dateA = new Date(pluginData[a][4] || 0);
const dateB = new Date(pluginData[b][4] || 0);
return dateB - dateA; // Newest first
});
case 'updated-oldest':
return keys.sort((a, b) => {
const dateA = new Date(pluginData[a][4] || 0);
const dateB = new Date(pluginData[b][4] || 0);
return dateA - dateB; // Oldest first
});
default:
return keys.sort((a, b) => a.localeCompare(b));
}
}
function renderPlugins(pluginData, installedList) {
const container = document.getElementById('plugins-list');
if (!container) return;
container.innerHTML = '';
const keys = sortPlugins(pluginData, currentSort);
if (keys.length === 0) {
container.innerHTML = '<div style="grid-column: 1/-1; text-align: center; padding: 40px; color: #888;">No plugins match your filters.</div>';
return;
}
const now = new Date();
for (const key of keys) {
const info = pluginData[key];
const author = info[0];
const repo = info[1];
const description = info[2];
const branch = info[3];
const updatedAt = info[4];
const isInstalled = installedList.indexOf(key) !== -1;
let timeStr = "";
if (updatedAt) {
const date = new Date(updatedAt);
const diffMs = now - date;
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (diffDays === 0) timeStr = "today";
else if (diffDays === 1) timeStr = "yesterday";
else if (diffDays < 30) timeStr = diffDays + " days ago";
else if (diffDays < 365) timeStr = Math.floor(diffDays/30) + " months ago";
else timeStr = Math.floor(diffDays/365) + " years ago";
}
const card = document.createElement('div');
card.className = 'card';
let html = `
<div class="card-header">
${key}
${isInstalled ? '<span class="installed-badge">Installed</span>' : ''}
</div>
<div class="card-desc">${description}</div>
<div class="card-meta">
Author: ${author} <br>
Repo: ${repo} (${branch})
${timeStr ? '<br> Last updated: ' + timeStr : ''}
</div>
<div class="actions">
<a class="btn-info btn-link" href="https://github.com/${author}/${repo}" target="_blank">Repo</a>
`;
const cardId = 'btn-' + key.replace(/[^a-z0-9]/gi, '-');
if (isInstalled) {
html += `
<button class="btn-update" data-key="${key}" data-action="update">Update</button>
<button class="btn-remove" data-key="${key}" data-action="remove">Remove</button>
`;
} else {
html += `
<button class="btn-install" data-key="${key}" data-action="install">Install</button>
`;
}
html += `</div>`;
card.innerHTML = html;
container.appendChild(card);
}
// Add event listeners only to action buttons
container.querySelectorAll('button[data-action]').forEach(btn => {
btn.onclick = function() {
handleAction(this.getAttribute('data-action'), this.getAttribute('data-key'));
};
});
}
async function handleAction(action, pluginKey) {
if (action === 'remove' && !confirm(`Are you sure you want to remove ${pluginKey}?`)) {
return;
}
setStatus(`Executing ${action} for ${pluginKey}... This may take a moment.`);
try {
const response = await sendCommand(action, { plugin_key: pluginKey });
if (response && response.status === 'success') {
alert(`Successfully completed ${action} for ${pluginKey}. Domoticz restart may be required.`);
loadPlugins();
} else {
alert(`Error during ${action}: ` + (response ? response.message : "Unknown error"));
setStatus("Operation failed.");
}
} catch (e) {
alert("Error: " + e.message);
setStatus("Operation failed.");
}
}
// Domoticz SPA might load this multiple times or at different ready states
if (document.readyState === 'complete' || document.readyState === 'interactive') {
setTimeout(init, 100);
} else {
document.addEventListener('DOMContentLoaded', init);
}
// Fallback for some browsers/SPA engines
window.onload = init;
})();
</script>