From f1ae2d73584d4ddded6f727c665d97f03ecf9c25 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 12 Jul 2015 17:08:14 +1200 Subject: Implement web data handler --- src/org/traccar/BasePipelineFactory.java | 8 +++++- src/org/traccar/Context.java | 12 +++++++++ src/org/traccar/DefaultDataHandler.java | 37 +++++++++++++++++++++++++++ src/org/traccar/MainEventHandler.java | 13 ++-------- src/org/traccar/WebDataHandler.java | 44 ++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 src/org/traccar/DefaultDataHandler.java create mode 100644 src/org/traccar/WebDataHandler.java (limited to 'src/org') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 4ff4030f0..689b63ab6 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -125,7 +125,13 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { pipeline.addLast("geocoder", new ReverseGeocoderHandler(Context.getReverseGeocoder(), processInvalidPositions)); } pipeline.addLast("remoteAddress", new RemoteAddressHandler()); - pipeline.addLast("handler", new MainEventHandler()); + if (Context.getDataManager() != null) { + pipeline.addLast("dataHandler", new DefaultDataHandler()); + } + if (Boolean.valueOf(Context.getProps().getProperty("forward.enable"))) { + pipeline.addLast("webHandler", new WebDataHandler(Context.getProps().getProperty("forward.url"))); + } + pipeline.addLast("mainHandler", new MainEventHandler()); return pipeline; } diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index cc7501c9a..6fb812f4f 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -35,6 +35,18 @@ public class Context { public static Properties getProps() { return properties; } + + /*public static boolean getPropBoolean(String key) { + return Boolean.valueOf(properties.getProperty(key)); + } + + public static int getPropInt() { + return Integer.valueOf(null); + } + + public static String getPropString() { + return null; + }*/ private static boolean loggerEnabled; diff --git a/src/org/traccar/DefaultDataHandler.java b/src/org/traccar/DefaultDataHandler.java new file mode 100644 index 000000000..430e20776 --- /dev/null +++ b/src/org/traccar/DefaultDataHandler.java @@ -0,0 +1,37 @@ +/* + * 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.helper.Log; +import org.traccar.model.Position; + +public class DefaultDataHandler extends BaseDataHandler { + + @Override + protected Position handlePosition(Position position) { + + try { + Context.getDataManager().addPosition(position); + Context.getDataManager().updateLatestPosition(position); + Context.getConnectionManager().update(position); + } catch (Exception error) { + Log.warning(error); + } + + return position; + } + +} diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java index 30ec75f58..b32d577de 100644 --- a/src/org/traccar/MainEventHandler.java +++ b/src/org/traccar/MainEventHandler.java @@ -31,27 +31,18 @@ public class MainEventHandler extends IdleStateAwareChannelHandler { public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { if (e.getMessage() != null) { - if (e.getMessage() instanceof Position) { Position position = (Position) e.getMessage(); // Log position StringBuilder s = new StringBuilder(); - s.append("device: ").append(position.getDeviceId()).append(", "); + s.append(formatChannel(e.getChannel())).append(" "); + s.append("id: ").append(position.getDeviceId()).append(", "); s.append("time: ").append(position.getFixTime()).append(", "); s.append("lat: ").append(position.getLatitude()).append(", "); s.append("lon: ").append(position.getLongitude()); Log.info(s.toString()); - - try { - Context.getDataManager().addPosition(position); - Context.getDataManager().updateLatestPosition(position); - Context.getConnectionManager().update(position); - } catch (Exception error) { - Log.warning(error); - } - } } } diff --git a/src/org/traccar/WebDataHandler.java b/src/org/traccar/WebDataHandler.java new file mode 100644 index 000000000..bb1a540a9 --- /dev/null +++ b/src/org/traccar/WebDataHandler.java @@ -0,0 +1,44 @@ +/* + * 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 com.ning.http.client.AsyncHttpClient; +import org.traccar.model.Position; + +public class WebDataHandler extends BaseDataHandler { + + private final String url; + + public WebDataHandler(String url) { + this.url = url; + } + + @Override + protected Position handlePosition(Position position) { + String request = url. + //replaceAll("\\{uniqueId}", String.valueOf(position.getDeviceId())). + replaceAll("\\{deviceId}", String.valueOf(position.getDeviceId())). + replaceAll("\\{fixTime}", String.valueOf(position.getFixTime().getTime())). + replaceAll("\\{latitude}", String.valueOf(position.getLatitude())). + replaceAll("\\{longitude}", String.valueOf(position.getLongitude())); + + AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); + asyncHttpClient.prepareGet(request).execute(); + + return position; + } + +} -- cgit v1.2.3