Skip to content

Commit

Permalink
Merge branch 'main' into feature/configuration-assets
Browse files Browse the repository at this point in the history
  • Loading branch information
pankalog authored Jan 8, 2024
2 parents 35adc5d + 784eb56 commit f4ee61b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.teltonika.*;
import org.openremote.model.value.AttributeDescriptor;
import org.openremote.model.value.MetaItemType;
import org.openremote.model.value.ValueType;

import java.io.IOException;
Expand Down Expand Up @@ -453,12 +454,14 @@ public void onPublish(RemotingConnection connection, Topic topic, ByteBuf body)
Optional<Attribute<?>> sessionAttr = assetChangedTripState(prevValue, newValue, pred, ref);

if (sessionAttr.isPresent()) {
getLogger().warning("New AssetStateDuration");
Attribute<?> session = sessionAttr.get();
session.addOrReplaceMeta(
new MetaItem<>(STORE_DATA_POINTS, true),
new MetaItem<>(RULE_STATE, true),
new MetaItem<>(READ_ONLY, true)
);
// Maybe set this to session.endTime?
attributes.get(CarAsset.LAST_CONTACT).ifPresent(date -> {
session.setTimestamp(date.getValue().get().getTime());
});
Expand Down Expand Up @@ -505,6 +508,7 @@ private void createNewAsset(String newDeviceId, String newDeviceImei, String rea
.setRealm(realm)
.setModelNumber(getConfig().getDefaultModelNumber())
.setId(newDeviceId);
testAsset.getAttribute(CarAsset.LOCATION).ifPresentOrElse(attr -> attr.addMeta(new MetaItem<>(STORE_DATA_POINTS, true)), () -> {getLogger().warning("Couldn't find CarAsset.LOCATION");});

testAsset.getAttributes().add(new Attribute<>(CarAsset.IMEI, newDeviceImei));

Expand All @@ -529,6 +533,18 @@ private void createNewAsset(String newDeviceId, String newDeviceImei, String rea
updateAsset(testAsset, attributes, topic, connection);
}

public class TeltonikaParameterId {
public String customId;
public int id;
TeltonikaParameterId(String customId){
this.customId = customId;
}

TeltonikaParameterId(int id){
this.id = id;
}
}

/**
* Returns list of attributes depending on the Teltonika JSON Payload.
* Uses the logic and results from parsing the Teltonika Parameter IDs.
Expand All @@ -537,7 +553,9 @@ private void createNewAsset(String newDeviceId, String newDeviceImei, String rea
* @return Map of {@link Attribute}s to be assigned to the {@link Asset}.
*/
private AttributeMap getAttributesFromPayload(String payloadContent) throws JsonProcessingException {
HashMap<Integer, TeltonikaParameter> params = new HashMap<>();


HashMap<String, TeltonikaParameter> params = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
try {
// Parse file with Parameter details
Expand All @@ -554,6 +572,19 @@ private AttributeMap getAttributesFromPayload(String payloadContent) throws Json
getLogger().warning("Could not parse the Teltonika Parameter file");
getLogger().info(e.toString());
}

// Add the custom parameters (pr, alt, ang, sat, sp, evt)
Map<String, TeltonikaParameter> customParams = Map.of(
"pr", new TeltonikaParameter(-1, "Priority", String.valueOf(1), "Unsigned", String.valueOf(0), String.valueOf(4), String.valueOf(1), "-", "0: Low - 1: High - 2: Panic", "all", "Permanent I/O Elements"),
"alt", new TeltonikaParameter(-1, "Altitude", "2", "Signed", "-1000", "+3000", "1", "m", "meters above sea level", "all", "Permanent I/O Elements"),
"ang", new TeltonikaParameter(-1, "Angle", "2", "Signed", "-360", "+460", "1", "deg", "degrees from north pole", "all", "Permanent I/O Elements"),
"sat", new TeltonikaParameter(-1, "Satellites", "1", "Unsigned", "0", "1000", "1", "-", "number of visible satellites", "all", "Permanent I/O Elements"),
"sp", new TeltonikaParameter(-1, "Speed", "2", "Signed", "0", "1000", "1", "km/h", "speed calculated from satellites", "all", "Permanent I/O Elements"),
"evt", new TeltonikaParameter(-1, "Event Triggered", "2", "Signed", "1", "10000", "1", "-", "Parameter ID which generated this payload", "all", "Permanent I/O Elements")

);
params.putAll(customParams);

//Parameters parsed, time to understand the payload
TeltonikaDataPayload payload;
try {
Expand Down Expand Up @@ -623,6 +654,8 @@ private Optional<Attribute<?>> assetChangedTripState(Attribute<?> previousValue,
}

// Grab all datapoints (To be replaced by AssetDatapointValueQuery)
// For optimization: Maybe pull the datapoints from the endTime of the previous AssetStateDuration.

List<AssetDatapoint> valueDatapoints = AssetDatapointService.getDatapoints(ref);
ArrayList<AssetDatapoint> list = new ArrayList<>(valueDatapoints);

Expand Down Expand Up @@ -689,6 +722,13 @@ private Optional<Attribute<?>> assetChangedTripState(Attribute<?> previousValue,
new Timestamp(previousValue.getTimestamp().get())
));

tripAttr.addMeta(
new MetaItem<>(STORE_DATA_POINTS, true),
new MetaItem<>(RULE_STATE, true),
new MetaItem<>(READ_ONLY, true)
);


return Optional.of(tripAttr);
}

Expand Down
28 changes: 12 additions & 16 deletions model/src/main/java/org/openremote/model/teltonika/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.openremote.model.Constants;
import org.openremote.model.asset.Asset;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeMap;
import org.openremote.model.attribute.MetaItem;
import org.openremote.model.attribute.MetaMap;
import org.openremote.model.custom.CarAsset;
import org.openremote.model.geo.GeoJSONPoint;
import org.openremote.model.util.ValueUtil;
import org.openremote.model.value.*;
Expand Down Expand Up @@ -54,23 +56,19 @@ public String toString() {
//TODO: Move any function from here; it's a POJO.
//TODO: Figure out what the parameters: pr, alt, ang, sat, sp, and evt do
// and implement their functionality (I can guess but I want to know exactly).
public AttributeMap getAttributes(Map<Integer, TeltonikaParameter> params, AttributeMap additionalAttributes, Logger logger){
public AttributeMap getAttributes(Map<String, TeltonikaParameter> params, AttributeMap additionalAttributes, Logger logger){
AttributeMap attributes = new AttributeMap();
for (Map.Entry<String,Object> entry : reported.entrySet()){
//Check if parameter is a number value, pointing to a TeltonikaParameter
String entryId = entry.getKey();
int parsedEntryId;
try {
parsedEntryId = Integer.parseInt(entryId);
}catch (Exception e){
continue;
}
//If the Teltonika Parameter HashMap doesn't contain the requested AVL ID, continue:

if (!params.containsKey(parsedEntryId)) continue;
if (!params.containsKey(entryId)) {
logger.warning("Could not parse retrieved parameter with AVL ID "+ entryId);
continue;
}

//Retrieve the parameter data
TeltonikaParameter parameter = params.get(Integer.valueOf(entry.getKey()));
TeltonikaParameter parameter = params.get(entryId);

//Create the MetaItem Map
MetaMap metaMap = new MetaMap();
Expand Down Expand Up @@ -177,7 +175,7 @@ public AttributeMap getAttributes(Map<Integer, TeltonikaParameter> params, Attri
constraintsMeta.setValue(constraints);

//TODO: Fix this, constraints for some reason are not being applied
// metaMap.add(constraintsMeta);
metaMap.add(constraintsMeta);
}
}catch (Exception e){
logger.severe(parameter.propertyName + "Failed constraints");
Expand All @@ -196,7 +194,7 @@ public AttributeMap getAttributes(Map<Integer, TeltonikaParameter> params, Attri
throw e;
}
//Use the MetaMap to create an AttributeDescriptor
AttributeDescriptor<?> attributeDescriptor = new AttributeDescriptor<>(parameter.propertyId.toString(), attributeType, metaMap);
AttributeDescriptor<?> attributeDescriptor = new AttributeDescriptor<>(entryId, attributeType, metaMap);



Expand All @@ -205,14 +203,12 @@ public AttributeMap getAttributes(Map<Integer, TeltonikaParameter> params, Attri
// Add it to the AttributeMap
attributes.addOrReplace(generatedAttribute);
}
//Special parameter definitions without being defined in the AVL Parameter List, thanks Teltonika

//latlng are the latitude-longitude coordinates, also check if it's 0,0, if it is, don't update.
if (reported.containsKey("latlng") && !Objects.equals(reported.get("latlng"), "0.000000,0.000000")){
try{
String latlngString = reported.get("latlng").toString();
GeoJSONPoint point = ParseLatLngToGeoJSONObject(latlngString);
Attribute<?> attr = new Attribute<>("location", ValueType.GEO_JSON_POINT, point);
Attribute<?> attr = new Attribute<>(Asset.LOCATION, point);

attributes.add(attr);
}catch (Exception e){
Expand All @@ -227,7 +223,7 @@ public AttributeMap getAttributes(Map<Integer, TeltonikaParameter> params, Attri
try{
long unixTimestampMillis = Long.parseLong(reported.get("ts").toString());
Timestamp deviceTimestamp = Timestamp.from(Instant.ofEpochMilli(unixTimestampMillis));
attributes.add(new Attribute<>("lastContact", ValueType.DATE_AND_TIME, deviceTimestamp).setTimestamp(deviceTimestamp.getTime()));
attributes.add(new Attribute<>(CarAsset.LAST_CONTACT, deviceTimestamp, deviceTimestamp.getTime()));


//Update all affected attribute timestamps
Expand Down

0 comments on commit f4ee61b

Please sign in to comment.