<commit_before>/*******************************************************************************
 * Copyright (c) 2014 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.foscam.ipcamera;

import com.whizzosoftware.hobson.api.device.AbstractHobsonDevice;
import com.whizzosoftware.hobson.api.device.DeviceType;
import com.whizzosoftware.hobson.api.property.PropertyContainer;
import com.whizzosoftware.hobson.api.property.TypedProperty;
import com.whizzosoftware.hobson.api.variable.HobsonVariable;
import com.whizzosoftware.hobson.api.variable.VariableConstants;
import com.whizzosoftware.hobson.api.variable.VariableProxyType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;

/**
 * A class representing a Foscam IP camera.
 *
 * @author <NAME>
 */
public class HobsonFoscamIPCamera extends AbstractHobsonDevice {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    public static final String CONFIG_USERNAME = "username";
    public static final String CONFIG_PASSWORD = "password";

    private String username;
    private String password;
    private InetAddress address;
    private boolean varsPublished = false;

    /**
     * Constructor.
     *
     * @param plugin the HobsonPlugin that created this device
     * @param id the camera device ID
     * @param name the camera name
     * @param address the address of the camera
     */
    public HobsonFoscamIPCamera(FoscamIPCameraPlugin plugin, String id, String name, InetAddress address) {
        super(plugin, id);

        setDefaultName(name);

        this.address = address;
    }

    @Override
    public void onStartup(PropertyContainer config) {
        super.onStartup(config);

        readConfig(config);

        // publish variables
        publishVariable(VariableConstants.IMAGE_STATUS_URL, getImageUrl(), HobsonVariable.Mask.READ_ONLY, VariableProxyType.MEDIA_URL);
        publishVariable(VariableConstants.VIDEO_STATUS_URL, getVideoUrl(), HobsonVariable.Mask.READ_ONLY, VariableProxyType.MEDIA_URL);

        varsPublished = true;
    }

    @Override
    public void onShutdown() {
    }

    @Override
    public DeviceType getType() {
        return DeviceType.CAMERA;
    }

    @Override
    public String getPreferredVariableName() {
        return VariableConstants.IMAGE_STATUS_URL;
    }

    @Override
    public void onDeviceConfigurationUpdate(PropertyContainer config) {
        super.onDeviceConfigurationUpdate(config);

        if (readConfig(config)) {
            // only fire updated URL variable notifications if variables have been published
            if (varsPublished) {<commit_msg>Refactored so cameras can't become unavailable.<commit_after>/*******************************************************************************
 * Copyright (c) 2014 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.foscam.ipcamera;

import com.whizzosoftware.hobson.api.device.AbstractHobsonDevice;
import com.whizzosoftware.hobson.api.device.DeviceType;
import com.whizzosoftware.hobson.api.property.PropertyContainer;
import com.whizzosoftware.hobson.api.property.TypedProperty;
import com.whizzosoftware.hobson.api.variable.HobsonVariable;
import com.whizzosoftware.hobson.api.variable.VariableConstants;
import com.whizzosoftware.hobson.api.variable.VariableProxyType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;

/**
 * A class representing a Foscam IP camera.
 *
 * @author <NAME>ol
 */
public class HobsonFoscamIPCamera extends AbstractHobsonDevice {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    public static final String CONFIG_USERNAME = "username";
    public static final String CONFIG_PASSWORD = "password";

    private String username;
    private String password;
    private InetAddress address;
    private boolean varsPublished = false;

    /**
     * Constructor.
     *
     * @param plugin the HobsonPlugin that created this device
     * @param id the camera device ID
     * @param name the camera name
     * @param address the address of the camera
     */
    public HobsonFoscamIPCamera(FoscamIPCameraPlugin plugin, String id, String name, InetAddress address) {
        super(plugin, id);

        setDefaultName(name);

        this.address = address;
    }

    @Override
    public void onStartup(PropertyContainer config) {
        super.onStartup(config);

        readConfig(config);

        // publish variables
        publishVariable(VariableConstants.IMAGE_STATUS_URL, getImageUrl(), HobsonVariable.Mask.READ_ONLY, VariableProxyType.MEDIA_URL);
        publishVariable(VariableConstants.VIDEO_STATUS_URL, getVideoUrl(), HobsonVariable.Mask.READ_ONLY, VariableProxyType.MEDIA_URL);

        varsPublished = true;
    }

    @Override
    public void onShutdown() {
    }

    @Override
    public DeviceType getType() {
        return DeviceType.CAMERA;
    }

    @Override
    public Long getLastCheckIn() {
        // the camera is not proactively checked unless an image is requested; so we always claim its active
        // TODO: change this to be based on a proactive ping
        return System.currentTimeMillis();
    }

    @Override
    public String getPreferredVariableName() {
        return VariableConstants.IMAGE_STATUS_URL;
    }

    @Override
    public void onDeviceConfigurationUpdate(PropertyContainer config) {
        super.onDeviceConfigurationUpdate(config);

        if (readConfig(config)) {
            // only fire updated URL variable notifications if variables have been published
            if (varsPublished) {
{'fraction_non_alphanumeric': 0.12466167807673938, 'fraction_numerical': 0.0031842063365706097, 'mean_word_length': 3.824884792626728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3440076', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1632, 'n_words': 438}