0

I' writing a parser for a particular acknowledgement message in my tracker-agent. When parsed, I would like to retrieve all pending commands/firmware updates for the tracker so I can compare them with the content of the acknowledgement and update the relevant operation so that they display accordingly in Cumulocity.

How can I do that? For example, maybe I could use OperationsHelper.getOperationsByStatusAndAgent() ? But how do I have access to OperationsHelper at this level?

EDIT

Here is what I'm going to try now:

TrackerDevice:

public Iterable<OperationRepresentation> getPendingOps() {
    OperationFilter opsFilter = new OperationFilter().byStatus(OperationStatus.PENDING)
            .byAgent(this.gid.getValue());
    return deviceControl.getOperationsByFilter(opsFilter).get().allPages();
}

My custom parser:

TrackerDevice trackerDevice = trackerAgent.getOrCreateTrackerDevice(reportCtx.getEntry(POS_IMEI));
// Looking into pending operations...
for (OperationRepresentation operation: trackerDevice.getPendingOps()) {
    Firmware frm = operation.get(Firmware.class);
    // ...for firmware updates
    if (frm != null) {
        String command = (String) frm.getProperty("command");

        // (GL200Fallback associated a command string to these)
        if (command != null) {
            Matcher m = commandPattern.matcher(command);

            // In each command there is a random count number...
            if (m.find()) {
                String cmdCountNumber = command.substring(m.start(), m.end());

                // ...that must match with the acknowledgement's
                if (cmdCountNumber.equals(reportCtx.getEntry(POS_COUNT_NUMBER))) {
                    trackerDevice.setOperationSuccessful(operation);
                    return true;
                }
            }
        }
    }
}
Gaetan L.
  • 649
  • 6
  • 20
  • Can you tell where this class OperationsHelper is from? I cannot find it in neither the tracker agent nor the generic java client. – TyrManuZ Nov 08 '16 at 10:14
  • /tracker-agent/src/main/java/c8y/trackeragent/operations/OperationsHelper.java – Gaetan L. Nov 08 '16 at 10:27
  • Ah ok that it was renamed to OperationExecutor in newer versions but the general functionality is still the same – TyrManuZ Nov 08 '16 at 11:08

1 Answers1

0

The getOperationsByStatusAndAgent() function will get the operations for all tracker devices. This is probably not what you want.

If you check the startPolling() method in OperationDispatcher you will see that the agent automatically polls the operations for each tracker. This should be triggered automatically whenever a new device connects or when you start the agent (then it will start the polling for all already registered devices).

You don't need to handle this yourself for a new tracker protocol. You just need to add classes for the protocol that implement the Translator interface. These will get triggered whenever the agent receives a new operation for the device.

Edit:

If you want to update the operation after the device answered there are multiple ways you could do it. First of all I would use the DeviceControlApi from the TrackerDevice to interact with operations. It is currently not exposed within the TrackerDevice but you could change that.

To update the operation later you need the ID of the operation. You can either cache the operation e.g. in the device object after you send it to the device or you need to query it e.g. by status and fragmentType

TyrManuZ
  • 2,039
  • 1
  • 14
  • 23
  • Yes I already managed to relay operations to my devices using translate(). However my issue now is to make the link between an opeation and an acknowledgement sent by a device at a later time. When the tracker-agent parses such an acknowledgement, I need to browse all the operations of that device to find the corresponding operation and update its status in Cumulocity, how can I do that? I'm thinking about doing it in the onParsed() method of my Fragment but I don't know how to browse the operations. – Gaetan L. Nov 08 '16 at 11:36
  • Ah ok I misunderstood the question then. I updated my answer to include that as well. The onParsed() method would certainly be a good place where to trigger this action. – TyrManuZ Nov 08 '16 at 13:10
  • Thought about it a little more, I just can't figure out just by looking in the code, how is this method called? I do believe it's called every time a device is communicating with the tracker-agent, and for all the currently pending operations, isn't it? (Cf. OperationsDispatcher.executePendingOps()). If so, if I do implement a translate() method in my parser and do something like if (operation.getText() == someValue) then operation.setStatus"ACCEPTED"). Do you see what I mean? Do you think that would work? – Gaetan L. Nov 08 '16 at 13:21
  • Oh no it doesn't work cause I don't have access to reportCtx in translate()... So could you please tell me how to expose DeviceControlApi in TrackerDevice and then invoke OperationsHelper? – Gaetan L. Nov 08 '16 at 13:48