-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.php
685 lines (459 loc) · 18.1 KB
/
cli.php
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<?php
///////////////////////////////////
// p O W e R P e L L e T
//
// A PHP application for managing intelligent power strips
// at a yearly video game convention.
//
//////////////////////////////////////////
//
// Pre-reqs
// PHP5 php5-sqlite
//
//////////////////////////////////////////
// Design
//
// cli.php
// A simple command line interface that managed data in sqlite
// database tables.
//
// cycladesdaemon.php
// Backgruond process that scans the database for tasks then
// executes on them. Uses 1st command line option to determine which strip.
//
//////////////////////////////////////////
//
// dbschema
//
// This is the table with a row for each power strip, or chain of cyclades
// power strips.
//
// TABLE strips
// Id INTEGER PRIMARY KEY, - self explan
// number INTEGER, - unique number for strip/chain
// type INTEGER, - type (1/2) for demon to latch onto
// connection INTEGER, - ethernet or direct serial
// path CHAR[48], - device or ip
// tcpport INTEGER, - tcp port for ip
// active INTEGER, -
// desc CHAR[64], - test description
// outlets INTEGER, - total number of outlets
// chainedstrips INTEGER, - cyclades - how many are chained (For current tables)
// login CHAR[32], - login to use
// pass CHAR[32], - pass to use
// baud INTEGER, - baud rate for direct attached serial
// parity CHAR[1], - not needed, can go away
// flow CHAR[1])"; - not needed, can go away
//
//
// This table has a row for each port on each power strip or strip chain.
//
// TABLE devices (
// Id INTEGER PRIMARY KEY, - self explanatory
// stripnumber INTEGER, - which strip # is the port on
// port INTEGER, - which port is this for
// status INTEGER, - currently on or off
// desc CHAR[128])"; - text description
//
// This table is a job queue. Each task to be done goes in here, and the daemon for the strip
// running in the background picks it up. Once it acts on it, it writes the status back.
// This prevents sync issues.
//
//
// TABLE commands (
// Id INTEGER PRIMARY KEY, - UID
// strip INTEGER, - strip #
// port INTEGER, - port #
// change INTEGER)"; - change being requested
//
// The current amount of current being drawn as reported by
// daemons. A trend is not kept in the database. Needs to be
// written out if desired.
//
// TABLE current (
// Id INTEGER PRIMARY KEY,
// strip INTEGER, - which strip
// substrip INTEGER, - which unit in a chain of strips
// average CHAR[6], - the average amperage reported
// peak CHAR[6])" - the peak amperage reported
//
//////////////////////////////////////////
//
// FLAWS
//
// There could probably stand to be a synchronization capability
// where by the daemons read the status of each port and
// write it into the database.
//
//
//////////////////////////////////////////
$dbhandle = sqlite_open('db/test.db', 0666, $error);
if (!$dbhandle) die ($error);
$fh = fopen('php://stdin', 'r');
$editingstripnumber = 1;
// Draws a simple header for the top of each menu
function drawHeader($text)
{
print "\033[2J";
print "\n";
print ".--[ PowerPellet v.0.1a ]------------------------------------------+\n";
print ": $text \n";
print "`------------------------------------------------+\n";
print "\n";
}
// Admin menu
function adminMenu()
{
while ($adminexit != 1) {
drawHeader('Admin Menu');
print "[P] ower Strip Add / Remove / Config \n";
print "[D] evice edit. What is on which port \n";
print "[I] nitialize databases \n";
print "[Q] uit to main menu \n";
$line = trim(fgets(STDIN));
switch (strtolower($line)) {
case 'q':
$adminexit = 1;
break;
case 'i':
initdb();
break;
case 'p':
stripedit();
break;
case 'd':
deviceedit();
break;
case 's':
selectstrip();
break;
}
}
}
// I guess we could re-use this for the main menu strip selection
// Select which strip is being edited
function selectstrip()
{
global $dbhandle,$editingstripnumber;
$result = sqlite_query($dbhandle, "SELECT number, desc, type, path, outlets, chainedstrips FROM strips");
print ". # . Name . T . Path . Out . Start .\n" ;
while ($row = sqlite_fetch_array($result, SQLITE_NUM)) {
$outputstring = " ";
$outputstring = substr_replace ($outputstring, $row['0'], 2, 0 );
$outputstring = substr_replace ($outputstring, $row['1'], 6, 0);
$outputstring = substr_replace ($outputstring, $row['2'], 40, 0);
$outputstring = substr_replace ($outputstring, $row['3'], 44, 0);
$outputstring = substr_replace ($outputstring, $row['4'], 59, 0);
print $outputstring . "\n";
}
print "Strip number:";
$editingstripnumber = trim(fgets(STDIN));
}
function deviceedit()
{
global $dbhandle,$editingstripnumber;
$outletpage = 0;
while ($exit != 1) {
drawHeader('Device Edit Menu');
print "\n";
print "Selected strip :" . $editingstripnumber . "\n";
// Get number of outlets so we can render a range if needed
$stripresult = sqlite_fetch_array(sqlite_query($dbhandle, "SELECT outlets, desc FROM strips WHERE number = $editingstripnumber"), SQLITE_NUM);
$outlets = $stripresult['0'];
$result = sqlite_query($dbhandle, "SELECT port, desc FROM devices WHERE stripnumber = $editingstripnumber ORDER BY port LIMIT 15 offset $outletpage");
print ". # . Desc .\n" ;
$j = 0;
while ($row = sqlite_fetch_array($result, SQLITE_NUM)) {
$outputstring = " ";
$outputstring = substr_replace ($outputstring, $row['0'], 2, 0 );
$outputstring = substr_replace ($outputstring, $row['1'], 6, 0);
print $outputstring . "\n";
$j++;
}
print "\n\n";
print "[Q]uit, [1] to [$j] to edit device desc, Page [D]own/[U]p, [S]elect strip:";
$line = trim(fgets(STDIN));
if (is_numeric($line)) {
if ((int)$line >= 1 || (int)$line <= $j) {
print "\n\n32 characters max. \n";
print "New description for port # $line :";
$descline = trim(fgets(STDIN));
$stm = "UPDATE devices SET desc='$descline' WHERE stripnumber='$editingstripnumber' AND port='$line'";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
print ("Cannot execute query: $error");
}
}
switch (strtolower($line)) {
case 'q':
$exit = 1;
break;
case 's':
selectStrip();
break;
case 'd':
if ($outletpage < $outlets) {
$outletpage = $outletpage + 15;
}
break;
case 'u':
if ($outletpage >= 15) {
$outletpage = $outletpage - 15;
}
break;
}
}
}
function stripedit()
{
global $dbhandle;
while ($exit != 1) {
drawHeader('Strip Edit Menu');
$result = sqlite_query($dbhandle, "SELECT number, desc, type, path, outlets, chainedstrips FROM strips");
print ". # . Name . T . Path . Out . Chained .\n" ;
while ($row = sqlite_fetch_array($result, SQLITE_NUM)) {
$outputstring = " ";
$outputstring = substr_replace ($outputstring, $row['0'], 2, 0 );
$outputstring = substr_replace ($outputstring, $row['1'], 6, 0);
$outputstring = substr_replace ($outputstring, $row['2'], 40, 0);
$outputstring = substr_replace ($outputstring, $row['3'], 44, 0);
$outputstring = substr_replace ($outputstring, $row['4'], 59, 0);
print $outputstring . "\n";
}
print "\n\n";
print "[q]uit to admin, [a]dd new strip, [d]elete strip, strip # to edit:";
$line = trim(fgets(STDIN));
switch (strtolower($line)) {
case 'q':
$exit = 1;
break;
case 'd':
print "Delete power strip. \n";
print "Power strip number to delete:";
$stripnumber = trim(fgets(STDIN));
$stm = "DELETE FROM strips WHERE number='$stripnumber'";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
die ("Cannot execute query: $error");
$stm = "DELETE FROM devices WHERE stripnumber='$stripnumber'";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
die ("Cannot execute query: $error");
break;
case 'a':
print "\033[2J";
print "Add new power strip. \n";
print "\n";
print "Unique number for power strip (ex. 1, 5, 12):";
$stripnumber = trim(fgets(STDIN));
print "\n\nType of strip\n [1] Cyclades Alterpath \n [2] Baytech RPC\n: ";
$striptype = trim(fgets(STDIN));
// There should be strip type logic here
print "\n\nHow is it connected\n [1] Serial \n [2] Ethernet \n:";
$stripconnection = trim(fgets(STDIN));
print "\n\nPath for connection\n Ethernet connections, type the IP. \n";
print " Serial connections, type the serial port (ex /dev/ttyUSB0)\n:";
$strippath = trim(fgets(STDIN));
print "\n\nTCP Port # for ethernet connected strips (press enter for serial):\n";
$stripport = trim(fgets(STDIN));
print "\n\nDescription of strip (128 char max)\n:";
$stripdesc = trim(fgets(STDIN));
print "\n\nNumber of outlets on strip, or chain of strips (Cyclades)\n";
print "For baytech RPC-3, this is normally 8.\n";
print "For Cyclades, this is 10 usually 10 per strip in the chain. \n";
print ":";
$stripoutlets = trim(fgets(STDIN));
print "For cyclades that are chained, what is the total number of chained strips \n";
print "ex. 3 strips are connected together, so it is 3.\n";
$chainedstrips = trim(fgets(STDIN));
print "\n\nLogin for strip:";
$striplogin = trim(fgets(STDIN));
print "\n\nPassword for strip:";
$strippass = trim(fgets(STDIN));
print "\n\nBaud rate for serial strip (press enter for ethernet):";
$stripbaud = trim(fgets(STDIN));
print "\n\nSerial parity rate (press enter for ethernet):";
$stripparity = trim(fgets(STDIN));
print "\n\nFlow control for serial (press enter for ethernet):";
$stripflow = trim(fgets(STDIN));
print "\n\n";
print "Num: $stripnumber\nType: $striptype\nConnection: $stripconnection\nPath: $strippath\nPort: $stripport\nDescription: $stripdesc\n";
print "Number of outlets: $stripoutlets\nLogin: $striplogin\nPass: $strippass\nBaud: $stripbaud\nParity: $stripparity\nFlow control: $stripflow\n\n";
print "Okay to add this strip? (\"Y\" to confirm yes, \"N\" to abort)";
$addline = trim(fgets(STDIN));
switch (strtolower($addline)) {
case 'y':
print "Updating strip information!\n";
$stm = "INSERT INTO strips (number, type, connection, path, tcpport, desc, outlets, chainedstrips, login, pass, baud, parity, flow) VALUES('$stripnumber', '$striptype', '$stripconnection', '$strippath', '$stripport', '$stripdesc', '$stripoutlets', '$chainedstrips', '$striplogin', '$strippass', '$stripbaud', '$stripparity', '$stripflow')";
$ok = sqlite_exec ($dbhandle, $stm, $error);
if (!$ok)
die ("Cannot execute query: $error");
print "Creating $stripoutlets device entries for strip!\n";
// Loop through and create entries for each device on each strip.
$j = 1;
while ($j <= $stripoutlets) {
$stm = "INSERT INTO devices (stripnumber, port, desc) VALUES('$stripnumber', '$j', 'No description')";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
die ("Cannot execute query: $error");
$j++;
}
// Loop through creating default line entries for each power current level for multiple chained strips.
print "Creating current entries for $chainedstrips units!\n";
$j = 1;
while ($j <= $chainedstrips) {
$stm = "INSERT INTO current (strip, substrip, average, peak) VALUES ('$stripnumber', '$j', 'init', 'init')";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
die ("Cannot execute query: $error");
$j++;
}
print "Complete. Press enter to continue.";
$line = trim(fgets(STDIN));
break;
}
}
}
}
function setport($mode)
{
global $dbhandle,$modstripnumber,$striparray;
if ($mode == '0') {
print ("\nPort to turn off:");
$input = trim(fgets(STDIN));
if (is_numeric($input)) {
$stm = "INSERT INTO commands (strip, port, change) VALUES ('$striparray[$modstripnumber]', '$input', '0')";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
print ("Cannot execute query: $error");
}
}
if ($mode == '1') {
print ("\nPort to turn on:");
$input = trim(fgets(STDIN));
if (is_numeric($input)) {
$stm = "INSERT INTO commands (strip, port, change) VALUES ('$striparray[$modstripnumber]', '$input', '1')";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
print ("Cannot execute query: $error");
}
}
}
function initdb()
{
global $dbhandle;
print "\033[2J";
print "This function will erase all databases then create them fresh.\n";
print "THIS IS DESTRUCTIVE. DO YOU WISH TO PROCEED?\n";
print "ALL CURRENT SETUP INFORMATION WILL BE LOST!\n";
print "Type \"resetit\" to erase, or hit enter to abort:";
$line = trim(fgets(STDIN));
switch (strtolower($line)) {
case 'resetit':
print "Reseting DBs!!1!!!!\n";
sqlite_exec($dbhandle, "drop table strips", $error);
sqlite_exec($dbhandle, "drop table devices", $error);
sqlite_exec($dbhandle, "drop table current", $error);
sqlite_exec($dbhandle, "drop table commands", $error);
$stm = "CREATE TABLE strips (Id INTEGER PRIMARY KEY,
number INTEGER, type INTEGER, connection INTEGER,
path CHAR[48], tcpport INTEGER, active INTEGER,
desc CHAR[64], outlets INTEGER, chainedstrips INTEGER, login CHAR[32],
pass CHAR[32], baud INTEGER, parity CHAR[1],
flow CHAR[1])";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
die ("Cannot execute query: $error");
echo "Database strips created successfully\n";
$stm = "CREATE TABLE devices (Id INTEGER PRIMARY KEY,
stripnumber INTEGER, port INTEGER, status INTEGER, desc CHAR[128])";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
die ("Cannot execute query: $error");
echo "Database devices created successfully\n";
$stm = "CREATE TABLE commands (Id INTEGER PRIMARY KEY, strip INTEGER, port INTEGER, change INTEGER)";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
die ("Cannot execute query: $error");
echo "Database commands created successfully\n";
$stm = "CREATE TABLE current (Id INTEGER PRIMARY KEY, strip INTEGER, substrip INTEGER, average CHAR[6], peak CHAR[6])";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
die ("Cannot execute query: $error");
echo "Database current created successfully\n";
print "Complete. Press enter to continue.";
$line = trim(fgets(STDIN));
break;
}
}
$modstripnumber = 0;
$outletpage = 0;
while ($exit != 1) {
// Copy into an array the list of strips that have database entries so we can next/previous
$stripcount = 0;
$result = sqlite_query($dbhandle, "SELECT number FROM strips ORDER BY number");
while ($row2 = sqlite_fetch_array($result, SQLITE_NUM)) {
print ("DEBUG=".$row2);
$striparray[$stripcount] = $row2['0'];
$stripcount++;
}
// Get number of outlets and strip description so we can page through ports if needed
$stripresult = sqlite_fetch_array(sqlite_query($dbhandle, "SELECT outlets, desc FROM strips WHERE number = $striparray[$modstripnumber]"), SQLITE_NUM);
$outlets = $stripresult['0'];
$result = sqlite_query($dbhandle, "SELECT port, desc, status FROM devices WHERE stripnumber = $striparray[$modstripnumber] ORDER BY port LIMIT 15 offset $outletpage");
drawHeader('Main Menu');
print "Strip: " . $striparray[$modstripnumber] . " Desc: " . $stripresult['1'] . " Outlets: " . $outlets . "\n\n";
print ". # . Desc . Status \n" ;
// Print the list of ports, and their status
$j = 0;
while ($row = sqlite_fetch_array($result, SQLITE_NUM)) {
$outputstring = " ";
$outputstring = substr_replace ($outputstring, $row['0'], 2, 0 );
$outputstring = substr_replace ($outputstring, $row['1'], 6, 0);
if ($row['2'] == '1') {
$outputstring = substr_replace ($outputstring, "On", 36, 0);
} else {
$outputstring = substr_replace ($outputstring, "Off", 36, 0);
}
print $outputstring . "\n";
$j++;
}
print "\nTurn [O]n port, [Z]Turn off port, [C]urrent, [!]Admin, [N]ext/[P]rev strip, Page [D]own/[U]p, [Q]uit :";
$line = trim(fgets(STDIN));
switch (strtolower($line)) {
case 'q':
$exit = 1;
sqlite_close($dbhandle);
exit();
case '!':
adminMenu();
break;
case 'o':
setport(1);
break;
case 'z':
setport(0);
break;
case 'n':
if ($modstripnumber != ($stripcount - 1)) {
$modstripnumber++;
}
break;
case 'p':
if ($modstripnumber > 0) {
$modstripnumber = $modstripnumber - 1;
}
break;
case 'd':
if ($outletpage < $outlets) {
$outletpage = $outletpage + 15;
}
break;
case 'u':
if ($outletpage >= 15) {
$outletpage = $outletpage - 15;
}
break;
}
}
?>