Thanks Tom and Dave. I've managed to modify the CSV extension so it outputs 
an XML file. I've attached the file with this post.

The format of the XML file is:
<current>
 <lastupdate>2016-11-15 10:48:27</lastupdate>
 <temperature>18.9</temperature>
 <pressure>1001</pressure>
 <humidity>87</humidity>
 <dewpoint>11.4</dewpoint>
 <windchill>18.9</windchill>
 <wind>
  <direction>288</direction>
  <speed>0.4</speed>
 </wind>
 <gust>
  <direction>125</direction>
  <speed>0.6</speed>
 </gust>
 <rain>
  <rainrate>0.26</rainrate>
  <rain>0.00</rain>
 </rain>
</current>


-- 
You received this message because you are subscribed to the Google Groups 
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
# $Id: currentweatherxml.py 1316 2016-11-16 00:50:49Z hiljolodewijk $
# Copyright 2016 HiljoLodewijk

import os
import os.path
import time

import weewx
import weewx.engine
import weeutil.weeutil

class CurrentWeatherXML(weewx.engine.StdService):
	def __init__(self, engine, config_dict):
		super(CurrentWeatherXML, self).__init__(engine, config_dict)
		d = config_dict.get('CurrentWeatherXML', {})
		self.filename	= d.get('filename', '/var/tmp/current.xml')
		self.binding	= d.get('binding', 'loop')
		self.nonevalue	= d.get('nonevalue', 'None')

		if self.binding == 'loop':
			self.bind(weewx.NEW_LOOP_PACKET, self.handle_new_loop)
		else:
			self.bind(weewx.NEW_ARCHIVE_RECORD, self.handle_new_archive)

	def handle_new_loop(self, event):
		self.write_data(event.packet)

	def handle_new_archive(self, event):
		delta = time.time() - event.record['dateTime']
		if delta > event.record['interval'] * 60:
			return
		self.write_data(event.record)

	def write_data(self, data):
		weatherdict = {
			"station":{
				"name": "",
				"latitude": "",
				"longitude": "",
				"device": ""
				},
			"lastupdate":time.strftime("%Y-%m-%d %H:%M:%S", time.localtime( int( data.get('dateTime') ) )),
			"temperature":self.format(data, 'outTemp', 1 ),
			"dewpoint":self.format(data, 'dewpoint', 1 ),
			"windchill":self.format(data, 'windchill', 1 ),
			"humidity":self.format(data, 'outHumidity', 0 ),
			"pressure":self.format(data, 'pressure', 0 ),
			"wind":{
				"speed":self.format(data, 'windSpeed', 1 ),
				"direction":self.format(data, 'windDir', 0 )
				},
			"gust":{
				"speed":self.format(data, 'windGust', 1 ),
				"direction":self.format(data, 'windGustDir', 0 )
				},
			"rain":{
				"rain":self.format(data, 'rain', 2 ),
				"rainrate":self.format(data, 'rainRate', 2 )
				}
			}

		with open(self.filename, "w") as f:
			f.write( self.dict2xml( weatherdict, "current" ) )

	def dict2xml(self, d, root="xml"):
		op = lambda tag: '<' + tag + '>'
		cl = lambda tag: '</' + tag + '>'
		ml = lambda v, xml: xml + '\n' + op(key) + str(v) + cl(key)

		xml = op(root) if root else ""

		for key, vl in d.iteritems():
			vtype = type(vl)
			if vtype is list:
				for v in vl:
					xml = ml(v, xml)
			if vtype is dict:
				xml = ml( '\t' + self.dict2xml(vl, None), xml)
			if vtype is not list and vtype is not dict:
				xml = ml(vl, xml)
		
		xml += '\n' + cl(root) if root else ""

		return xml

	def sort_keys(self, record):
		fields = ['local_time']
		for k in sorted(record):
			if k != 'local_time':
				fields.append(k)
		return fields

	def sort_data(self, record):
		fields = [str(record['local_time'])]
		for k in sorted(record):
			if k != 'local_time':
				fields.append(str(record[k]))
		return fields

	def format(self, data, label, places=None):
		value = data.get(label)
		if value is None:
			value = self.nonevalue
		elif places is not None:
			try:
				v = float(value)
				fmt = "%%.%df" % places
				value = fmt % v
			except ValueError:
				pass
		return str(value)

Reply via email to