#!/bin/bash
# PODNAME: cheerlights
# ABSTRACT: make hypnocube respond to cheerlights

# to change global cheerlights colors
# http://www.cheerlights.com/control-cheerlights
# Example Tweet: @CheerLights Paint the town red

CUBE=/tmp/hypnocube

SLEEP=60
if [ "$1" != "" ] ; then
  SLEEP=$1
fi

lastcolor=""
while true
do
  # let us know that things are still working
#  echo "clear black ; update" >>/tmp/hypnocube
#  sleep 0.5
  # fetch the cheelightss status and update the hypnocube
  color=`curl -s http://api.thingspeak.com/channels/1417/field/1/last.json | perl -ne '/.*?"field1":"(.*?)"/ ; print "$1";'`
  if [ "$color" != "$lastcolor" ] ; then
    date=`date`
    echo "$date : $color"
    lastcolor=$color
  fi
  echo "sphere $color ; update" >> $CUBE
  # and give it a chance to change
  sleep $SLEEP
done

# perl pod may get added so lets exit before thats an issue
exit ;

__END__

=pod

=encoding UTF-8

=head1 NAME

cheerlights - make hypnocube respond to cheerlights

=head1 VERSION

version 1.900.400

=head1 AUTHOR

Kevin Mulholland <moodfarm@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Kevin Mulholland.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
