Skip to content

Commit

Permalink
Added an execSensor, compliments the execActuator
Browse files Browse the repository at this point in the history
  • Loading branch information
rkoshak committed Jun 6, 2018
1 parent 14fe0c3 commit 9a9a439
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -109,3 +109,7 @@ In Advanced mode it reads the value and stores last five readings in an array. T

Be sure that Adafruits DHT Python library is installed, instructions can be found at
https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated

# Exec Specitics
The exec sensor will periodically execute the given shell script and publish the result or 'ERROR' if the script didn't return a 0 exitcode to the configured destination.
Avoid making the polling period shorter than it takes the script to run in the worst case or else you will run out of threads to run the sensorReporter.
9 changes: 9 additions & 0 deletions default.ini
Expand Up @@ -118,6 +118,15 @@ StateCallback = switchLed
StateCallbackArgs = Actuator1,Actuator2
Poll = -1

[Sensor10]
; Sensor that executes the configured script or program and publishes the restults to the destination
Class = execSensor.execSensor
Type = Exec
Connection = MQTT
Poll = 30
Script = ./iphone.sh 123.45.56.8 fe:dc:ba:98:76:54
Destination = scripts/presence/iphone/results

[Actuator1]
Class = rpiGPIOActuator.rpiGPIOActuator
; The chosen connection must support a register(path, handler) method the actuators can call
Expand Down
74 changes: 74 additions & 0 deletions execSensor.py
@@ -0,0 +1,74 @@
"""
Copyright 2018 Richard Koshak
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Script: execSensor.py
Author: Rich Koshak
Date: June 6, 2018
Purpose: Periodically calls a script/program and publiahes the result
"""

import sys
import time
import os

if os.name == 'posix' and sys.version_info[0] < 3:
import subprocess32 as subprocess
else:
import subprocess

class execSensor:
"""Periodically calls a script/program and publishes the result"""

def __init__(self, publishers, logger, params, sensors, actuators):
"""Sets the script to call and destination for the results"""

self.logger = logger
self.publishers = publishers
self.poll = float(params("Poll"))
self.script = params("Script")
self.dest = params("Destination")
self.startTime = time.time()

"""Parse out the script and arguments, ignoring dangerous characters"""
self.cmdArgs = []
for arg in self.script.split(' '):
if arg.find(';') == -1 or arg.find('|') == -1 or arg.find('//') == -1:
self.cmdArgs.append(arg)

self.results = ""

self.logger.info('----------Configuring execSensor call script {0} and destination {1} with interval {2}'.format(self.script, self.dest, self.poll))
self.checkState

def checkState(self):
"""calls the script and publishes the result"""

self.logger.info('Executing script with the following arguments: {0}'.format(self.cmdArgs))
try:
self.results = subprocess.check_output(self.cmdArgs, shell=False, universal_newlines=True)
self.logger.info('Command results to be published to {0}\n{1}'.format(self.dest, self.results))
except subprocess.CalledProcessError as e:
self.logger.warn('Command returned an error code: {0}\n{1}'.format(e.returncode, e.output))
self.results = 'Error'

self.publishState()

def publishState(self):
"""Publishes the most recent result from the script"""
for conn in self.publishers:
conn.publish(self.results, self.dest)

def cleanup(self):
"""Does nothing"""

0 comments on commit 9a9a439

Please sign in to comment.