<commit_before>/*******************************************************************************
 * Copyright (c) 2015 Whizzo Software, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package com.whizzosoftware.hobson.bootstrap.api.telemetry;

import com.whizzosoftware.hobson.api.HobsonRuntimeException;
import com.whizzosoftware.hobson.api.device.DeviceContext;
import com.whizzosoftware.hobson.api.device.DeviceManager;
import com.whizzosoftware.hobson.api.device.HobsonDevice;
import com.whizzosoftware.hobson.api.hub.HubContext;
import com.whizzosoftware.hobson.api.telemetry.TelemetryInterval;
import com.whizzosoftware.hobson.api.telemetry.TelemetryManager;
import com.whizzosoftware.hobson.api.telemetry.TemporalValue;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.rrd4j.ConsolFun;
import org.rrd4j.DsType;
import org.rrd4j.core.*;

import java.io.File;
import java.io.IOException;
import java.util.*;

import static org.rrd4j.ConsolFun.AVERAGE;

/**
 * An OSGI implementation of a TelemetryManager.
 *
 * @author <NAME>
 */
public class OSGITelemetryManager implements TelemetryManager {
    private static final String TELEMETRY_DELIMITER = ":";

    volatile DeviceManager deviceManager;

    private final Map<String,Object> telemetryMutexes = new HashMap<>();

    @Override
    public void enableDeviceTelemetry(DeviceContext ctx, boolean enabled) {
        deviceManager.setDeviceConfigurationProperty(ctx, "telemetry", enabled, true);
    }

    @Override
    public Collection<HobsonDevice> getAllTelemetryEnabledDevices(HubContext ctx) {
        List<HobsonDevice> results = new ArrayList<HobsonDevice>();
        for (HobsonDevice device : deviceManager.getAllDevices(ctx)) {
            if (isDeviceTelemetryEnabled(device.getContext())) {
                results.add(device);
            }
        }
        return results;
    }

    @Override
    public Map<String, Collection<TemporalValue>> getDeviceTelemetry(DeviceContext ctx, long endTime, TelemetryInterval interval) {
        Map<String,Collection<TemporalValue>> results = new HashMap<>();
        HobsonDevice device = deviceManager.getDevice(ctx);
        String[] variables = device.getRuntime().getTelemetryVariableNames();
        if (variables != null) {<commit_msg>Moved telemetry variable name getter out of device runtime.<commit_after>/*******************************************************************************
 * Copyright (c) 2015 Whizzo Software, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package com.whizzosoftware.hobson.bootstrap.api.telemetry;

import com.whizzosoftware.hobson.api.HobsonRuntimeException;
import com.whizzosoftware.hobson.api.device.DeviceContext;
import com.whizzosoftware.hobson.api.device.DeviceManager;
import com.whizzosoftware.hobson.api.device.HobsonDevice;
import com.whizzosoftware.hobson.api.hub.HubContext;
import com.whizzosoftware.hobson.api.telemetry.TelemetryInterval;
import com.whizzosoftware.hobson.api.telemetry.TelemetryManager;
import com.whizzosoftware.hobson.api.telemetry.TemporalValue;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.rrd4j.ConsolFun;
import org.rrd4j.DsType;
import org.rrd4j.core.*;

import java.io.File;
import java.io.IOException;
import java.util.*;

import static org.rrd4j.ConsolFun.AVERAGE;

/**
 * An OSGI implementation of a TelemetryManager.
 *
 * @author <NAME>
 */
public class OSGITelemetryManager implements TelemetryManager {
    private static final String TELEMETRY_DELIMITER = ":";

    volatile DeviceManager deviceManager;

    private final Map<String,Object> telemetryMutexes = new HashMap<>();

    @Override
    public void enableDeviceTelemetry(DeviceContext ctx, boolean enabled) {
        deviceManager.setDeviceConfigurationProperty(ctx, "telemetry", enabled, true);
    }

    @Override
    public Collection<HobsonDevice> getAllTelemetryEnabledDevices(HubContext ctx) {
        List<HobsonDevice> results = new ArrayList<HobsonDevice>();
        for (HobsonDevice device : deviceManager.getAllDevices(ctx)) {
            if (isDeviceTelemetryEnabled(device.getContext())) {
                results.add(device);
            }
        }
        return results;
    }

    @Override
    public Map<String, Collection<TemporalValue>> getDeviceTelemetry(DeviceContext ctx, long endTime, TelemetryInterval interval) {
        Map<String,Collection<TemporalValue>> results = new HashMap<>();
        HobsonDevice device = deviceManager.getDevice(ctx);
        String[] variables = device.getTelemetryVariableNames();
        if (variables != null) {
{'fraction_non_alphanumeric': 0.15035570082676408, 'fraction_numerical': 0.004614497212074601, 'mean_word_length': 4.760797342192691, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3145454', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1424, 'n_words': 314}