Example #1
0
def meminiSense(startDateTime,hostIP,BASE_PORT,streaming=True,logging=True):
	global meminifilename
	
	global agiType
	global agiIndx
	global agiStatus

	global server_address
	
	server_address = (hostIP, BASE_PORT)
	startTimeDT = rNTPTime.stripDateTime(startDateTime)

	#logging.basicConfig(level=logging.INFO)
	#file_path = "/media/card/memini/"
	#if file_path is None:
	#    print("DID NOT FIND PEBBLE_DATA_LOC")
	#    #print(os.environ)
	#    exit(1)
	#if not os.path.exists(file_path):
	#    os.mkdir(file_path) 
	#
	#filename = str(file_path) + "memini_data.csv"
	running = True
	#pebble = PebbleConnection(SerialTransport("/dev/rfcomm0"))
	#pebble.connect()
	
	while True:
		try:
			pebble = PebbleConnection(SerialTransport("/dev/rfcomm0"))
			pebble.connect()
			pebble.pump_reader()
			#print "connection try 0!"
			appUUID = APP_UUID[:]

			print "initializing Pebble connection.."
			# print pebble.send_and_read(AppRunState(data=AppRunStateRequest()), AppRunState).data.uuid

			while running:
				try:
					print "Attempt to Connect"
					#logging.info("Attempting to connect to pebble")
					pebble.run_async()
					print "Connection Success"
					#logging.info("Pebble connection success")
					break
				except libpebble2.exceptions.TimeoutError:
					print "Pebble timeouted, retrying.."
					continue

			while pebble.connected:
				try:
					print "Pebble Connected.."
					restart_app_on_watch(pebble,appUUID)
					with open(meminifilename,'a') as dataFile:
						dataFile.write("message,timestamp,button_id,click_type,battery_charge\n")
					
					# Register service for app messages
					print "initializing Services..."
					mainMsgService = AppMessageService(pebble)
					mainCommHandler = CommunicationKeeper(appUUID, mainMsgService)
					mainMsgService.register_handler("nack", mainCommHandler.nack_received)
					mainMsgService.register_handler("ack", mainCommHandler.ack_received)
					mainMsgHandler = AppMsgHandler(appUUID)
					mainMsgService.register_handler("appmessage", mainMsgHandler.message_received_event)

					break
				except Exception as err:
					print err
					print "Error Initializing Services, retrying.."
					pebble.run_async()
					continue

			while pebble.connected:
				# time.sleep(10) 
				is_app_on(pebble,appUUID)
				pebbleMessage = []
				pebbleMessage.append("checkNoti") #check for notiFlag from basestation
				pebbleMessage.append(str(BASE_PORT))
				temp = rNTPTime.checkNoti(hostIP, pebbleMessage, 5) 
				# if True: #for testing
				if int(rNTPTime.notiFlag) == 1:
					memini_main(mainMsgService,mainCommHandler,mainMsgHandler)
					# print("sending Message")
				else:
					#print "NO NOTIFICATION"
					time.sleep(NOTI_CHECK_DELAY)
		except SerialException:
			print("Pebble Disconnected!")
			time.sleep(30)
			continue

		except Exception as err:
			print err
			pass
Example #2
0

def get_id(sessions):
    for session in sessions:
        if session['log_tag'] == SESSION_TAG:
            infomsg = "FOUND ID " + str(session['session_id'])
            logging.info(infomsg)
            return session['session_id']
    return -1


logging.info("Starting pebble connection")

pebble = PebbleConnection(ST("/dev/rfcomm0"), log_packet_level=logging.DEBUG)
pebble.connect()
pebble.pump_reader()
try:
    while running:
        try:
            logging.info("Attempting to connect to pebble")
            pebble.run_async()
            logging.info("Pebble connection success")
            break
        except libpebble2.exceptions.TimeoutError:
            logging.info("Pebble timeouted, retrying..")
        continue

    while pebble.connected:

        data_srv = DataLoggingService(pebble, 5000)
        data_srv.set_send_enable(True)