diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-07-27 14:25:33 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-07-27 14:25:33 +1200 |
commit | cf6440ef1ae4f68dc0cdcb1579f9347a938d8675 (patch) | |
tree | 9a1f95881492da97d195b677a8331cc76011a35a /src/org/traccar/StringProtocolEncoder.java | |
parent | ba7c66163bc0d22a9c1939f2bb0f47e85cfffb9d (diff) | |
download | trackermap-server-cf6440ef1ae4f68dc0cdcb1579f9347a938d8675.tar.gz trackermap-server-cf6440ef1ae4f68dc0cdcb1579f9347a938d8675.tar.bz2 trackermap-server-cf6440ef1ae4f68dc0cdcb1579f9347a938d8675.zip |
Implement Suntech commands support
Diffstat (limited to 'src/org/traccar/StringProtocolEncoder.java')
-rw-r--r-- | src/org/traccar/StringProtocolEncoder.java | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/org/traccar/StringProtocolEncoder.java b/src/org/traccar/StringProtocolEncoder.java new file mode 100644 index 000000000..dcb9ccc0e --- /dev/null +++ b/src/org/traccar/StringProtocolEncoder.java @@ -0,0 +1,51 @@ +/* + * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * + * 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.traccar; + +import org.traccar.model.Command; + +import java.util.Map; + +public abstract class StringProtocolEncoder extends BaseProtocolEncoder { + + public interface ValueFormatter { + public String formatValue(String key, Object value); + } + + protected String formatCommand(Command command, String format, ValueFormatter valueFormatter, String... keys) { + + String result = String.format(format, (Object[]) keys); + + result = result.replaceAll("\\{" + Command.KEY_UNIQUE_ID + "}", getUniqueId(command.getDeviceId())); + for (Map.Entry<String, Object> entry : command.getOther().entrySet()) { + String value = null; + if (valueFormatter != null) { + value = valueFormatter.formatValue(entry.getKey(), entry.getValue()); + } + if (value == null) { + value = entry.getValue().toString(); + } + result = result.replaceAll("\\{" + entry.getKey() + "}", value); + } + + return result; + } + + protected String formatCommand(Command command, String format, String... keys) { + return formatCommand(command, format, null, keys); + } + +} |