mirror of
https://github.com/EDCD/EDDN.git
synced 2025-06-17 23:51:30 +03:00
Update examples
This commit is contained in:
parent
a733ff22ed
commit
eb137fbcba
@ -40,18 +40,18 @@ $oldTime = false;
|
||||
function echoLog($str)
|
||||
{
|
||||
global $oldTime, $logVerboseFile;
|
||||
|
||||
|
||||
if($logVerboseFile !== false)
|
||||
$logVerboseFileParsed = str_replace('%DATE%', date('Y-m-d'), $logVerboseFile);
|
||||
|
||||
|
||||
if($logVerboseFile !== false && !file_exists($logVerboseFileParsed))
|
||||
{
|
||||
file_put_contents(
|
||||
$logVerboseFileParsed,
|
||||
$logVerboseFileParsed,
|
||||
'<style type="text/css">html { white-space: pre; font-family: Courier New,Courier,Lucida Sans Typewriter,Lucida Typewriter,monospace; }</style>'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
if($oldTime != date('H:i:s') || $oldTime === false)
|
||||
{
|
||||
$oldTime = date('H:i:s');
|
||||
@ -59,9 +59,9 @@ function echoLog($str)
|
||||
}
|
||||
else
|
||||
$str = ' ' . ' | ' . $str;
|
||||
|
||||
|
||||
fwrite(STDOUT, $str . PHP_EOL);
|
||||
|
||||
|
||||
if($logVerboseFile !== false)
|
||||
file_put_contents(
|
||||
$logVerboseFileParsed,
|
||||
@ -73,11 +73,11 @@ function echoLog($str)
|
||||
function echoLogJSON($json)
|
||||
{
|
||||
global $logJSONFile;
|
||||
|
||||
|
||||
if($logJSONFile !== false)
|
||||
{
|
||||
$logJSONFileParsed = str_replace('%DATE%', date('Y-m-d'), $logJSONFile);
|
||||
|
||||
|
||||
file_put_contents(
|
||||
$logJSONFileParsed,
|
||||
$json . PHP_EOL,
|
||||
@ -106,11 +106,11 @@ while (true)
|
||||
echoLog('Connect to ' . $relayEDDN);
|
||||
echoLog('');
|
||||
echoLog('');
|
||||
|
||||
|
||||
while (true)
|
||||
{
|
||||
$message = $subscriber->recv();
|
||||
|
||||
|
||||
if ($message === false)
|
||||
{
|
||||
$subscriber->disconnect($relayEDDN);
|
||||
@ -119,90 +119,90 @@ while (true)
|
||||
echoLog('');
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
$message = zlib_decode($message);
|
||||
$json = json_decode($message, true);
|
||||
$converted = false;
|
||||
|
||||
|
||||
// Handle commodity v1
|
||||
if($json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1' . (($debugEDDN === true) ? '/test' : ''))
|
||||
if($json['$schemaRef'] == 'https://eddn.edcd.io/schemas/commodity/1' . (($debugEDDN === true) ? '/test' : ''))
|
||||
{
|
||||
echoLogJSON($message);
|
||||
echoLog('Receiving commodity-v1 message...');
|
||||
echoLog(' - Converting to v2...');
|
||||
|
||||
|
||||
$temp = array();
|
||||
$temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2' . (($debugEDDN === true) ? '/test' : '');
|
||||
$temp['$schemaRef'] = 'https://eddn.edcd.io/schemas/commodity/3' . (($debugEDDN === true) ? '/test' : '');
|
||||
$temp['header'] = $json['header'];
|
||||
|
||||
|
||||
$temp['message'] = array();
|
||||
$temp['message']['systemName'] = $json['message']['systemName'];
|
||||
$temp['message']['stationName'] = $json['message']['stationName'];
|
||||
$temp['message']['timestamp'] = $json['message']['timestamp'];
|
||||
|
||||
|
||||
$temp['message']['commodities'] = array();
|
||||
|
||||
|
||||
$commodity = array();
|
||||
|
||||
|
||||
if(array_key_exists('itemName', $json['message']))
|
||||
$commodity['name'] = $json['message']['itemName'];
|
||||
|
||||
|
||||
if(array_key_exists('buyPrice', $json['message']))
|
||||
$commodity['buyPrice'] = $json['message']['buyPrice'];
|
||||
if(array_key_exists('stationStock', $json['message']))
|
||||
$commodity['supply'] = $json['message']['stationStock'];
|
||||
if(array_key_exists('supplyLevel', $json['message']))
|
||||
$commodity['supplyLevel'] = $json['message']['supplyLevel'];
|
||||
|
||||
|
||||
if(array_key_exists('sellPrice', $json['message']))
|
||||
$commodity['sellPrice'] = $json['message']['sellPrice'];
|
||||
if(array_key_exists('demand', $json['message']))
|
||||
$commodity['demand'] = $json['message']['demand'];
|
||||
if(array_key_exists('demandLevel', $json['message']))
|
||||
$commodity['demandLevel'] = $json['message']['demandLevel'];
|
||||
|
||||
|
||||
$temp['message']['commodities'][] = $commodity;
|
||||
$json = $temp;
|
||||
unset($temp, $commodity);
|
||||
|
||||
|
||||
$converted = true;
|
||||
}
|
||||
|
||||
|
||||
// Handle commodity v2
|
||||
if($json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/2' . (($debugEDDN === true) ? '/test' : ''))
|
||||
|
||||
|
||||
// Handle commodity v3
|
||||
if($json['$schemaRef'] == 'https://eddn.edcd.io/schemas/commodity/3' . (($debugEDDN === true) ? '/test' : ''))
|
||||
{
|
||||
if($converted === false)
|
||||
{
|
||||
echoLogJSON($message);
|
||||
echoLog('Receiving commodity-v2 message...');
|
||||
echoLog('Receiving commodity-v3 message...');
|
||||
}
|
||||
|
||||
|
||||
$authorised = false;
|
||||
$excluded = false;
|
||||
|
||||
|
||||
if(in_array($json['header']['softwareName'], $authorisedSoftwares))
|
||||
$authorised = true;
|
||||
if(in_array($json['header']['softwareName'], $excludedSoftwares))
|
||||
$excluded = true;
|
||||
|
||||
|
||||
echoLog(' - Software: ' . $json['header']['softwareName'] . ' / ' . $json['header']['softwareVersion']);
|
||||
echoLog(' - ' . (($authorised === true)
|
||||
? 'AUTHORISED'
|
||||
echoLog(' - ' . (($authorised === true)
|
||||
? 'AUTHORISED'
|
||||
: (( $excluded === true) ? 'EXCLUDED' : 'UNAUTHORISED')
|
||||
));
|
||||
|
||||
|
||||
if($authorised === true && $excluded === false)
|
||||
{
|
||||
// Do what you want with the data...
|
||||
// Have fun !
|
||||
|
||||
|
||||
// For example
|
||||
echoLog(' - Timestamp: ' . $json['message']['timestamp']);
|
||||
echoLog(' - Uploader ID: ' . $json['header']['uploaderID']);
|
||||
echoLog(' - System Name: ' . $json['message']['systemName']);
|
||||
echoLog(' - Station Name: ' . $json['message']['stationName']);
|
||||
|
||||
|
||||
foreach($json['message']['commodities'] AS $commodity)
|
||||
{
|
||||
echoLog(' - Name: ' . $commodity['name']);
|
||||
@ -217,13 +217,13 @@ while (true)
|
||||
}
|
||||
// End example
|
||||
}
|
||||
|
||||
|
||||
unset($authorised, $excluded);
|
||||
|
||||
|
||||
echoLog('');
|
||||
echoLog('');
|
||||
}
|
||||
|
||||
|
||||
unset($converted);
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +1,11 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*
|
||||
* $eddn = new EDDN(array(
|
||||
* 'uploaderID' => 'abcdef0123456789',
|
||||
* 'softwareName' => 'My Awesome Market Uploader',
|
||||
* 'softwareVersion' => 'v3.14'
|
||||
* ));
|
||||
*
|
||||
* $result = $eddn->publishCommodityV1(
|
||||
* 'Eranin',
|
||||
* 'Azeban Orbital',
|
||||
* time(),
|
||||
* array(
|
||||
* "itemName" => "Gold",
|
||||
* "buyPrice" => 1024,
|
||||
* "supply" => 7,
|
||||
* "stationStock" => "Low",
|
||||
* "sellPrice" => 1138,
|
||||
* "demand" => 42,
|
||||
* "demandLevel" => "Med"
|
||||
* )
|
||||
* ); // return true;
|
||||
*
|
||||
* )); *
|
||||
*
|
||||
* $result = $eddn->publishCommodityV2(
|
||||
* 'Eranin',
|
||||
@ -47,117 +31,97 @@
|
||||
* )
|
||||
* )
|
||||
* ); // return true;
|
||||
*
|
||||
*
|
||||
**/
|
||||
|
||||
class EDDN
|
||||
{
|
||||
private static $_debug = true;
|
||||
|
||||
|
||||
private static $_gateways = array(
|
||||
'http://eddn-gateway.elite-markets.net:8080/upload/',
|
||||
'http://eddn-gateway.ed-td.space:8080/upload/'
|
||||
'https://eddn.edcd.io:8080/upload/',
|
||||
);
|
||||
|
||||
|
||||
private static $_schemas = array(
|
||||
'commodity-v1' => array(
|
||||
'production' => 'http://schemas.elite-markets.net/eddn/commodity/1',
|
||||
'test' => 'http://schemas.elite-markets.net/eddn/commodity/1/test'
|
||||
'commodity-v3' => array(
|
||||
'production' => 'https://eddn.edcd.io/schemas/commodity/3',
|
||||
'test' => 'https://eddn.edcd.io/schemas/commodity/3/test',
|
||||
),
|
||||
'commodity-v2' => array(
|
||||
'production' => 'http://schemas.elite-markets.net/eddn/commodity/2',
|
||||
'test' => 'http://schemas.elite-markets.net/eddn/commodity/2/test'
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
private $_uploaderID = null;
|
||||
private $_softwareName = null;
|
||||
private $_softwareVersion = null;
|
||||
|
||||
|
||||
public function __Construct(array $options)
|
||||
{
|
||||
if(array_key_exists('uploaderID', $options))
|
||||
$this->setUploaderID($options['uploaderID']);
|
||||
else
|
||||
throw new Exception('Option "uploaderID" is required.');
|
||||
|
||||
|
||||
if(array_key_exists('softwareName', $options))
|
||||
$this->setSoftwareName($options['softwareName']);
|
||||
else
|
||||
throw new Exception('Option "softwareName" is required.');
|
||||
|
||||
|
||||
if(array_key_exists('softwareVersion', $options))
|
||||
$this->setSoftwareVersion($options['softwareVersion']);
|
||||
else
|
||||
throw new Exception('Option "softwareVersion" is required.');
|
||||
}
|
||||
|
||||
public function publishCommodityV1($systemName, $stationName, $timestamp, array $commodity)
|
||||
{
|
||||
$schema = self::$_schemas['commodity-v1'][((self::$_debug === true) ? 'test' : 'production')];
|
||||
|
||||
$message = array();
|
||||
$message['systemName'] = $systemName;
|
||||
$message['stationName'] = $stationName;
|
||||
$message['timestamp'] = date('c', $timestamp);
|
||||
|
||||
foreach($commodity AS $key => $value)
|
||||
$message[$key] = $value;
|
||||
|
||||
return $this->_postToEDDN($schema, $message);
|
||||
}
|
||||
|
||||
public function publishCommodityV2($systemName, $stationName, $timestamp, array $commodities)
|
||||
|
||||
public function publishCommodityV3($systemName, $stationName, $timestamp, array $commodities)
|
||||
{
|
||||
$schema = self::$_schemas['commodity-v2'][((self::$_debug === true) ? 'test' : 'production')];
|
||||
|
||||
|
||||
$message = array();
|
||||
$message['systemName'] = $systemName;
|
||||
$message['stationName'] = $stationName;
|
||||
$message['timestamp'] = date('c', $timestamp);
|
||||
|
||||
|
||||
$message['commodities'] = $commodities;
|
||||
|
||||
|
||||
return $this->_postToEDDN($schema, $message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private function _generateHeader()
|
||||
{
|
||||
$header = array();
|
||||
|
||||
|
||||
$header['uploaderID'] = $this->getUploaderID();
|
||||
$header['softwareName'] = $this->getSoftwareName();
|
||||
$header['softwareVersion'] = $this->getSoftwareVersion();
|
||||
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
|
||||
private function _postToEDDN($schema, array $message)
|
||||
{
|
||||
$array = array();
|
||||
$array['$schemaRef'] = $schema;
|
||||
$array['header'] = $this->_generateHeader();
|
||||
$array['message'] = $message;
|
||||
|
||||
|
||||
$json = json_encode($array);
|
||||
|
||||
|
||||
if(function_exists('curl_version'))
|
||||
{
|
||||
$gateway = self::$_gateways[array_rand(self::$_gateways)];
|
||||
|
||||
|
||||
$ch = curl_init();
|
||||
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $gateway);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
|
||||
|
||||
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
unset($ch);
|
||||
|
||||
|
||||
if($result == 'OK')
|
||||
return true;
|
||||
else
|
||||
@ -166,46 +130,46 @@ class EDDN
|
||||
else
|
||||
throw new Exception('You must have CURL extension in order to publish to EDDN');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function setUploaderID($value)
|
||||
{
|
||||
$this->_uploaderID = $value;
|
||||
}
|
||||
|
||||
|
||||
public function getUploaderID()
|
||||
{
|
||||
return $this->_uploaderID;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function setSoftwareName($value)
|
||||
{
|
||||
$this->_softwareName = $value;
|
||||
}
|
||||
|
||||
|
||||
public function getSoftwareName()
|
||||
{
|
||||
return $this->_softwareName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function setSoftwareVersion($value)
|
||||
{
|
||||
$this->_softwareVersion = $value;
|
||||
}
|
||||
|
||||
|
||||
public function getSoftwareVersion()
|
||||
{
|
||||
return $this->_softwareVersion;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function setDebug($value)
|
||||
{
|
||||
self::$_debug = $value;
|
||||
}
|
||||
|
||||
|
||||
public function getDebug()
|
||||
{
|
||||
return self::$_debug;
|
||||
|
@ -50,10 +50,10 @@ def date(__format):
|
||||
__oldTime = False
|
||||
def echoLog(__str):
|
||||
global __oldTime, __logVerboseFile
|
||||
|
||||
|
||||
if __logVerboseFile != False:
|
||||
__logVerboseFileParsed = __logVerboseFile.replace('%DATE%', str(date('%Y-%m-%d')))
|
||||
|
||||
|
||||
if __logVerboseFile != False and not os.path.exists(__logVerboseFileParsed):
|
||||
f = open(__logVerboseFileParsed, 'w')
|
||||
f.write('<style type="text/css">html { white-space: pre; font-family: Courier New,Courier,Lucida Sans Typewriter,Lucida Typewriter,monospace; }</style>')
|
||||
@ -64,7 +64,7 @@ def echoLog(__str):
|
||||
__str = str(__oldTime) + ' | ' + str(__str)
|
||||
else:
|
||||
__str = ' ' + ' | ' + str(__str)
|
||||
|
||||
|
||||
print __str
|
||||
sys.stdout.flush()
|
||||
|
||||
@ -72,26 +72,26 @@ def echoLog(__str):
|
||||
f = open(__logVerboseFileParsed, 'a')
|
||||
f.write(__str + '\n')
|
||||
f.close()
|
||||
|
||||
|
||||
|
||||
def echoLogJSON(__json):
|
||||
global __logJSONFile
|
||||
|
||||
|
||||
if __logJSONFile != False:
|
||||
__logJSONFileParsed = __logJSONFile.replace('%DATE%', str(date('%Y-%m-%d')))
|
||||
|
||||
|
||||
f = open(__logJSONFileParsed, 'a')
|
||||
f.write(str(__json) + '\n')
|
||||
f.close()
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
echoLog('Starting EDDN Subscriber')
|
||||
echoLog('')
|
||||
|
||||
|
||||
context = zmq.Context()
|
||||
subscriber = context.socket(zmq.SUB)
|
||||
|
||||
|
||||
subscriber.setsockopt(zmq.SUBSCRIBE, "")
|
||||
subscriber.setsockopt(zmq.RCVTIMEO, __timeoutEDDN)
|
||||
|
||||
@ -101,10 +101,10 @@ def main():
|
||||
echoLog('Connect to ' + __relayEDDN)
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
|
||||
|
||||
poller = zmq.Poller()
|
||||
poller.register(subscriber, zmq.POLLIN)
|
||||
|
||||
|
||||
while True:
|
||||
socks = dict(poller.poll(__timeoutEDDN))
|
||||
if socks:
|
||||
@ -113,83 +113,83 @@ def main():
|
||||
__message = zlib.decompress(__message)
|
||||
__json = simplejson.loads(__message)
|
||||
__converted = False
|
||||
|
||||
|
||||
|
||||
|
||||
# Handle commodity v1
|
||||
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1' + ('/test' if (__debugEDDN == True) else ''):
|
||||
if __json['$schemaRef'] == 'https://eddn.edcd.io/schemas/commodity/1' + ('/test' if (__debugEDDN == True) else ''):
|
||||
echoLogJSON(__message)
|
||||
echoLog('Receiving commodity-v1 message...')
|
||||
echoLog(' - Converting to v2...')
|
||||
|
||||
echoLog(' - Converting to v3...')
|
||||
|
||||
__temp = {}
|
||||
__temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else '')
|
||||
__temp['$schemaRef'] = 'https://eddn.edcd.io/schemas/commodity/3' + ('/test' if (__debugEDDN == True) else '')
|
||||
__temp['header'] = __json['header']
|
||||
|
||||
|
||||
__temp['message'] = {}
|
||||
__temp['message']['systemName'] = __json['message']['systemName']
|
||||
__temp['message']['stationName'] = __json['message']['stationName']
|
||||
__temp['message']['timestamp'] = __json['message']['timestamp']
|
||||
|
||||
|
||||
__temp['message']['commodities'] = []
|
||||
|
||||
|
||||
__commodity = {}
|
||||
|
||||
|
||||
if 'itemName' in __json['message']:
|
||||
__commodity['name'] = __json['message']['itemName']
|
||||
|
||||
|
||||
if 'buyPrice' in __json['message']:
|
||||
__commodity['buyPrice'] = __json['message']['buyPrice']
|
||||
if 'stationStock' in __json['message']:
|
||||
__commodity['supply'] = __json['message']['stationStock']
|
||||
if 'supplyLevel' in __json['message']:
|
||||
__commodity['supplyLevel'] = __json['message']['supplyLevel']
|
||||
|
||||
|
||||
if 'sellPrice' in __json['message']:
|
||||
__commodity['sellPrice'] = __json['message']['sellPrice']
|
||||
if 'demand' in __json['message']:
|
||||
__commodity['demand'] = __json['message']['demand']
|
||||
if'demandLevel' in __json['message']:
|
||||
__commodity['demandLevel'] = __json['message']['demandLevel']
|
||||
|
||||
|
||||
__temp['message']['commodities'].append(__commodity)
|
||||
__json = __temp
|
||||
del __temp, __commodity
|
||||
|
||||
|
||||
__converted = True
|
||||
|
||||
# Handle commodity v2
|
||||
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else ''):
|
||||
|
||||
# Handle commodity v3
|
||||
if __json['$schemaRef'] == 'https://eddn.edcd.io/schemas/commodity/3' + ('/test' if (__debugEDDN == True) else ''):
|
||||
if __converted == False:
|
||||
echoLogJSON(__message)
|
||||
echoLog('Receiving commodity-v2 message...')
|
||||
|
||||
echoLog('Receiving commodity-v3 message...')
|
||||
|
||||
__authorised = False
|
||||
__excluded = False
|
||||
|
||||
|
||||
if __json['header']['softwareName'] in __authorisedSoftwares:
|
||||
__authorised = True
|
||||
if __json['header']['softwareName'] in __excludedSoftwares:
|
||||
__excluded = True
|
||||
|
||||
|
||||
echoLog(' - Software: ' + __json['header']['softwareName'] + ' / ' + __json['header']['softwareVersion'])
|
||||
echoLog(' - ' + 'AUTHORISED' if (__authorised == True) else
|
||||
('EXCLUDED' if (__excluded == True) else 'UNAUTHORISED')
|
||||
)
|
||||
|
||||
|
||||
if __authorised == True and __excluded == False:
|
||||
# Do what you want with the data...
|
||||
# Have fun !
|
||||
|
||||
|
||||
# For example
|
||||
echoLog(' - Timestamp: ' + __json['message']['timestamp'])
|
||||
echoLog(' - Uploader ID: ' + __json['header']['uploaderID'])
|
||||
echoLog(' - System Name: ' + __json['message']['systemName'])
|
||||
echoLog(' - Station Name: ' + __json['message']['stationName'])
|
||||
|
||||
|
||||
for __commodity in __json['message']['commodities']:
|
||||
echoLog(' - Name: ' + __commodity['name'])
|
||||
echoLog(' - Buy Price: ' + str(__commodity['buyPrice']))
|
||||
echoLog(' - Supply: ' + str(__commodity['supply'])
|
||||
echoLog(' - Supply: ' + str(__commodity['supply'])
|
||||
+ ((' (' + __commodity['supplyLevel'] + ')') if 'supplyLevel' in __commodity else '')
|
||||
)
|
||||
echoLog(' - Sell Price: ' + str(__commodity['sellPrice']))
|
||||
@ -197,33 +197,33 @@ def main():
|
||||
+ ((' (' + __commodity['demandLevel'] + ')') if 'demandLevel' in __commodity else '')
|
||||
)
|
||||
# End example
|
||||
|
||||
|
||||
del __authorised, __excluded
|
||||
|
||||
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
|
||||
|
||||
del __converted
|
||||
else:
|
||||
print 'Disconnect from ' + __relayEDDN + ' (After timeout)'
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
subscriber.disconnect(__relayEDDN)
|
||||
break
|
||||
|
||||
|
||||
except zmq.ZMQError, e:
|
||||
subscriber.disconnect(__relayEDDN)
|
||||
|
||||
|
||||
echoLog('')
|
||||
echoLog('Disconnect from ' + __relayEDDN + ' (After receiving ZMQError)')
|
||||
echoLog('ZMQSocketException: ' + str(e))
|
||||
echoLog('')
|
||||
|
||||
|
||||
time.sleep(10)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
@ -51,10 +51,10 @@ def date(__format):
|
||||
__oldTime = False
|
||||
def echoLog(__str):
|
||||
global __oldTime, __logVerboseFile
|
||||
|
||||
|
||||
if __logVerboseFile != False:
|
||||
__logVerboseFileParsed = __logVerboseFile.replace('%DATE%', str(date('%Y-%m-%d')))
|
||||
|
||||
|
||||
if __logVerboseFile != False and not os.path.exists(__logVerboseFileParsed):
|
||||
f = open(__logVerboseFileParsed, 'w')
|
||||
f.write('<style type="text/css">html { white-space: pre; font-family: Courier New,Courier,Lucida Sans Typewriter,Lucida Typewriter,monospace; }</style>')
|
||||
@ -65,7 +65,7 @@ def echoLog(__str):
|
||||
__str = str(__oldTime) + ' | ' + str(__str)
|
||||
else:
|
||||
__str = ' ' + ' | ' + str(__str)
|
||||
|
||||
|
||||
print (__str)
|
||||
sys.stdout.flush()
|
||||
|
||||
@ -73,26 +73,26 @@ def echoLog(__str):
|
||||
f = open(__logVerboseFileParsed, 'a')
|
||||
f.write(__str + '\n')
|
||||
f.close()
|
||||
|
||||
|
||||
|
||||
def echoLogJSON(__json):
|
||||
global __logJSONFile
|
||||
|
||||
|
||||
if __logJSONFile != False:
|
||||
__logJSONFileParsed = __logJSONFile.replace('%DATE%', str(date('%Y-%m-%d')))
|
||||
|
||||
|
||||
f = open(__logJSONFileParsed, 'a')
|
||||
f.write(str(__json) + '\n')
|
||||
f.close()
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
echoLog('Starting EDDN Subscriber')
|
||||
echoLog('')
|
||||
|
||||
|
||||
context = zmq.Context()
|
||||
subscriber = context.socket(zmq.SUB)
|
||||
|
||||
|
||||
subscriber.setsockopt(zmq.SUBSCRIBE, b"")
|
||||
subscriber.setsockopt(zmq.RCVTIMEO, __timeoutEDDN)
|
||||
|
||||
@ -102,17 +102,17 @@ def main():
|
||||
echoLog('Connect to ' + __relayEDDN)
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
|
||||
|
||||
while True:
|
||||
__message = subscriber.recv()
|
||||
|
||||
|
||||
if __message == False:
|
||||
subscriber.disconnect(__relayEDDN)
|
||||
echoLog('Disconnect from ' + __relayEDDN)
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
break
|
||||
|
||||
|
||||
echoLog('Got a message')
|
||||
|
||||
__message = zlib.decompress(__message)
|
||||
@ -124,83 +124,83 @@ def main():
|
||||
echoLog('Failed to parse message as json')
|
||||
|
||||
__converted = False
|
||||
|
||||
|
||||
|
||||
|
||||
# Handle commodity v1
|
||||
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1' + ('/test' if (__debugEDDN == True) else ''):
|
||||
if __json['$schemaRef'] == 'https://eddn.edcd.io/schemas/commodity/1' + ('/test' if (__debugEDDN == True) else ''):
|
||||
echoLogJSON(__message)
|
||||
echoLog('Receiving commodity-v1 message...')
|
||||
echoLog(' - Converting to v2...')
|
||||
|
||||
echoLog(' - Converting to v3...')
|
||||
|
||||
__temp = {}
|
||||
__temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else '')
|
||||
__temp['$schemaRef'] = 'https://eddn.edcd.io/schemas/commodity/3' + ('/test' if (__debugEDDN == True) else '')
|
||||
__temp['header'] = __json['header']
|
||||
|
||||
|
||||
__temp['message'] = {}
|
||||
__temp['message']['systemName'] = __json['message']['systemName']
|
||||
__temp['message']['stationName'] = __json['message']['stationName']
|
||||
__temp['message']['timestamp'] = __json['message']['timestamp']
|
||||
|
||||
|
||||
__temp['message']['commodities'] = []
|
||||
|
||||
|
||||
__commodity = {}
|
||||
|
||||
|
||||
if 'itemName' in __json['message']:
|
||||
__commodity['name'] = __json['message']['itemName']
|
||||
|
||||
|
||||
if 'buyPrice' in __json['message']:
|
||||
__commodity['buyPrice'] = __json['message']['buyPrice']
|
||||
if 'stationStock' in __json['message']:
|
||||
__commodity['supply'] = __json['message']['stationStock']
|
||||
if 'supplyLevel' in __json['message']:
|
||||
__commodity['supplyLevel'] = __json['message']['supplyLevel']
|
||||
|
||||
|
||||
if 'sellPrice' in __json['message']:
|
||||
__commodity['sellPrice'] = __json['message']['sellPrice']
|
||||
if 'demand' in __json['message']:
|
||||
__commodity['demand'] = __json['message']['demand']
|
||||
if'demandLevel' in __json['message']:
|
||||
__commodity['demandLevel'] = __json['message']['demandLevel']
|
||||
|
||||
|
||||
__temp['message']['commodities'].append(__commodity)
|
||||
__json = __temp
|
||||
del __temp, __commodity
|
||||
|
||||
|
||||
__converted = True
|
||||
|
||||
# Handle commodity v2
|
||||
if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/2' + ('/test' if (__debugEDDN == True) else ''):
|
||||
|
||||
# Handle commodity v3
|
||||
if __json['$schemaRef'] == 'https://eddn.edcd.io/schemas/commodity/3' + ('/test' if (__debugEDDN == True) else ''):
|
||||
if __converted == False:
|
||||
echoLogJSON(__message)
|
||||
echoLog('Receiving commodity-v2 message...')
|
||||
|
||||
echoLog('Receiving commodity-v3 message...')
|
||||
|
||||
__authorised = False
|
||||
__excluded = False
|
||||
|
||||
|
||||
if __json['header']['softwareName'] in __authorisedSoftwares:
|
||||
__authorised = True
|
||||
if __json['header']['softwareName'] in __excludedSoftwares:
|
||||
__excluded = True
|
||||
|
||||
|
||||
echoLog(' - Software: ' + __json['header']['softwareName'] + ' / ' + __json['header']['softwareVersion'])
|
||||
echoLog(' - ' + 'AUTHORISED' if (__authorised == True) else
|
||||
('EXCLUDED' if (__excluded == True) else 'UNAUTHORISED')
|
||||
)
|
||||
|
||||
|
||||
if __authorised == True and __excluded == False:
|
||||
# Do what you want with the data...
|
||||
# Have fun !
|
||||
|
||||
|
||||
# For example
|
||||
echoLog(' - Timestamp: ' + __json['message']['timestamp'])
|
||||
echoLog(' - Uploader ID: ' + __json['header']['uploaderID'])
|
||||
echoLog(' - System Name: ' + __json['message']['systemName'])
|
||||
echoLog(' - Station Name: ' + __json['message']['stationName'])
|
||||
|
||||
|
||||
for __commodity in __json['message']['commodities']:
|
||||
echoLog(' - Name: ' + __commodity['name'])
|
||||
echoLog(' - Buy Price: ' + str(__commodity['buyPrice']))
|
||||
echoLog(' - Supply: ' + str(__commodity['supply'])
|
||||
echoLog(' - Supply: ' + str(__commodity['supply'])
|
||||
+ ((' (' + __commodity['supplyLevel'] + ')') if 'supplyLevel' in __commodity else '')
|
||||
)
|
||||
echoLog(' - Sell Price: ' + str(__commodity['sellPrice']))
|
||||
@ -208,18 +208,18 @@ def main():
|
||||
+ ((' (' + __commodity['demandLevel'] + ')') if 'demandLevel' in __commodity else '')
|
||||
)
|
||||
# End example
|
||||
|
||||
|
||||
del __authorised, __excluded
|
||||
|
||||
|
||||
echoLog('')
|
||||
echoLog('')
|
||||
|
||||
|
||||
else:
|
||||
echoLog('Unknown schema: ' + __json['$schemaRef']);
|
||||
|
||||
del __converted
|
||||
|
||||
|
||||
|
||||
|
||||
except zmq.ZMQError as e:
|
||||
echoLog('')
|
||||
echoLog('ZMQSocketException: ' + str(e))
|
||||
@ -227,8 +227,8 @@ def main():
|
||||
echoLog('Disconnect from ' + __relayEDDN)
|
||||
echoLog('')
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Loading…
x
Reference in New Issue
Block a user