Use ES6 modules

This commit is contained in:
Matthew Oslan
2019-05-12 01:38:27 -04:00
parent f0c32a32ba
commit 31af9d3320
4 changed files with 42 additions and 39 deletions

View File

@@ -1,8 +1,9 @@
import * as express from "express"; import * as express from "express";
import { CronJob } from "cron";
const CronJob = require( "cron" ).CronJob, import * as server from "../server";
server = require( "../server.js" ),
count = { temp: 0, humidity: 0 }; const count = { temp: 0, humidity: 0 };
let today: PWSStatus = {}, let today: PWSStatus = {},
yesterday: PWSStatus = {}, yesterday: PWSStatus = {},
@@ -15,7 +16,7 @@ function sameDay(d1: Date, d2: Date): boolean {
d1.getDate() === d2.getDate(); d1.getDate() === d2.getDate();
} }
exports.captureWUStream = function( req: express.Request, res: express.Response ) { export const captureWUStream = function( req: express.Request, res: express.Response ) {
let prev: number, curr: number; let prev: number, curr: number;
if ( !( "dateutc" in req.query ) || !sameDay( current_date, new Date( req.query.dateutc + "Z") )) { if ( !( "dateutc" in req.query ) || !sameDay( current_date, new Date( req.query.dateutc + "Z") )) {
@@ -43,11 +44,11 @@ exports.captureWUStream = function( req: express.Request, res: express.Response
res.send( "success\n" ); res.send( "success\n" );
}; };
exports.useLocalWeather = function(): boolean { export const useLocalWeather = function(): boolean {
return server.pws !== "none" ? true : false; return server.pws !== "none" ? true : false;
}; };
exports.getLocalWeather = function(): LocalWeather { export const getLocalWeather = function(): LocalWeather {
const result: LocalWeather = {}; const result: LocalWeather = {};
// Use today's weather if we dont have information for yesterday yet (i.e. on startup) // Use today's weather if we dont have information for yesterday yet (i.e. on startup)

View File

@@ -1,21 +1,21 @@
import * as express from "express"; import * as express from "express";
import * as http from "http";
import * as https from "https";
import * as SunCalc from "suncalc";
import * as moment from "moment-timezone";
import * as geoTZ from "geo-tz";
import * as local from "./local";
import { AdjustmentOptions, GeoCoordinates, TimeData, WateringData, WeatherData } from "../types"; import { AdjustmentOptions, GeoCoordinates, TimeData, WateringData, WeatherData } from "../types";
const http = require( "http" ), // Define regex filters to match against location
https = require ( "https" ), const filters = {
local = require( "./local"), gps: /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/,
SunCalc = require( "suncalc" ), pws: /^(?:pws|icao|zmw):/,
moment = require( "moment-timezone" ), url: /^https?:\/\/([\w\.-]+)(:\d+)?(\/.*)?$/,
geoTZ = require( "geo-tz" ), time: /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})([+-])(\d{2})(\d{2})/,
timezone: /^()()()()()()([+-])(\d{2})(\d{2})/
// Define regex filters to match against location };
filters = {
gps: /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/,
pws: /^(?:pws|icao|zmw):/,
url: /^https?:\/\/([\w\.-]+)(:\d+)?(\/.*)?$/,
time: /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})([+-])(\d{2})(\d{2})/,
timezone: /^()()()()()()([+-])(\d{2})(\d{2})/
};
/** /**
* Resolves a location description to geographic coordinates. * Resolves a location description to geographic coordinates.
@@ -174,7 +174,8 @@ async function getOWMWeatherData( coordinates: GeoCoordinates ): Promise< Weathe
* @return A Promise that will be resolved with WateringData. * @return A Promise that will be resolved with WateringData.
*/ */
async function getLocalWateringData( coordinates: GeoCoordinates ): Promise< WateringData > { async function getLocalWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
return local.getLocalWeather(); // TODO is this type assertion safe?
return local.getLocalWeather() as WateringData;
} }
/** /**
@@ -279,7 +280,7 @@ function checkWeatherRestriction( adjustmentValue: number, weather: WateringData
return false; return false;
} }
exports.getWeatherData = async function( req: express.Request, res: express.Response ) { export const getWeatherData = async function( req: express.Request, res: express.Response ) {
const location: string = getParameter(req.query.loc); const location: string = getParameter(req.query.loc);
if ( !location ) { if ( !location ) {
@@ -309,7 +310,7 @@ exports.getWeatherData = async function( req: express.Request, res: express.Resp
// API Handler when using the weatherX.py where X represents the // API Handler when using the weatherX.py where X represents the
// adjustment method which is encoded to also carry the watering // adjustment method which is encoded to also carry the watering
// restriction and therefore must be decoded // restriction and therefore must be decoded
exports.getWateringData = async function( req: express.Request, res: express.Response ) { export const getWateringData = async function( req: express.Request, res: express.Response ) {
// The adjustment method is encoded by the OpenSprinkler firmware and must be // The adjustment method is encoded by the OpenSprinkler firmware and must be
// parsed. This allows the adjustment method and the restriction type to both // parsed. This allows the adjustment method and the restriction type to both

View File

@@ -1,16 +1,20 @@
const packageJson = require( "../package.json" ), import * as express from "express";
express = require( "express" ), import * as cors from "cors";
weather = require( "./routes/weather.js" ), import * as dotenv from "dotenv";
local = require( "./routes/local.js" ),
cors = require( "cors" ); import * as weather from "./routes/weather";
import * as local from "./routes/local";
const packageJson = require( "../package.json" );
let host = process.env.HOST || "127.0.0.1", let host = process.env.HOST || "127.0.0.1",
port = process.env.PORT || 3000, port = process.env.PORT || 3000;
pws = process.env.PWS || "none",
app = express(); export let pws = process.env.PWS || "none";
export const app = express();
if ( !process.env.HOST || !process.env.PORT || !process.env.LOCAL_PWS ) { if ( !process.env.HOST || !process.env.PORT || !process.env.LOCAL_PWS ) {
require( "dotenv" ).load(); dotenv.load();
host = process.env.HOST || host; host = process.env.HOST || host;
port = process.env.PORT || port; port = process.env.PORT || port;
pws = process.env.PWS || pws; pws = process.env.PWS || pws;
@@ -42,13 +46,10 @@ app.use( function( req, res ) {
} ); } );
// Start listening on the service port // Start listening on the service port
app.listen( port, host, function() { app.listen( +port, host, function() {
console.log( "%s now listening on %s:%s", packageJson.description, host, port ); console.log( "%s now listening on %s:%s", packageJson.description, host, port );
if (pws !== "none" ) { if (pws !== "none" ) {
console.log( "%s now listening for local weather stream", packageJson.description ); console.log( "%s now listening for local weather stream", packageJson.description );
} }
} ); } );
exports.app = app;
exports.pws = pws;

View File

@@ -1,6 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es6", "target": "es5",
"noImplicitReturns": true, "noImplicitReturns": true,
"noEmitOnError": true, "noEmitOnError": true,
"outDir": "js/", "outDir": "js/",