<commit_before>/*
 *  ChromecastBridge.js
 *
 *  <NAME>
 *  IOTDB.org
 *  2015-04-19
 *
 *  Copyright [2013-2015] [<NAME>]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

"use strict";

var chromecastjs = require('chromecast-js')

var iotdb = require('iotdb');
var _ = iotdb._;
var bunyan = iotdb.bunyan;

var logger = bunyan.createLogger({
    name: 'homestar-chromecast',
    module: 'ChromecastBridge',
});

var mode_play = "iot-attribute:media.mode.play";
var mode_pause = "iot-attribute:media.mode.pause";
var mode_stop = "iot-attribute:media.mode.stop";

/**
 *  See {iotdb.bridge.Bridge#Bridge} for documentation.
 *  <p>
 *  @param {object|undefined} native
 *  only used for instances, should be 
 */
var ChromecastBridge = function (initd, native) {
    var self = this;

    self.initd = _.defaults(initd,
        iotdb.keystore().get("bridges/ChromecastBridge/initd"), {
            poll: 30
        }
    );
    self.native = native;   // the thing that does the work - keep this name

    if (self.native) {
        self.queue = _.queue("ChromecastBridge");
    }
};

ChromecastBridge.prototype = new iotdb.Bridge();

ChromecastBridge.prototype.name = function () {
    return "ChromecastBridge";
};

/* --- lifecycle --- */

/**
 *  See {iotdb.bridge.Bridge#XXX} for documentation.
 */
ChromecastBridge.prototype.discover = function () {
    var self = this;

    logger.info({
        method: "discover"
    }, "called");

    /*
     *  This is the core bit of discovery. As you find new
     *  thimgs, make a new ChromecastBridge and call 'discovered'.
     *  The first argument should be self.initd, the second
     *  the thing that you do work with
     */
    var cp = iotdb.module("iotdb-upnp").control_point();

    cp.on("device", function (native) {
        if (native.deviceType !== "urn:dial-multiscreen-org:device:dial:1") {
            return;
        } else if (native.manufacturer !== 'Google Inc.') {
            return;
        }

        var device = new chromecastjs.Device({
            addresses: [ native.host, ],
            name: native.friendlyName,
            manufacturer: native.manufacturer,
            uuid: native.uuid,
            modelName: native.modelName,
        });

        device.on('connected', function() {
            self.discovered(new ChromecastBridge(self.initd, device));
        });
        device.connect()
    });
    cp.search();
};

/**
 *  See {iotdb.bridge.Bridge#XXX} for documentation.
 */
ChromecastBridge.prototype.connect = function (connectd) {
    var self = this;
    if (!self.native) {
        return;
    }

    self._setup_polling();
    self.pull();
};

ChromecastBridge.prototype._setup_polling = function () {
    var self = this;
    if (!self.initd.poll) {
        return;
    }

    var timer = setInterval(function () {
        if (!self.native) {
            clearInterval(timer);
            return;
        }

        self.pull();
    }, self.initd.poll * 1000);
};

ChromecastBridge.prototype._forget = function () {
    var self = this;
    if (!self.native) {
        return;
    }
<commit_msg>deal with 'connectd'<commit_after>/*
 *  ChromecastBridge.js
 *
 *  <NAME>
 *  IOTDB.org
 *  2015-04-19
 *
 *  Copyright [2013-2015] [<NAME>]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

"use strict";

var chromecastjs = require('chromecast-js')

var iotdb = require('iotdb');
var _ = iotdb._;
var bunyan = iotdb.bunyan;

var logger = bunyan.createLogger({
    name: 'homestar-chromecast',
    module: 'ChromecastBridge',
});

var mode_play = "iot-attribute:media.mode.play";
var mode_pause = "iot-attribute:media.mode.pause";
var mode_stop = "iot-attribute:media.mode.stop";

/**
 *  See {iotdb.bridge.Bridge#Bridge} for documentation.
 *  <p>
 *  @param {object|undefined} native
 *  only used for instances, should be 
 */
var ChromecastBridge = function (initd, native) {
    var self = this;

    self.initd = _.defaults(initd,
        iotdb.keystore().get("bridges/ChromecastBridge/initd"), {
            poll: 30
        }
    );
    self.native = native;   // the thing that does the work - keep this name

    if (self.native) {
        self.queue = _.queue("ChromecastBridge");
    }
};

ChromecastBridge.prototype = new iotdb.Bridge();

ChromecastBridge.prototype.name = function () {
    return "ChromecastBridge";
};

/* --- lifecycle --- */

/**
 *  See {iotdb.bridge.Bridge#XXX} for documentation.
 */
ChromecastBridge.prototype.discover = function () {
    var self = this;

    logger.info({
        method: "discover"
    }, "called");

    /*
     *  This is the core bit of discovery. As you find new
     *  thimgs, make a new ChromecastBridge and call 'discovered'.
     *  The first argument should be self.initd, the second
     *  the thing that you do work with
     */
    var cp = iotdb.module("iotdb-upnp").control_point();

    cp.on("device", function (native) {
        if (native.deviceType !== "urn:dial-multiscreen-org:device:dial:1") {
            return;
        } else if (native.manufacturer !== 'Google Inc.') {
            return;
        }

        var device = new chromecastjs.Device({
            addresses: [ native.host, ],
            name: native.friendlyName,
            manufacturer: native.manufacturer,
            uuid: native.uuid,
            modelName: native.modelName,
        });

        device.on('connected', function() {
            self.discovered(new ChromecastBridge(self.initd, device));
        });
        device.connect()
    });
    cp.search();
};

/**
 *  See {iotdb.bridge.Bridge#XXX} for documentation.
 */
ChromecastBridge.prototype.connect = function (connectd) {
    var self = this;
    if (!self.native) {
        return;
    }

    self._validate_connect(connectd);

    self._setup_polling();
    self.pull();
};

ChromecastBridge.prototype._setup_polling = function () {
    var self = this;
    if (!self.initd.poll) {
        return;
    }

    var timer = setInterval(function () {
        if (!self.native) {
            clearInterval(timer);
            return;
        }

        self.pull();
    }, self.initd.poll * 1000);
};

ChromecastBridge.prototype._forget = function () {
    var self = this;
    if (!self.native) {
        return;
    }

{'fraction_non_alphanumeric': 0.12963471573743476, 'fraction_numerical': 0.007415545179895633, 'mean_word_length': 2.5251694094869315, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3262142', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2256, 'n_words': 583}