-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrowbot
executable file
·119 lines (85 loc) · 2.98 KB
/
growbot
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
#!/usr/bin/env perl
=head1 NAME
GrowBot
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 DEPENDENCIES
GrowBot requires Perl version 5.14 or later.
=head1 FEEDBACK
=head2 Reporting Bugs
Report bugs to the GitHub issue tracker at:
https://github.com/sandain/growbot/issues
=head1 AUTHOR - Jason M. Wood
Email [email protected]
=head1 COPYRIGHT AND LICENSE
Copyright (c) 2020-2022 Jason M. Wood <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
=cut
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use v5.14;
use DateTime;
use lib qw (lib);
use GrowBot;
# Set the script name.
$0 = "growbot";
# The configuration file to load.
my $configFile = "config.json";
# Create a PID file.
open my $pid, ">", "growbot.pid" or die "Could not write to PID file: $!";
print $pid "$$\n";
close $pid;
# Create a new GrowBot using the configuration file.
my $growbot = GrowBot->new ($configFile);
# Capture SIGINT, and SIGTERM to allow for a graceful exit.
$SIG{INT} = $SIG{TERM} = sub {
foreach my $device ($growbot->devices) {
$growbot->enqueueAction (
$device,
"Close",
DateTime->now (time_zone => $growbot->timeZone),
100
);
}
};
printf "%s %s\n", $growbot->name, $growbot->version;
# Start the GrowBot.
$growbot->start;
# Enqueue default actions for each device.
foreach my $device ($growbot->devices) {
foreach my $action (@{$growbot->{config}{Devices}{$device}{DefaultActions}}) {
$growbot->enqueueAction (
$device,
$action,
DateTime->now (time_zone => $growbot->timeZone),
0
);
}
}
# Wait for the Growbot to finish.
$growbot->wait;
# Close the GrowBot.
$growbot->close;
printf "Done.\n";
# Remove the PID file.
unlink "growbot.pid" or die "Count not remove PID file: $!";