
org.ardulink.camel.ArdulinkComponent Maven / Gradle / Ivy
/**
Copyright 2013 project Ardulink http://www.ardulink.org/
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.
*/
package org.ardulink.camel;
import static java.lang.Character.toUpperCase;
import static java.lang.Integer.parseInt;
import static java.util.Collections.list;
import static java.util.stream.Collectors.toCollection;
import static org.ardulink.core.Pin.analogPin;
import static org.ardulink.core.Pin.digitalPin;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
import java.util.StringTokenizer;
import org.apache.camel.Endpoint;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.support.DefaultComponent;
import org.ardulink.core.Pin;
/**
* [ardulinktitle] [ardulinkversion]
*
* project Ardulink http://www.ardulink.org/
*
* [adsense]
*
*/
@UriEndpoint(scheme = "ardulink", syntax = "ardulink:type", title = "Ardulink Link")
public class ArdulinkComponent extends DefaultComponent {
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
EndpointConfig config = new EndpointConfig().type(remaining)
.listenTo(parsePins(getOptional(parameters, "listenTo").orElse(""))).linkParams(parameters);
parameters.clear();
ArdulinkEndpoint endpoint = new ArdulinkEndpoint(uri, this, config);
setProperties(endpoint, parameters);
return endpoint;
}
private Collection parsePins(String pinsString) {
return list(new StringTokenizer(pinsString, ",")).stream() //
.map(String::valueOf) //
.map(String::trim) //
.map(ArdulinkComponent::toPin) //
.collect(toCollection(LinkedHashSet::new));
}
private static Pin toPin(String pin) {
if (pin.length() >= 2) {
char pinType = toUpperCase(pin.charAt(0));
int num = parseInt(pin.substring(1));
if (pinType == 'A') {
return analogPin(num);
} else if (pinType == 'D') {
return digitalPin(num);
}
}
throw new IllegalStateException("Cannot parse " + pin + " as pin");
}
private Optional getOptional(Map parameters, String key) {
return parameters.containsKey(key) ? Optional.of(String.valueOf(parameters.remove(key))) : Optional.empty();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy