Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aiordanescu
Created December 28, 2015 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aiordanescu/b0ca7658916f45cf2d68 to your computer and use it in GitHub Desktop.
Save aiordanescu/b0ca7658916f45cf2d68 to your computer and use it in GitHub Desktop.
[AUTH]
USER = "USERNAME"
PASS = "PASSWORD"
LOGIN_URL = "https://apps.lifeshield.com/myingridStruts/login.do"
INST_URL = "https://apps.lifeshield.com/myingridStruts/setArmState.do?armstate=armstayinst"
AWAY_URL = "https://apps.lifeshield.com/myingridStruts/setArmState.do?armstate=armaway"
STAY_URL = "https://apps.lifeshield.com/myingridStruts/setArmState.do?armstate=armstay"
DISARM_URL = "https://apps.lifeshield.com/myingridStruts/setArmState.do?armstate=disarm"
[CONFIG]
BASE = "/var/www/html/lifeshield"
SLEEP = 1
LOCK_FILE = lock
FAULTED_URL = "https://apps.lifeshield.com/myingridStruts/getFaultedItems.do"
<?php
function lock_found () {
// Get global config
global $config;
exit(1);
}
function check_lock () {
// Get global config
global $config;
// Check if locked file is in place
// if it is there, you must make sure your
// account is not locked else just
if(file_exists($config['LOCK_FILE'])) {
lock_found();
}
}
function ls_login () {
// Get global config
global $config;
// Check lock file before every login
check_lock();
// Get AUTH variables from config
$ini_array = parse_ini_file("config.ini.php", true);
$auth = ($ini_array['AUTH']);
$loginurl = ($auth['LOGIN_URL']);
$username = ($auth['USER']);
$password = ($auth['PASS']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginurl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=' . $username .'&password=' . $password . '&apiVersion=LV3_R4&responseFormat=json');
// get target url contents:
$content = curl_exec($ch);
curl_close ($ch);
$result=json_decode($content);
$status=$result->status;
return $status->{'@code'};
}
function armstate () {
// Get global config
global $config;
// Check lock file before every login
check_lock();
// Get AUTH variables from config
$ini_array = parse_ini_file("config.ini.php", true);
$auth = ($ini_array['AUTH']);
$loginurl = ($auth['LOGIN_URL']);
$username = ($auth['USER']);
$password = ($auth['PASS']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginurl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=' . $username .'&password=' . $password . '&apiVersion=LV3_R4&responseFormat=json');
// get target url contents:
$content = curl_exec($ch);
curl_close ($ch);
$result=json_decode($content);
$data=$result->data;
return $data->armstate;
}
function disarm () {
// Get global config
global $config;
// Check lock file before every login
check_lock();
// Get AUTH variables from config
$ini_array = parse_ini_file("config.ini.php", true);
$auth = ($ini_array['AUTH']);
$targeturl = ($auth['DISARM_URL']);
// initialize curl instance
$ch = curl_init();
// use cookie
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
// enable return transfer:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// set URL to target content:
curl_setopt($ch, CURLOPT_URL, $targeturl);
// get target url contents:
$content = curl_exec($ch);
curl_close ($ch);
echo $content;
}
function arm_stay () {
// Get global config
global $config;
// Check lock file before every login
check_lock();
// Get AUTH variables from config
$ini_array = parse_ini_file("config.ini.php", true);
$auth = ($ini_array['AUTH']);
$loginurl = ($auth['LOGIN_URL']);
$targeturl = ($auth['STAY_URL']);
// initialize curl instance
$ch = curl_init();
// use cookie
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
// enable return transfer:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// set URL to target content:
curl_setopt($ch, CURLOPT_URL, $targeturl);
// get target url contents:
$content = curl_exec($ch);
curl_close ($ch);
echo $content;
}
function arm_away () {
// Get global config
global $config;
// Check lock file before every login
check_lock();
// Get AUTH variables from config
$ini_array = parse_ini_file("config.ini.php", true);
$auth = ($ini_array['AUTH']);
$loginurl = ($auth['LOGIN_URL']);
$targeturl = ($auth['AWAY_URL']);
// initialize curl instance
$ch = curl_init();
// use cookie
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
// enable return transfer:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// set URL to target content:
curl_setopt($ch, CURLOPT_URL, $targeturl);
// get target url contents:
$content = curl_exec($ch);
curl_close ($ch);
echo $content;
}
function arm_instant () {
// Get global config
global $config;
// Check lock file before every login
check_lock();
// Get AUTH variables from config
$ini_array = parse_ini_file("config.ini.php", true);
$auth = ($ini_array['AUTH']);
$loginurl = ($auth['LOGIN_URL']);
$targeturl = ($auth['INST_URL']);
// initialize curl instance
$ch = curl_init();
// use cookie
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
// enable return transfer:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// set URL to target content:
curl_setopt($ch, CURLOPT_URL, $targeturl);
// get target url contents:
$content = curl_exec($ch);
curl_close ($ch);
echo $content;
}
if(function_exists($_GET['f'])) {
// Get settings from ini file
$ini_array = parse_ini_file("config.ini.php", true);
// Create global CONFIG array
$config = ($ini_array['CONFIG']);
// Change to base directory
chdir ($config['BASE']);
// Check if lock file exists
check_lock();
// Make sure logged in
$content = ls_login();
$returned = $_GET['f']();
}
echo $returned
?>
/**
* Local HTTP GET
*
* Author: Alex Iordanescu
*
* Date: 2014-04-14
*/
import groovy.json.JsonSlurper
preferences {
input("ip", "string", title:"IP Address", description: "192.168.1.10", required: true, displayDuringSetup: true)
input("port", "string", title:"Port", description: "80", defaultValue: "80", required: true, displayDuringSetup: true)
input("username", "string", title:"Username", description: "", required: false, displayDuringSetup: true)
input("password", "password", title:"Password", description: "", required: false, displayDuringSetup: true)
input("uri", "string", title:"URI", description: "URI", required: true, displayDuringSetup: true)
}
metadata {
// Automatically generated. Make future change here.
definition (name: "Local HTTP GET", namespace: "alex", author: "Alex Iordanescu") {
capability "Actuator"
capability "Switch"
capability "Momentary"
capability "Sensor"
}
// simulator metadata
simulator {
}
// UI tile definitions
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
state "off", label: 'Push', action: "momentary.push", backgroundColor: "#ffffff", nextState: "on"
state "on", label: 'Push', action: "momentary.push", backgroundColor: "#53a7c0"
}
main "switch"
details "switch"
}
}
def parse(String description) {
}
def push() {
sendEvent(name: "switch", value: "on", isStateChange: true, display: false)
sendEvent(name: "switch", value: "off", isStateChange: true, display: false)
sendEvent(name: "momentary", value: "pushed", isStateChange: true)
postAction(uri)
}
def on() {
push()
}
def off() {
push()
}
// ------------------------------------------------------------------
private postAction(uri){
setDeviceNetworkId(ip,port)
def userpass = encodeCredentials(username, password)
def headers = getHeader(userpass)
def hubAction = new physicalgraph.device.HubAction(
method: "GET",
path: uri,
headers: headers
)
log.debug("Executing hubAction on " + getHostAddress())
hubAction
}
// ------------------------------------------------------------------
// Helper methods
// ------------------------------------------------------------------
def parseDescriptionAsMap(description) {
description.split(",").inject([:]) { map, param ->
def nameAndValue = param.split(":")
map += [(nameAndValue[0].trim()):nameAndValue[1].trim()]
}
}
private encodeCredentials(username, password){
log.debug "Encoding credentials"
def userpassascii = "${username}:${password}"
def userpass = "Basic " + userpassascii.encodeAsBase64().toString()
//log.debug "ASCII credentials are ${userpassascii}"
//log.debug "Credentials are ${userpass}"
return userpass
}
private getHeader(userpass){
log.debug "Getting headers"
def headers = [:]
headers.put("HOST", getHostAddress())
headers.put("Authorization", userpass)
//log.debug "Headers are ${headers}"
return headers
}
private delayAction(long time) {
new physicalgraph.device.HubAction("delay $time")
}
private setDeviceNetworkId(ip,port){
def iphex = convertIPtoHex(ip)
def porthex = convertPortToHex(port)
device.deviceNetworkId = "$iphex:$porthex"
log.debug "Device Network Id set to ${iphex}:${porthex}"
}
private getHostAddress() {
return "${ip}:${port}"
}
private String convertIPtoHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
return hex
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
return hexport
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment