Skip to content

Commit

Permalink
feat: Console
Browse files Browse the repository at this point in the history
Update CHANGELOG.md
Update response.dev.js
Update response.js
Update setENV.mjs
  • Loading branch information
VirgilClyne committed Dec 2, 2024
1 parent 3c02cc2 commit de2f04f
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 223 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
### 🛠️ Bug Fixes
* 修复 `$argument``$persistentStore` 载入顺序颠倒的问题
* 正确顺序为先读取 `$argument` 再读取 `$persistentStore (BoxJs)`
* 即,有相同键名时,`$persistentStore (BoxJs)` 的值会覆盖 `$argument` 的值

### 🔣 Dependencies
* 升级了 `@nsnanocat/util`
* `util``submodule` 更改为 `package`
* `$platform` 改为 `$app`
* 使用了全新的 `Console` polyfill
78 changes: 39 additions & 39 deletions src/class/AirQuality.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log } from "@nsnanocat/util";
import { Console } from "@nsnanocat/util";

export default class AirQuality {
static Name = "AirQuality";
Expand Down Expand Up @@ -429,58 +429,58 @@ export default class AirQuality {
};

static Pollutants(pollutants = [], scale = "WAQI_InstantCast") {
log(`☑️ Pollutants, scale: ${scale}`, "");
Console.log("☑️ Pollutants", `scale: ${scale}`);
pollutants = pollutants.map(pollutant => {
// Convert unit based on standard
const PollutantStandard = this.#Config.Scales[scale].pollutants[pollutant.pollutantType];
pollutant.convertedAmount = this.ConvertUnit(pollutant.amount, pollutant.units, PollutantStandard.units, PollutantStandard.ppxToXGM3);
const PollutantStandard = AirQuality.#Config.Scales[scale].pollutants[pollutant.pollutantType];
pollutant.convertedAmount = AirQuality.ConvertUnit(pollutant.amount, pollutant.units, PollutantStandard.units, PollutantStandard.ppxToXGM3);
pollutant.convertedUnits = PollutantStandard.units;
pollutant = { ...PollutantStandard, ...pollutant };
// Calculate AQI for each pollutant
let categoryIndexKey;
for (const [key, value] of Object.entries(pollutant.ranges)) {
categoryIndexKey = parseInt(key, 10);
categoryIndexKey = Number.parseInt(key, 10);
if (pollutant.convertedAmount >= value[0] && pollutant.convertedAmount <= value[1]) break;
};
pollutant.range = pollutant.ranges[categoryIndexKey];
pollutant.categoryIndex = parseInt(categoryIndexKey, 10);
pollutant.category = this.#Config.Scales[scale].categoryIndex[categoryIndexKey];
pollutant.categoryIndex = Number.parseInt(categoryIndexKey, 10);
pollutant.category = AirQuality.#Config.Scales[scale].categoryIndex[categoryIndexKey];
pollutant.AQI = Math.round(
((pollutant.category[1] - pollutant.category[0]) * (pollutant.convertedAmount - pollutant.range[0])) / (pollutant.range[1] - pollutant.range[0])
+ pollutant.category[0],
);
return pollutant;
});
//log(`🚧 Pollutants, pollutants: ${JSON.stringify(pollutants, null, 2)}`, "");
log(`✅ Pollutants`, "");
//Console.debug(`pollutants: ${JSON.stringify(pollutants, null, 2)}`);
Console.log("✅ Pollutants");
return pollutants;
};

static ConvertScale(pollutants = [], scale = "WAQI_InstantCast", convertUnits = false) {
log(`☑️ ConvertScale`, "");
pollutants = this.Pollutants(pollutants, scale);
Console.log("☑️ ConvertScale");
pollutants = AirQuality.Pollutants(pollutants, scale);
const { AQI: index, pollutantType: primaryPollutant } = pollutants.reduce((previous, current) => previous.AQI > current.AQI ? previous : current);
let airQuality = {
const airQuality = {
"index": index,
"pollutants": pollutants,
"scale": this.#Config.Scales[scale].scale,
"scale": AirQuality.#Config.Scales[scale].scale,
"primaryPollutant": primaryPollutant,
"categoryIndex": this.CategoryIndex(index, scale),
"categoryIndex": AirQuality.CategoryIndex(index, scale),
};
airQuality.isSignificant = airQuality.categoryIndex >= this.#Config.Scales[scale].significant;
airQuality.isSignificant = airQuality.categoryIndex >= AirQuality.#Config.Scales[scale].significant;
if (convertUnits) airQuality.pollutants = airQuality.pollutants.map(pollutant => {
pollutant.amount = pollutant.convertedAmount;
pollutant.units = pollutant.convertedUnits;
return pollutant;
});
//log(`🚧 ConvertScale, airQuality: ${JSON.stringify(airQuality, null, 2)}`, "");
log(`✅ ConvertScale`, "");
//Console.debug(`airQuality: ${JSON.stringify(airQuality, null, 2)}`);
Console.log("✅ ConvertScale");
return airQuality;
};

static ConvertUnit(amount = Number(), unitFrom, unitTo, ppxToXGM3Value = -1) {
//log(`☑️ ConvertUnit`, "");
//log(`☑️ ConvertUnit\namount: ${amount} ppxToXGM3Value: ${ppxToXGM3Value}\nunitFrom: ${unitFrom} unitTo: ${unitTo}`, "");
static ConvertUnit(amount, unitFrom, unitTo, ppxToXGM3Value = -1) {
Console.log("☑️ ConvertUnit");
Console.debug(`amount: ${amount}`, `ppxToXGM3Value: ${ppxToXGM3Value}`, `unitFrom: ${unitFrom}`, `unitTo: ${unitTo}`);
if (amount < 0) amount = -1;
else switch (unitFrom) {
case 'PARTS_PER_MILLION':
Expand All @@ -494,10 +494,10 @@ export default class AirQuality {
amount = amount * ppxToXGM3Value;
break;
case 'MICROGRAMS_PER_CUBIC_METER': {
const inPpb = this.ConvertUnit(amount, unitFrom, 'PARTS_PER_BILLION', ppxToXGM3Value);
const inPpb = AirQuality.ConvertUnit(amount, unitFrom, 'PARTS_PER_BILLION', ppxToXGM3Value);
amount = inPpb * ppxToXGM3Value;
break;
};
}
default:
amount = -1;
break;
Expand All @@ -511,10 +511,10 @@ export default class AirQuality {
amount = amount * 0.001;
break;
case 'MILLIGRAMS_PER_CUBIC_METER': {
const inPpm = this.ConvertUnit(amount, unitFrom, 'PARTS_PER_MILLION', ppxToXGM3Value);
const inPpm = AirQuality.ConvertUnit(amount, unitFrom, 'PARTS_PER_MILLION', ppxToXGM3Value);
amount = inPpm * ppxToXGM3Value;
break;
};
}
case 'MICROGRAMS_PER_CUBIC_METER':
amount = amount * ppxToXGM3Value;
break;
Expand All @@ -534,10 +534,10 @@ export default class AirQuality {
amount = amount / ppxToXGM3Value;
break;
case 'PARTS_PER_BILLION': {
const inUgM3 = this.ConvertUnit(amount, unitFrom, 'MICROGRAMS_PER_CUBIC_METER', ppxToXGM3Value);
const inUgM3 = AirQuality.ConvertUnit(amount, unitFrom, 'MICROGRAMS_PER_CUBIC_METER', ppxToXGM3Value);
amount = inUgM3 / ppxToXGM3Value;
break;
};
}
default:
amount = -1;
break;
Expand All @@ -551,10 +551,10 @@ export default class AirQuality {
amount = amount * 0.001;
break;
case 'PARTS_PER_MILLION': {
const inMgM3 = this.ConvertUnit(amount, unitFrom, 'MILLIGRAMS_PER_CUBIC_METER', ppxToXGM3Value);
const inMgM3 = AirQuality.ConvertUnit(amount, unitFrom, 'MILLIGRAMS_PER_CUBIC_METER', ppxToXGM3Value);
amount = inMgM3 / ppxToXGM3Value;
break;
};
}
case 'PARTS_PER_BILLION':
amount = amount / ppxToXGM3Value;
break;
Expand All @@ -567,7 +567,7 @@ export default class AirQuality {
amount = -1;
break;
};
//log(`✅ ConvertUnit, amount: ${amount}`, "");
//Console.log("✅ ConvertUnit", `amount: ${amount}`);
return amount;
};

Expand All @@ -576,21 +576,21 @@ export default class AirQuality {
case "number":
break;
case "string":
aqi = parseInt(aqi, 10);
aqi = Number.parseInt(aqi, 10);
break;
};
log(`☑️ CategoryIndex, aqi: ${aqi}`, "");
Console.log("☑️ CategoryIndex", `aqi: ${aqi}`);
let categoryIndex;
for (const [key, value] of Object.entries(this.#Config.Scales[scale].categoryIndex)) {
categoryIndex = parseInt(key, 10);
for (const [key, value] of Object.entries(AirQuality.#Config.Scales[scale].categoryIndex)) {
categoryIndex = Number.parseInt(key, 10);
if (aqi >= value[0] && aqi <= value[1]) break;
};
log(`✅ CategoryIndex, categoryIndex: ${categoryIndex}`, "");
Console.log("✅ CategoryIndex", `categoryIndex: ${categoryIndex}`);
return categoryIndex;
};

static ComparisonTrend(todayAQI, yesterdayAQI) {
log(`☑️ ComparisonTrend, todayAQI: ${todayAQI}, yesterdayAQI: ${yesterdayAQI}`, "");
Console.log("☑️ ComparisonTrend", `todayAQI: ${todayAQI}`, `yesterdayAQI: ${yesterdayAQI}`);
let trend = "UNKNOWN";
if (isNaN(todayAQI - yesterdayAQI)) trend = "UNKNOWN";
else switch (todayAQI - yesterdayAQI) {
Expand Down Expand Up @@ -636,12 +636,12 @@ export default class AirQuality {
};
break;
};
log(`✅ ComparisonTrend, trend: ${trend}`, "");
Console.log("✅ ComparisonTrend", `trend: ${trend}`);
return trend;
};

static FixUnits(pollutants = []) {
log(`☑️ FixUnits`, "");
Console.log("☑️ FixUnits");
pollutants = pollutants.map(pollutant => {
switch (pollutant.units) {
case "PARTS_PER_MILLION":
Expand All @@ -657,8 +657,8 @@ export default class AirQuality {
};
return pollutant;
});
//log(`🚧 FixUnits, pollutants: ${JSON.stringify(pollutants, null, 2)}`, "");
log(`✅ FixUnits`, "");
//Console.debug(`pollutants: ${JSON.stringify(pollutants, null, 2)}`);
Console.log("✅ FixUnits");
return pollutants;
};
};
34 changes: 17 additions & 17 deletions src/class/ColorfulClouds.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Lodash as _, fetch, log, logError } from "@nsnanocat/util";
import { Console, fetch, Lodash as _ } from "@nsnanocat/util";
import AirQuality from "./AirQuality.mjs";
import ForecastNextHour from "./ForecastNextHour.mjs";
import parseWeatherKitURL from "../function/parseWeatherKitURL.mjs";
Expand All @@ -8,7 +8,7 @@ export default class ColorfulClouds {
constructor(options) {
this.Name = "ColorfulClouds";
this.Version = "3.0.5";
log(`\n🟧 ${this.Name} v${this.Version}\n`, "");
Console.log(`🟧 ${this.Name} v${this.Version}`);
this.url = new URL($request.url);
this.header = { "Content-Type": "application/json" };
const Parameters = parseWeatherKitURL(this.url);
Expand All @@ -30,7 +30,7 @@ export default class ColorfulClouds {
};

async RealTime(token = this.token) {
log(`☑️ RealTime`, "");
Console.log("☑️ RealTime");
const request = {
"url": `https://api.caiyunapp.com/v2.6/${token}/${this.longitude},${this.latitude}/realtime`,
"header": this.header,
Expand Down Expand Up @@ -58,7 +58,7 @@ export default class ColorfulClouds {
"sourceType": "STATION",
},
"categoryIndex": AirQuality.CategoryIndex(body?.result?.realtime?.air_quality?.aqi.chn, "HJ_633"),
"index": parseInt(body?.result?.realtime?.air_quality?.aqi.chn, 10),
"index": Number.parseInt(body?.result?.realtime?.air_quality?.aqi.chn, 10),
"isSignificant": true,
"pollutants": this.#CreatePollutants(body?.result?.realtime?.air_quality),
"previousDayComparison": "UNKNOWN",
Expand All @@ -79,14 +79,14 @@ export default class ColorfulClouds {
} catch (error) {
this.logErr(error);
} finally {
//log(`🚧 RealTime airQuality: ${JSON.stringify(airQuality, null, 2)}`, "");
log(`✅ RealTime`, "");
//Console.debug(`airQuality: ${JSON.stringify(airQuality, null, 2)}`);
Console.log("✅ RealTime");
return airQuality;
};
};

async Minutely(token = this.token) {
log(`☑️ Minutely`, "");
Console.log("☑️ Minutely");
const request = {
"url": `https://api.caiyunapp.com/v2.6/${token}/${this.longitude},${this.latitude}/minutely?unit=metric:v2`,
"header": this.header,
Expand Down Expand Up @@ -152,16 +152,16 @@ export default class ColorfulClouds {
throw Error(JSON.stringify(body ?? {}));
};
} catch (error) {
logError(error);
Console.error(error);
} finally {
//log(`🚧 forecastNextHour: ${JSON.stringify(forecastNextHour, null, 2)}`, "");
log(`✅ Minutely`, "");
//Console.debug(`forecastNextHour: ${JSON.stringify(forecastNextHour, null, 2)}`);
Console.log("✅ Minutely");
return forecastNextHour;
};
};

async Hourly(token = this.token, hourlysteps = 1, begin = Date.now()) {
log(`☑️ Hourly`, "");
Console.log("☑️ Hourly");
const request = {
"url": `https://api.caiyunapp.com/v2.6/${token}/${this.longitude},${this.latitude}/hourly?hourlysteps=${hourlysteps}&begin=${parseInt(begin / 1000, 10)}`,
"header": this.header,
Expand Down Expand Up @@ -189,7 +189,7 @@ export default class ColorfulClouds {
"sourceType": "STATION",
},
"categoryIndex": AirQuality.CategoryIndex(body?.result?.hourly?.air_quality?.aqi?.[0]?.value?.chn, "HJ_633"),
"index": parseInt(body?.result?.hourly?.air_quality?.aqi?.[0]?.value?.chn, 10),
"index": Number.parseInt(body?.result?.hourly?.air_quality?.aqi?.[0]?.value?.chn, 10),
"isSignificant": true,
"pollutants": [],
"previousDayComparison": "UNKNOWN",
Expand All @@ -210,14 +210,14 @@ export default class ColorfulClouds {
} catch (error) {
this.logErr(error);
} finally {
//log(`🚧 Hourly airQuality: ${JSON.stringify(airQuality, null, 2)}`, "");
log(`✅ Hourly`, "");
//Console.debug(`airQuality: ${JSON.stringify(airQuality, null, 2)}`);
Console.log("✅ Hourly");
return airQuality;
};
};

#CreatePollutants(pollutantsObj = {}) {
console.log(`☑️ CreatePollutants`, "");
Console.log("☑️ CreatePollutants");
let pollutants = [];
for (const [key, value] of Object.entries(pollutantsObj)) {
switch (key) {
Expand All @@ -243,8 +243,8 @@ export default class ColorfulClouds {
break;
};
};
//console.log(`🚧 CreatePollutants, pollutants: ${JSON.stringify(pollutants, null, 2)}`, "");
console.log(`✅ CreatePollutants`, "");
//Console.debug(`pollutants: ${JSON.stringify(pollutants, null, 2)}`);
Console.log("✅ CreatePollutants");
return pollutants;
};
};
Loading

0 comments on commit de2f04f

Please sign in to comment.