import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import java.lang.Integer
var DateTime startedAt = new DateTime()
var Boolean homeIsEmptyNotificationWasSent = false
rule "TvReceiver"
when
Item GI received command OFF
then
var String baseUrl = "http://192.168.0.198/"
var String command
command = "curl -s " + baseUrl + "psw_check.html --referer " + baseUrl + " --data username=xxx&password=xxx"
executeCommandLine(command, 2000)
createTimer(now.plusSeconds(8)) [|
command = "curl -s " + baseUrl + "system_shutdown.html --referer " + baseUrl + " --data shutdown=1"
executeCommandLine(command, 2000)
postUpdate(GI, OFF)
]
end
rule "TV"
when
LgTvPower received command OFF
then
postUpdate(LgTvState, OFF)
end
rule "Debug"
when
System started or
Time cron "*/20 * * * * ?"
then
logInfo("Debug", iPhonePavel.toString())
logInfo("Debug", iPhoneAlina.toString())
end
rule "Torrent"
when
System started or
Time cron "0 * * * * ?"
then
var String json = executeCommandLine("/home/pi/dlna-files.sh", 1000)
var DecimalType total = new DecimalType(transform("JSONPATH", "$.total", json))
logInfo("Torrent", dlnaFilesCount.toString() + ", json=" + json)
if (dlnaFilesCount.state != NULL && dlnaFilesCount.state < total) {
var String lastFile = transform("JSONPATH", "$.lastFile", json)
sendBroadcastNotification("Файл скачался " + lastFile) // File loaded
}
postUpdate(dlnaFilesCount, total)
end
rule "ServerInfo"
when
System started or
Time cron "*/30 * * * * ?"
then
var String jsonTemp = executeCommandLine("/home/pi/temp.sh", 1000)
var DecimalType cpuTemp = new DecimalType(transform("JSONPATH", "$.cpu", jsonTemp))
postUpdate(serverCpuTemp, cpuTemp)
var DecimalType gpuTemp = new DecimalType(transform("JSONPATH", "$.gpu", jsonTemp))
postUpdate(serverGpuTemp, gpuTemp)
var String jsonIp = executeCommandLine("/home/pi/wget.sh https://api.ipify.org/?format=json", 2000)
if (jsonIp == "") {
jsonIp = '{"ip":"0.0.0.0"}'
}
var String ip = transform("JSONPATH", "$.ip", jsonIp)
postUpdate(serverIp, ip)
end
rule "WhoAtHome"
when
System started or
Item whoAtHomeRepeat received command ON or
Time cron "0 * * * * ?"
then
var String curlCmd = "curl -s http://192.168.0.1/userRpm/WlanStationRpm.htm --referer http://192.168.0.1 --cookie Authorization=Basic%20xxxx%3D%3D"
var String html = executeCommandLine(curlCmd, 2000)
var Boolean isRepeat = receivedCommand != null
var String state
// PAVEL
state = transform("JS", "mac_online.js", "xx-xx-xx-xx-xx-xx" + html)
if (state == "BadRequest") {
if (!isRepeat) {
logInfo("WhoAtHome", "state=BadRequest, timer created")
createTimer(now.plusSeconds(10)) [|
sendCommand(whoAtHomeRepeat, ON)
]
}
else {
logInfo("WhoAtHome", "state=BadRequest, timer loop detected")
}
return false
}
if (iPhonePavel.state != state) {
postUpdate(isAnybodyHome, new DateTimeType())
}
postUpdate(iPhonePavel, state)
// ALINA
state = transform("JS", "mac_online.js", "xx-xx-xx-xx-xx-xx" + html)
if (iPhoneAlina.state != state) {
postUpdate(isAnybodyHome, new DateTimeType())
}
postUpdate(iPhoneAlina, state)
end
rule "HomeIsEmpty"
when
Time cron "25 */5 * * * ?"
then
var Boolean isAnybodyHomeBoolean = Phones?.members.filter(s | s.state == "Online").size > 0
var DateTime isAnybodyHomeDt = new DateTime((isAnybodyHome.state as DateTimeType).calendar.timeInMillis)
logInfo("HomeIsEmpty", "isAnybodyHomeBoolean = " + if(isAnybodyHomeBoolean) "true" else "false")
logInfo("HomeIsEmpty", "isAnybodyHomeDt = " + isAnybodyHomeDt.toString())
if (isAnybodyHomeBoolean) {
homeIsEmptyNotificationWasSent = false
}
if (!isAnybodyHomeBoolean && isAnybodyHomeDt.plusMinutes(3).isBefore(now)) {
if (!homeIsEmptyNotificationWasSent) {
var String time = new DateTimeType().format("%1$tH:%1$tM")
sendBroadcastNotification("Все ушли из дома " + time) // All is out from home
homeIsEmptyNotificationWasSent = true
}
if (GI.state == ON) sendCommand(GI, OFF)
if (LgTvState.state == ON) sendCommand(LgTvPower, OFF)
}
end
rule "Weather"
when
System started or
Time cron "10 */2 * * * ?"
then
if (weatherTemperatureChartPeriod.state == NULL) {
postUpdate(weatherTemperatureChartPeriod, 1);
}
var String curlCmd = "curl -s \"http://site.com/api/sensorsValues?sensors=2176,2177&uuid=xxx&api_key=xxx\""
var String json = executeCommandLine(curlCmd, 2000)
var String value
value = transform("JS", "weather.js", "temperature|" + json)
postUpdate(weatherTemperature, value)
postUpdate(weatherTemperatureMax, weatherTemperature.maximumSince(now.toDateMidnight).state)
postUpdate(weatherTemperatureMin, weatherTemperature.minimumSince(now.toDateMidnight).state)
value = transform("JS", "weather.js", "humidity|" + json)
postUpdate(weatherHumidity, value)
end