mirror of
https://github.com/mewtek/i2ME-Legacy.git
synced 2025-05-12 17:30:24 -05:00
HeatingAndCooling record generator
This commit is contained in:
parent
ec4d1d580b
commit
5320d22fae
19
main.py
19
main.py
@ -2,7 +2,7 @@ import asyncio
|
||||
from asyncio.log import logger
|
||||
from asyncore import loop
|
||||
import logging,coloredlogs
|
||||
from recordGenerators import DailyForecast,CurrentObservations,HourlyForecast,AirQuality,AirportDelays,PollenForecast,Breathing,Alerts
|
||||
import recordGenerators as RG
|
||||
from radar import TWCRadarProcessor, RadarProcessor
|
||||
import os
|
||||
|
||||
@ -35,7 +35,7 @@ l.info("Developed by mewtek32, Floppaa, Goldblaze, and needlenose")
|
||||
|
||||
async def grabAlertsLoop():
|
||||
while True:
|
||||
Alerts.makeRecord()
|
||||
RG.Alerts.makeRecord()
|
||||
await asyncio.sleep(60)
|
||||
|
||||
|
||||
@ -47,18 +47,19 @@ async def grabAlertsLoop():
|
||||
|
||||
async def FiveMinUpdaters():
|
||||
while True:
|
||||
CurrentObservations.makeDataFile()
|
||||
RG.CurrentObservations.makeDataFile()
|
||||
l.debug("Sleeping for 5 minutes...")
|
||||
await asyncio.sleep(5 * 60)
|
||||
|
||||
async def HourUpdaters():
|
||||
while True:
|
||||
DailyForecast.makeDataFile()
|
||||
HourlyForecast.makeDataFile()
|
||||
AirQuality.writeData()
|
||||
PollenForecast.makeDataFile()
|
||||
AirportDelays.writeData()
|
||||
Breathing.makeDataFile()
|
||||
RG.DailyForecast.makeDataFile()
|
||||
RG.HourlyForecast.makeDataFile()
|
||||
RG.AirQuality.writeData()
|
||||
RG.PollenForecast.makeDataFile()
|
||||
RG.AirportDelays.writeData()
|
||||
RG.Breathing.makeDataFile()
|
||||
RG.HeatingAndCooling.makeRecord()
|
||||
l.debug("Sleeping for an hour...")
|
||||
await asyncio.sleep(60 * 60)
|
||||
|
||||
|
76
recordGenerators/HeatingAndCooling.py
Normal file
76
recordGenerators/HeatingAndCooling.py
Normal file
@ -0,0 +1,76 @@
|
||||
import shutil
|
||||
import requests
|
||||
import logging,coloredlogs
|
||||
from py2Lib import bit
|
||||
import Util.MachineProductCfg as MPC
|
||||
import records.LFRecord as LFR
|
||||
import gzip
|
||||
from os import remove
|
||||
import xml.dom.minidom
|
||||
|
||||
l = logging.getLogger(__name__)
|
||||
coloredlogs.install()
|
||||
|
||||
geocodes = []
|
||||
coopIds = []
|
||||
|
||||
for i in MPC.getPrimaryLocations():
|
||||
coopIds.append(LFR.getCoopId(i))
|
||||
geocodes.append(LFR.getLatLong(i).replace('/', ','))
|
||||
|
||||
apiKey = "21d8a80b3d6b444998a80b3d6b1449d3"
|
||||
|
||||
def getData(coopId, geocode):
|
||||
fetchUrl = f"https://api.weather.com/indices/heatCool/daypart/7day?geocode={geocode}&language=en-US&format=xml&apiKey={apiKey}"
|
||||
|
||||
res = requests.get(fetchUrl)
|
||||
|
||||
if res.status_code != 200:
|
||||
l.error("DO NOT REPORT THE ERROR BELOW")
|
||||
l.error(f"Failed to write HeatingAndCooling record -- Status code {res.status_code}")
|
||||
return
|
||||
|
||||
data = res.text()
|
||||
data = data[63:-26]
|
||||
|
||||
i2Doc = f'<HeatingAndCooling id="000000000" locationKey="{coopId}" isWxScan="0">\n {data}\n <clientKey>{coopId}</clientKey>\n </HeatingAndCooling>'
|
||||
|
||||
f = open('./.temp/HeatingAndCooling.i2m', 'a')
|
||||
f.write(i2Doc)
|
||||
f.close()
|
||||
|
||||
def makeRecord():
|
||||
l.info("Writing HeatingAndCooling record.")
|
||||
|
||||
header = '<Data type="HeatingAndCooling"'
|
||||
footer = '</Data>'
|
||||
|
||||
with open('./.temp/HeatingAndCooling.i2m', 'a') as doc:
|
||||
doc.write(header)
|
||||
|
||||
for (x, y) in zip(coopIds, geocodes):
|
||||
getData(x,y)
|
||||
|
||||
with open('./.temp/HeatingAndCooling.i2m', 'a') as end:
|
||||
end.write(footer)
|
||||
|
||||
dom = xml.dom.minidom.parse('./.temp/HeatingAndCooling.i2m')
|
||||
xmlPretty = dom.toprettyxml(indent= " ")
|
||||
|
||||
with open('./.temp/HeatingAndCooling.i2m', 'w') as g:
|
||||
g.write(xmlPretty[23:])
|
||||
g.close()
|
||||
|
||||
|
||||
# Compresss i2m to gzip
|
||||
with open ('./.temp/HeatingAndCooling.i2m', 'rb') as f_in:
|
||||
with gzip.open('./.temp/HeatingAndCooling.gz', 'wb') as f_out:
|
||||
shutil.copyfileobj(f_in, f_out)
|
||||
|
||||
file = "./.temp/HeatingAndCooling.gz"
|
||||
command = '<MSG><Exec workRequest="storeData(File={0},QGROUP=__HeatingAndCooling__,Feed=HeatingAndCooling)" /><GzipCompressedMsg fname="HeatingAndCooling" /></MSG>'
|
||||
|
||||
bit.sendFile([file], [command], 1, 0)
|
||||
|
||||
remove('./.temp/HeatingAndCooling.i2m')
|
||||
remove('./.temp/HeatingAndCooling.gz')
|
Loading…
x
Reference in New Issue
Block a user