@@ -9,8 +9,8 @@ discard block |
||
| 9 | 9 | Route4Me::setApiKey(Constants::API_KEY); |
| 10 | 10 | |
| 11 | 11 | $optimizationParameters = [ |
| 12 | - 'start_date' => '2019-10-15', |
|
| 13 | - 'end_date' => '2019-10-20' |
|
| 12 | + 'start_date' => '2019-10-15', |
|
| 13 | + 'end_date' => '2019-10-20' |
|
| 14 | 14 | ]; |
| 15 | 15 | |
| 16 | 16 | $optimizationProblem = new OptimizationProblem(); |
@@ -18,5 +18,5 @@ discard block |
||
| 18 | 18 | $optimizations = $optimizationProblem->get($optimizationParameters); |
| 19 | 19 | |
| 20 | 20 | foreach ($optimizations as $optimization) { |
| 21 | - echo 'Optimization problem ID -> '.$optimization->optimization_problem_id.'<br>'; |
|
| 21 | + echo 'Optimization problem ID -> '.$optimization->optimization_problem_id.'<br>'; |
|
| 22 | 22 | } |
@@ -21,22 +21,22 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | $addresses = []; |
| 23 | 23 | foreach ($json as $address) { |
| 24 | - $addresses[] = Address::fromArray($address); |
|
| 24 | + $addresses[] = Address::fromArray($address); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | $parameters = RouteParameters::fromArray([ |
| 28 | - 'algorithm_type' => Algorithmtype::CVRP_TW_SD, |
|
| 29 | - 'route_name' => 'Multiple Depot, Multiple Driver with 24 Stops, Time Window', |
|
| 30 | - 'route_date' => time() + 24 * 60 * 60, |
|
| 31 | - 'route_time' => 60 * 60 * 7, |
|
| 32 | - 'distance_unit' => DistanceUnit::MILES, |
|
| 33 | - 'device_type' => DeviceType::WEB, |
|
| 34 | - 'optimize' => OptimizationType::DISTANCE, |
|
| 35 | - 'metric' => Metric::GEODESIC, |
|
| 36 | - 'route_max_duration' => 86400, |
|
| 37 | - 'travel_mode' => TravelMode::DRIVING, |
|
| 38 | - 'vehicle_capacity' => 1, |
|
| 39 | - 'vehicle_max_distance_mi' => 10000, |
|
| 28 | + 'algorithm_type' => Algorithmtype::CVRP_TW_SD, |
|
| 29 | + 'route_name' => 'Multiple Depot, Multiple Driver with 24 Stops, Time Window', |
|
| 30 | + 'route_date' => time() + 24 * 60 * 60, |
|
| 31 | + 'route_time' => 60 * 60 * 7, |
|
| 32 | + 'distance_unit' => DistanceUnit::MILES, |
|
| 33 | + 'device_type' => DeviceType::WEB, |
|
| 34 | + 'optimize' => OptimizationType::DISTANCE, |
|
| 35 | + 'metric' => Metric::GEODESIC, |
|
| 36 | + 'route_max_duration' => 86400, |
|
| 37 | + 'travel_mode' => TravelMode::DRIVING, |
|
| 38 | + 'vehicle_capacity' => 1, |
|
| 39 | + 'vehicle_max_distance_mi' => 10000, |
|
| 40 | 40 | ]); |
| 41 | 41 | |
| 42 | 42 | $optimizationParams = new OptimizationProblemParams(); |
@@ -46,11 +46,11 @@ discard block |
||
| 46 | 46 | $problem = OptimizationProblem::optimize($optimizationParams); |
| 47 | 47 | |
| 48 | 48 | foreach ((array) $problem as $key => $value) { |
| 49 | - if (is_string($value)) { |
|
| 50 | - echo $key.' --> '.$value.'<br>'; |
|
| 51 | - } else { |
|
| 52 | - echo "************ $key ************* <br>"; |
|
| 53 | - Route4Me::simplePrint((array) $value, true); |
|
| 54 | - echo '******************************* <br>'; |
|
| 55 | - } |
|
| 49 | + if (is_string($value)) { |
|
| 50 | + echo $key.' --> '.$value.'<br>'; |
|
| 51 | + } else { |
|
| 52 | + echo "************ $key ************* <br>"; |
|
| 53 | + Route4Me::simplePrint((array) $value, true); |
|
| 54 | + echo '******************************* <br>'; |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | $addresses = []; |
| 23 | 23 | foreach ($jsonAddresses as $address) { |
| 24 | - $addresses[] = Address::fromArray($address); |
|
| 24 | + $addresses[] = Address::fromArray($address); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | $jsonDepots = json_decode(file_get_contents('./depots.json'), true); |
@@ -29,21 +29,21 @@ discard block |
||
| 29 | 29 | // List of depots |
| 30 | 30 | $depots = []; |
| 31 | 31 | foreach ($jsonDepots as $depot) { |
| 32 | - $depots[] = Address::fromArray($depot); |
|
| 32 | + $depots[] = Address::fromArray($depot); |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | $parameters = RouteParameters::fromArray([ |
| 36 | - 'route_name' => 'Multiple Depots Seprate Section '.date('Y-m-d H:i'), |
|
| 37 | - 'algorithm_type' => AlgorithmType::CVRP_TW_MD, |
|
| 38 | - 'distance_unit' => DistanceUnit::MILES, |
|
| 39 | - 'device_type' => DeviceType::WEB, |
|
| 40 | - 'optimize' => OptimizationType::TIME, |
|
| 41 | - 'metric' => Metric::GEODESIC, |
|
| 42 | - 'route_max_duration' => 86400 * 2, |
|
| 43 | - 'travel_mode' => TravelMode::DRIVING, |
|
| 44 | - 'vehicle_capacity' => 50, |
|
| 45 | - 'vehicle_max_distance_mi' => 10000, |
|
| 46 | - 'parts' => 50, |
|
| 36 | + 'route_name' => 'Multiple Depots Seprate Section '.date('Y-m-d H:i'), |
|
| 37 | + 'algorithm_type' => AlgorithmType::CVRP_TW_MD, |
|
| 38 | + 'distance_unit' => DistanceUnit::MILES, |
|
| 39 | + 'device_type' => DeviceType::WEB, |
|
| 40 | + 'optimize' => OptimizationType::TIME, |
|
| 41 | + 'metric' => Metric::GEODESIC, |
|
| 42 | + 'route_max_duration' => 86400 * 2, |
|
| 43 | + 'travel_mode' => TravelMode::DRIVING, |
|
| 44 | + 'vehicle_capacity' => 50, |
|
| 45 | + 'vehicle_max_distance_mi' => 10000, |
|
| 46 | + 'parts' => 50, |
|
| 47 | 47 | ]); |
| 48 | 48 | |
| 49 | 49 | $optimizationParams = new OptimizationProblemParams(); |
@@ -18,13 +18,13 @@ |
||
| 18 | 18 | |
| 19 | 19 | // Get route manifest |
| 20 | 20 | $params = [ |
| 21 | - 'directions' => 1, |
|
| 22 | - 'route_id' => $route_id, |
|
| 21 | + 'directions' => 1, |
|
| 22 | + 'route_id' => $route_id, |
|
| 23 | 23 | ]; |
| 24 | 24 | |
| 25 | 25 | $route = Route::getRoutes($params); |
| 26 | 26 | |
| 27 | 27 | foreach ($route->addresses as $addr1) { |
| 28 | - Route4Me::simplePrint((array) $addr1, true); |
|
| 29 | - echo '<br>'; |
|
| 28 | + Route4Me::simplePrint((array) $addr1, true); |
|
| 29 | + echo '<br>'; |
|
| 30 | 30 | } |
@@ -22,23 +22,23 @@ discard block |
||
| 22 | 22 | $addresses = []; |
| 23 | 23 | |
| 24 | 24 | foreach ($json as $address) { |
| 25 | - $addresses[] = Address::fromArray($address); |
|
| 25 | + $addresses[] = Address::fromArray($address); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | $parameters = RouteParameters::fromArray([ |
| 29 | - 'algorithm_type' => Algorithmtype::CVRP_TW_SD, |
|
| 30 | - 'route_name' => 'Multiple Depot, Multiple Driver, Time Window', |
|
| 31 | - 'route_date' => time() + 24 * 60 * 60, |
|
| 32 | - 'route_time' => 60 * 60 * 7, |
|
| 33 | - 'rt' => true, |
|
| 34 | - 'distance_unit' => DistanceUnit::MILES, |
|
| 35 | - 'device_type' => DeviceType::WEB, |
|
| 36 | - 'optimize' => OptimizationType::TIME, |
|
| 37 | - 'metric' => Metric::GEODESIC, |
|
| 38 | - 'route_max_duration' => 86400 * 3, |
|
| 39 | - 'travel_mode' => TravelMode::DRIVING, |
|
| 40 | - 'vehicle_capacity' => 99, |
|
| 41 | - 'vehicle_max_distance_mi' => 99999, |
|
| 29 | + 'algorithm_type' => Algorithmtype::CVRP_TW_SD, |
|
| 30 | + 'route_name' => 'Multiple Depot, Multiple Driver, Time Window', |
|
| 31 | + 'route_date' => time() + 24 * 60 * 60, |
|
| 32 | + 'route_time' => 60 * 60 * 7, |
|
| 33 | + 'rt' => true, |
|
| 34 | + 'distance_unit' => DistanceUnit::MILES, |
|
| 35 | + 'device_type' => DeviceType::WEB, |
|
| 36 | + 'optimize' => OptimizationType::TIME, |
|
| 37 | + 'metric' => Metric::GEODESIC, |
|
| 38 | + 'route_max_duration' => 86400 * 3, |
|
| 39 | + 'travel_mode' => TravelMode::DRIVING, |
|
| 40 | + 'vehicle_capacity' => 99, |
|
| 41 | + 'vehicle_max_distance_mi' => 99999, |
|
| 42 | 42 | ]); |
| 43 | 43 | |
| 44 | 44 | $optimizationParams = new OptimizationProblemParams(); |
@@ -48,11 +48,11 @@ discard block |
||
| 48 | 48 | $problem = OptimizationProblem::optimize($optimizationParams); |
| 49 | 49 | |
| 50 | 50 | foreach ((array) $problem as $key => $value) { |
| 51 | - if (is_string($value)) { |
|
| 52 | - echo $key.' --> '.$value.'<br>'; |
|
| 53 | - } else { |
|
| 54 | - echo "************ $key ************* <br>"; |
|
| 55 | - Route4Me::simplePrint((array) $value, true); |
|
| 56 | - echo '******************************* <br>'; |
|
| 57 | - } |
|
| 51 | + if (is_string($value)) { |
|
| 52 | + echo $key.' --> '.$value.'<br>'; |
|
| 53 | + } else { |
|
| 54 | + echo "************ $key ************* <br>"; |
|
| 55 | + Route4Me::simplePrint((array) $value, true); |
|
| 56 | + echo '******************************* <br>'; |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -19,18 +19,18 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | $addresses = []; |
| 21 | 21 | foreach ($json as $address) { |
| 22 | - $addresses[] = Address::fromArray($address); |
|
| 22 | + $addresses[] = Address::fromArray($address); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | $parameters = RouteParameters::fromArray([ |
| 26 | - 'algorithm_type' => Algorithmtype::TSP, |
|
| 27 | - 'route_name' => 'Single Driver Multiple TimeWindows 12 Stops', |
|
| 28 | - 'route_date' => time() + 24 * 60 * 60, |
|
| 29 | - 'route_time' => 5 * 3600 + 30 * 60, |
|
| 30 | - 'distance_unit' => DistanceUnit::MILES, |
|
| 31 | - 'device_type' => DeviceType::WEB, |
|
| 32 | - 'optimize' => OptimizationType::DISTANCE, |
|
| 33 | - 'metric' => Metric::GEODESIC, |
|
| 26 | + 'algorithm_type' => Algorithmtype::TSP, |
|
| 27 | + 'route_name' => 'Single Driver Multiple TimeWindows 12 Stops', |
|
| 28 | + 'route_date' => time() + 24 * 60 * 60, |
|
| 29 | + 'route_time' => 5 * 3600 + 30 * 60, |
|
| 30 | + 'distance_unit' => DistanceUnit::MILES, |
|
| 31 | + 'device_type' => DeviceType::WEB, |
|
| 32 | + 'optimize' => OptimizationType::DISTANCE, |
|
| 33 | + 'metric' => Metric::GEODESIC, |
|
| 34 | 34 | ]); |
| 35 | 35 | |
| 36 | 36 | $optimizationParams = new OptimizationProblemParams(); |
@@ -40,11 +40,11 @@ discard block |
||
| 40 | 40 | $problem = OptimizationProblem::optimize($optimizationParams); |
| 41 | 41 | |
| 42 | 42 | foreach ((array) $problem as $key => $value) { |
| 43 | - if (is_string($value)) { |
|
| 44 | - echo $key.' --> '.$value.'<br>'; |
|
| 45 | - } else { |
|
| 46 | - echo "************ $key ************* <br>"; |
|
| 47 | - Route4Me::simplePrint((array) $value, true); |
|
| 48 | - echo '******************************* <br>'; |
|
| 49 | - } |
|
| 43 | + if (is_string($value)) { |
|
| 44 | + echo $key.' --> '.$value.'<br>'; |
|
| 45 | + } else { |
|
| 46 | + echo "************ $key ************* <br>"; |
|
| 47 | + Route4Me::simplePrint((array) $value, true); |
|
| 48 | + echo '******************************* <br>'; |
|
| 49 | + } |
|
| 50 | 50 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | $addresses = []; |
| 23 | 23 | foreach ($json as $address) { |
| 24 | - $addresses[] = Address::fromArray($address); |
|
| 24 | + $addresses[] = Address::fromArray($address); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | $bundling = new AddressBundling(); |
@@ -32,15 +32,15 @@ discard block |
||
| 32 | 32 | $bundling->service_time_rules->additional_items_mode = Enum\AddressBundlingModes\AdditionalItemsMode::KEEP_ORIGINAL; |
| 33 | 33 | |
| 34 | 34 | $parameters = RouteParameters::fromArray([ |
| 35 | - 'algorithm_type' => Algorithmtype::TSP, |
|
| 36 | - 'route_name' => 'Single Driver Multiple TimeWindows 12 Stops', |
|
| 37 | - 'route_date' => time() + 24 * 60 * 60, |
|
| 38 | - 'route_time' => 5 * 3600 + 30 * 60, |
|
| 39 | - 'distance_unit' => DistanceUnit::MILES, |
|
| 40 | - 'device_type' => DeviceType::WEB, |
|
| 41 | - 'optimize' => OptimizationType::DISTANCE, |
|
| 42 | - 'metric' => Metric::GEODESIC, |
|
| 43 | - 'bundling' => $bundling, |
|
| 35 | + 'algorithm_type' => Algorithmtype::TSP, |
|
| 36 | + 'route_name' => 'Single Driver Multiple TimeWindows 12 Stops', |
|
| 37 | + 'route_date' => time() + 24 * 60 * 60, |
|
| 38 | + 'route_time' => 5 * 3600 + 30 * 60, |
|
| 39 | + 'distance_unit' => DistanceUnit::MILES, |
|
| 40 | + 'device_type' => DeviceType::WEB, |
|
| 41 | + 'optimize' => OptimizationType::DISTANCE, |
|
| 42 | + 'metric' => Metric::GEODESIC, |
|
| 43 | + 'bundling' => $bundling, |
|
| 44 | 44 | ]); |
| 45 | 45 | |
| 46 | 46 | $optimizationParams = new OptimizationProblemParams(); |
@@ -50,11 +50,11 @@ discard block |
||
| 50 | 50 | $problem = OptimizationProblem::optimize($optimizationParams); |
| 51 | 51 | |
| 52 | 52 | foreach ((array) $problem as $key => $value) { |
| 53 | - if (is_string($value)) { |
|
| 54 | - echo $key.' --> '.$value.'<br>'; |
|
| 55 | - } else { |
|
| 56 | - echo "************ $key ************* <br>"; |
|
| 57 | - Route4Me::simplePrint((array) $value, true); |
|
| 58 | - echo '******************************* <br>'; |
|
| 59 | - } |
|
| 53 | + if (is_string($value)) { |
|
| 54 | + echo $key.' --> '.$value.'<br>'; |
|
| 55 | + } else { |
|
| 56 | + echo "************ $key ************* <br>"; |
|
| 57 | + Route4Me::simplePrint((array) $value, true); |
|
| 58 | + echo '******************************* <br>'; |
|
| 59 | + } |
|
| 60 | 60 | } |
@@ -9,31 +9,31 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class AddressBundling extends Common |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Address bundling mode |
|
| 14 | - */ |
|
| 15 | - public $mode = AddressBundlingModes\Mode::ADDRESS; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * Address bundling mode parameters: |
|
| 19 | - * - If Mode=3, contains an array of the field names of the Address object; |
|
| 20 | - * - If Mode=4, contains an array of the custom fields of the Address object. |
|
| 21 | - */ |
|
| 22 | - public $mode_params= []; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Address bundling merge mode |
|
| 26 | - */ |
|
| 27 | - public $merge_mode = AddressBundlingModes\MergeMode::KEEP_AS_SEPARATE_DESTINATIONS; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Service time rules of the address bundling |
|
| 31 | - */ |
|
| 32 | - public $service_time_rules; |
|
| 33 | - |
|
| 34 | - public function __construct() |
|
| 35 | - { |
|
| 36 | - $this->service_time_rules = new ServiceTimeRulesClass(); |
|
| 37 | - } |
|
| 12 | + /** |
|
| 13 | + * Address bundling mode |
|
| 14 | + */ |
|
| 15 | + public $mode = AddressBundlingModes\Mode::ADDRESS; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * Address bundling mode parameters: |
|
| 19 | + * - If Mode=3, contains an array of the field names of the Address object; |
|
| 20 | + * - If Mode=4, contains an array of the custom fields of the Address object. |
|
| 21 | + */ |
|
| 22 | + public $mode_params= []; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Address bundling merge mode |
|
| 26 | + */ |
|
| 27 | + public $merge_mode = AddressBundlingModes\MergeMode::KEEP_AS_SEPARATE_DESTINATIONS; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Service time rules of the address bundling |
|
| 31 | + */ |
|
| 32 | + public $service_time_rules; |
|
| 33 | + |
|
| 34 | + public function __construct() |
|
| 35 | + { |
|
| 36 | + $this->service_time_rules = new ServiceTimeRulesClass(); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | 39 | } |
@@ -13,214 +13,214 @@ |
||
| 13 | 13 | |
| 14 | 14 | class MyQ |
| 15 | 15 | { |
| 16 | - /** @var string|null $username contains the username used to authenticate with the MyQ API */ |
|
| 17 | - protected $username = null; |
|
| 18 | - /** @var string|null $password contains the password used to authenticate with the MyQ API */ |
|
| 19 | - protected $password = null; |
|
| 20 | - /** @var string|null $appId is the application ID used to register with the MyQ API */ |
|
| 21 | - protected $appId = 'NWknvuBd7LoFHfXmKNMBcgajXtZEgKUh4V7WNzMidrpUUluDpVYVZx+xT4PCM5Kx'; |
|
| 22 | - //protected $appId = 'Vj8pQggXLhLy0WHahglCD4N1nAkkXQtGYpq2HrHD7H1nvmbT55KqtN6RSF4ILB%2fi'; |
|
| 23 | - /** @var string|null $securityToken is the auth token returned after a successful login */ |
|
| 24 | - protected $securityToken = null; |
|
| 25 | - /** @var string|null $userAgent is the User-Agent header value sent with each API request */ |
|
| 26 | - protected $userAgent = 'Chamberlain/3.4.1'; |
|
| 27 | - /** @var string|null $culture is the API culture code for the API */ |
|
| 28 | - protected $culture = 'en'; |
|
| 29 | - /** @var string|null $contentType is the content type used for all cURL requests */ |
|
| 30 | - protected $contentType = 'application/json'; |
|
| 31 | - /** @var array $headers contain HTTP headers for cURL requests */ |
|
| 32 | - protected $_headers = []; |
|
| 33 | - protected $_deviceId = null; |
|
| 34 | - protected $_locationName = null; |
|
| 35 | - protected $_doorName = null; |
|
| 36 | - protected $_loginUrl = 'https://myqexternal.myqdevice.com/api/v4/User/Validate'; |
|
| 37 | - protected $_getDeviceDetailUrl = 'https://myqexternal.myqdevice.com/api/v4/userdevicedetails/get?&filterOn=true'; |
|
| 38 | - protected $_putDeviceStateUrl = '/api/v4/DeviceAttribute/PutDeviceAttribute'; |
|
| 39 | - /** @var resource|null $_conn is the web connection to the MyQ API */ |
|
| 40 | - protected $_conn = null; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Initializes class. Optionally allows user to override variables. |
|
| 44 | - * |
|
| 45 | - * @param array $params A associative array for overwriting class variables |
|
| 46 | - * |
|
| 47 | - * @return MyQ |
|
| 48 | - */ |
|
| 49 | - public function __construct($params = []) |
|
| 50 | - { |
|
| 51 | - // Overwrite class variables |
|
| 52 | - foreach ($params as $k => $v) { |
|
| 53 | - $this->$k = $v; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - // Initialize cURL request headers |
|
| 57 | - if (0 == sizeof($this->_headers)) { |
|
| 58 | - $this->_headers = [ |
|
| 59 | - 'MyQApplicationId' => $this->appId, |
|
| 60 | - 'Culture' => $this->culture, |
|
| 61 | - 'Content-Type' => $this->contentType, |
|
| 62 | - 'User-Agent' => $this->userAgent, |
|
| 63 | - ]; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // Initialize cURL connection |
|
| 67 | - $this->_init(); |
|
| 68 | - |
|
| 69 | - return $this; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Perform a login request. |
|
| 74 | - * |
|
| 75 | - * @param string|null $username Username to use when logging in |
|
| 76 | - * @param string|null $password Password to use for logging in |
|
| 77 | - * |
|
| 78 | - * @return MyQ |
|
| 79 | - */ |
|
| 80 | - public function login($username = null, $password = null) |
|
| 81 | - { |
|
| 82 | - // Set username/password if not null |
|
| 83 | - if (!is_null($username)) { |
|
| 84 | - $this->username = $username; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - if (!is_null($password)) { |
|
| 88 | - $this->password = $password; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // confirm that we have a valid username/password |
|
| 92 | - $error = []; |
|
| 93 | - if (is_null($this->username)) { |
|
| 94 | - $error[] = 'username'; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - if (is_null($this->password)) { |
|
| 98 | - $error[] = 'password'; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - if (sizeof($error) > 0) { |
|
| 102 | - throw new MyQException('Missing required auth credential: '.implode(',', $error)); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $this->_login(); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - public function getState() |
|
| 109 | - { |
|
| 110 | - $this->_getDetails(); |
|
| 111 | - $timeInState = time() - $this->_doorStateTime; |
|
| 112 | - echo implode(',', [ |
|
| 113 | - $this->_locationName, |
|
| 114 | - $this->_doorName, |
|
| 115 | - $this->_doorState, |
|
| 116 | - (int) $timeInState, |
|
| 117 | - ]); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - public function getDetails() |
|
| 121 | - { |
|
| 122 | - return $this->_getDetails(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - private function _init() |
|
| 126 | - { |
|
| 127 | - if (!isset($this->_conn)) { |
|
| 128 | - $this->_conn = curl_init(); |
|
| 129 | - |
|
| 130 | - curl_setopt_array($this->_conn, [ |
|
| 131 | - CURLOPT_RETURNTRANSFER => true, |
|
| 132 | - CURLOPT_ENCODING => '', |
|
| 133 | - CURLOPT_MAXREDIRS => 10, |
|
| 134 | - CURLOPT_TIMEOUT => 30, |
|
| 135 | - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
| 136 | - CURLOPT_FAILONERROR => true, |
|
| 137 | - CURLOPT_FOLLOWLOCATION => true, |
|
| 138 | - CURLOPT_FRESH_CONNECT => true, |
|
| 139 | - CURLOPT_FORBID_REUSE => true, |
|
| 140 | - CURLOPT_USERAGENT => $this->userAgent, |
|
| 141 | - ]); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $this->_setHeaders(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - private function _setHeaders() |
|
| 148 | - { |
|
| 149 | - $headers = []; |
|
| 150 | - |
|
| 151 | - foreach ($this->_headers as $k => $v) { |
|
| 152 | - $headers[] = "$k: $v"; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - curl_setopt($this->_conn, CURLOPT_HTTPHEADER, $headers); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - private function _login() |
|
| 159 | - { |
|
| 160 | - $this->_init(); |
|
| 161 | - |
|
| 162 | - curl_setopt($this->_conn, CURLOPT_CUSTOMREQUEST, 'POST'); |
|
| 163 | - curl_setopt($this->_conn, CURLOPT_URL, $this->_loginUrl); |
|
| 164 | - |
|
| 165 | - $post = json_encode(['username' => $this->username, 'password' => $this->password]); |
|
| 166 | - |
|
| 167 | - curl_setopt($this->_conn, CURLOPT_POSTFIELDS, $post); |
|
| 168 | - |
|
| 169 | - $output = curl_exec($this->_conn); |
|
| 170 | - |
|
| 171 | - $data = json_decode($output); |
|
| 172 | - |
|
| 173 | - if (false == $data || !isset($data->SecurityToken)) { |
|
| 174 | - throw new MyQException("Error processing login request: $output"); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $this->_headers['SecurityToken'] = $data->SecurityToken; |
|
| 178 | - |
|
| 179 | - return $this; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - private function _getDetails() |
|
| 183 | - { |
|
| 184 | - $this->_init(); |
|
| 185 | - |
|
| 186 | - curl_setopt($this->_conn, CURLOPT_CUSTOMREQUEST, 'GET'); |
|
| 187 | - curl_setopt($this->_conn, CURLOPT_URL, $this->_getDeviceDetailUrl); |
|
| 188 | - |
|
| 189 | - $output = curl_exec($this->_conn); |
|
| 190 | - |
|
| 191 | - $data = json_decode($output); |
|
| 192 | - |
|
| 193 | - if (false == $data || !isset($data->Devices)) { |
|
| 194 | - throw new MyQException("Error fetching device details: $output"); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - // Find our door device ID |
|
| 198 | - foreach ($data->Devices as $device) { |
|
| 199 | - if (false !== stripos($device->MyQDeviceTypeName, 'Gateway')) { |
|
| 200 | - // Find location name |
|
| 201 | - foreach ($device->Attributes as $attr) { |
|
| 202 | - if ('desc' == $attr->AttributeDisplayName) { |
|
| 203 | - $this->_locationName = $attr->Value; |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - $this->_deviceId = $device->MyQDeviceId; |
|
| 209 | - |
|
| 210 | - foreach ($device->Attributes as $attr) { |
|
| 211 | - switch ($attr->AttributeDisplayName) { |
|
| 212 | - case 'desc': |
|
| 213 | - $this->_doorName = $attr->Value; |
|
| 214 | - break; |
|
| 215 | - case 'doorstate': |
|
| 216 | - $this->_doorState = $attr->Value; |
|
| 217 | - // UpdatedTime is a timestamp in ms, so we truncate |
|
| 218 | - $this->_doorStateTime = (int) $attr->UpdatedTime / 1000; |
|
| 219 | - break; |
|
| 220 | - default: |
|
| 221 | - continue; |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - } |
|
| 16 | + /** @var string|null $username contains the username used to authenticate with the MyQ API */ |
|
| 17 | + protected $username = null; |
|
| 18 | + /** @var string|null $password contains the password used to authenticate with the MyQ API */ |
|
| 19 | + protected $password = null; |
|
| 20 | + /** @var string|null $appId is the application ID used to register with the MyQ API */ |
|
| 21 | + protected $appId = 'NWknvuBd7LoFHfXmKNMBcgajXtZEgKUh4V7WNzMidrpUUluDpVYVZx+xT4PCM5Kx'; |
|
| 22 | + //protected $appId = 'Vj8pQggXLhLy0WHahglCD4N1nAkkXQtGYpq2HrHD7H1nvmbT55KqtN6RSF4ILB%2fi'; |
|
| 23 | + /** @var string|null $securityToken is the auth token returned after a successful login */ |
|
| 24 | + protected $securityToken = null; |
|
| 25 | + /** @var string|null $userAgent is the User-Agent header value sent with each API request */ |
|
| 26 | + protected $userAgent = 'Chamberlain/3.4.1'; |
|
| 27 | + /** @var string|null $culture is the API culture code for the API */ |
|
| 28 | + protected $culture = 'en'; |
|
| 29 | + /** @var string|null $contentType is the content type used for all cURL requests */ |
|
| 30 | + protected $contentType = 'application/json'; |
|
| 31 | + /** @var array $headers contain HTTP headers for cURL requests */ |
|
| 32 | + protected $_headers = []; |
|
| 33 | + protected $_deviceId = null; |
|
| 34 | + protected $_locationName = null; |
|
| 35 | + protected $_doorName = null; |
|
| 36 | + protected $_loginUrl = 'https://myqexternal.myqdevice.com/api/v4/User/Validate'; |
|
| 37 | + protected $_getDeviceDetailUrl = 'https://myqexternal.myqdevice.com/api/v4/userdevicedetails/get?&filterOn=true'; |
|
| 38 | + protected $_putDeviceStateUrl = '/api/v4/DeviceAttribute/PutDeviceAttribute'; |
|
| 39 | + /** @var resource|null $_conn is the web connection to the MyQ API */ |
|
| 40 | + protected $_conn = null; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Initializes class. Optionally allows user to override variables. |
|
| 44 | + * |
|
| 45 | + * @param array $params A associative array for overwriting class variables |
|
| 46 | + * |
|
| 47 | + * @return MyQ |
|
| 48 | + */ |
|
| 49 | + public function __construct($params = []) |
|
| 50 | + { |
|
| 51 | + // Overwrite class variables |
|
| 52 | + foreach ($params as $k => $v) { |
|
| 53 | + $this->$k = $v; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + // Initialize cURL request headers |
|
| 57 | + if (0 == sizeof($this->_headers)) { |
|
| 58 | + $this->_headers = [ |
|
| 59 | + 'MyQApplicationId' => $this->appId, |
|
| 60 | + 'Culture' => $this->culture, |
|
| 61 | + 'Content-Type' => $this->contentType, |
|
| 62 | + 'User-Agent' => $this->userAgent, |
|
| 63 | + ]; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // Initialize cURL connection |
|
| 67 | + $this->_init(); |
|
| 68 | + |
|
| 69 | + return $this; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Perform a login request. |
|
| 74 | + * |
|
| 75 | + * @param string|null $username Username to use when logging in |
|
| 76 | + * @param string|null $password Password to use for logging in |
|
| 77 | + * |
|
| 78 | + * @return MyQ |
|
| 79 | + */ |
|
| 80 | + public function login($username = null, $password = null) |
|
| 81 | + { |
|
| 82 | + // Set username/password if not null |
|
| 83 | + if (!is_null($username)) { |
|
| 84 | + $this->username = $username; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + if (!is_null($password)) { |
|
| 88 | + $this->password = $password; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // confirm that we have a valid username/password |
|
| 92 | + $error = []; |
|
| 93 | + if (is_null($this->username)) { |
|
| 94 | + $error[] = 'username'; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + if (is_null($this->password)) { |
|
| 98 | + $error[] = 'password'; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + if (sizeof($error) > 0) { |
|
| 102 | + throw new MyQException('Missing required auth credential: '.implode(',', $error)); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $this->_login(); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + public function getState() |
|
| 109 | + { |
|
| 110 | + $this->_getDetails(); |
|
| 111 | + $timeInState = time() - $this->_doorStateTime; |
|
| 112 | + echo implode(',', [ |
|
| 113 | + $this->_locationName, |
|
| 114 | + $this->_doorName, |
|
| 115 | + $this->_doorState, |
|
| 116 | + (int) $timeInState, |
|
| 117 | + ]); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + public function getDetails() |
|
| 121 | + { |
|
| 122 | + return $this->_getDetails(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + private function _init() |
|
| 126 | + { |
|
| 127 | + if (!isset($this->_conn)) { |
|
| 128 | + $this->_conn = curl_init(); |
|
| 129 | + |
|
| 130 | + curl_setopt_array($this->_conn, [ |
|
| 131 | + CURLOPT_RETURNTRANSFER => true, |
|
| 132 | + CURLOPT_ENCODING => '', |
|
| 133 | + CURLOPT_MAXREDIRS => 10, |
|
| 134 | + CURLOPT_TIMEOUT => 30, |
|
| 135 | + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
| 136 | + CURLOPT_FAILONERROR => true, |
|
| 137 | + CURLOPT_FOLLOWLOCATION => true, |
|
| 138 | + CURLOPT_FRESH_CONNECT => true, |
|
| 139 | + CURLOPT_FORBID_REUSE => true, |
|
| 140 | + CURLOPT_USERAGENT => $this->userAgent, |
|
| 141 | + ]); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $this->_setHeaders(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + private function _setHeaders() |
|
| 148 | + { |
|
| 149 | + $headers = []; |
|
| 150 | + |
|
| 151 | + foreach ($this->_headers as $k => $v) { |
|
| 152 | + $headers[] = "$k: $v"; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + curl_setopt($this->_conn, CURLOPT_HTTPHEADER, $headers); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + private function _login() |
|
| 159 | + { |
|
| 160 | + $this->_init(); |
|
| 161 | + |
|
| 162 | + curl_setopt($this->_conn, CURLOPT_CUSTOMREQUEST, 'POST'); |
|
| 163 | + curl_setopt($this->_conn, CURLOPT_URL, $this->_loginUrl); |
|
| 164 | + |
|
| 165 | + $post = json_encode(['username' => $this->username, 'password' => $this->password]); |
|
| 166 | + |
|
| 167 | + curl_setopt($this->_conn, CURLOPT_POSTFIELDS, $post); |
|
| 168 | + |
|
| 169 | + $output = curl_exec($this->_conn); |
|
| 170 | + |
|
| 171 | + $data = json_decode($output); |
|
| 172 | + |
|
| 173 | + if (false == $data || !isset($data->SecurityToken)) { |
|
| 174 | + throw new MyQException("Error processing login request: $output"); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $this->_headers['SecurityToken'] = $data->SecurityToken; |
|
| 178 | + |
|
| 179 | + return $this; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + private function _getDetails() |
|
| 183 | + { |
|
| 184 | + $this->_init(); |
|
| 185 | + |
|
| 186 | + curl_setopt($this->_conn, CURLOPT_CUSTOMREQUEST, 'GET'); |
|
| 187 | + curl_setopt($this->_conn, CURLOPT_URL, $this->_getDeviceDetailUrl); |
|
| 188 | + |
|
| 189 | + $output = curl_exec($this->_conn); |
|
| 190 | + |
|
| 191 | + $data = json_decode($output); |
|
| 192 | + |
|
| 193 | + if (false == $data || !isset($data->Devices)) { |
|
| 194 | + throw new MyQException("Error fetching device details: $output"); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + // Find our door device ID |
|
| 198 | + foreach ($data->Devices as $device) { |
|
| 199 | + if (false !== stripos($device->MyQDeviceTypeName, 'Gateway')) { |
|
| 200 | + // Find location name |
|
| 201 | + foreach ($device->Attributes as $attr) { |
|
| 202 | + if ('desc' == $attr->AttributeDisplayName) { |
|
| 203 | + $this->_locationName = $attr->Value; |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + $this->_deviceId = $device->MyQDeviceId; |
|
| 209 | + |
|
| 210 | + foreach ($device->Attributes as $attr) { |
|
| 211 | + switch ($attr->AttributeDisplayName) { |
|
| 212 | + case 'desc': |
|
| 213 | + $this->_doorName = $attr->Value; |
|
| 214 | + break; |
|
| 215 | + case 'doorstate': |
|
| 216 | + $this->_doorState = $attr->Value; |
|
| 217 | + // UpdatedTime is a timestamp in ms, so we truncate |
|
| 218 | + $this->_doorStateTime = (int) $attr->UpdatedTime / 1000; |
|
| 219 | + break; |
|
| 220 | + default: |
|
| 221 | + continue; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | 226 | } |