#!/usr/bin/perl

# Look for power usage dropping beneath 2kW, then initiate an asterisk call and
# exit.

use strict;
use FileHandle;

my $subclient = "/usr/local/bin/mosquitto_sub -t sensors/cc128/ch1 -q 2";

open(SUB,"$subclient|");
SUB->autoflush(1);

my $power;
my $timestamp;
my $current_state = 0;
# Get Messages
while( my $line = <SUB> ) {
	if($line =~ m#([0-9]+) ([0-9]+)#){
		$timestamp = $1;
		$power = $2;
		if($current_state == 0){
			if($power > 2000){
				$current_state = 1;
			}
		}else{
			if($power < 2000){
				system("/usr/local/bin/oven_call.sh");
				close(SUB);
				exit 0;
			}
		}
	}
}

