Guest User

Untitled

a guest
Jun 29th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #
  3. # Author: Kyle Manna
  4. #
  5. import collections
  6. import csv
  7. import json
  8. import paramiko
  9. import sys
  10. import time
  11.  
  12. #
  13. # Class to interface to Trimble GM100 GPS Discipline PTP Grandmaster Clock
  14. #
  15. class GM100:
  16.  
  17. def __init__(self, host):
  18. self._host = host
  19. self._user = 'trimblesuper'
  20. self._pass = 'trimblesuper'
  21. pass
  22.  
  23. def connect(self):
  24. self._ssh = paramiko.SSHClient()
  25. self._ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
  26. self._ssh.connect(self._host, username=self._user, password=self._pass)
  27.  
  28. self._chan = self._ssh.invoke_shell()
  29.  
  30. def wait_for_prompt(self):
  31. resp = b''
  32. while not resp.endswith(b'> '):
  33. buf = self._chan.recv(1024)
  34. resp += buf
  35.  
  36. return resp
  37.  
  38. def get_gnss(self):
  39.  
  40. self._chan.send('view freq\n\r')
  41. resp = self.wait_for_prompt()
  42.  
  43. if b'No frequency status available at this time.' in resp:
  44. return None
  45.  
  46. # Discard first line (command sent) and last line (command prompt)
  47. resp = resp.split(b'\r\n')[1:-1]
  48.  
  49. # Split the 'Key: Value' byte array in to a stripped string list
  50. resp = [i.decode().strip().split(': ',1) for i in resp]
  51.  
  52. # Convert the list to a kv dictionary
  53. gnss = collections.OrderedDict([(kv[0], kv[1]) for kv in resp])
  54.  
  55. return gnss
  56.  
  57. def readline(self):
  58. resp = b''
  59. while not resp.endswith(b'\r\n'):
  60. # HACK cringe reading one byte at a time
  61. buf = self._chan.recv(1)
  62. resp += buf
  63.  
  64. return resp
  65.  
  66. def view_stream(self):
  67. self._chan.send('view stream\n\r')
  68. # Discard command echo'd back
  69. self.readline()
  70.  
  71. keys = self.readline().decode().strip().split(',')
  72.  
  73. while True:
  74. values = gm100.readline().decode().strip().split(',')
  75. yield collections.OrderedDict(zip(keys, values))
  76.  
  77. #
  78. # Test application dumps the frequency data to stdout in CSV format
  79. #
  80. if __name__ == '__main__':
  81.  
  82. gm100 = GM100(sys.argv[1])
  83.  
  84. gm100.connect()
  85. gm100.wait_for_prompt()
  86.  
  87. """
  88. sample = gm100.get_gnss()
  89. while not sample:
  90. sample = gm100.get_gnss()
  91.  
  92. fieldnames = sample.keys()
  93. writer = csv.DictWriter(sys.stdout, fieldnames=fieldnames)
  94. writer.writeheader()
  95.  
  96. while True:
  97. sample = gm100.get_gnss()
  98. #print(json.dumps(sample))
  99. writer.writerow(sample)
  100. time.sleep(10)
  101. """
  102.  
  103. writer = None
  104.  
  105. for i in gm100.view_stream():
  106. if not writer:
  107. writer = csv.DictWriter(sys.stdout, fieldnames=i.keys())
  108. writer.writeheader()
  109.  
  110. #print(json.dumps(i))
  111. writer.writerow(i)
Add Comment
Please, Sign In to add comment