use of org.openhab.binding.plugwise.PlugwiseBindingProvider in project openhab1-addons by openhab.
the class PlugwiseBinding method scheduleJobs.
private void scheduleJobs(Scheduler scheduler) {
for (PlugwiseBindingProvider provider : providers) {
for (PlugwiseBindingConfigElement element : provider.getIntervalList()) {
PlugwiseCommandType type = element.getCommandType();
if (type.getJobClass() == null) {
continue;
}
if (stick.getDevice(element.getId()) == null) {
logger.debug("The Plugwise device with id {} is not yet defined", element.getId());
// check if the config string really contains a MAC address
Pattern MAC_PATTERN = Pattern.compile("(\\w{16})");
Matcher matcher = MAC_PATTERN.matcher(element.getId());
if (matcher.matches()) {
List<CirclePlus> cps = stick.getDevicesByClass(CirclePlus.class);
if (!cps.isEmpty()) {
CirclePlus cp = cps.get(0);
if (!cp.getMAC().equals(element.getId())) {
// a circleplus has been added/detected and it is not what is in the binding config
PlugwiseDevice device = new Circle(element.getId(), stick, element.getId());
stick.addDevice(device);
logger.debug("Plugwise added Circle with MAC address: {}", element.getId());
}
} else {
logger.warn("Plugwise can not guess the device that should be added. Consider defining it in the openHAB configuration file");
}
} else {
logger.warn("Plugwise can not add a valid device without a proper MAC address. {} can not be used", element.getId());
}
}
if (stick.getDevice(element.getId()) != null) {
String jobName = element.getId() + "-" + type.getJobClass().toString();
if (!isExistingJob(scheduler, jobName)) {
// set up the Quartz jobs
JobDataMap map = new JobDataMap();
map.put(STICK_JOB_DATA_KEY, stick);
map.put(MAC_JOB_DATA_KEY, stick.getDevice(element.getId()).MAC);
JobDetail job = newJob(type.getJobClass()).withIdentity(jobName, "Plugwise-" + provider.toString()).usingJobData(map).build();
Trigger trigger = newTrigger().withIdentity(element.getId() + "-" + type.getJobClass().toString(), "Plugwise-" + provider.toString()).startNow().withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(element.getInterval())).build();
try {
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
logger.error("An exception occurred while scheduling a Plugwise Quartz Job", e);
}
}
} else {
logger.error("Error scheduling a Quartz Job for a non-defined Plugwise device (" + element.getId() + ")");
}
}
}
List<CirclePlus> cps = stick.getDevicesByClass(CirclePlus.class);
if (!cps.isEmpty()) {
CirclePlus cp = cps.get(0);
String jobName = cp.MAC + "-SetCirclePlusClock";
if (!isExistingJob(scheduler, jobName)) {
JobDataMap map = new JobDataMap();
map.put(CirclePlus.CIRCLE_PLUS_JOB_DATA_KEY, cp);
JobDetail job = newJob(SetClockJob.class).withIdentity(jobName, "Plugwise").usingJobData(map).build();
CronTrigger trigger = newTrigger().withIdentity(jobName, "Plugwise").startNow().withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 * * ?")).build();
try {
Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
sched.scheduleJob(job, trigger);
} catch (SchedulerException e) {
logger.error("Error scheduling Circle+ setClock Quartz Job", e);
}
}
}
}
use of org.openhab.binding.plugwise.PlugwiseBindingProvider in project openhab1-addons by openhab.
the class PlugwiseBinding method postUpdate.
/**
* Method to post updates to the OH runtime.
*
*
* @param MAC of the Plugwise device concerned
* @param ctype is the Plugwise Command type
* @param value is the value (to be converted) to post
*/
public void postUpdate(String MAC, PlugwiseCommandType ctype, Object value) {
if (MAC != null && ctype != null && value != null) {
for (PlugwiseBindingProvider provider : providers) {
Set<String> qualifiedItems = provider.getItemNames(MAC, ctype);
// Make sure we also capture those devices that were pre-defined with a friendly name in a .cfg or alike
Set<String> qualifiedItemsFriendly = provider.getItemNames(stick.getDevice(MAC).getName(), ctype);
qualifiedItems.addAll(qualifiedItemsFriendly);
State type = null;
try {
type = createStateForType(ctype, value);
} catch (BindingConfigParseException e) {
logger.error("Error parsing a value {} to a state variable of type {}", value.toString(), ctype.getTypeClass().toString());
}
for (String item : qualifiedItems) {
if (type instanceof State) {
eventPublisher.postUpdate(item, type);
} else {
throw new IllegalClassException("Cannot process update of type " + (type == null ? "null" : type.toString()));
}
}
}
}
}
use of org.openhab.binding.plugwise.PlugwiseBindingProvider in project openhab1-addons by openhab.
the class PlugwiseBinding method internalReceiveCommand.
@Override
protected void internalReceiveCommand(String itemName, Command command) {
PlugwiseBindingProvider provider = findFirstMatchingBindingProvider(itemName);
if (command != null) {
String commandAsString = command.toString();
List<Command> commands = new ArrayList<Command>();
// check if the command is valid for this item by checking if a pw ID exists
String checkID = provider.getPlugwiseID(itemName, command);
if (checkID != null) {
commands.add(command);
} else {
// ooops - command is not defined, but maybe we have something of the same Type (e.g Decimal, String
// types)
// commands = provider.getCommandsByType(itemName, command.getClass());
commands = provider.getAllCommands(itemName);
}
for (Command someCommand : commands) {
String plugwiseID = provider.getPlugwiseID(itemName, someCommand);
PlugwiseCommandType plugwiseCommandType = provider.getPlugwiseCommandType(itemName, someCommand);
if (plugwiseID != null) {
if (plugwiseCommandType != null) {
@SuppressWarnings("unused") boolean result = executeCommand(plugwiseID, plugwiseCommandType, commandAsString);
// Each command is responsible to make sure that a result value for the action is polled from
// the device
// which then will be used to do a postUpdate
// if new commands would be added later on that do not have this possibility, then a kind of
// auto-update has to be performed here below
} else {
logger.error("wrong command type for binding [Item={}, command={}]", itemName, commandAsString);
}
} else {
logger.error("{} is an unrecognised command for Item {}", commandAsString, itemName);
}
}
}
}
Aggregations