Adding an attribute to the message

memesaregood8 days ago

I'm tinkering with the position handlers, particularly EngineHoursHandler, and made some changes into the onPosition():

    @Override
    public void onPosition(Position position, Callback callback) {
        if (!position.hasAttribute(Position.KEY_HOURS)) {
            Position last = cacheManager.getPosition(position.getDeviceId());
            if (last != null) {
                long hours = last.getLong(Position.KEY_HOURS);
                long workHours = last.hasAttribute(Position.KEY_WORKHOURS) ? last.getLong(Position.KEY_WORKHOURS) : 0;
                long idleHours = last.hasAttribute(Position.KEY_IDLEHOURS) ? last.getLong(Position.KEY_IDLEHOURS) : 0;
                if (last.getBoolean(Position.KEY_IGNITION) && position.getBoolean(Position.KEY_IGNITION)) {
                    long result = position.getDeviceTime().getTime() - last.getDeviceTime().getTime();
                    hours += result;
                    if (last.getInteger("speed") > 5) {
                        workHours += result / 1000;
                    } else {
                        idleHours += result / 1000;
                    }
                }
                if (hours != 0) {
                    position.set(Position.KEY_HOURS, hours);
                }
                position.set(Position.KEY_WORKHOURS, workHours);
                position.set(Position.KEY_IDLEHOURS, idleHours);
                System.out.println(String.format("Processed hours:%d, %d", workHours, idleHours));
            }
        }
        callback.processed(false);

I want to split the hours between 'idling hours' where the engine is idling, and actual 'work hours' where the engine is under load.

It seems to work... kinda... it's just the KEY_WORKHOURS and KEY_IDLEHOURS are always 0 in any Position, though the hours counter is updated just fine, even though the data is essentially the same.

Is this the right way to achieve what I'm trying to do? Am I missing something?

Anton Tananaev8 days ago

The way you're getting speed value is incorrect. There's a direct property on the position. It's not an attribute.

memesaregood7 days ago

Thank you kindly.