-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrounded.py
1544 lines (1202 loc) · 55.1 KB
/
grounded.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Name: grounded.py
Desc: A cross-platform program for getting started with your LabJack.
"""
# Local Imports
from devicemanager import DeviceManager
from skymotemanager import SkyMoteManager
from fio import FIO, UE9FIO
from groundedutils import *
from skymotefirmwareutils import FirmwareFile, BRIDGE_PRODUCT_ID
# Required Packages Imports
# - CherryPy
import cherrypy
import cherrypy.lib
from cherrypy.lib.static import serve_file
from Cheetah.Template import Template
# - LabJackPython
import LabJackPython, Modbus
# - gdata
import gdata.docs.service
import gdata.service
# Standard Library Imports
from datetime import datetime
import os, os.path, zipfile
import json, httplib2
from urllib import urlencode, quote, unquote
import sys, socket
import cStringIO as StringIO, ConfigParser
import webbrowser
import sys
import traceback
import threading
# Mimetypes helps select the correct type based on extension.
import mimetypes
mimetypes.init()
mimetypes.types_map['.dwg']='image/x-dwg'
mimetypes.types_map['.ico']='image/x-icon'
mimetypes.types_map['.bz2']='application/x-bzip2'
mimetypes.types_map['.gz']='application/x-gzip'
mimetypes.types_map['.csv']='text/plain'
# Global Constants
CLOUDDOT_GROUNDED_VERSION = "0.12"
CLOUDDOT_GROUNDED_CONF = "./clouddotgrounded.conf"
if not getattr(sys, 'frozen', ''):
# not frozen: in regular python interpreter
IS_FROZEN = False
else:
# py2exe: running in an executable
IS_FROZEN = True
# Global Utility Functions
# - Create some decorator methods for functions.
def exposeRawFunction(f):
""" Simply exposes the function """
f.exposed = True
return f
def exposeJsonFunction(f):
"""
Creates and exposes a function which will JSON encode the output of the
passed in function.
"""
def jsonFunction(self, *args, **kwargs):
cherrypy.response.headers['content-type'] = "application/json"
result = f(self, *args, **kwargs)
return json.dumps(result)+"\n"
jsonFunction.exposed = True
return jsonFunction
# Class Definitions
class DevicesPage(object):
""" A class for handling all things /devices/
"""
def __init__(self, dm):
self.dm = dm
@exposeJsonFunction
def index(self):
""" Handles /devices/, returns a JSON list of all known devices.
"""
self.dm.updateDeviceDict()
devices = self.dm.listAll()
t = serve_file2("templates/devices.tmpl")
t.devices = devices
t.hashPrefix = "d"
t2 = serve_file2("templates/device-summary-list.tmpl")
t2.UE9_MIN_FIRMWARE = UE9_MIN_FIRMWARE_VERSION
t2.U6_MIN_FIRMWARE = U6_MIN_FIRMWARE_VERSION
t2.U3_MIN_FIRMWARE = U3_MIN_FIRMWARE_VERSION
t2.devices = [ deviceAsDict(d) for d in self.dm.devices.values() ]
devices['html'] = t.respond()
devices['htmlSummaryList'] = t2.respond()
if self.dm.usbOverride:
t3 = serve_file2("templates/usb-only.tmpl")
devices['usbOverride'] = t3.respond()
return devices
@exposeJsonFunction
def default(self, serial, cmd = None, *pargs, **kwargs):
""" Handles URLs like: /devices/<serial number>/
if the URL is just /devices/<serial number>/ it returns the
"details" of that device.
if the URL is more like /devices/<serial number>/<command>, it runs
that command on the device.
"""
if cmd is None:
returnDict = self.dm.details(serial)
if not returnDict['meetsFirmwareRequirements']:
returnDict['html'] = """<div>Device doesn't meet firmware requirements</div>"""
returnDict['htmlScanning'] = ""
t = serve_file2("templates/device-details.tmpl")
t.device = returnDict
tScanning = serve_file2("templates/device-scanning.tmpl")
tScanning.device = returnDict
returnDict['html'] = t.respond()
returnDict['htmlScanning'] = tScanning.respond()
return returnDict
else:
cmd = cmd.lower()
return { "result" : self.dm.callDeviceFunction(serial, cmd, pargs, kwargs)[1] }
@exposeJsonFunction
def timerCounterConfig(self, serial, counterSelected = False):
devType = self.dm.getDevice(serial).devType
currentConfig = self.dm.readTimerCounterConfig(serial.encode("ascii", "replace"))
print currentConfig
t = serve_file2("templates/device-configureTimerCounter.tmpl")
t.devType = devType
t.timerModeUrls = createTimerModeToHelpUrlList(devType)
t.updateUrl = "/devices/updateTimerCounterConfig/%s" % serial
t.currentConfig = currentConfig
t.offsetChoices = createTimerChoicesList(devType)
tcPinLocationHtml = self.tcPinLocationSummary(serial)
returnDict = dict(html = t.respond(), serial = serial, counterSelected = counterSelected, tcPinLocationHtml = tcPinLocationHtml)
return returnDict
def offsetToLabel(self, offset):
if offset < 8:
return "FIO%i" % offset
else:
return "EIO%i" % (offset - 8)
@exposeRawFunction
def tcPinLocationSummary(self, serial, timerClockBase = 0, timerClockDivisor = 1, pinOffset = 0, counter0Enable = 0, counter1Enable = 0, **timerSettings):
t = serve_file2("templates/tc-pin-location-summary.tmpl")
pinOffset = int(pinOffset)
timerClockBase = int(timerClockBase)
counter0Enable = int(counter0Enable)
counter1Enable = int(counter1Enable)
tcPins = []
numTimersEnabled = 0
for i in range(6):
if "timer%iEnabled" % i in timerSettings:
if int(timerSettings["timer%iEnabled" % i]):
tcPins.append((self.offsetToLabel(pinOffset), "Timer %i" % i))
pinOffset += 1
else:
break
if counter0Enable:
tcPins.append((self.offsetToLabel(pinOffset), "Counter 0"))
pinOffset += 1
if counter1Enable:
tcPins.append((self.offsetToLabel(pinOffset), "Counter 1"))
pinOffset += 1
t.tcPins = tcPins
return t.respond()
@exposeRawFunction
def resetCounter(self, serial, inputNumber = 0):
self.dm.resetCounter(serial, int(inputNumber))
return self.scan(serial, noCache = True)
@exposeRawFunction
def updateTimerCounterConfig(self, serial, timerClockBase = 0, timerClockDivisor = 1, pinOffset = 0, counter0Enable = 0, counter1Enable = 0, **timerSettings):
print "got: serial =", serial
print "timerClockBase =", timerClockBase
print "timerClockDivisor =", timerClockDivisor
print "pinOffset =", pinOffset
print "counter0Enable =", counter0Enable
print "counter1Enable =", counter1Enable
print "timerSettings =", timerSettings
self.dm.updateTimerCounterConfig(serial, int(timerClockBase), int(timerClockDivisor), int(pinOffset), int(counter0Enable), int(counter1Enable), timerSettings)
return "Ok."
def renderU3Templates(self, inputConnection):
if inputConnection['fioNumber'] < 4 and inputConnection['device']['productName'].endswith("HV"):
t = serve_file2("templates/u3-hv-analog-connection-dialog.tmpl")
t.longSettling = bool(inputConnection['gainIndex'])
t.quickSample = bool(inputConnection['settlingFactor'])
return t.respond()
else:
t = serve_file2("templates/u3-connection-dialog.tmpl")
t.longSettling = bool(inputConnection['gainIndex'])
t.quickSample = bool(inputConnection['settlingFactor'])
t.isHv = inputConnection['device']['productName'].endswith("HV")
return t.respond()
def renderU6Templates(self, inputConnection):
if inputConnection['chType'] == ANALOG_TYPE:
t = serve_file2("templates/u6-analog-connection-dialog.tmpl")
t.inputConnection = inputConnection['label']
t.isPro = inputConnection['device']['productName'].endswith("Pro")
t.isEvenChannel = not bool(inputConnection['fioNumber'] % 2)
t.inputConnectionPair = "AIN%s" % (inputConnection['fioNumber']+1)
return t.respond()
else:
t = serve_file2("templates/u6-digital-connection-dialog.tmpl")
return t.respond()
def renderUE9Templates(self, inputConnection):
if inputConnection['chType'] == ANALOG_TYPE:
t = serve_file2("templates/ue9-analog-connection-dialog.tmpl")
t.inputConnection = inputConnection['label']
t.isPro = inputConnection['device']['productName'].endswith("Pro")
return t.respond()
else:
t = serve_file2("templates/ue9-digital-connection-dialog.tmpl")
return t.respond()
@exposeJsonFunction
def inputInfo(self, serial = None, inputNumber = 0):
""" Returns a JSON of the current state of an input.
"""
inputConnection = self.dm.getFioInfo(serial, int(inputNumber))
if inputConnection['chType'] == "DAC":
t = serve_file2("templates/dac.tmpl")
t.dac = inputConnection
inputConnection['html'] = t.respond()
elif inputConnection['device']['devType'] == 3:
inputConnection['html'] = self.renderU3Templates(inputConnection)
elif inputConnection['device']['devType'] == 6:
inputConnection['html'] = self.renderU6Templates(inputConnection)
elif inputConnection['device']['devType'] == 9:
inputConnection['html'] = self.renderUE9Templates(inputConnection)
return inputConnection
@exposeRawFunction
def support(self, serial):
dev = self.dm.getDevice(serial)
print "Headers:", cherrypy.request.headers
t = serve_file2("templates/support.tmpl")
t.device = deviceAsDict(dev)
if dev.devType == 3:
t.supportUrl = "http://labjack.com/support/u3"
t.supportUsersGuideUrl = "http://labjack.com/support/u3/users-guide"
elif dev.devType == 6:
t.supportUrl = "http://labjack.com/support/u6"
t.supportUsersGuideUrl = "http://labjack.com/support/u6/users-guide"
elif dev.devType == 9:
t.supportUrl = "http://labjack.com/support/ue9"
t.supportUsersGuideUrl = "http://labjack.com/support/ue9/users-guide"
# Call the exportConfig function on the device.
devDict, result = self.dm.callDeviceFunction(serial, "exportconfig", [], {})
# exportConfig returns a ConfigParser object. We need it as a string.
fakefile = StringIO.StringIO()
result.write(fakefile)
t.config = fakefile.getvalue()
t.groundedVersion = CLOUDDOT_GROUNDED_VERSION
t.ljpVersion = LabJackPython.LABJACKPYTHON_VERSION
t.usbOrLJSocket = self.dm.usbOverride
userAgent = cherrypy.request.headers['User-Agent']
os = userAgent.split("(")[1]
os = os.split(")")[0]
os = os.split(";")[2].strip()
t.os = os
t.isWindows = (LabJackPython.os.name == 'nt')
t.driverVersion = LabJackPython.GetDriverVersion()
t.userAgent = userAgent
return t.respond()
@exposeJsonFunction
def updateInputInfo(self, serial, inputNumber, chType, negChannel = None, state = None, gainIndex = None, resolutionIndex = None, settlingFactor = None ):
""" For configuring an input.
serial = serial number of device
inputNumber = the row number of that input
chType = ( analogIn, digitalIn, digitalOut )
negChannel = the negative channel, only matters for analogIn type
state = 1 for high, 0 for low. Only matters for digitalOut
gainIndex = the gain index for the U6/UE9, LongSettling for U3.
resolutionIndex = the resolution for U6/UE9, nothing on U3.
settlingFactor = for settling time on U6/UE9, QuickSample on U3.
"""
if negChannel and int(negChannel) < 30 and self.dm.getDevice(serial).devType == 3:
print "Setting %s to analog." % negChannel
inputConnection = FIO( int(negChannel), "FIO%s" % negChannel, chType, state, 31 )
self.dm.updateFio(serial, inputConnection)
# Make a temp FIO with the new settings.
inputConnection = FIO( int(inputNumber), "FIO%s" % inputNumber, chType, state, negChannel )
if gainIndex is not None:
inputConnection.gainIndex = int(gainIndex)
if resolutionIndex is not None:
inputConnection.resolutionIndex = int(resolutionIndex)
if settlingFactor is not None:
inputConnection.settlingFactor = int(settlingFactor)
# Tells the device manager to update the input
self.dm.updateFio(serial, inputConnection)
return { "result" : 0 }
@exposeRawFunction
def toggleDigitalOutput(self, serial, inputNumber):
""" Toggle a digital output line.
"""
inputConnection = self.dm.getFioInfo(serial, int(inputNumber))
chType = inputConnection["chType"]
state = inputConnection["state"]
if chType == "digitalOut":
state = not state
newInputConnection = FIO( int(inputNumber), "FIO%s" % inputNumber, chType, state )
self.dm.updateFio(serial, newInputConnection)
return self.scan(serial, noCache = True)
@exposeJsonFunction
def scan(self, serial = None, noCache = False):
""" Scan reads all the inputs and such off the device. Renders the
result as a JSON.
"""
print "Scan: serial = %s" % serial
serialNumber, results = self.dm.scan(serial, noCache)
if results:
return results
else:
raise cherrypy.HTTPError(404)
@exposeJsonFunction
def setDefaults(self, serial = None):
""" Makes the calls to set the current state of the device as the
power-up default.
"""
results = { 'state' : self.dm.setDefaults(serial) }
return results
@exposeRawFunction
def importConfigFromFile(self, myFile, serial):
""" Allows people to upload a config file which gets loaded onto
the device.
"""
# loadConfig takes a ConfigParser object, so we need to make one.
parserobj = ConfigParser.SafeConfigParser()
parserobj.readfp(myFile.file)
# Have to device load in the configuration.
devDict, result = self.dm.callDeviceFunction(serial, "loadconfig", [parserobj], {})
# Rebuild the FioList because settings could have changed.
self.dm.remakeFioList(serial)
# Redirect them away because there's nothing to be rendered.
# TODO: Return JSON for Mike C.
raise cherrypy.HTTPRedirect("/")
if IS_FROZEN:
# All of this depends on us being 'frozen' in an executable.
ZIP_FILE = zipfile.ZipFile(os.path.abspath(sys.executable), 'r')
def renderFromLocalFile(filepath):
# Figure out the content type from the file extension.
ext = ""
i = filepath.rfind('.')
if i != -1:
ext = filepath[i:].lower()
content_type = mimetypes.types_map.get(ext, None)
# Set or remove the content type
if content_type is not None:
cherrypy.response.headers['Content-Type'] = content_type
else:
cherrypy.response.headers.pop('Content-Type')
try:
# Open up the file and read it into the response body
f = open(filepath)
cherrypy.response.body = "".join(f.readlines())
f.close()
print "Body set, returning true"
# Tell CherryPy we got this one
return True
except Exception, e:
print "Got Exception in renderFromLocalFile: %s" % e
# Tell CherryPy we didn't render and it should try.
return False
def renderFromZipFile():
""" renderFromZipFile handles pulling static files out of the ZipFile
and rendering them like nothing happened.
"""
print "renderFromZipFile got run: %s" % cherrypy.request.path_info
# Get rid of the leading "/"
filepath = cherrypy.request.path_info[1:]
#print "filepath: %s" % filepath
# Check if the file being requested is in the ZipFile
if filepath not in ZIP_FILE.namelist():
#print "%s not in name list." % filepath
# Check if the file is local
localAbsPath = os.path.join(current_dir,filepath)
#print "localAbsPath: ", localAbsPath
if (filepath.startswith("logfiles") or filepath.startswith("configfiles")) and os.path.exists(localAbsPath):
return renderFromLocalFile(localAbsPath)
else:
# If it isn't then we pass the responsibility on.
return False
# Figure out the content type from the file extension.
ext = ""
i = filepath.rfind('.')
if i != -1:
ext = filepath[i:].lower()
content_type = mimetypes.types_map.get(ext, None)
# Set or remove the content type
if content_type is not None:
cherrypy.response.headers['Content-Type'] = content_type
else:
cherrypy.response.headers.pop('Content-Type')
try:
# Open up the file and read it into the response body
f = ZIP_FILE.open(filepath)
cherrypy.response.body = "".join(f.readlines())
f.close()
print "Body set, returning true"
# Tell CherryPy we got this one
return True
except Exception, e:
print "Got Exception in renderFromZipFile: %s" % e
# Tell CherryPy we didn't render and it should try.
return False
cherrypy.tools.renderFromZipFile = cherrypy._cptools.HandlerTool(renderFromZipFile)
def serve_file2(path):
""" A slightly modified version of serve_file, to handle the case where
we are running inside an executable.
"""
if IS_FROZEN:
f = ZIP_FILE.open(path)
body = "".join(f.readlines())
f.close()
if path.endswith(".tmpl"):
return Template(source=body)
else:
return body
else:
if path.endswith(".tmpl"):
return Template(file=os.path.join(current_dir,path))
else:
return serve_file(os.path.join(current_dir,path))
class CloudDotPage:
""" A class for handling all things /clouddot/
"""
def __init__(self, dm):
self.dm = dm
self.parser = ConfigParser.SafeConfigParser()
self.readConfigFile()
def readConfigFile(self):
""" Reads in the username and API Key from the grounded config file and
saves them to the device manager.
"""
self.parser.read(CLOUDDOT_GROUNDED_CONF)
if self.parser.has_section("CloudDot"):
if self.parser.has_option("CloudDot", "username"):
self.dm.username = self.parser.get("CloudDot", "username")
if self.parser.has_option("CloudDot", "api key"):
self.dm.apikey = self.parser.get("CloudDot", "api key")
def saveConfigFile(self):
""" Takes the username and API Key saved in the device manager and
writes them out to the grounded config file.
"""
self.parser.read(CLOUDDOT_GROUNDED_CONF)
if not self.parser.has_section("CloudDot"):
self.parser.add_section("CloudDot")
self.parser.set("CloudDot", "username", self.dm.username)
self.parser.set("CloudDot", "api key", self.dm.apikey)
with open(CLOUDDOT_GROUNDED_CONF, 'wb') as configfile:
self.parser.write(configfile)
@exposeJsonFunction
def info(self, serial):
""" Handles /clouddot/info/<serial>
"""
# Tell people (firefox) not to cash this page.
cherrypy.response.headers['Cache-Control'] = "no-cache"
return self._buildInfoDict(serial)
def _buildInfoDict(self, serial):
returnDict = {}
if self.dm.username is None or self.dm.apikey is None:
t = serve_file2("templates/clouddot-user-page.tmpl")
returnDict['html'] = t.respond()
returnDict['type'] = "user-page"
returnDict['connected'] = False
else:
t = serve_file2("templates/clouddot-connect.tmpl")
t.username = self.dm.username
t.device = deviceAsDict(self.dm.getDevice(serial))
returnDict['html'] = t.respond()
returnDict['type'] = "connect-page"
returnDict['connected'] = (str(serial) in self.dm.xmppThreads)
return returnDict
@exposeJsonFunction
def connect(self, serial):
print "Connecting %s to CloudDot." % serial
self.dm.connectDeviceToCloudDot(serial)
return {'result' : "connected"}
@exposeJsonFunction
def disconnect(self, serial):
print "Disconnecting %s from CloudDot." % serial
self.dm.disconnectDeviceFromCloudDot(serial)
return {'result' : "disconnected"}
@exposeJsonFunction
def fetch(self):
print "returning user info"
return {'username' : self.dm.username, 'apikey' : self.dm.apikey}
@exposeJsonFunction
def ping(self, serial):
dev = self.dm.getDevice(serial)
data = { 'userName' : self.dm.username, "apiKey" : self.dm.apikey}
pingurl = "http://cloudapi.labjack.com/%s/devices/%s/ping.json" % (self.dm.username, serial)
pingurl += "?%s" % urlencode(data)
h = httplib2.Http()
resp, content = h.request(pingurl, "GET")
if resp['status'] != '200':
return { "message" : "The device %s has not been added CloudDot. Please add it." % serial}
else:
result = json.loads(content)
if result['connected']:
return { "message" : "%s is connected to CloudDot." % dev.name}
else:
return { "message" : "%s is not connected to CloudDot. Please try disconnecting and reconnecting." % dev.name}
@exposeJsonFunction
def check(self, label = None, username = None, apikey = None):
"""
Checks for valid username and apikey
"""
print "Check called..."
print "label = %s, username = %s, apikey = %s" % (label, username, apikey)
return self._checkUsernameAndApiKey(label, username, apikey)
def _checkUsernameAndApiKey(self, label = None, username = None, apikey = None):
if label is None:
return False
elif label == "username":
devurl = "http://cloudapi.labjack.com/%s/devices.json" % username
h = httplib2.Http()
resp, content = h.request(devurl, "GET")
if resp['status'] != '200':
return {'username-valid' : 0}
else:
return {'username-valid' : 1}
elif label == "apikey":
data = { 'userName' : username, "apiKey" : apikey}
devurl = "http://cloudapi.labjack.com/%s/info.json?%s" % (username, urlencode(data))
h = httplib2.Http()
resp, content = h.request(devurl, "GET")
if resp['status'] == '401':
return {'username-valid' : 1, 'apikey-valid' : 0}
elif resp['status'] != '200':
return {'username-valid' : 0, 'apikey-valid' : 0}
else:
return {'username-valid' : 1, 'apikey-valid' : 1}
@exposeJsonFunction
def update(self, serial = None, username = None, apikey = None):
""" Updates the saved username and API Key.
"""
print "Update called: Username = %s, apikey = %s" % (username, apikey)
results = self._checkUsernameAndApiKey("apikey", username, apikey)
if results['username-valid'] and results['username-valid']:
self.dm.username = username
self.dm.apikey = apikey
self.saveConfigFile()
#raise cherrypy.HTTPRedirect("/")
else:
print "Username or API Key was invaild."
infoDict = self._buildInfoDict(serial)
infoDict.update(results)
return infoDict
class ConfigPage(object):
""" A class for handling all things /config/
"""
def __init__(self, dm):
self.dm = dm
def getConfigFiles(self, serial):
l = []
logfilesDir = os.path.join(current_dir,"configfiles/%s" % serial)
try:
files = sorted(os.listdir(logfilesDir), reverse = True, key = lambda x: os.stat(os.path.join(logfilesDir,x)).st_ctime)
except OSError:
return []
for filename in files:
newName = replaceUnderscoresWithColons(filename)
size = os.stat(os.path.join(logfilesDir,filename)).st_size
size = float(size)/1024
sizeStr = "%.2f KB" % size
aLog = dict(name = newName, url= "/configfiles/%s/%s" % (serial, filename), loadurl = "/config/load/%s/%s" % (serial, filename), removeurl = "/config/remove/%s/%s" % (serial, filename), size = sizeStr)
l.append(aLog)
return l
def getBasicConfigFiles(self, serial):
l = []
dev = self.dm.getDevice(serial)
logfilesDir = os.path.join(current_dir,"configfiles/%s" % dev.deviceName)
if not os.path.isdir(logfilesDir):
os.mkdir(logfilesDir)
files = sorted(os.listdir(logfilesDir), reverse = True, key = lambda x: os.stat(os.path.join(logfilesDir,x)).st_ctime)
for filename in files:
newName = filename
size = os.stat(os.path.join(logfilesDir,filename)).st_size
size = float(size)/1024
sizeStr = "%.2f KB" % size
aLog = dict(name = newName, url= "/configfiles/%s/%s" % (dev.deviceName, filename), loadurl = "/config/load/%s/%s" % (serial, filename), size = sizeStr)
l.append(aLog)
return l
@exposeRawFunction
def filelist(self, serial):
t = serve_file2("templates/config-file-list.tmpl")
t.configfiles = self.getConfigFiles(serial)
t.basicconfigfiles = self.getBasicConfigFiles(serial)
dev = self.dm.getDevice(serial)
t.productName = dev.deviceName
return t.respond()
@exposeRawFunction
def exportConfigToFile(self, serial):
""" Allows people to download the configuration of their LabJack.
"""
# Save current config as power-up default
devDict, result = self.dm.callDeviceFunction(serial, "setdefaults", [], {})
# Call the exportConfig function on the device.
devDict, result = self.dm.callDeviceFunction(serial, "exportconfig", [], {})
filename = "%%Y-%%m-%%d %%H__%%M %s conf.txt" % (sanitize(devDict['name']),)
filename = datetime.now().strftime(filename)
dirpath = os.path.join(current_dir,"configfiles/%s" % serial)
if not os.path.isdir(dirpath):
os.mkdir(dirpath)
filepath = "%s/%s" % (dirpath, filename)
configfile = file(filepath, "w")
result.write(configfile)
configfile.close()
m = "Configuration "%s" saved." % replaceUnderscoresWithColons(filename)
return m
# exportConfig returns a ConfigParser object. We need it as a string.
#fakefile = StringIO.StringIO()
#result.write(fakefile)
# Set the headers
#cherrypy.response.headers['Content-Type'] = "application/x-download"
#cd = '%s; filename="%s"' % ("attachment", "labjack-%s-%s-conf.txt" % (devDict['productName'], devDict['serial']) )
#cherrypy.response.headers["Content-Disposition"] = cd
# Send the data
#return fakefile.getvalue()
@exposeRawFunction
def load(self, serial, filename):
configfileDir = os.path.join(current_dir,"configfiles/%s" % serial)
path = os.path.join(configfileDir, filename)
print "Regular path: ", path
dev = self.dm.getDevice(serial)
configfileDir = os.path.join(current_dir,"configfiles/%s" % dev.deviceName)
basicpath = os.path.join(configfileDir, filename)
print "Basic path: ", path
try:
try:
configFile = file(path, 'r')
except IOError:
configFile = file(basicpath, 'r')
# loadConfig takes a ConfigParser object, so we need to make one.
parserobj = ConfigParser.SafeConfigParser()
parserobj.readfp(configFile)
# Have to device load in the configuration.
devDict, result = self.dm.callDeviceFunction(serial, "loadconfig", [parserobj], {})
# Save current config as power-up default
devDict, result = self.dm.callDeviceFunction(serial, "setdefaults", [], {})
# Rebuild the FioList because settings could have changed.
self.dm.remakeFioList(serial)
m = "Configuration "%s" loaded." % replaceUnderscoresWithColons(filename)
except OSError:
m = "Couldn't find a file named "%s"." % replaceUnderscoresWithColons(filename)
return m
@exposeRawFunction
def remove(self, serial, filename):
logfileDir = os.path.join(current_dir,"configfiles/%s" % serial)
path = os.path.join(logfileDir, filename)
try:
os.remove(path)
m = "Configuration "%s" deleted." % replaceUnderscoresWithColons(filename)
except OSError:
m = "Couldn't find a file named "%s"." % replaceUnderscoresWithColons(filename)
#TODO: Do something else here. Maybe some sort of response for AJAX?
return m
class LoggingPage(object):
""" A class for handling all things /logs/
"""
def __init__(self, dm):
self.dm = dm
self.gd_client = None
self.parser = ConfigParser.SafeConfigParser()
def header(self):
return "<html><body>"
def footer(self):
return "</body></html>"
def loadSessionToken(self):
self.parser.read(CLOUDDOT_GROUNDED_CONF)
if self.parser.has_section("googledocs") and self.parser.has_option("googledocs", "session_token"):
token = self.parser.get("googledocs", "session_token")
print "token = %s" % token
else:
return None
if token and len(token) > 0:
return token
else:
return None
@exposeRawFunction
def savetoken(self, filename, *args, **kwargs):
singleUseToken = kwargs['token']
self.gd_client.SetAuthSubToken(singleUseToken)
self.gd_client.UpgradeToSessionToken()
sessionToken = self.gd_client.GetAuthSubToken()
self.parser.add_section("googledocs")
self.parser.set("googledocs", "session_token", str(sessionToken))
with open(CLOUDDOT_GROUNDED_CONF, 'wb') as configfile:
self.parser.write(configfile)
raise cherrypy.HTTPRedirect("/logs/upload/%s" % unquote(filename))
@exposeRawFunction
def upload(self, filename):
if self.gd_client is None:
self.gd_client = gdata.docs.service.DocsService()
token = self.loadSessionToken()
if not token:
next = '%s/logs/savetoken/%s' % (cherrypy.request.base, quote(filename))
print "next: ", next
auth_sub_url = gdata.service.GenerateAuthSubRequestUrl(next, GOOGLE_DOCS_SCOPE, secure=False, session=True)
raise cherrypy.HTTPRedirect(auth_sub_url)
else:
self.gd_client.SetAuthSubToken(token, scopes=[GOOGLE_DOCS_SCOPE])
googleDocName = replaceUnderscoresWithColons(filename)
csvPath = os.path.join(current_dir,"logfiles/%s" % filename)
csvStringIO = StringIO.StringIO(file(csvPath).read())
virtual_media_source = gdata.MediaSource(file_handle=csvStringIO, content_type='text/csv', content_length=len(csvStringIO.getvalue()))
db_entry = self.gd_client.Upload(virtual_media_source, googleDocName)
message = urlencode({ "message" : "File %s has been successfully uploaded to google docs." % replaceUnderscoresWithColons(filename)})
raise cherrypy.HTTPRedirect("/logs?%s" % message)
def getLogFiles(self):
activeFiles = {}
for thread in self.dm.loggingThreads.values():
activeFiles["%s" % thread.filename] = thread.serial
print "activeFiles:", activeFiles
l = []
logfilesDir = os.path.join(current_dir,"logfiles")
files = sorted(os.listdir(logfilesDir), reverse = True, key = lambda x: os.stat(os.path.join(logfilesDir,x)).st_ctime)
for filename in files:
newName = replaceUnderscoresWithColons(filename)
active = False
stopurl = ""
if filename in activeFiles:
print "%s is active" % filename
active = True
stopurl = "/logs/stop?serial=%s" % activeFiles[filename]
size = os.stat(os.path.join(logfilesDir,filename)).st_size
size = float(size)/1024
sizeStr = "%.2f KB" % size
uploadEnable = True
if size > 1024:
uploadEnable = False
aLog = dict(name = newName, url= "/logfiles/%s" % filename, uploadurl = "/logs/upload/%s" % filename, uploadEnable = uploadEnable, removeurl = "/logs/remove/%s" % filename, size = sizeStr, active = active, stopurl = stopurl)
l.append(aLog)
return l
@exposeRawFunction
def index(self, message = ""):
""" Handles /logs/
"""
# Tell people (firefox) not to cache this page.
cherrypy.response.headers['Cache-Control'] = "no-cache"
t = serve_file2("templates/index.tmpl")
tMainContent = serve_file2("templates/logfiles.tmpl")
tMainContent.logfiles = self.getLogFiles()
tMainContent.message = message
tMainContent.includeWrapper = True
t.mainContent = tMainContent.respond()
t.currentPage = "logs"
t.title = "Logs | LabJack CloudDot Grounded"
return t.respond()
@exposeRawFunction
def logFileList(self):
""" Just the list of files. Used for AJAX updating.
"""
cherrypy.response.headers['Cache-Control'] = "no-cache"
tFileList = serve_file2("templates/logfiles.tmpl")
tFileList.logfiles = self.getLogFiles()
tFileList.message = ""
tFileList.includeWrapper = False
return tFileList.respond()
@exposeRawFunction
def remove(self, filename):
logfileDir = os.path.join(current_dir,"logfiles")
path = os.path.join(logfileDir, filename)
try:
os.remove(path)
m = "File %s has been successfully deleted." % replaceUnderscoresWithColons(filename)
except OSError:
m = "Couldn't find a file named %s." % replaceUnderscoresWithColons(filename)
message = urlencode({ "message" : m})
raise cherrypy.HTTPRedirect("/logs?%s" % message)
@exposeRawFunction
def test(self):
cherrypy.response.headers['Cache-Control'] = "no-cache"
t = serve_file2("templates/testlog.tmpl")
devs = list()
for d in self.dm.devices.values():
devs.append({"name" : d.getName(), "serial" : d.serialNumber})
t.devices = devs
return t.respond()
@exposeRawFunction
def loggingSummary(self):
summaries = self.dm.makeLoggingSummary()
t = serve_file2("templates/logging-summaries.tmpl")
t.summaries = summaries
return t.respond()
@exposeRawFunction
def start(self, serial = None, headers = None):
if serial is None:
print "serial is null"
return False
else:
if headers:
headers = headers.split(",")
self.dm.startDeviceLogging(serial, headers = headers)