diff --git a/.gitignore b/.gitignore index 5165406..db4561e 100644 --- a/.gitignore +++ b/.gitignore @@ -52,4 +52,3 @@ docs/_build/ # PyBuilder target/ -*.htm diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..0f7d901 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,2 @@ +*.htm +*.log \ No newline at end of file diff --git a/examples/PHP/Client.php b/examples/PHP/Client.php index 69f1c6b..55828d1 100644 --- a/examples/PHP/Client.php +++ b/examples/PHP/Client.php @@ -3,7 +3,15 @@ * Configuration */ $relayEDDN = 'tcp://eddn-relay.elite-markets.net:9500'; -$logFile = dirname(__FILE__) . '/Logs_EDDN_%DATE%.htm'; +$timeoutEDDN = 600000; + +// Set to false if you do not want verbose logging +$logVerboseFile = dirname(__FILE__) . '/Logs_Verbose_EDDN_%DATE%.htm'; +//$logVerboseFile = false; + +// Set to false if you do not want JSON logging +$logJSONFile = dirname(__FILE__) . '/Logs_JSON_EDDN_%DATE%.log'; +//$logJSONFile = false; // A sample list of authorised softwares $authorisedSoftwares = array( @@ -19,21 +27,23 @@ $excludedSoftwares = array( 'My Awesome Market Uploader' ); + + /** * START */ $oldTime = false; function echoLog($str) { - global $oldTime, $logFile; + global $oldTime, $logVerboseFile; - $logFileParsed = str_replace('%DATE%', date('Y-m-d'), $logFile); + if($logVerboseFile !== false) + $logVerboseFileParsed = str_replace('%DATE%', date('Y-m-d'), $logVerboseFile); - - if(!file_exists($logFileParsed)) + if($logVerboseFile !== false && !file_exists($logVerboseFileParsed)) { file_put_contents( - $logFileParsed, + $logVerboseFileParsed, '' ); } @@ -48,82 +58,101 @@ function echoLog($str) fwrite(STDOUT, $str . PHP_EOL); - file_put_contents( - $logFileParsed, - $str . PHP_EOL, - FILE_APPEND - ); + if($logVerboseFile !== false) + file_put_contents( + $logVerboseFileParsed, + $str . PHP_EOL, + FILE_APPEND + ); +} + +function echoLogJSON($json) +{ + global $logJSONFile; + + if($logJSONFile !== false) + { + $logJSONFileParsed = str_replace('%DATE%', date('Y-m-d'), $logJSONFile); + + file_put_contents( + $logJSONFileParsed, + $json . PHP_EOL, + FILE_APPEND + ); + } } // UTC date_default_timezone_set('UTC'); -echoLog('Starting EDDN Subscribe'); +echoLog('Starting EDDN Subscriber'); echoLog(''); -$context = new ZMQContext(); -$socket = $context->getSocket(ZMQ::SOCKET_SUB); -$socket->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, ""); -$socket->setSockOpt(ZMQ::SOCKOPT_RCVTIMEO, 600000); +$context = new ZMQContext(); +$subscriber = $context->getSocket(ZMQ::SOCKET_SUB); + +$subscriber->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, ""); +$subscriber->setSockOpt(ZMQ::SOCKOPT_RCVTIMEO, $timeoutEDDN); while (true) { try { - $socket->connect($relayEDDN); + $subscriber->connect($relayEDDN); + while (true) { - $message = $socket->recv(); + $message = $subscriber->recv(); if ($message === false) { - $socket->disconnect($relayEDDN); + $subscriber->disconnect($relayEDDN); break; } - $json = zlib_decode($message); - $array = json_decode($json, true); + $message = zlib_decode($message); + $json = json_decode($message, true); $converted = false; - // Handle commodity v1 - if($array['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1') + if($json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1') { + echoLogJSON($message); echoLog('Receiving commodity-v1 message...'); echoLog(' - Converting to v2...'); $temp = array(); $temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2'; - $temp['header'] = $array['header']; + $temp['header'] = $json['header']; $temp['message'] = array(); - $temp['message']['systemName'] = $array['message']['systemName']; - $temp['message']['stationName'] = $array['message']['stationName']; - $temp['message']['timestamp'] = $array['message']['timestamp']; + $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', $array['message'])) - $commodity['name'] = $array['message']['itemName']; + if(array_key_exists('itemName', $json['message'])) + $commodity['name'] = $json['message']['itemName']; - if(array_key_exists('buyPrice', $array['message'])) - $commodity['buyPrice'] = $array['message']['buyPrice']; - if(array_key_exists('stationStock', $array['message'])) - $commodity['supply'] = $array['message']['stationStock']; - if(array_key_exists('supplyLevel', $array['message'])) - $commodity['supplyLevel'] = $array['message']['supplyLevel']; + 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', $array['message'])) - $commodity['sellPrice'] = $array['message']['sellPrice']; - if(array_key_exists('demand', $array['message'])) - $commodity['demand'] = $array['message']['demand']; - if(array_key_exists('demandLevel', $array['message'])) - $commodity['demandLevel'] = $array['message']['demandLevel']; + 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; - $array = $temp; + $json = $temp; unset($temp, $commodity); $converted = true; @@ -131,21 +160,23 @@ while (true) // Handle commodity v2 - if($array['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/2') + if($json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/2') { if($converted === false) + { + echoLogJSON($message); echoLog('Receiving commodity-v2 message...'); - unset($converted); + } $authorised = false; $excluded = false; - if(in_array($array['header']['softwareName'], $authorisedSoftwares)) + if(in_array($json['header']['softwareName'], $authorisedSoftwares)) $authorised = true; - if(in_array($array['header']['softwareName'], $excludedSoftwares)) + if(in_array($json['header']['softwareName'], $excludedSoftwares)) $excluded = true; - echoLog(' - Software: ' . $array['header']['softwareName'] . ' / ' . $array['header']['softwareVersion']); + echoLog(' - Software: ' . $json['header']['softwareName'] . ' / ' . $json['header']['softwareVersion']); echoLog(' - ' . (($authorised === true) ? 'AUTHORISED' : (( $excluded === true) ? 'EXCLUDED' : 'UNAUTHORISED') @@ -157,21 +188,21 @@ while (true) // Have fun ! // For example - echoLog(' - Timestamp: ' . $array['message']['timestamp']); - echoLog(' - System Name: ' . $array['message']['systemName']); - echoLog(' - Station Name: ' . $array['message']['stationName']); + echoLog(' - Timestamp: ' . $json['message']['timestamp']); + echoLog(' - System Name: ' . $json['message']['systemName']); + echoLog(' - Station Name: ' . $json['message']['stationName']); - foreach($array['message']['commodities'] AS $commodity) + foreach($json['message']['commodities'] AS $commodity) { echoLog(' - Name: ' . $commodity['name']); echoLog(' - Buy Price: ' . $commodity['buyPrice']); - echoLog(' - Supply: ' . $commodity['supply'] . ( - (array_key_exists('supplyLevel', $commodity)) ? ' (' . $commodity['supplyLevel'] . ')' : '' - )); + echoLog(' - Supply: ' . $commodity['supply'] + . ((array_key_exists('supplyLevel', $commodity)) ? ' (' . $commodity['supplyLevel'] . ')' : '') + ); echoLog(' - Sell Price: ' . $commodity['sellPrice']); - echoLog(' - Demand: ' . $commodity['demand'] . ( - (array_key_exists('demandLevel', $commodity)) ? ' (' . $commodity['demandLevel'] . ')' : '' - )); + echoLog(' - Demand: ' . $commodity['demand'] + . ((array_key_exists('demandLevel', $commodity)) ? ' (' . $commodity['demandLevel'] . ')' : '') + ); } // End example } @@ -179,13 +210,16 @@ while (true) unset($authorised, $excluded); } + unset($converted); echoLog(''); echoLog(''); } } catch (ZMQSocketException $e) { + echoLog(''); echoLog('ZMQSocketException: ' . $e); + echoLog(''); sleep(10); } } diff --git a/examples/Python/Client.py b/examples/Python/Client.py index b52f47c..45b89cf 100644 --- a/examples/Python/Client.py +++ b/examples/Python/Client.py @@ -1,21 +1,207 @@ import zlib import zmq.green as zmq import simplejson -import sys +import sys, os, datetime, time +""" + " Configuration +""" +__relayEDDN = 'tcp://eddn-relay.elite-markets.net:9500' +__timeoutEDDN = 600000 + +# Set to False if you do not want verbose logging +__logVerboseFile = os.path.dirname(__file__) + '/Logs_Verbose_EDDN_%DATE%.htm' +#__logVerboseFile = False + +# Set to False if you do not want JSON logging +__logJSONFile = os.path.dirname(__file__) + '/Logs_JSON_EDDN_%DATE%.log' +#__logJSONFile = False + +# A sample list of authorised softwares +__authorisedSoftwares = [ + "ED-TD.SPACE", + "EliteOCR", + "RegulatedNoise", + "RegulatedNoise__DJ", + "Maddavo's Market Share" +] + +# Used this to excludes yourself for example has you don't want to handle your own messages ^^ +__excludedSoftwares = [ + 'My Awesome Market Uploader' +] + + + +""" + " Start +""" +def date(__format): + d = datetime.datetime.utcnow() + return d.strftime(__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('') + f.close() + + if (__oldTime == False) or (__oldTime != date('%H:%M:%S')): + __oldTime = date('%H:%M:%S') + __str = str(__oldTime) + ' | ' + str(__str) + else: + __str = ' ' + ' | ' + str(__str) + + print __str + sys.stdout.flush() + + if __logVerboseFile != False: + 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(): - context = zmq.Context() - subscriber = context.socket(zmq.SUB) - + echoLog('Starting EDDN Subscriber') + echoLog('') + + context = zmq.Context() + subscriber = context.socket(zmq.SUB) + subscriber.setsockopt(zmq.SUBSCRIBE, "") - subscriber.connect('tcp://eddn-relay.elite-markets.net:9500') + subscriber.setsockopt(zmq.RCVTIMEO, __timeoutEDDN) while True: - market_json = zlib.decompress(subscriber.recv()) - market_data = simplejson.loads(market_json) - print market_data - sys.stdout.flush() + try: + subscriber.connect(__relayEDDN) + + while True: + __message = subscriber.recv() + + if __message == False: + subscriber.disconnect(__relayEDDN) + break + + __message = zlib.decompress(__message) + __json = simplejson.loads(__message) + __converted = False + + + # Handle commodity v1 + if __json['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1': + echoLogJSON(__message) + echoLog('Receiving commodity-v1 message...'); + echoLog(' - Converting to v2...'); + + __temp = {} + __temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2' + __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': + if __converted == False: + echoLogJSON(__message) + echoLog('Receiving commodity-v2 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(' - 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']) + + ((' (' + __commodity['supplyLevel'] + ')') if 'supplyLevel' in __commodity else '') + ) + echoLog(' - Sell Price: ' + str(__commodity['sellPrice'])) + echoLog(' - Demand: ' + str(__commodity['demand']) + + ((' (' + __commodity['demandLevel'] + ')') if 'demandLevel' in __commodity else '') + ) + # End example + + + del __authorised, __excluded + + del __converted + echoLog('') + echoLog('') + + + except zmq.ZMQError, e: + echoLog('') + echoLog('ZMQSocketException: ' + str(e)) + echoLog('') + time.sleep(10) + + if __name__ == '__main__': main()