class Cgminer: def __init__(self): self.api = CgminerAPI() self.config_path = os.path.dirname(os.path.realpath(__file__)) + '/config/cgminer' def active(self): output = os.popen('ps x | grep cgminer').read() if re.search('config/cgminer', output): return True return False def start(self): subprocess.Popen(['cgminer', '-c', 'config/cgminer', '-T'], stdout=open('logs', 'w')) def stop(self): subprocess.Popen(['killall', 'cgminer'], stdout=subprocess.PIPE) def restart(self): self.stop() time.sleep(3) self.start() def devices(self): data = {} try: data['edevs'] = self.api.edevs() except Exception as e: raise CgminerError('Problem with API edevs method: ' + e.message) try: data['estats'] = self.api.estats() except Exception as e: raise CgminerError('Problem with API estats method: ' + e.message) result = [] try: for i in xrange(0, len(data['edevs']['DEVS'])): dev = data['edevs']['DEVS'][i] stat = data['estats']['STATS'][i] result.append({ 'id': dev['ID'], 'name': dev['Name'], 'mhs': dev['MHS 1m'], 'ghs': dev['MHS 1m'] / 1000, 'temperature': dev['Temperature'], 'accepted': dev['Accepted'], 'rejected': dev['Rejected'], 'clockrate': stat['base clockrate'], 'fan': stat['fan percent'], 'voltage': stat['Asic0 voltage 0'] }) except Exception as e: raise CgminerError('Problem with devices data preparing: ' + e.message) return result def summary(self): try: summary = self.api.summary() except Exception as e: raise CgminerError('Problem with API summary method: ' + e.message) try: pools = self.api.pools() except Exception as e: raise CgminerError('Problem with API pools method: ' + e.message) try: edevs = self.api.edevs() except Exception as e: raise CgminerError('Problem with API edevs method: ' + e.message) try: total = summary['SUMMARY'][0]['Accepted'] + summary['SUMMARY'][0]['Rejected'] if total == 0: accepted_percent = 0 rejected_percent = 0 else: accepted_percent = int(summary['SUMMARY'][0]['Accepted'] / total * 100) rejected_percent = int(summary['SUMMARY'][0]['Rejected'] / total * 100) result = { 'mhs': 0, 'ghs': 0, 'accepted': summary['SUMMARY'][0]['Accepted'], 'rejected': summary['SUMMARY'][0]['Rejected'], 'accepted_percent': accepted_percent, 'rejected_percent': rejected_percent, 'pool': { 'url': pools['POOLS'][0]['URL'], 'user': pools['POOLS'][0]['User'] } } for edev in edevs['DEVS']: result['mhs'] += edev['MHS 1m'] result['ghs'] += edev['MHS 1m'] / 1000 except Exception as e: raise CgminerError('Problem with summary data preparing: ' + e.message) return result def pools(self): with open(self.config_path, 'r') as f: return json.loads(f.read())['pools'] # TODO: catch exceptions def update_pools(self, pools): ordered_pools = sorted(pools, key=lambda pool: int(pool['priority'])) with open(self.config_path, 'r') as f: config = json.loads(f.read()) config['pools'] = ordered_pools with open(self.config_path, 'w') as f: json.dump(config, f) def clockrate(self): with open(self.config_path, 'r') as f: return json.loads(f.read())['hfa-hash-clock'] def update_clockrate(self, clockrate): with open(self.config_path, 'r') as f: config = json.loads(f.read()) config['hfa-hash-clock'] = clockrate with open(self.config_path, 'w') as f: json.dump(config, f) def fan_speed(self): with open(self.config_path, 'r') as f: config = json.loads(f.read()) if 'hfa-fan' in config: return config['hfa-fan'] else: return 'auto' def update_fan_speed(self, fan_speed): with open(self.config_path, 'r') as f: config = json.loads(f.read()) if fan_speed == 'auto': if 'hfa-fan' in config: del config['hfa-fan'] else: config['hfa-fan'] = fan_speed with open(self.config_path, 'w') as f: json.dump(config, f) def overheat(self): with open(self.config_path, 'r') as f: return json.loads(f.read())['hfa-temp-overheat'] def update_overheat(self, overheat): with open(self.config_path, 'r') as f: config = json.loads(f.read()) config['hfa-temp-overheat'] = overheat with open(self.config_path, 'w') as f: json.dump(config, f) def save(self): try: print self.api.save(self.config_path) except Exception as e: raise CgminerError('Problem with API save method: ' + e.message) return True def latest_hashrate_poins(self): points = [] try: for edev in self.api.edevs()['DEVS']: points.append(edev['MHS 5m'] / 1000) except Exception as e: raise CgminerError('Problem with API edevs method: ' + e.message) return points
print("Connected to broker " + os.getenv('MQTT_HOST') + ":" + os.getenv('MQTT_PORT')) global Connected # Use global variable Connected = True # Signal connection else: print("Connection failed") client = mqtt.Client() # Register connect callback client.on_connect = on_connect # Set access token client.username_pw_set(os.getenv('MQTT_PASS')) # Connect to ThingsBoard using default MQTT port and 60 seconds keepalive interval client.connect(os.getenv('MQTT_HOST'), int(os.getenv('MQTT_PORT')), 60) client.loop_start() # start the loop try: while True: try: client.publish("v1/devices/me/telemetry", json.dumps(cgminer.estats()['STATS'][0])) sleep(int(os.getenv('SLEEP_TIME', 1))) except Exception as e: print(e) sleep(10) except KeyboardInterrupt: client.disconnect() client.loop_stop()
def on_message(message): print("message received: " + str(message)) # If there is an error processing the message return an error string, otherwise return nothing. cgminer = CgminerAPI(host=os.getenv('CGMINER_HOST'), port=int(os.getenv('CGMINER_PORT'))) client = cayenne.client.CayenneMQTTClient() client.on_message = on_message client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID) # For a secure connection use port 8883 when calling client.begin: # client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883) timestamp = 0 while True: client.loop() if (time.time() > timestamp + 5): STATS = cgminer.estats()['STATS'][0] client.celsiusWrite(1, int(STATS["temp1"])) client.celsiusWrite(2, int(STATS["temp2"])) client.virtualWrite(3, float(STATS['voltage']), cayenne.client.TYPE_VOLTAGE, cayenne.client.UNIT_VOLTS) client.virtualWrite(4, float(STATS["GHS 5s"]), cayenne.client.TYPE_VOLTAGE, 'GHs') timestamp = time.time()
class Cgminer: def __init__(self): self.api = CgminerAPI() self.config_path = os.path.dirname( os.path.realpath(__file__)) + '/config/cgminer' def active(self): output = os.popen('ps x | grep cgminer').read() if re.search('config/cgminer', output): return True return False def start(self): subprocess.Popen(['cgminer', '-c', 'config/cgminer', '-T'], stdout=open('logs', 'w')) def stop(self): subprocess.Popen(['killall', 'cgminer'], stdout=subprocess.PIPE) def restart(self): self.stop() time.sleep(3) self.start() def devices(self): data = {} try: data['edevs'] = self.api.edevs() except Exception as e: raise CgminerError('Problem with API edevs method: ' + e.message) try: data['estats'] = self.api.estats() except Exception as e: raise CgminerError('Problem with API estats method: ' + e.message) result = [] try: for i in xrange(0, len(data['edevs']['DEVS'])): dev = data['edevs']['DEVS'][i] stat = data['estats']['STATS'][i] result.append({ 'id': dev['ID'], 'name': dev['Name'], 'mhs': dev['MHS 1m'], 'ghs': dev['MHS 1m'] / 1000, 'temperature': dev['Temperature'], 'accepted': dev['Accepted'], 'rejected': dev['Rejected'], 'clockrate': stat['base clockrate'], 'fan': stat['fan percent'], 'voltage': stat['Asic0 voltage 0'] }) except Exception as e: raise CgminerError('Problem with devices data preparing: ' + e.message) return result def summary(self): try: summary = self.api.summary() except Exception as e: raise CgminerError('Problem with API summary method: ' + e.message) try: pools = self.api.pools() except Exception as e: raise CgminerError('Problem with API pools method: ' + e.message) try: edevs = self.api.edevs() except Exception as e: raise CgminerError('Problem with API edevs method: ' + e.message) try: total = summary['SUMMARY'][0]['Accepted'] + summary['SUMMARY'][0][ 'Rejected'] if total == 0: accepted_percent = 0 rejected_percent = 0 else: accepted_percent = int(summary['SUMMARY'][0]['Accepted'] / total * 100) rejected_percent = int(summary['SUMMARY'][0]['Rejected'] / total * 100) result = { 'mhs': 0, 'ghs': 0, 'accepted': summary['SUMMARY'][0]['Accepted'], 'rejected': summary['SUMMARY'][0]['Rejected'], 'accepted_percent': accepted_percent, 'rejected_percent': rejected_percent, 'pool': { 'url': pools['POOLS'][0]['URL'], 'user': pools['POOLS'][0]['User'] } } for edev in edevs['DEVS']: result['mhs'] += edev['MHS 1m'] result['ghs'] += edev['MHS 1m'] / 1000 except Exception as e: raise CgminerError('Problem with summary data preparing: ' + e.message) return result def pools(self): with open(self.config_path, 'r') as f: return json.loads(f.read())['pools'] # TODO: catch exceptions def update_pools(self, pools): ordered_pools = sorted(pools, key=lambda pool: int(pool['priority'])) with open(self.config_path, 'r') as f: config = json.loads(f.read()) config['pools'] = ordered_pools with open(self.config_path, 'w') as f: json.dump(config, f) def clockrate(self): with open(self.config_path, 'r') as f: return json.loads(f.read())['hfa-hash-clock'] def update_clockrate(self, clockrate): with open(self.config_path, 'r') as f: config = json.loads(f.read()) config['hfa-hash-clock'] = clockrate with open(self.config_path, 'w') as f: json.dump(config, f) def fan_speed(self): with open(self.config_path, 'r') as f: config = json.loads(f.read()) if 'hfa-fan' in config: return config['hfa-fan'] else: return 'auto' def update_fan_speed(self, fan_speed): with open(self.config_path, 'r') as f: config = json.loads(f.read()) if fan_speed == 'auto': if 'hfa-fan' in config: del config['hfa-fan'] else: config['hfa-fan'] = fan_speed with open(self.config_path, 'w') as f: json.dump(config, f) def overheat(self): with open(self.config_path, 'r') as f: return json.loads(f.read())['hfa-temp-overheat'] def update_overheat(self, overheat): with open(self.config_path, 'r') as f: config = json.loads(f.read()) config['hfa-temp-overheat'] = overheat with open(self.config_path, 'w') as f: json.dump(config, f) def save(self): try: print self.api.save(self.config_path) except Exception as e: raise CgminerError('Problem with API save method: ' + e.message) return True def latest_hashrate_poins(self): points = [] try: for edev in self.api.edevs()['DEVS']: points.append(edev['MHS 5m'] / 1000) except Exception as e: raise CgminerError('Problem with API edevs method: ' + e.message) return points
class Cgminer: def __init__(self): self.api = CgminerAPI() def devices(self): data = {} try: data['edevs'] = self.api.edevs() except Exception as e: raise CgminerError('Problem with API edevs method: ' + e.message) try: data['estats'] = self.api.estats() except Exception as e: raise CgminerError('Problem with API estats method: ' + e.message) result = [] try: for i in xrange(0, len(data['edevs']['DEVS'])): dev = data['edevs']['DEVS'][i] stat = data['estats']['STATS'][i] result.append({ 'id': dev['ID'], 'name': dev['Name'], 'mhs': dev['MHS 5m'], 'ghs': dev['MHS 5m'] / 1000, 'temperature': dev['Temperature'], 'accepted': dev['Accepted'], 'rejected': dev['Rejected'], 'clockrate': stat['base clockrate'], 'fan': stat['fan percent'], 'voltage': stat['Asic0 voltage 0'] }) except Exception as e: raise CgminerError('Problem with devices data preparing: ' + e.message) return result def summary(self): try: summary = self.api.summary() except Exception as e: raise CgminerError('Problem with API summary method: ' + e.message) try: pools = self.api.pools() except Exception as e: raise CgminerError('Problem with API pools method: ' + e.message) try: total = summary['SUMMARY'][0]['Accepted'] + summary['SUMMARY'][0]['Rejected'] accepted_percent = int(summary['SUMMARY'][0]['Accepted'] / total * 100) rejected_percent = int(summary['SUMMARY'][0]['Rejected'] / total * 100) result = { 'mhs': summary['SUMMARY'][0]['MHS 5m'], 'ghs': summary['SUMMARY'][0]['MHS 5m'] / 1000, 'accepted': summary['SUMMARY'][0]['Accepted'], 'rejected': summary['SUMMARY'][0]['Rejected'], 'accepted_percent': accepted_percent, 'rejected_percent': rejected_percent, 'pool': { 'url': pools['POOLS'][0]['URL'], 'user': pools['POOLS'][0]['User'] } } except Exception as e: raise CgminerError('Problem with preparing data: ' + e.message) return result def save(self, file_path): try: print self.api.save(file_path) except Exception as e: raise CgminerError('Problem with API save method: ' + e.message) return True def latest_hashrate_poins(self): points = [] try: for edev in self.api.edevs()['DEVS']: points.append(edev['MHS 5m'] / 1000) except Exception as e: raise CgminerError('Problem with API edevs method: ' + e.message) return points