Passed
Push — master ( 487089...443b76 )
by Chris
03:30
created

broadcast_message()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 3
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
use DaveRandom\LibLifxLan\DataTypes\Light\ColorTransition;
4
use DaveRandom\LibLifxLan\DataTypes\Light\HsbkColor;
5
use DaveRandom\LibLifxLan\Encoding\Exceptions\InvalidMessageException;
6
use DaveRandom\LibLifxLan\Encoding\PacketEncoder;
7
use DaveRandom\LibLifxLan\Messages\Device\Commands\SetPower;
8
use DaveRandom\LibLifxLan\Messages\Light\Commands\SetColor;
9
use DaveRandom\LibLifxLan\Messages\Message;
10
use DaveRandom\LibLifxLan\Packet;
11
use DaveRandom\Network\IPEndpoint;
12
use function DaveRandom\LibLifxLan\Examples\udp_create_socket;
1 ignored issue
show
introduced by
The function DaveRandom\LibLifxLan\Examples\udp_create_socket was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
13
14
require __DIR__ . '/00_bootstrap.php';
15
16
// Time in seconds that color transition should take
17
const TRANSITION_TIME = 1;
18
19
// Minimum time in seconds a single color should be used
20
const INTERVAL_MIN = 1;
21
22
// Maximum time in seconds a single color should be used
23
const INTERVAL_MAX = 10;
24
25
// Ranges of values for colors
26
const HUE_MIN = 0;
27
const HUE_MAX = 65535;
28
const SATURATION_MIN = 10000;
29
const SATURATION_MAX = 65535;
30
const BRIGHTNESS_MIN = 10000;
31
const BRIGHTNESS_MAX = 65535;
32
const TEMPERATURE_MIN = 3500;
33
const TEMPERATURE_MAX = 3500;
34
35
$encoder = new PacketEncoder();
36
$socket = udp_create_socket(IPEndpoint::parse(LOCAL_ENDPOINT));
37
$endpoint = IPEndpoint::parse(BROADCAST_ENDPOINT);
38
39
function broadcast_message(PacketEncoder $encoder, $socket, Message $message)
40
{
41
    static $endpoint;
42
43
    try {
44
        $data = $encoder->encodePacket(Packet::createFromMessage($message, SOURCE_ID, null, 0, 0));
45
    } catch (InvalidMessageException $e) {
46
        exit((string)$e);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
47
    }
48
49
    \stream_socket_sendto($socket, $data, 0, (string)($endpoint ?? $endpoint = IPEndpoint::parse(BROADCAST_ENDPOINT)));
50
}
51
52
// Turn lights on
53
broadcast_message($encoder, $socket, new SetPower(65535));
54
55
while (true) {
56
    $hue = \rand(HUE_MIN, HUE_MAX);
57
    $saturation = \rand(SATURATION_MIN, SATURATION_MAX);
58
    $brightness = \rand(BRIGHTNESS_MIN, BRIGHTNESS_MAX);
59
    $temperature = \rand(TEMPERATURE_MIN, TEMPERATURE_MAX);
60
61
    $color = new HsbkColor($hue, $saturation, $brightness, $temperature);
62
63
    broadcast_message($encoder, $socket, new SetColor(new ColorTransition($color, (int)(TRANSITION_TIME * 1000))));
64
65
    \sleep(\rand(INTERVAL_MIN, INTERVAL_MAX));
66
}
67