Anne Nevin
Published © CC BY-SA

Motion Detection Alarm System

Use Reactive Blocks to program a Java application that sends SMS when motion is detected

EasyWork in progress3,556
Motion Detection Alarm System

Things used in this project

Story

Read more

Schematics

FA4ABZ2I8A0YTAT.MEDIUM.jpg

Fa4abz2i8a0ytat.medium

Code

The Java methods added for this alarm system

Java
The code that was manually written for this project is shown here. You will have access to all the code after you download the application within Reactive Blocks.
package br.ref.motiondetection.alarmsystem.component;

import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import com.bitreactive.library.periodictimers.periodictimer.InitParameters;
import com.bitreactive.library.twilio.sms.smssend.SMSSendData;

import no.ntnu.item.arctis.runtime.Block;

public class Component extends Block {


	public String getId() {
		String retVal = "";
		if (cfg.containsKey("Camera-Id")) {
			retVal = (String)cfg.get("Camera-Id");
		}
		return retVal;
	}

	public InitParameters getTimer() {
		return new InitParameters(500);
	}

	private String getTime() {		
		SimpleDateFormat f = new SimpleDateFormat("HH:MM");
		Date d = new Date();
		return f.format(d);
	}

	private long getNowMs() {
		return new Date().getTime();
	}
		
	private Date lastAlarm;
	private Date silenceStart;
	private Date motionStart;
	private long silenceCounter;	
	private long motionCounter;	
	private int alarmCount;

	public boolean handleDetected() {
		boolean retVal = false;
		if (motionCounter == 10) {
			motionStart = new Date();
			if (lastAlarm == null || ((getNowMs() - lastAlarm.getTime()) > 1000*60*5)) {
				lastAlarm = new Date();
				alarmCount++;
				retVal = true;
				if (alarmCount > 100) {
					retVal = false;
				}
			} 				
			silenceCounter = 0;
		}
		motionCounter++;
		return retVal;
	}
	public void handleSilence() {  
		// Do some filtering .....
		if (silenceCounter == 10) {
			silenceStart = new Date();
			motionCounter = 0;
		}
		silenceCounter++;
	}

	
	public String hasDisplay(String s) {
		if (GraphicsEnvironment.isHeadless()) {
			s = null;
		}
		return s;
	}

	
	public void showError(String s) {
		logError(s);
	}

	public String getCaption() {
		return "Bitreactive motion detection";
		
	}
	
	public String detectionStatus() {
		String retVal = getTime();
		
		if (motionCounter > 10) {
			retVal += " motion for " + (getNowMs() - motionStart.getTime())/1000 + "s ";
		} else if (silenceCounter > 10) {			
			retVal += " silence for " + (getNowMs() - silenceStart.getTime())/1000 + "s ";			
		} else {
			retVal += " undecided";			
		}

		retVal += "(" + silenceCounter + "/" + motionCounter + ")";	

		retVal += " #Alarms: " + alarmCount;	
		logInfo(retVal);
		return retVal;
	}
	
	int receiverIdx;
	
	
	public void initReceiverList() {
		receivers = new Vector<String>();
		String rcvList = cfg.get("Twilio-Receiver-List");
		if (rcvList != null && !rcvList.isEmpty()) {				
			String l[] = rcvList.split(",");
			for  (int i = 0;i < l.length;i++) {
				receivers.add(l[i]);
			}
		}
	}
	
	private Vector<String> receivers;
	public java.util.Map<java.lang.String,java.lang.String> cfg;
	public com.bitreactive.library.twilio.sms.smssend.SMSSendData overflowItem;
	public java.awt.image.BufferedImage curImage;
	
	public void prepareSend()
	{
		logInfo("Motion detected, preparing to send SMS to " + receivers.size() + " receivers");
		receiverIdx = 0;
	}
	
	
	public SMSSendData getMessage() {
		if (receivers == null || receiverIdx >= receivers.size()) {
			return null;
		}
		String recipient = receivers.get(receiverIdx++);
		if (recipient == null) {
			return getMessage();			
		}

		SMSSendData smsSendData = new SMSSendData();
		
		
		String accountSid = (String)cfg.get("Twilio-accountSid");
		String authorizationToken =  (String)cfg.get("Twilio-authorizationToken");
		String fromNumber = (String)cfg.get("Twilio-fromNumber"); 		

		boolean isError = false;
		if (accountSid == null || accountSid.equals("")) {
			logWarn("Missing config parameter Twilio-accountSid, unable to send SMS");
			isError = true;
		}

		if (authorizationToken == null || authorizationToken.equals("")) {
			logWarn("Missing config parameter Twilio-authorizationToken, unable to send SMS");
			isError = true;
		}

		
		if (fromNumber == null || fromNumber.equals("")) {
			logWarn("Missing config parameter Twilio-fromNumber, unable to send SMS");
			isError = true;
		}

		if (isError) {
			return null;
		}
		
				
		smsSendData.ToNumber = recipient;
		smsSendData.Message = String.format("Motion sensor alarm detected");
		smsSendData.AccountSid = accountSid;
		smsSendData.AuthorizationToken = authorizationToken;
		smsSendData.FromNumber = fromNumber;		
		
		logInfo("Sending message to " + recipient);
		return smsSendData;
	}

	public void bye() {
		logInfo("Motion detection system has closed down, have a nice day");
	}

	public void overflow(List<SMSSendData> overflowList) {
		for (SMSSendData elem : overflowList) {
			logError("Overflow when trying to send SMS data to " + elem.ToNumber);
		}
	}
	

}

Credits

Anne Nevin

Anne Nevin

1 project • 2 followers

Comments