Tips: Drifttidsmätare
Posted: Fri Mar 17, 2023 9:45 am
Den som vill veta hur länge en viss device har varit påslagen kan fixa detta genom att först skapa en fil som innehåller de enheters deviceid som man vill hålla koll på, därefter skapar man tre scripts.
C:/apps/nexahome/minute_meter.txt
start.bsh
minute_meter.bsh
minute_meter_today.bsh
Ovanstående script använder sig av metoderna getScriptName(), getParams() samt getParam() som finns i version 4.1.10 och senare.
Updaterad: 2017-02-21
C:/apps/nexahome/minute_meter.txt
Code: Select all
3=0
5=0
Code: Select all
startTimer(minute_meter, 60);
Code: Select all
import java.text.SimpleDateFormat;
scriptName = getScriptName();
timeout = 60;
sumFile = "C:/apps/nexahome/minute_meter.txt";
comment = "Devices has been turned on for x minutes.";
day_script = "minute_meter_today.bsh";
day_file_prefix = "C:/apps/nexahome/minute_meter.";
day_file_date_format = "yyyy-MM-dd";
day_file_suffix = ".txt";
Properties prop = new Properties();
InputStream is = null;
OutputStream os = null;
updated = false;
try {
is = new FileInputStream(sumFile);
prop.load(is);
keys = prop.keys();
while (keys.hasMoreElements()) {
String id = (String)keys.nextElement();
String value = prop.getProperty(id);
long minutes = Long.parseLong(value);
if (deviceIsOn(id)) {
updated = true;
minutes = minutes + 1;
SimpleDateFormat sdf = new SimpleDateFormat(day_file_date_format);
date = sdf.format(Calendar.getInstance().getTime());
execFile(day_script);
execParam(day_file_prefix + date + day_file_suffix);
execParam(id);
}
prop.setProperty(id, Long.toString(minutes));
}
if (updated) {
os = new FileOutputStream(sumFile);
prop.store(os, comment);
}
startTimer(scriptName, timeout);
hideOutput();
} catch (IOException ioe) {
echo(ioe.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ioe) {
echo(ioe.getMessage());
}
}
if (os != null) {
try {
os.close();
} catch (IOException ioe) {
echo(ioe.getMessage());
}
}
}
Code: Select all
dayFile = "";
id = "";
comment = "Devices has been turned on for x minutes today.";
scriptName = getScriptName();
params = getParams(scriptName);
if (params != null) {
var len = Integer.parseInt(params);
if (len >= 2) {
dayFile = getParam(scriptName, 1); // first
id = getParam(scriptName, 2); // second
}
}
if (dayFile != null && dayFile.length() != 0 && id != null && id.length() != 0) {
InputStream is = null;
OutputStream os = null;
try {
updated = false;
Properties prop = new Properties();
File f = new File(dayFile);
if (f.exists()) {
is = new FileInputStream(f);
prop.load(is);
}
String value = prop.getProperty(id);
if (value == null) {
value = "0";
}
long minutes = Long.parseLong(value);
if (deviceIsOn(id)) {
updated = true;
minutes = minutes + 1;
}
prop.setProperty(id, Long.toString(minutes));
if (updated) {
os = new FileOutputStream(f);
prop.store(os, comment);
}
hideOutput();
} catch (IOException ioe) {
echo(ioe.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ioe) {
echo(ioe.getMessage());
}
}
if (os != null) {
try {
os.close();
} catch (IOException ioe) {
echo(ioe.getMessage());
}
}
}
}
Updaterad: 2017-02-21