diff --git a/routes/local.ts b/routes/local.ts index 833c473..10aa1fa 100644 --- a/routes/local.ts +++ b/routes/local.ts @@ -1,8 +1,9 @@ import * as express from "express"; +import { CronJob } from "cron"; -const CronJob = require( "cron" ).CronJob, - server = require( "../server.js" ), - count = { temp: 0, humidity: 0 }; +import * as server from "../server"; + +const count = { temp: 0, humidity: 0 }; let today: PWSStatus = {}, yesterday: PWSStatus = {}, @@ -15,7 +16,7 @@ function sameDay(d1: Date, d2: Date): boolean { 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; 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" ); }; -exports.useLocalWeather = function(): boolean { +export const useLocalWeather = function(): boolean { return server.pws !== "none" ? true : false; }; -exports.getLocalWeather = function(): LocalWeather { +export const getLocalWeather = function(): LocalWeather { const result: LocalWeather = {}; // Use today's weather if we dont have information for yesterday yet (i.e. on startup) diff --git a/routes/weather.ts b/routes/weather.ts index 535d41b..d0fcef9 100644 --- a/routes/weather.ts +++ b/routes/weather.ts @@ -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"; -const http = require( "http" ), - https = require ( "https" ), - local = require( "./local"), - SunCalc = require( "suncalc" ), - moment = require( "moment-timezone" ), - geoTZ = require( "geo-tz" ), - - // 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})/ - }; +// Define regex filters to match against location +const 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. @@ -174,7 +174,8 @@ async function getOWMWeatherData( coordinates: GeoCoordinates ): Promise< Weathe * @return A Promise that will be resolved with 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; } -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); 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 // adjustment method which is encoded to also carry the watering // 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 // parsed. This allows the adjustment method and the restriction type to both diff --git a/server.ts b/server.ts index 2716c85..d9b4d9a 100644 --- a/server.ts +++ b/server.ts @@ -1,16 +1,20 @@ -const packageJson = require( "../package.json" ), - express = require( "express" ), - weather = require( "./routes/weather.js" ), - local = require( "./routes/local.js" ), - cors = require( "cors" ); +import * as express from "express"; +import * as cors from "cors"; +import * as dotenv from "dotenv"; + +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", - port = process.env.PORT || 3000, - pws = process.env.PWS || "none", - app = express(); + port = process.env.PORT || 3000; + +export let pws = process.env.PWS || "none"; +export const app = express(); if ( !process.env.HOST || !process.env.PORT || !process.env.LOCAL_PWS ) { - require( "dotenv" ).load(); + dotenv.load(); host = process.env.HOST || host; port = process.env.PORT || port; pws = process.env.PWS || pws; @@ -42,13 +46,10 @@ app.use( function( req, res ) { } ); // 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 ); if (pws !== "none" ) { console.log( "%s now listening for local weather stream", packageJson.description ); } } ); - -exports.app = app; -exports.pws = pws; diff --git a/tsconfig.json b/tsconfig.json index 98a056c..aaf0e43 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es6", + "target": "es5", "noImplicitReturns": true, "noEmitOnError": true, "outDir": "js/",