How to use the thingtalk.Units function in thingtalk

To help you get started, we’ve selected a few thingtalk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stanford-oval / thingpedia-common-devices / io.home-assistant / sensor.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of io.home-assistant
//
// Copyright 2019 Xiaomeng Jin 
//
// See LICENSE for details
"use strict";

const Units = require('thingtalk').Units

const HomeAssistantDevice = require('./base');

module.exports = class HomeAssistantSensor extends HomeAssistantDevice {
	constructor(engine, state, master, entityId) {
        super(engine, state, master, entityId);
        const [domain,] = entityId.split('.');
		this.domain = domain;
        this.device_class = this.state.attributes.device_class;
        if (['binary_sensor', 'cover'].includes(this.domain)) {
            let supportedDeviceClasses = {
                battery: {
                    on: 'low',
                    off: 'normal'
                },
                cold: {
github stanford-oval / genie-toolkit / languages / thingtalk / load-thingpedia.js View on Github external
// Copyright 2017-2019 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna 
//         Silei Xu 
//
// See COPYING for details
"use strict";

const assert = require('assert');

const ThingTalk = require('thingtalk');
const Ast = ThingTalk.Ast;
const Type = ThingTalk.Type;
const Grammar = ThingTalk.Grammar;
const SchemaRetriever = ThingTalk.SchemaRetriever;
const Units = ThingTalk.Units;

const { clean, typeToStringSafe } = require('./utils');

function identity(x) {
    return x;
}

class ThingpediaLoader {
    async init(runtime, grammar, options) {
        this._runtime = runtime;
        this._grammar = grammar;

        this._tpClient = options.thingpediaClient;
        if (!options.schemaRetriever)
            options.schemaRetriever = new SchemaRetriever(this._tpClient, null, !options.debug);
        this._schemas = options.schemaRetriever;
github stanford-oval / genie-toolkit / lib / sentence-generator / index.js View on Github external
//
// See COPYING for details
"use strict";

const assert = require('assert');
const stream = require('stream');
const path = require('path');
const events = require('events');

const ThingTalk = require('thingtalk');
const Ast = ThingTalk.Ast;
const Type = ThingTalk.Type;
const Grammar = ThingTalk.Grammar;
const SchemaRetriever = ThingTalk.SchemaRetriever;
const NNSyntax = ThingTalk.NNSyntax;
const Units = ThingTalk.Units;

const { clean, makeDummyEntities } = require('../utils');

const $runtime = require('./runtime');
const importGenie = require('../genie-compiler');
const { typeToStringSafe } = require('../../languages/ast_manip');
const i18n = require('../i18n');

function identity(x) {
    return x;
}

const BASIC_TARGET_GEN_SIZE = 100000;
const CONTEXTUAL_TARGET_GEN_SIZE = 10000;

class SentenceGenerator extends events.EventEmitter {
github stanford-oval / thingpedia-common-devices / scripts / org.schema / normalize-data.js View on Github external
function parseMeasure(str) {
    const match = /^(-?(?:(?:0|[1-9][0-9]*)\.[0-9]*(?:[eE][+-]?[0-9]+)?|\.[0-9]+(?:[eE][+-]?[0-9]+)?|(?:0|[1-9][0-9]*)(?:[eE][+-]?[0-9]+)?))\s+([a-zA-Z]+)$/.exec(str);
    if (!match) {
        console.error(`Invalid measurement value ${str}`);
        return undefined;
    }

    const [, value, unit] = match;
    if (!ThingTalk.Units.UnitsToBaseUnit[unit]) {
        console.error(`Invalid measurement unit ${unit}`);
        return undefined;
    }

    return ThingTalk.Units.transformToBaseUnit(parseFloat(value), unit);
}
github stanford-oval / genie-toolkit / tool / webqa-normalize-data.js View on Github external
const match = /^(-?(?:(?:0|[1-9][0-9]*)\.[0-9]*(?:[eE][+-]?[0-9]+)?|\.[0-9]+(?:[eE][+-]?[0-9]+)?|(?:0|[1-9][0-9]*)(?:[eE][+-]?[0-9]+)?))\s+([a-zA-Z]+)$/.exec(str);
    if (!match) {
        console.error(`Invalid measurement value ${str}`);
        return undefined;
    }

    let [, value, unit] = match;
    if (MEASURE_UNIT_REMAP[unit])
        unit = MEASURE_UNIT_REMAP[unit];

    if (!ThingTalk.Units.UnitsToBaseUnit[unit]) {
        console.error(`Invalid measurement unit ${unit}`);
        return undefined;
    }

    return ThingTalk.Units.transformToBaseUnit(parseFloat(value), unit);
}
github stanford-oval / thingpedia-common-devices / scripts / org.schema / normalize-data.js View on Github external
function parseMeasure(str) {
    const match = /^(-?(?:(?:0|[1-9][0-9]*)\.[0-9]*(?:[eE][+-]?[0-9]+)?|\.[0-9]+(?:[eE][+-]?[0-9]+)?|(?:0|[1-9][0-9]*)(?:[eE][+-]?[0-9]+)?))\s+([a-zA-Z]+)$/.exec(str);
    if (!match) {
        console.error(`Invalid measurement value ${str}`);
        return undefined;
    }

    const [, value, unit] = match;
    if (!ThingTalk.Units.UnitsToBaseUnit[unit]) {
        console.error(`Invalid measurement unit ${unit}`);
        return undefined;
    }

    return ThingTalk.Units.transformToBaseUnit(parseFloat(value), unit);
}
github stanford-oval / genie-toolkit / tool / webqa-normalize-data.js View on Github external
function parseMeasure(str) {
    const match = /^(-?(?:(?:0|[1-9][0-9]*)\.[0-9]*(?:[eE][+-]?[0-9]+)?|\.[0-9]+(?:[eE][+-]?[0-9]+)?|(?:0|[1-9][0-9]*)(?:[eE][+-]?[0-9]+)?))\s+([a-zA-Z]+)$/.exec(str);
    if (!match) {
        console.error(`Invalid measurement value ${str}`);
        return undefined;
    }

    let [, value, unit] = match;
    if (MEASURE_UNIT_REMAP[unit])
        unit = MEASURE_UNIT_REMAP[unit];

    if (!ThingTalk.Units.UnitsToBaseUnit[unit]) {
        console.error(`Invalid measurement unit ${unit}`);
        return undefined;
    }

    return ThingTalk.Units.transformToBaseUnit(parseFloat(value), unit);
}