--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from __future__ import division
+import sys
+from ais.ntools import read_cfg
+from ais.common import add_nmea1, AIS_STATUS_NOT_AVAILABLE, AIS_ROT_NOT_AVAILABLE, AIS_SOG_NOT_AVAILABLE, AIS_LATLON_SCALE, AIS_COG_NOT_AVAILABLE, AIS_NO_HEADING
+
+SPOT_CONFIG_FILE = '/etc/ais/spot'
+SPOT_TIMESTAMP_FILE = '/var/lib/ais/spot/timestamp'
+SPOT_LOG_DIR = '/var/lib/ais/spot/'
+
+def spot_xml_to_python(xmlstring):
+ '''
+ Takes an xml document fetched on
+ https://share.findmespot.com/messageService/guestlinkservlet
+ an convert it (returns) a list of dictionaries with the message content.
+ '''
+ import xml.etree.ElementTree
+ py_doc = []
+ tree = xml.etree.ElementTree.fromstring(xmlstring)
+ for msg in tree.findall('message'):
+ msg_dict = {}
+ for child in msg.getchildren():
+ msg_dict[child.tag] = child.text
+ py_doc.append(msg_dict)
+ return py_doc
+
+
+__logfile__ = None
+def getLogFile():
+ import os
+ from datetime import datetime
+ global __logfile__
+ if __logfile__ is None:
+ logfilename = os.path.join(SPOT_LOG_DIR, datetime.utcnow().strftime('%Y%m%d.log'))
+ __logfile__ = file(logfilename, 'a')
+ return __logfile__
+
+
+cfg = read_cfg(SPOT_CONFIG_FILE)
+xmlurl = cfg['url']
+
+import urllib2
+xmlsourcebytes = urllib2.urlopen(xmlurl).read()
+xmlsource = unicode(xmlsourcebytes, 'UTF-8')
+
+
+try:
+ timestampfile = open(SPOT_TIMESTAMP_FILE, 'r+b')
+ old_timestamp = long(timestampfile.read())
+except IOError:
+ print >> sys.stderr, "Can't read old time stamp"
+ timestampfile = open(SPOT_TIMESTAMP_FILE, 'wb')
+ old_timestamp = 0L
+max_timestamp = old_timestamp
+
+for msg in spot_xml_to_python(xmlsource):
+ esn = msg.get('esn', None)
+ if esn is None:
+ print >> sys.stderr, 'No esn in message', msg
+ continue
+ strmmsi = cfg.get('esn_'+esn, None)
+ if strmmsi is None:
+ print >> sys.stderr, 'ens', ens, 'is not registered in', SPOT_CONFIG_FILE, msg
+ #print strmmsi, ':', msg
+ timestamp = long(msg['timeInGMTSecond'])
+ if timestamp > old_timestamp:
+ getLogFile().write(repr(msg)+'\n')
+ print 'New message:', msg
+ add_nmea1(strmmsi,
+ timestamp,
+ AIS_STATUS_NOT_AVAILABLE,
+ AIS_ROT_NOT_AVAILABLE,
+ AIS_SOG_NOT_AVAILABLE,
+ int(float(msg['latitude']) * AIS_LATLON_SCALE),
+ int(float(msg['longitude']) * AIS_LATLON_SCALE),
+ AIS_COG_NOT_AVAILABLE,
+ AIS_NO_HEADING,
+ 'ST01',
+ )
+ if timestamp > max_timestamp:
+ max_timestamp = timestamp
+
+timestampfile.seek(0)
+timestampfile.write(str(max_timestamp))