Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.83 KB | None | 0 0
  1. from sense_hat import SenseHat
  2. import datetime
  3. import thread
  4. import time
  5.  
  6. import requests
  7.  
  8. def getserial():
  9. # Extract serial from cpuinfo file
  10. cpuserial = "0000000000000000"
  11. try:
  12. f = open('/proc/cpuinfo','r')
  13. for line in f:
  14. if line[0:6]=='Serial':
  15. cpuserial = line[10:26]
  16. f.close()
  17. except:
  18. cpuserial = "ERROR000000000"
  19.  
  20. return cpuserial
  21.  
  22.  
  23. def sendCrashRequest (url, user, pwd, headers, u_source, u_type, u_pressure, u_humidity, u_temperature_from_pressure, u_temperature_from_humidity, u_orientation_degree_pitch, u_orientation_degree_roll, u_orientation_degree_yaw, u_gyroscope_pitch, u_gyroscope_roll, u_gyroscope_yaw, u_orientation_radians_pitch, u_orientation_radians_roll, u_orientation_radians_yaw, u_accelerometer_yaw, u_accelerometer_raw_x, u_accelerometer_raw_y, u_accelerometer_raw_z, u_compass, u_compass_raw_x, u_compass_raw_y, u_compass_raw_z):
  24. print("crash request: " +str(datetime.datetime.now()))
  25. response = requests.post(url, auth=(user, pwd), headers=headers ,data="{\
  26. 'u_source': '"+str(u_source)+"',\
  27. 'u_type': '"+str(u_type)+"',\
  28. 'u_pressure': '"+str(u_pressure)+"',\
  29. 'u_humidity': '"+str(u_humidity)+"',\
  30. 'u_temperature_from_pressure': '"+str(u_temperature_from_pressure)+"',\
  31. 'u_temperature_from_humidity': '"+str(u_temperature_from_humidity)+"',\
  32. 'u_orientation_degree_pitch': '"+str(u_orientation_degree_pitch)+"',\
  33. 'u_orientation_degree_roll': '"+str(u_orientation_degree_roll)+"',\
  34. 'u_orientation_degree_yaw': '"+str(u_orientation_degree_yaw)+"',\
  35. 'u_gyroscope_pitch': '"+str(u_gyroscope_pitch)+"',\
  36. 'u_gyroscope_roll': '"+str(u_gyroscope_roll)+"',\
  37. 'u_gyroscope_yaw': '"+str(u_gyroscope_yaw)+"',\
  38. 'u_orientation_radians_pitch': '"+str(u_orientation_radians_pitch)+"',\
  39. 'u_orientation_radians_roll': '"+str(u_orientation_radians_roll)+"',\
  40. 'u_orientation_radians_yaw': '"+str(u_orientation_radians_yaw)+"',\
  41. 'u_accelerometer_roll': '"+str(u_accelerometer_roll)+"',\
  42. 'u_accelerometer_pitch': '"+str(u_accelerometer_pitch)+"',\
  43. 'u_accelerometer_yaw': '"+str(u_accelerometer_yaw)+"',\
  44. 'u_accelerometer_raw_x': '"+str(u_accelerometer_raw_x)+"',\
  45. 'u_accelerometer_raw_y': '"+str(u_accelerometer_raw_y)+"',\
  46. 'u_accelerometer_raw_z': '"+str(u_accelerometer_raw_z)+"',\
  47. 'u_compass_raw_x': '"+str(u_compass_raw_x)+"',\
  48. 'u_compass_raw_y': '"+str(u_compass_raw_y)+"',\
  49. 'u_compass_raw_z': '"+str(u_compass_raw_z)+"',\
  50. 'u_compass': '"+str(u_compass)+"'}")
  51.  
  52.  
  53. thread.exit()
  54.  
  55. def sendPeriodicRequest (url, user, pwd, headers, u_source, u_type, u_pressure, u_humidity, u_temperature_from_pressure, u_temperature_from_humidity, u_orientation_degree_pitch, u_orientation_degree_roll, u_orientation_degree_yaw, u_gyroscope_pitch, u_gyroscope_roll, u_gyroscope_yaw, u_orientation_radians_pitch, u_orientation_radians_roll, u_orientation_radians_yaw, u_accelerometer_yaw, u_accelerometer_raw_x, u_accelerometer_raw_y, u_accelerometer_raw_z, u_compass, u_compass_raw_x, u_compass_raw_y, u_compass_raw_z):
  56. while True:
  57. print("periodic request: " +str(datetime.datetime.now()))
  58. response = requests.post(url, auth=(user, pwd), headers=headers ,data="{\
  59. 'u_source': '"+str(u_source)+"',\
  60. 'u_type': '"+str(u_type)+"',\
  61. 'u_pressure': '"+str(u_pressure)+"',\
  62. 'u_humidity': '"+str(u_humidity)+"',\
  63. 'u_temperature_from_pressure': '"+str(u_temperature_from_pressure)+"',\
  64. 'u_temperature_from_humidity': '"+str(u_temperature_from_humidity)+"',\
  65. 'u_orientation_degree_pitch': '"+str(u_orientation_degree_pitch)+"',\
  66. 'u_orientation_degree_roll': '"+str(u_orientation_degree_roll)+"',\
  67. 'u_orientation_degree_yaw': '"+str(u_orientation_degree_yaw)+"',\
  68. 'u_gyroscope_pitch': '"+str(u_gyroscope_pitch)+"',\
  69. 'u_gyroscope_roll': '"+str(u_gyroscope_roll)+"',\
  70. 'u_gyroscope_yaw': '"+str(u_gyroscope_yaw)+"',\
  71. 'u_orientation_radians_pitch': '"+str(u_orientation_radians_pitch)+"',\
  72. 'u_orientation_radians_roll': '"+str(u_orientation_radians_roll)+"',\
  73. 'u_orientation_radians_yaw': '"+str(u_orientation_radians_yaw)+"',\
  74. 'u_accelerometer_roll': '"+str(u_accelerometer_roll)+"',\
  75. 'u_accelerometer_pitch': '"+str(u_accelerometer_pitch)+"',\
  76. 'u_accelerometer_yaw': '"+str(u_accelerometer_yaw)+"',\
  77. 'u_accelerometer_raw_x': '"+str(u_accelerometer_raw_x)+"',\
  78. 'u_accelerometer_raw_y': '"+str(u_accelerometer_raw_y)+"',\
  79. 'u_accelerometer_raw_z': '"+str(u_accelerometer_raw_z)+"',\
  80. 'u_compass_raw_x': '"+str(u_compass_raw_x)+"',\
  81. 'u_compass_raw_y': '"+str(u_compass_raw_y)+"',\
  82. 'u_compass_raw_z': '"+str(u_compass_raw_z)+"',\
  83. 'u_compass': '"+str(u_compass)+"'}")
  84.  
  85.  
  86.  
  87.  
  88. # Set the request parameters
  89. url = 'https://dev20467.service-now.com/api/now/table/u_cardata'
  90.  
  91. # Eg. User name="admin", Password="admin" for this code sample.
  92. user = 'admin'
  93. pwd = '1234'
  94.  
  95. # Set proper headers
  96. headers = {"Content-Type":"application/json","Accept":"application/json"}
  97.  
  98. sense = SenseHat()
  99.  
  100. accident = False
  101.  
  102. # Initialize Variables
  103. u_source = getserial();
  104. u_type = "";
  105.  
  106. u_pressure = 0
  107. u_humidity = 0
  108.  
  109. u_temperature_from_pressure = 0
  110. u_temperature_from_humidity = 0
  111.  
  112. u_orientation_degree_pitch = 0
  113. u_orientation_degree_roll = 0
  114. u_orientation_degree_yaw = 0
  115.  
  116. u_gyroscope_pitch = 0
  117. u_gyroscope_roll = 0
  118. u_gyroscope_yaw = 0
  119.  
  120. u_orientation_radians_pitch = 0
  121. u_orientation_radians_roll = 0
  122. u_orientation_radians_yaw = 0
  123.  
  124. u_accelerometer_pitch = 0
  125. u_accelerometer_roll = 0
  126. u_accelerometer_yaw = 0
  127.  
  128. u_compass = 0
  129. u_compass_raw_x = 0
  130. u_compass_raw_y = 0
  131. u_compass_raw_z = 0
  132.  
  133.  
  134. #Send every 5 seconds a Request
  135. thread.start_new_thread(sendPeriodicRequest, (url, user, pwd, headers, u_source, 'periodic', u_pressure, u_humidity, u_temperature_from_pressure, u_temperature_from_humidity, u_orientation_degree_pitch, u_orientation_degree_roll, u_orientation_degree_yaw, u_gyroscope_pitch, u_gyroscope_roll, u_gyroscope_yaw, u_orientation_radians_pitch, u_orientation_radians_roll, u_orientation_radians_yaw, u_accelerometer_yaw, u_accelerometer_raw_x, u_accelerometer_raw_y, u_accelerometer_raw_z, u_compass, u_compass_raw_x, u_compass_raw_y, u_compass_raw_z));
  136.  
  137. while True:
  138.  
  139. u_pressure = round(sense.get_pressure(),3)
  140. u_humidity = round(sense.get_humidity(),3)
  141.  
  142. u_temperature_from_pressure = round(sense.get_temperature_from_pressure(),3)
  143. u_temperature_from_humidity = round(sense.get_temperature_from_humidity(),3)
  144.  
  145. u_orientation_degree = sense.get_orientation_degrees(),3)
  146. u_orientation_degree_pitch = round(u_orientation_degree.get('pitch'),3)
  147. u_orientation_degree_roll = round(u_orientation_degree.get('roll'),3)
  148. u_orientation_degree_yaw = round(u_orientation_degree.get('yaw'),3)
  149.  
  150. u_gyroscope = sense.get_gyroscope()
  151. u_gyroscope_pitch = round(u_gyroscope.get('pitch'),3)
  152. u_gyroscope_roll = round(u_gyroscope.get('roll'),3)
  153. u_gyroscope_yaw = round(u_gyroscope.get('yaw'),3)
  154.  
  155. u_orientation_radians = sense.get_orientation_radians()
  156. u_orientation_radians_pitch = round(u_orientation_radians.get('pitch'),3)
  157. u_orientation_radians_roll = round(u_orientation_radians.get('roll'),3)
  158. u_orientation_radians_yaw = round(u_orientation_radians.get('yaw'),3)
  159.  
  160. u_accelerometer = sense.get_accelerometer()
  161. u_accelerometer_pitch = round(u_accelerometer.get('pitch'),3)
  162. u_accelerometer_roll = round(u_accelerometer.get('roll'),3)
  163. u_accelerometer_yaw = round(u_accelerometer.get('yaw'),3)
  164.  
  165. u_accelerometer_raw = sense.get_accelerometer_raw()
  166. u_accelerometer_raw_x = round(u_accelerometer_raw.get('x'),3)
  167. u_accelerometer_raw_y = round(u_accelerometer_raw.get('y'),3)
  168. u_accelerometer_raw_z = round(u_accelerometer_raw.get('z'),3)
  169.  
  170. u_compass = round(sense.get_compass(),3)
  171.  
  172. u_compass_raw = sense.get_compass_raw()
  173. u_compass_raw_x = round(u_compass.get('x'),3)
  174. u_compass_raw_y = round(u_compass.get('y'),3)
  175. u_compass_raw_z = round(u_compass.get('z'),3)
  176.  
  177.  
  178. # Accident - On Left Side
  179. if u_orientation_degree_roll > 70 and u_orientation_degree_roll < 110:
  180. accident = True
  181. u_type = "Crash - On Left Side";
  182.  
  183. # Accident - On Right Side
  184. if u_orientation_degree_roll > 250 and u_orientation_degree_roll < 290:
  185. accident = True
  186. u_type = "Crash - On Right Side";
  187.  
  188. # Accident - On Roof
  189. if u_orientation_degree_roll >= 110 and u_orientation_degree_roll <= 250:
  190. accident = True
  191. u_type = "Crash - On Roof";
  192.  
  193. if accident:
  194.  
  195. #Need to install requests package for python
  196. #easy_install requests
  197.  
  198. # Do the HTTP request
  199. thread.start_new_thread(sendCrashRequest, (url, user, pwd, headers, u_source, 'crash', u_pressure, u_humidity, u_temperature_from_pressure, u_temperature_from_humidity, u_orientation_degree_pitch, u_orientation_degree_roll, u_orientation_degree_yaw, u_gyroscope_pitch, u_gyroscope_roll, u_gyroscope_yaw, u_orientation_radians_pitch, u_orientation_radians_roll, u_orientation_radians_yaw, u_accelerometer_yaw, u_accelerometer_raw_x, u_accelerometer_raw_y, u_accelerometer_raw_z, u_compass, u_compass_raw_x, u_compass_raw_y, u_compass_raw_z));
  200.  
  201.  
  202. u_orientation_degree_roll = 0
  203. accident = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement