forked from jedimstr/BFGMobileAdapter
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CGMinerMobileAdapter.py
198 lines (171 loc) · 6.05 KB
/
CGMinerMobileAdapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/python
#
# Originally derived from Hazado's BFGMobileAdapter script
#
# CGMinerMobileAdapter
#
# Copyright 2014 Axadiw
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version. See COPYING for more details.
import os
import time
import datetime
import argparse
import json
import logging
import httplib
import pprint
import socket
import urllib
import urllib2
def start_mining():
os.system("mine start")
def stop_mining():
os.system("mine stop")
def restart_mining():
os.system("mine restart")
actions = {
"STOP" : stop_mining,
"START" : start_mining,
"RESTART" : restart_mining
}
print '[ -=-=-=-=- Starting CGMinerMobileAdapter - CGMiner to MobileMiner Interface -=-=-=-=- ]'
while 1:
logging.basicConfig(
format='%(asctime)s %(levelname)s %(message)s',
level=logging.DEBUG
)
settingsPath=os.path.dirname(os.path.abspath(__file__))+"/settings.conf"
if (not os.path.isfile(settingsPath)):
print "File settings.conf not found"
os._exit(-1)
f = open(settingsPath)
settingsContent = open(settingsPath).readlines()
emailAddy = settingsContent[0].rstrip('\n')
applicationKey = settingsContent[1].rstrip('\n')
machineName = settingsContent[2].rstrip('\n')
f.close()
rootURL= 'https://api.mobileminerapp.com'
apiKey = 'yhErhKxFMCmEkf'
reqURL = rootURL+'/MiningStatisticsInput?emailAddress='+emailAddy+'&applicationKey='+applicationKey+'&apiKey='+apiKey
parser = argparse.ArgumentParser()
parser.add_argument("command", default="devs", nargs='?')
parser.add_argument("parameter", default="", nargs='?')
parser.add_argument("--hostname", default="localhost")
parser.add_argument("--port", type=int, default=4028)
args = parser.parse_args()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(30)
try:
s.connect((args.hostname, args.port))
except socket.error, e:
logging.error(e)
try:
s.send("{\"command\" : \"%s\", \"parameter\" : \"%s\"}"
% (args.command, args.parameter)
)
except socket.error, e:
logging.error(e)
data = []
data2 = []
print '['+str(datetime.datetime.now()).split('.')[0]+'] Getting Data from CGMiner RPC API using port:'+str(args.port)
try:
data = s.recv(32768)
except socket.error, e:
logging.error(e)
try:
s.close()
except socket.error,e:
logging.error(e)
if data:
data = json.loads(data.replace('\x00', ''))
#print data
try:
for item in data['DEVS']:
device = dict()
device[u'MachineName'] = machineName
device[u'MinerName'] = u'CGMiner'
device[u'CoinSymbol'] = u'LTC'
device[u'CoinName'] = u'Litecoin'
device[u'Algorithm'] = u'Scrypt'
if not item.get('Name'):
device[u'Kind'] = u'GPU'
device[u'FanSpeed'] = item[u'Fan Speed']
device[u'FanPercent'] = item[u'Fan Percent']
device[u'GpuClock'] = item[u'GPU Clock']
device[u'MemoryClock'] = item[u'Memory Clock']
device[u'GpuVoltage'] = item[u'GPU Voltage']
device[u'GpuActivity'] = item[u'GPU Activity']
device[u'PowerTune'] = item[u'Powertune']
device[u'Intensity'] = item[u'Intensity']
elif item[u'Name'] == u'OCL':
device[u'Kind'] = item[u'Name']
device[u'FanSpeed'] = item[u'Fan Speed']
device[u'FanPercent'] = item[u'Fan Percent']
device[u'GpuClock'] = item[u'GPU Clock']
device[u'MemoryClock'] = item[u'Memory Clock']
device[u'GpuVoltage'] = item[u'GPU Voltage']
device[u'GpuActivity'] = item[u'GPU Activity']
device[u'PowerTune'] = item[u'Powertune']
device[u'Intensity'] = item[u'Intensity']
else:
device[u'Kind'] = item[u'Name']
if not item.get('Name'):
device[u'Index'] = item[u'GPU']
else:
device[u'Index'] = item[u'ID']
if item[u'Enabled'] == u'Y':
device[u'Enabled'] = True
else:
device[u'Enabled'] = False
if u'Temperature' in item:
device[u'Temperature'] = item[u'Temperature']
device[u'Status'] = item[u'Status']
device[u'AverageHashrate'] = item[u'MHS av'] * 1000
device[u'CurrentHashrate'] = item[u'MHS 5s'] * 1000
device[u'AcceptedShares'] = item[u'Accepted']
device[u'RejectedShares'] = item[u'Rejected']
device[u'HardwareErrors'] = item[u'Hardware Errors']
device[u'Utility'] = item[u'Utility']
data2.append(device)
#print data2
req = urllib2.Request(reqURL)
req.add_header('Content-Type', 'application/json')
except Exception:
import traceback
logging.warning('Generic Exception: ' + traceback.format_exc())
try:
response = urllib2.urlopen(req, json.dumps(data2), 30)
except urllib2.HTTPError, e:
logging.warning('HTTPError = ' + str(e.code))
except urllib2.URLError, e:
logging.warning('URLError = ' + str(e.reason))
except httplib.HTTPException, e:
logging.warning('HTTPException')
except Exception:
import traceback
logging.warning('Generic Exception: ' + traceback.format_exc())
print '['+str(datetime.datetime.now()).split('.')[0]+'] Sending to MobileMiner API from '+machineName
getCommandsURL = rootURL+'/RemoteCommands?emailAddress='+emailAddy+'&applicationKey='+applicationKey+'&machineName='+machineName+'&apiKey='+apiKey
getCommandsReq = urllib2.Request(getCommandsURL)
getCommandsReq.add_header('Content-Type', 'application/json')
try:
getCommandsResponse = urllib2.urlopen(getCommandsReq, None, 30)
commands = json.loads(getCommandsResponse.read())
#print commands
for d in commands:
commandText = d['CommandText']
#print commandText
actions[commandText]()
removeCommandsURL = 'https://mobileminer.azurewebsites.net/api/RemoteCommands?emailAddress='+emailAddy+'&applicationKey='+applicationKey+'&machineName='+machineName+'&apiKey='+apiKey+'&commandId='+str(d['Id'])
removeCommandsReq = urllib2.Request(removeCommandsURL)
removeCommandsReq.get_method = lambda: 'DELETE'
removeCommandsReq.add_header('Content-Type', 'application/json')
urllib2.urlopen(removeCommandsReq, None, 30)
except Exception:
import traceback
logging.warning('GetCommands Generic Exception: ' + traceback.format_exc())
time.sleep(60)