<commit_before>import Hapi = require('hapi');
import winston = require("winston");
import {HedgehogClient} from 'hedgehog-client';

import ApiResource from "../../ApiResource";
import SerializerRegistry from "../../../serializer/SerializerRegistry";
import ApiEndpoint from "../../ApiEndpoint";
import {IReply} from "hapi";
import JsonApiDocumentBuilder from "../../../jsonapi/JsonApiBuilder";
import {getLinkUrl, getRequestUrl} from "../../../utils";
import {SensorType, default as Sensor} from "../../../../common/Sensor";
import {DataType} from "../../../jsonapi/JsonApiBuilder";

export default class SensorResource extends ApiResource {

    public constructor (private hedgehog: HedgehogClient, private serializerRegistry: SerializerRegistry) {
        super('/sensors');
    }

    @ApiEndpoint('GET', '/{sensorId}')
    public async getSensor (req: Hapi.Request, reply: IReply) {
        const sensorPort = Number(req.params['sensorId']);

        let sensor = new Sensor();
        sensor.port = sensorPort;

        if(sensorPort < 8) {
            sensor.value = await this.hedgehog.getAnalog(sensorPort);
            sensor.type = SensorType.Analog;
        } else {
            sensor.value = await this.hedgehog.getDigital(sensorPort);
            sensor.type = SensorType.Digital;
        }

        let documentBuilder = new JsonApiDocumentBuilder();
        documentBuilder.setLinks(getLinkUrl(req, `/api/sensors/${sensorPort}`), null);
        documentBuilder.addResource(await this.serializerRegistry.serialize(sensor, req, documentBuilder));
        return reply(documentBuilder.getProduct());
    }

    @ApiEndpoint('GET')
    public async getSensorList (req: Hapi.Request, reply: IReply) {
        let documentBuilder = new JsonApiDocumentBuilder();
        documentBuilder.setLinks(getRequestUrl(req), null);
        documentBuilder.setDataType(DataType.Many);

        for (let i = 0; i < 15; i++) {
            let sensor = new Sensor();
            sensor.port = i;

            if(i < 8) {
                sensor.value = await this.hedgehog.getAnalog(i);
                sensor.type = SensorType.Analog;
            } else {
                sensor.value = await this.hedgehog.getDigital(i);
                sensor.type = SensorType.Digital;
            }

            documentBuilder.addResource(await this.serializerRegistry.serialize(sensor, req, documentBuilder));
        }

        return reply(documentBuilder.getProduct());
    }
}
<commit_msg>implement set sensor pullup<commit_after>///<reference path="../../../jsonapi/Parser.ts"/>
import Hapi = require('hapi');
import winston = require("winston");
import {HedgehogClient} from 'hedgehog-client';

import ApiResource from "../../ApiResource";
import SerializerRegistry from "../../../serializer/SerializerRegistry";
import ApiEndpoint from "../../ApiEndpoint";
import {IReply} from "hapi";
import JsonApiDocumentBuilder from "../../../jsonapi/JsonApiBuilder";
import {getLinkUrl, getRequestUrl} from "../../../utils";
import {SensorType, default as Sensor} from "../../../../common/Sensor";
import {DataType} from "../../../jsonapi/JsonApiBuilder";
import {JsonApiDocument, JsonApiResource} from "../../../jsonapi/JsonApiObjects";
import {RequirementType, ObjectParser} from "../../../jsonapi/Parser";

export default class SensorResource extends ApiResource {

    public constructor (private hedgehog: HedgehogClient, private serializerRegistry: SerializerRegistry) {
        super('/sensors');
    }

    @ApiEndpoint('GET', '/{sensorId}')
    public async getSensor (req: Hapi.Request, reply: IReply) {
        const sensorPort = Number(req.params['sensorId']);

        let sensor = new Sensor();
        sensor.port = sensorPort;

        if(sensorPort < 8) {
            sensor.value = await this.hedgehog.getAnalog(sensorPort);
            sensor.type = SensorType.Analog;
        } else {
            sensor.value = await this.hedgehog.getDigital(sensorPort);
            sensor.type = SensorType.Digital;
        }

        let documentBuilder = new JsonApiDocumentBuilder();
        documentBuilder.setLinks(getLinkUrl(req, `/api/sensors/${sensorPort}`), null);
        documentBuilder.addResource(await this.serializerRegistry.serialize(sensor, req, documentBuilder));
        return reply(documentBuilder.getProduct());
    }

    @ApiEndpoint('GET')
    public async getSensorList (req: Hapi.Request, reply: IReply) {
        let documentBuilder = new JsonApiDocumentBuilder();
        documentBuilder.setLinks(getRequestUrl(req), null);
        documentBuilder.setDataType(DataType.Many);

        for (let i = 0; i < 15; i++) {
            let sensor = new Sensor();
            sensor.port = i;

            if(i < 8) {
                sensor.value = await this.hedgehog.getAnalog(i);
                sensor.type = SensorType.Analog;
            } else {
                sensor.value = await this.hedgehog.getDigital(i);
                sensor.type = SensorType.Digital;
            }

            documentBuilder.addResource(await this.serializerRegistry.serialize(sensor, req, documentBuilder));
        }

        return reply(documentBuilder.getProduct());
    }

    @ApiEndpoint('PATCH', '/{sensorId}')
    public async setSensorPullup (req: Hapi.Request, reply: IReply) {
        const sensorPort = Number(req.params['sensorId']);

        let parser = JsonApiDocument.getParser().addProperties({
            name: 'data',
            required: RequirementType.Required,
            handler: JsonApiResource.getParser().addProperties({
                name: 'attributes',
                required: RequirementType.Required,
                handler: new ObjectParser(() => ({}), {
                        name: 'pullup',
                        required: RequirementType.Required
                })
            })
        });

        let pullup: boolean;
        try {
            pullup = (parser.parse(req.payload).data as JsonApiResource).attributes.pullup;
        } catch (err) {
            winston.error(err);
            return reply({
                error: 'Error while parsing the request. Argument might be missing.'
            }).code(400);
        }
        await this.hedgehog.setInputState(sensorPort, pullup);
        return reply('');
    }
}

{'fraction_non_alphanumeric': 0.13227091633466134, 'fraction_numerical': 0.0020717131474103584, 'mean_word_length': 2.519910263600673, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6982837', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1666, 'n_words': 380}