Skip to content

Commit

Permalink
Implement UI and backend for localization settings.
Browse files Browse the repository at this point in the history
Setting the country, timezone, and language are fully implemented,
but the unit settings will require some additional effort to plumb
through the whole UI.

References WebThingsIO#1523

Fixes WebThingsIO#808
Fixes WebThingsIO#1113
Fixes WebThingsIO#1530
Fixes WebThingsIO#2123
  • Loading branch information
mrstegeman committed Oct 18, 2019
1 parent 2dbd2a5 commit e7889e9
Show file tree
Hide file tree
Showing 19 changed files with 903 additions and 44 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"callsites": "^3.1.0",
"compression": "^1.7.4",
"config": "^3.2.2",
"country-list": "^2.2.0",
"csv-parse": "^4.4.7",
"dnssd": "^0.4.1",
"express": "^4.17.1",
Expand All @@ -54,6 +55,7 @@
"glob-to-regexp": "^0.4.1",
"http-proxy": "^1.18.0",
"ip-regex": "^4.1.0",
"iso-639-1": "^2.1.0",
"jsonwebtoken": "^8.5.1",
"mkdirp": "^0.5.1",
"ncp": "^2.0.0",
Expand Down
154 changes: 153 additions & 1 deletion src/controllers/settings_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

const CertificateManager = require('../certificate-manager');
const config = require('config');
const Constants = require('../constants');
const fetch = require('node-fetch');
const fs = require('fs');
const ISO6391 = require('iso-639-1');
const jwtMiddleware = require('../jwt-middleware');
const mDNSserver = require('../mdns-server');
const Platform = require('../platform');
const path = require('path');
const pkg = require('../../package.json');
const Platform = require('../platform');
const PromiseRouter = require('express-promise-router');
const Settings = require('../models/settings');
const TunnelService = require('../ssltunnel');
Expand Down Expand Up @@ -441,4 +445,152 @@ SettingsController.get('/network/addresses', auth, (request, response) => {
}
});

SettingsController.get('/localization/country', auth, (request, response) => {
let valid = [];
if (Platform.implemented('getValidWirelessCountries')) {
valid = Platform.getValidWirelessCountries();
}

let current = '';
if (Platform.implemented('getWirelessCountry')) {
current = Platform.getWirelessCountry();
}

const setImplemented = Platform.implemented('setWirelessCountry');
response.json({valid, current, setImplemented});
});

SettingsController.put('/localization/country', auth, (request, response) => {
if (!request.body || !request.body.hasOwnProperty('country')) {
response.status(400).send('Missing country property');
return;
}

if (Platform.implemented('setWirelessCountry')) {
if (Platform.setWirelessCountry(request.body.country)) {
response.status(200).json({});
} else {
response.status(500).send('Failed to update country');
}
} else {
response.status(500).send('Setting country not implemented');
}
});

SettingsController.get('/localization/timezone', auth, (request, response) => {
let valid = [];
if (Platform.implemented('getValidTimezones')) {
valid = Platform.getValidTimezones();
}

let current = '';
if (Platform.implemented('getTimezone')) {
current = Platform.getTimezone();
}

const setImplemented = Platform.implemented('setTimezone');
response.json({valid, current, setImplemented});
});

SettingsController.put('/localization/timezone', auth, (request, response) => {
if (!request.body || !request.body.hasOwnProperty('zone')) {
response.status(400).send('Missing zone property');
return;
}

if (Platform.implemented('setTimezone')) {
if (Platform.setTimezone(request.body.zone)) {
response.status(200).json({});
} else {
response.status(500).send('Failed to update timezone');
}
} else {
response.status(500).send('Setting timezone not implemented');
}
});

SettingsController.get(
'/localization/language',
auth,
async (request, response) => {
const fluentDir = path.join(Constants.BUILD_STATIC_PATH, 'fluent');
const valid = [];
try {
for (const dirname of fs.readdirSync(fluentDir)) {
const [language, country] = dirname.split('-');

valid.push({
code: dirname,
name: `${ISO6391.getName(language)}${country ? ` (${country})` : ''}`,
});
}

valid.sort((a, b) => a.name.localeCompare(b.name));
} catch (_) {
response.status(500).send('Failed to retrieve list of languages');
return;
}

try {
const current = await Settings.get('localization.language');
response.json({valid, current});
} catch (_) {
response.status(500).send('Failed to get current language');
}
}
);

SettingsController.put(
'/localization/language',
auth,
async (request, response) => {
if (!request.body || !request.body.hasOwnProperty('language')) {
response.status(400).send('Missing language property');
return;
}

try {
await Settings.set('localization.language', request.body.language);
response.json({});
} catch (_) {
response.status(500).send('Failed to set language');
}
}
);

SettingsController.get(
'/localization/units',
auth,
async (request, response) => {
let temperature;

try {
temperature = await Settings.get('localization.units.temperature');
} catch (e) {
// pass
}

response.json({
temperature: temperature || 'celsius',
});
}
);

SettingsController.put(
'/localization/units',
auth,
async (request, response) => {
for (const [key, value] of Object.entries(request.body)) {
try {
await Settings.set(`localization.units.${key}`, value);
} catch (_) {
response.status(500).send('Failed to set unit');
return;
}
}

response.json({});
}
);

module.exports = SettingsController;
6 changes: 6 additions & 0 deletions src/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ const wrappedMethods = [
'restartSystem',
'scanWirelessNetworks',
'update',
'getValidTimezones',
'getTimezone',
'setTimezone',
'getValidWirelessCountries',
'getWirelessCountry',
'setWirelessCountry',
];

for (const method of wrappedMethods) {
Expand Down
Loading

0 comments on commit e7889e9

Please sign in to comment.