<commit_before>import { server as config } from './config';
import express from 'express';
import bodyParser from 'body-parser';
import serveStatic from 'serve-static';
import cors from 'cors';
import util from './lib/util';
import NotifyCondition from './lib/fiware/NotifyCondition';
import ContextBroker from './lib/fiware/ContextBroker';

// setup context broker
const contextBroker = new ContextBroker({
	url: config.brokerUrl
});

// create APP
const app = express();

// allow cors from all requests
app.use(cors());

// use json body parser
app.use(bodyParser.json());

// serve static files from apps
app.use(serveStatic('apps', {
	index: 'index.html'
}));

// provide some helpful information on default route
app.get('/', (req, res) => {
	logRequest(req);

	res.send(`
		<h1>Fiware test server</h1>
		<h2>Supported methods</h2>
		<ul>
			<li>
				<strong>GET /</strong>
				- displays this help information
			</li>
			<li>
				<strong>GET /setup</strong>
				- setups test data for empty context broker
			</li>
			<li>
				<strong>PUT /update/:entity/:attribute</strong>
			- updates given entity attribute with value defined in body json "value"
			</li>
			<li>
				<strong>GET /info/:id</strong>
				- displays entity info
				</li>
			<li>
				<strong>POST /mirror</strong>
				- renders requested JSON request in response and in console
			</li>
			<li>
				<strong>POST /aggregate/:valueAttributeName/:historyAttributeName/:maxHistoryEntries</strong>
				- aggregates changed values to an array of last values
			</li>
		</ul>
		
		<h2>Examples</h2>
		<ul>
			<li>
				<a href="/setup"><strong>GET /setup</strong></a>
				- creates the initial test entity or resets it if already exists
			</li>
			<li>
				<strong>PUT /update/lab/brightness</strong> {"value": 50}
				- updates lab brightness to 50 percent
			</li>
			<li>
				<a href="/info/lab"><strong>GET /info/lab</strong></a>
				- display lab information
			</li>
		</ul>
		
		<h2>Apps</h2>
		<ul>
			<li>
				<a href="/chart"><strong>GET /chart</strong></a>
				- displays live brightness chart from FIWARE, updated by a wireless Arduino YUN device.
			</li>
		</ul>
	`);
});

// sets up the test data
app.get('/setup', (req, res) => {
	logRequest(req);

	const id = 'lab';
	const type = 'room';
	const attributes = [{
		name: 'brightness',
		type: 'float',
		value: '0'
	}, {
		name: 'brightness-history',
		type: 'array',
		value: [0]
	}, {
		name: 'brightness-count',
		type: 'number',
		value: '0'
	}, {
		name: 'last-updated',
		type: 'date',
		value: util.getIsoDate()
	}];
	
	contextBroker
		.createEntity(id, type, attributes)
		.then(handleQueryResponse())
	
		.then(contextBroker.createSubscription({
			entities: [{
				type: type,
				isPattern: false,
				id: id
			}],
			attributes: [
				'brightness',
				'brightness-history',<commit_msg>Providing initial value for brightness history.<commit_after>import { server as config } from './config';
import express from 'express';
import bodyParser from 'body-parser';
import serveStatic from 'serve-static';
import cors from 'cors';
import util from './lib/util';
import NotifyCondition from './lib/fiware/NotifyCondition';
import ContextBroker from './lib/fiware/ContextBroker';

// setup context broker
const contextBroker = new ContextBroker({
	url: config.brokerUrl
});

// create APP
const app = express();

// allow cors from all requests
app.use(cors());

// use json body parser
app.use(bodyParser.json());

// serve static files from apps
app.use(serveStatic('apps', {
	index: 'index.html'
}));

// provide some helpful information on default route
app.get('/', (req, res) => {
	logRequest(req);

	res.send(`
		<h1>Fiware test server</h1>
		<h2>Supported methods</h2>
		<ul>
			<li>
				<strong>GET /</strong>
				- displays this help information
			</li>
			<li>
				<strong>GET /setup</strong>
				- setups test data for empty context broker
			</li>
			<li>
				<strong>PUT /update/:entity/:attribute</strong>
			- updates given entity attribute with value defined in body json "value"
			</li>
			<li>
				<strong>GET /info/:id</strong>
				- displays entity info
				</li>
			<li>
				<strong>POST /mirror</strong>
				- renders requested JSON request in response and in console
			</li>
			<li>
				<strong>POST /aggregate/:valueAttributeName/:historyAttributeName/:maxHistoryEntries</strong>
				- aggregates changed values to an array of last values
			</li>
		</ul>
		
		<h2>Examples</h2>
		<ul>
			<li>
				<a href="/setup"><strong>GET /setup</strong></a>
				- creates the initial test entity or resets it if already exists
			</li>
			<li>
				<strong>PUT /update/lab/brightness</strong> {"value": 50}
				- updates lab brightness to 50 percent
			</li>
			<li>
				<a href="/info/lab"><strong>GET /info/lab</strong></a>
				- display lab information
			</li>
		</ul>
		
		<h2>Apps</h2>
		<ul>
			<li>
				<a href="/chart"><strong>GET /chart</strong></a>
				- displays live brightness chart from FIWARE, updated by a wireless Arduino YUN device.
			</li>
		</ul>
	`);
});

// sets up the test data
app.get('/setup', (req, res) => {
	logRequest(req);

	const id = 'lab';
	const type = 'room';
	const attributes = [{
		name: 'brightness',
		type: 'float',
		value: '0'
	}, {
		name: 'brightness-history',
		type: 'array',
		value: [[util.getIsoDate(), 0]]
	}, {
		name: 'brightness-count',
		type: 'number',
		value: '0'
	}, {
		name: 'last-updated',
		type: 'date',
		value: util.getIsoDate()
	}];
	
	contextBroker
		.createEntity(id, type, attributes)
		.then(handleQueryResponse())
	
		.then(contextBroker.createSubscription({
			entities: [{
				type: type,
				isPattern: false,
				id: id
			}],
			attributes: [
				'brightness',
				'brightness-history',
{'fraction_non_alphanumeric': 0.15848331294775467, 'fraction_numerical': 0.005242005940940066, 'mean_word_length': 3.1934065934065936, 'pattern_counts': {'":': 2, '<': 123, '<?xml version=': 0, '>': 127, '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': '5797162', 'n_tokens_mistral': 2258, 'n_tokens_neox': 1923, 'n_words': 545}