-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_live.php
More file actions
79 lines (65 loc) · 2.25 KB
/
Copy pathtest_live.php
File metadata and controls
79 lines (65 loc) · 2.25 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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Tapo\P110;
// --- Load credentials ---
$credsFile = __DIR__ . '/credentials.json';
if (!file_exists($credsFile)) {
fwrite(STDERR, "Missing credentials.json\n");
exit(1);
}
$creds = json_decode(file_get_contents($credsFile), true);
// --- Parse args ---
if ($argc < 3) {
fwrite(STDERR, "Usage: php test_live.php <hostname> <command>\n");
fwrite(STDERR, "Commands: info, on, off, toggle, status, energy, rename <name>\n");
exit(1);
}
$host = $argv[1];
$command = $argv[2];
$plug = new P110($host, $creds['email'], $creds['password']);
switch ($command) {
case 'info':
$info = $plug->getDeviceInfo();
echo "Name: " . $plug->getDeviceName() . "\n";
echo "Model: " . $info['model'] . "\n";
echo "Status: " . ($info['device_on'] ? 'ON' : 'OFF') . "\n";
echo "IP: " . $info['ip'] . "\n";
echo "FW: " . $info['fw_ver'] . "\n";
echo "Signal: " . $info['rssi'] . " dBm\n";
break;
case 'on':
$plug->turnOn();
echo "Turned ON\n";
break;
case 'off':
$plug->turnOff();
echo "Turned OFF\n";
break;
case 'toggle':
$plug->toggleState();
echo "Toggled to " . ($plug->getStatus() ? 'ON' : 'OFF') . "\n";
break;
case 'status':
echo ($plug->getStatus() ? 'ON' : 'OFF') . "\n";
break;
case 'energy':
$energy = $plug->getEnergyUsage();
echo "Current power: " . $energy['current_power'] . " mW\n";
echo "Today energy: " . $energy['today_energy'] . " Wh\n";
echo "Month energy: " . $energy['month_energy'] . " Wh\n";
echo "Today runtime: " . $energy['today_runtime'] . " min\n";
break;
case 'rename':
if ($argc < 4) {
fwrite(STDERR, "Usage: php test_live.php <hostname> rename <new_name>\n");
exit(1);
}
$newName = $argv[3];
$plug->request('set_device_info', ['nickname' => base64_encode($newName)]);
echo "Renamed to: {$newName}\n";
break;
default:
fwrite(STDERR, "Unknown command: {$command}\n");
fwrite(STDERR, "Commands: info, on, off, toggle, status, energy, rename <name>\n");
exit(1);
}