Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up of Features from S Rutter #6

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5c22c57
Auto Discovery of TPLinkDevices on network
shanerutter Nov 29, 2017
43a40be
Stopped stream_socket_client from giving off warning messages
shanerutter Nov 29, 2017
c2ea0ee
Faster querying of network devices by allowing config to support a ti…
shanerutter Nov 30, 2017
dab8bcb
Expose devices config via getConfig
shanerutter Nov 30, 2017
9efbc7f
Checking auto discovery for empty responses and null json_decodes
shanerutter Nov 30, 2017
ac1c507
PSR-2 compliance changes
shanerutter Dec 1, 2017
7a897f8
Exclude .idea dev folder
shanerutter Dec 1, 2017
b7d3d57
Updated readme.md
shanerutter Dec 1, 2017
64b4fa2
Updated readme.md
shanerutter Dec 1, 2017
e3faa21
Merge branch 'feature/auto_discovery' of github.com:shanerutter/tplin…
shanerutter Dec 1, 2017
bfb0fe3
Laravel config updated with timeout setting
shanerutter Dec 1, 2017
a9c457c
Added missing bit from readme
shanerutter Dec 1, 2017
d7d3efe
Added missing bit from readme
shanerutter Dec 1, 2017
81f6bb4
Merge branch 'feature/auto_discovery' of github.com:shanerutter/tplin…
shanerutter Dec 1, 2017
6de9fb7
Merge branch 'feature/auto_discovery' of github.com:shanerutter/tplin…
shanerutter Dec 1, 2017
94200f4
Merge branch 'feature/auto_discovery' of github.com:shanerutter/tplin…
shanerutter Dec 1, 2017
f0ed9de
Read me edit for deviceList
shanerutter Dec 1, 2017
8b750c7
Added jonnywilliamson changes with some bug fixes
shanerutter Jun 19, 2019
4f73c27
Merge branch 'feature/power_control_methods' into feature/auto_discovery
shanerutter Jun 19, 2019
0030c64
Removed error supression
shanerutter Jun 20, 2019
4f30033
Changed requirement for ip lib to use latest
shanerutter Jun 20, 2019
d3f2f2b
Made newDevice public to allow manually adding new items to existing …
shanerutter Jun 20, 2019
ca5a794
Ability to set TPLinkDevice encryption key on construct
shanerutter Jun 20, 2019
ba2f253
Fix for hs105
shanerutter Jun 20, 2019
8485ca9
- Added stream timeout capabilities as some devices cause the stream …
shanerutter Jul 9, 2019
63487bf
Updated readme
shanerutter Jul 9, 2019
830c016
Auto discovery added callback function when a new device is discovered
shanerutter Jul 9, 2019
aa9f2cd
Added laravel 5.5 auto package discovery support
shanerutter Jul 9, 2019
8bad2b3
Added alias to auto package discovery
shanerutter Jul 9, 2019
e20c416
Added support for a "deviceType" field against the device to allow di…
shanerutter Jul 9, 2019
fc2c5e6
Added max discover limit to the autoDiscoverTPLinkDevices function, t…
shanerutter Jul 9, 2019
e0c165e
Added ability to control smart light bulbs on / off with colour contr…
shanerutter Aug 9, 2019
b348685
First review fixes.
jonnywilliamson Aug 17, 2019
fb8252c
Code re-formatted and some extra info added to readme.
jonnywilliamson Aug 20, 2019
0545c39
Fix center paragraph
jonnywilliamson Aug 20, 2019
be391a8
Use original laravel collections package
jonnywilliamson Dec 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.phar
composer.lock
vendor/
build/
build/
.idea/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: php

php:
- 7.2
- 7.1
- 7.0
- 5.6
Expand Down
74 changes: 58 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PHP Library to Control and Access a TP-Link Smartplug!
PHP Library to Control and Access a TP-Link Smartplug OR TP Smart Bulbs!
=========
<p align="center">
<a href="https://github.com/jonnywilliamson/tplinksmartplug"><img src="https://raw.githubusercontent.com/jonnywilliamson/tplinksmartplug/master/tplinkplug.jpg" alt="Smart Plug"></a><br />
Expand Down Expand Up @@ -29,9 +29,9 @@ composer require williamson/tplinksmartplug

### Laravel Installation/Integration

This library supports Laravel's auto discovery feature for auto registering the service provider and facade. If your version of Laravel supports auto discovery, after you have added this package YOU ARE NOW DONE!
###### Now Supports Laravel 5.5 auto package discovery (you do not need to do the below step if you have Laravel 5.5+)

If you are using a very old version of Laravel, once this package is installed, you need to register the package's service provider, in `config/app.php`:
Once the TPLink Smartplug library is installed, you need to register the library's service provider, in `config/app.php`:

```php
'providers' => [
Expand All @@ -41,13 +41,7 @@ If you are using a very old version of Laravel, once this package is installed,
```
##### Facades

Only if your version of Laravel does NOT support auto discovery then add the following to the aliases section of 'app.php'.
```php
'aliases' => [
//...
"TPLink" => Williamson\TPLinkSmartplug\Laravel\Facades\TPLinkFacade::class
]
```
By default, this library will *automatically* register a facade to be used in Laravel. The package checks first to ensure `TPLink` has not already be registered and if this is the case, will register `TPLink` as your quick access to the library. More examples to follow.

##### Config file
This package requires a config file so that you can provide the address/details of the TPLink devices you would like to control. To generate this file, run the following command:
Expand All @@ -65,13 +59,17 @@ The config file is a very simple array structured file. A config file is require
<?php
return [
'lamp' => [
'ip' => '192.168.1.100', //Or hostname eg: home.example.com
'ip' => '192.168.1.100', //Or hostname eg: home.example.com
'port' => '9999',
'timeout' => 5, // Optional, timeout setting (how long we will try communicate with device before giving up)
'timeout_stream' => 5, // Optional, timeout setting for stream (how long to wait for the response from the device)
],
];
```

You may add as many devices as you wish, as long as you specify the IP address (or host address if required) and port number to access each one. Giving each device a name makes it easy to identify them when coding later. _(Please note that the name you give here does NOT have to match the actual name you might have assigned the device using an official app like Kasa. They do NOT have to match)_
You may add as many devices as you wish, as long as you specify the IP address (or host address if required) and port number to access each one. Giving each device a name makes it easy to identify them when coding later. _(Please note that the name you give here does NOT have to match the actual name you might have assigned the device using an official app like Kasa. They do NOT have to match)

If you do not know the IP address of your devices, you can use the `autoDiscoverTPLinkDevices` method to automatically scan and find devices on your network for you to save into your config file.

## Usage
You can access your device either through the `TPLinkManager` class (especially useful if you have multiple devices), or directly using the `TPLinkDevice` class.
Expand Down Expand Up @@ -138,10 +136,53 @@ If a command requires a parameter, provide that as well:
$tpDevice->sendCommand(TPLinkCommand::setLED(false));
```

####Toggle Power
There is one command that is called directly on the `TPLinkDevice` and that is the `togglePower()` method.
#### Auto Discovery
You can search your local network for devices using `TPLinkManager`, using the method `autoDiscoverTPLinkDevices`
all found devices will be added to the 'TPLinkManager' config automatically, exposed using `deviceList()`.

However this information will not automatically be persisted. You must save the details into your config file if you wish to avoid having to keep scanning your network.

You must provide the IP range you wish to scan, examples of usage are as follows:
```php
//Non laravel
$tpLinkManager->autoDiscoverTPLinkDevices('192.168.0.*');
$tpLinkManager->autoDiscoverTPLinkDevices('192.168.0.10-192.168.0.40');

//Laravel
// with facade
TPLink::autoDiscoverTPLinkDevices('192.168.0.*');
TPLink::autoDiscoverTPLinkDevices('192.168.0.10-192.168.0.40');

// without facade
app('tplink')->autoDiscoverTPLinkDevices('192.168.0.*');
app('tplink')->autoDiscoverTPLinkDevices('192.168.0.10-192.168.0.40');

app(TPLinkManager::class)->autoDiscoverTPLinkDevices('192.168.0.*');
app(TPLinkManager::class)->autoDiscoverTPLinkDevices('192.168.0.10-192.168.0.40');
```

The auto discovery command will take a while to scan, once completed you can use `deviceList()` method to view the new configuration and any found devices.

```php
//Non laravel
$tpLinkManager->deviceList();

//Laravel
// with facade
$devices = TPLink::deviceList();

// without facade
$devices = app('tplink')->deviceList();
$devices = app(TPLinkManager::class)->deviceList();
```

#### Toggle Power
There are a few convenience commands that can be called directly on the `TPLinkDevice`. Those methods are
`togglePower()`
`powerOn()`
`powerOff()`

If you only wish to toggle the current power state of the plug, use it as follows:
Eg. if you only wish to toggle the current power state of the plug, use it as follows:
```php
//Non laravel

Expand Down Expand Up @@ -224,6 +265,7 @@ Any issues, feedback, suggestions or questions please use issue tracker [here][l
- [softScheck](https://github.com/softScheck/tplink-smartplug) (Who did the reverse engineering and provided the secrets on how to talk to the Smartplug.)
- [Jonathan Williamson][link-author]
- [Syed Irfaq R.](https://github.com/irazasyed) For the idea behind how to manage multiple devices.
- [Shane Rutter](https://shanerutter.co.uk) Various features such as Auto-Discovery

## Disclaimer

Expand All @@ -234,7 +276,7 @@ See License section for more details.

This project is released under the [MIT][link-license] License.

© 2017 [Jonathan Williamson][link-author], All rights reserved.
© 2019 [Jonathan Williamson][link-author], All rights reserved.

[link-author]: https://github.com/jonnywilliamson
[link-repo]: https://github.com/jonnywilliamson/tplinksmartplug
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
"name": "williamson/tplinksmartplug",
"description": "A PHP library to control and receive information from a TP-Link smartplug.",
"license": "MIT",
"keywords": ["TPLink", "PHP", "HS110", "HS100", "Timer", "SmartPlug", "Laravel"],
"keywords": ["TPLink", "PHP", "HS110", "HS100", "Timer", "SmartPlug", "Laravel", "SmartBulbs", "Bulbs"],
"authors": [
{
"name": "Jonathan Williamson",
"email": "[email protected]"
}
],
"require": {
"tightenco/collect": "^5.3"
"illuminate/collections": ">5.0",
"s1lentium/iptools": "^1.1"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest commit in this library has been tagged at 1.1.1 so using * previously here isn't necessary/a great practice

},
"require-dev": {
"phpunit/phpunit": "^5.7"
Expand Down
27 changes: 11 additions & 16 deletions src/Laravel/TPLinkServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

namespace Williamson\TPLinkSmartplug\Laravel;

use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;
use Williamson\TPLinkSmartplug\TPLinkManager;
use Williamson\TPLinkSmartplug\Laravel\Facades\TPLinkFacade;

class TPLinkServiceProvider extends ServiceProvider
{

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Bootstrap the application services.
*
Expand All @@ -37,15 +32,15 @@ public function register()
});

$this->app->alias(TPLinkManager::class, 'tplink');
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [TPLinkManager::class];
// Auto-register the TPLink facade if the user hasn't already
// assigned it to another class. Takes care of Laravel <5.5 users.
if (class_exists(AliasLoader::class)) {
$loader = AliasLoader::getInstance();

if (!array_key_exists('TPLink', $loader->getAliases())) {
$loader->alias('TPLink', TPLinkFacade::class);
}
}
}
}
21 changes: 15 additions & 6 deletions src/Laravel/config/TPLink.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
<?php
return [
'heater' => [
'ip' => '192.168.1.100', //Or hostname eg: home.example.com
'port' => '9999',
'ip' => '192.168.1.100', //Or hostname eg: home.example.com
'port' => '9999',
'timeout' => 5, // Optional, timeout setting (how long we will try communicate with device before giving up)
'timeout_stream' => 3, // Optional, timeout setting for stream (how long to wait for the response from the device)
'deviceType' => 'IOT.SMARTPLUGSWITCH', // Smart Bulbs are also supported: 'IOT.SMARTBULB'
],

// 'bedroom' => [
// 'ip' => '192.168.1.100', //Or hostname
// 'port' => '9999',
// 'ip' => '192.168.1.101',
// 'port' => '9999',
// 'timeout' => 5,
// 'timeout_stream' => 3,
// 'deviceType' => 'IOT.SMARTPLUGSWITCH',
// ],

// 'livingroom' => [
// 'ip' => '192.168.1.101', //Or hostname
// 'port' => '9999',
// 'ip' => '192.168.1.102',
// 'port' => '9999',
// 'timeout' => 10,
// 'timeout_stream' => 3,
// 'deviceType' => 'IOT.SMARTBULB',
// ],

];
Loading