-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
cpark
driver for Drivewyze Central Park API
- Loading branch information
Showing
13 changed files
with
466 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/us/mn/state/dot/tms/server/comm/cpark/CParkPoller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* IRIS -- Intelligent Roadway Information System | ||
* Copyright (C) 2025 Minnesota Department of Transportation | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
*/ | ||
package us.mn.state.dot.tms.server.comm.cpark; | ||
|
||
import us.mn.state.dot.sched.DebugLog; | ||
import us.mn.state.dot.tms.CommLink; | ||
import us.mn.state.dot.tms.DeviceRequest; | ||
import us.mn.state.dot.tms.server.ControllerImpl; | ||
import us.mn.state.dot.tms.server.comm.SamplePoller; | ||
import us.mn.state.dot.tms.server.comm.ThreadedPoller; | ||
import static us.mn.state.dot.tms.utils.URIUtil.HTTPS; | ||
|
||
/** | ||
* CParkPoller is a reader for Drivewyze Central Park JSON. | ||
* | ||
* @author Douglas Lau | ||
*/ | ||
public class CParkPoller extends ThreadedPoller<CParkProp> | ||
implements SamplePoller | ||
{ | ||
/** Central park debug log */ | ||
static private final DebugLog CPARK_LOG = new DebugLog("cpark"); | ||
|
||
/** Log a message to the debug log */ | ||
static public void slog(String msg) { | ||
CPARK_LOG.log(msg); | ||
} | ||
|
||
/** Create a new CPark poller */ | ||
public CParkPoller(CommLink link) { | ||
super(link, HTTPS, CPARK_LOG); | ||
} | ||
|
||
/** Create a comm thread */ | ||
@Override | ||
protected CParkThread createCommThread(String uri, int timeout, | ||
int nrd) | ||
{ | ||
return new CParkThread(this, queue, scheme, uri, timeout, nrd, | ||
CPARK_LOG); | ||
} | ||
|
||
/** Send device request to a controller. | ||
* @param c Controller to poll. */ | ||
@Override | ||
public void sendRequest(ControllerImpl c, DeviceRequest r) { | ||
// nothing to do? | ||
} | ||
|
||
/** Query sample data. | ||
* @param c Controller to poll. | ||
* @param per_sec Sample period in seconds. */ | ||
@Override | ||
public void querySamples(ControllerImpl c, int per_sec) { | ||
if (c.getPollPeriodSec() == per_sec) | ||
addOp(new OpQuerySpots(c, per_sec)); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/us/mn/state/dot/tms/server/comm/cpark/CParkProp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* IRIS -- Intelligent Roadway Information System | ||
* Copyright (C) 2025 Minnesota Department of Transportation | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
*/ | ||
package us.mn.state.dot.tms.server.comm.cpark; | ||
|
||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.IOException; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import us.mn.state.dot.sched.TimeSteward; | ||
import static us.mn.state.dot.tms.server.Constants.MISSING_DATA; | ||
import us.mn.state.dot.tms.server.ControllerImpl; | ||
import us.mn.state.dot.tms.server.comm.ControllerProperty; | ||
import us.mn.state.dot.tms.server.comm.Operation; | ||
import us.mn.state.dot.tms.server.comm.ParsingException; | ||
import us.mn.state.dot.tms.server.comm.ProtocolException; | ||
|
||
/** | ||
* Available spots property. | ||
* | ||
* @author Douglas Lau | ||
*/ | ||
public class CParkProp extends ControllerProperty { | ||
|
||
/** Time stamp */ | ||
private final long stamp; | ||
|
||
/** Get timestamp at end of interval */ | ||
public long getTime() { | ||
return stamp; | ||
} | ||
|
||
/** Sample perdiod */ | ||
private final int per_sec; | ||
|
||
/** Get the interval period (sec) */ | ||
public int getPeriod() { | ||
return per_sec; | ||
} | ||
|
||
/** Parking spot occupancy array */ | ||
private final int[] scans = new int[64]; | ||
|
||
/** Get parking spot occupancy array */ | ||
public int[] getScans() { | ||
return scans; | ||
} | ||
|
||
/** Create an available spots property */ | ||
public CParkProp(int p) { | ||
stamp = TimeSteward.currentTimeMillis(); | ||
per_sec = p; | ||
for (int i = 0; i < scans.length; i++) | ||
scans[i] = MISSING_DATA; | ||
} | ||
|
||
/** Decode a QUERY response */ | ||
@Override | ||
public void decodeQuery(ControllerImpl c, InputStream is) | ||
throws IOException | ||
{ | ||
try { | ||
JSONObject jo = new JSONObject(parseResp(is)); | ||
for (int i = 0; i < 64; i++) { | ||
String key = String.valueOf(i + 1); | ||
JSONObject spot = jo.optJSONObject(key); | ||
if (spot != null) | ||
parseSpot(i, spot); | ||
} | ||
} | ||
catch (JSONException e) { | ||
throw new ParsingException("JSON: " + e.getMessage()); | ||
} | ||
} | ||
|
||
/** Parse parking spot object */ | ||
private void parseSpot(int i, JSONObject spot) { | ||
try { | ||
boolean available = spot.getBoolean("available"); | ||
scans[i] = (available) ? 0 : 1; | ||
CParkPoller.slog("spot:" + i + ", " + available); | ||
int duration = spot.getInt("duration"); | ||
CParkPoller.slog("duration: " + duration); | ||
} | ||
catch (JSONException e) { | ||
// spot not defined | ||
} | ||
} | ||
|
||
/** Parse a response */ | ||
private String parseResp(InputStream is) throws IOException { | ||
char[] buf = new char[1024]; | ||
InputStreamReader isr = new InputStreamReader(is); | ||
StringBuilder res = new StringBuilder(); | ||
for (int i = 0;; i++) { | ||
int n = isr.read(buf, 0, 1024); | ||
if (n < 0) | ||
break; | ||
else if (n > 0) | ||
res.append(buf, 0, n); | ||
if (i > 100) | ||
throw new ParsingException("RESP TOO BIG"); | ||
} | ||
return res.toString(); | ||
} | ||
|
||
/** Get a string representation of the property */ | ||
@Override | ||
public String toString() { | ||
return "spots"; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/us/mn/state/dot/tms/server/comm/cpark/CParkThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* IRIS -- Intelligent Roadway Information System | ||
* Copyright (C) 2025 Minnesota Department of Transportation | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
*/ | ||
package us.mn.state.dot.tms.server.comm.cpark; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import us.mn.state.dot.sched.DebugLog; | ||
import us.mn.state.dot.tms.server.comm.CommThread; | ||
import us.mn.state.dot.tms.server.comm.Messenger; | ||
import us.mn.state.dot.tms.server.comm.MessengerException; | ||
import us.mn.state.dot.tms.server.comm.OpQueue; | ||
|
||
/** | ||
* CPark thread, used to provide a Messenger with x-access-token header. | ||
* | ||
* @author Douglas Lau | ||
*/ | ||
public class CParkThread extends CommThread<CParkProp> { | ||
|
||
/** Create a new CPark thread */ | ||
public CParkThread(CParkPoller p, OpQueue<CParkProp> q, URI s, | ||
String u, int rt, int nrd, DebugLog log) | ||
{ | ||
super(p, q, s, u, rt, nrd, log); | ||
} | ||
|
||
/** Create a messenger */ | ||
@Override | ||
protected Messenger createMessenger(URI s, String u, int rt, int nrd) | ||
throws MessengerException, IOException | ||
{ | ||
return new TokenMessenger( | ||
Messenger.create(s, u, rt, nrd), | ||
Messenger.createURI(s, u).toURL(), | ||
rt | ||
); | ||
} | ||
} |
Oops, something went wrong.