def fill_gen_add_payment(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() cursor.execute( "select ReservationID, ToPay, ReservationDate from Reservations") row = cursor.fetchone() ratio = [i / 10 for i in range(1, 11)] c = Connector() cn = c.connect(secretpassword) while row: reservationid = row[0] topay = row[1] date = row[2] money_deposited = math.floor(int(topay) * random.choice(ratio)) (y, m, d) = date.split("-") begin = dt.date(int(y), int(m), int(d)).toordinal() book_ord = dt.datetime.fromordinal(begin + random.randint(1, 7)) date_of_payment = "/".join( (str(book_ord.year), str(book_ord.month), str(book_ord.day))) result = (reservationid, money_deposited, date_of_payment) print("Add Payment " + str(result)) c.apply_proc('GeneratorAddPayment', result) row = cursor.fetchone() conn.close() cn.close()
def fill_gen_book_places_for_workshop(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() cursor.execute("select Reservations.ReservationID, WorkshopID, MaxSpots " "from DaysOfConf " "inner join Workshops on Workshops.DayID = DaysOfConf.DayID " "inner join Reservations on Reservations.DayID = DaysOfConf.DayID") row = cursor.fetchone() c = Connector() cn = c.connect(secretpassword) while row: reservationid = row[0] workshopid = row[1] maxspots = math.floor(row[2] / 3) result = (reservationid, workshopid, maxspots) print("Add Workshop Reservation " + str(result)) c.apply_proc('BookPlacesForWorkshop', result) row = cursor.fetchone() conn.close() cn.close()
def fill_gen_book_places_for_workshop(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() cursor.execute( "select Reservations.ReservationID, WorkshopID, MaxSpots " "from DaysOfConf " "inner join Workshops on Workshops.DayID = DaysOfConf.DayID " "inner join Reservations on Reservations.DayID = DaysOfConf.DayID") row = cursor.fetchone() c = Connector() cn = c.connect(secretpassword) while row: reservationid = row[0] workshopid = row[1] maxspots = math.floor(row[2] / 3) result = (reservationid, workshopid, maxspots) print("Add Workshop Reservation " + str(result)) c.apply_proc('BookPlacesForWorkshop', result) row = cursor.fetchone() conn.close() cn.close()
def fill_gen_add_payment(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() cursor.execute("select ReservationID, ToPay, ReservationDate from Reservations") row = cursor.fetchone() ratio = [i / 10 for i in range(1, 11)] c = Connector() cn = c.connect(secretpassword) while row: reservationid = row[0] topay = row[1] date = row[2] money_deposited = math.floor(int(topay) * random.choice(ratio)) (y, m, d) = date.split("-") begin = dt.date(int(y), int(m), int(d)).toordinal() book_ord = dt.datetime.fromordinal(begin + random.randint(1, 7)) date_of_payment = "/".join((str(book_ord.year), str(book_ord.month), str(book_ord.day))) result = (reservationid, money_deposited, date_of_payment) print("Add Payment " + str(result)) c.apply_proc('GeneratorAddPayment', result) row = cursor.fetchone() conn.close() cn.close()
def fill_gen_add_attendees(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() cursor.execute( "select company.clientid, spotsreserved from company " "inner join clients on clients.clientid = company.clientid " "inner join reservations on reservations.clientid = clients.clientid") row = cursor.fetchone() c = Connector() cn = c.connect(secretpassword) while row: clientid = row[0] spots_reserved = row[1] for reservation in range(spots_reserved): name, surname = getNameSurname() result = (clientid, name, surname) print("Add Attendee " + str(result)) c.apply_proc('AddAttendee', result) row = cursor.fetchone() conn.close() cn.close()
def fill_gen_add_attendees(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() cursor.execute("select company.clientid, spotsreserved from company " "inner join clients on clients.clientid = company.clientid " "inner join reservations on reservations.clientid = clients.clientid") row = cursor.fetchone() c = Connector() cn = c.connect(secretpassword) while row: clientid = row[0] spots_reserved = row[1] for reservation in range(spots_reserved): name, surname = getNameSurname() result = (clientid, name, surname) print("Add Attendee " + str(result)) c.apply_proc('AddAttendee', result) row = cursor.fetchone() conn.close() cn.close()
def mode(self, mode = 'hour', color = [0, 0, 0]): """ Update Dotti to the given mode Args: mode (str): either 'hour' or 'color' color (arr[int, int, int]): R,G,B color to use """ conn = Connector(self.mac) conn.connect() if not conn.isconnected: conn.connect() if not conn.isconnected: return try: if mode == 'color': conn.writeCharacteristic('0x2a', '0601'+self.__twoDigitHex(int(color[0]))+self.__twoDigitHex(int(color[1]))+self.__twoDigitHex(int(color[2]))+'00') elif mode == 'hour': conn.writeCharacteristic('0x2a', '040502') except Exception as error: logger.error(str(error)) conn.disconnect() return
def db_template(sql): try: conn = Connector() conn.connect() cur = conn.cursor() cur.execute(sql) conn.commit() except Exception as e: print('load sql') print('e\t' + str(e)) finally: conn.close()
def create_saas_db(): conn = Connector() conn.connect() conn.execute(Table_sql) conn.execute(Field_sql) conn.execute(Data_sql) conn.execute(Index_sql) conn.close() print("创建universal table成功!")
def db_template_return(sql): rows = [] try: conn = Connector() conn.connect() cur = conn.cursor() cur.execute(sql) rows = cur.fetchall() conn.commit() except Exception as e: print('load sql') print('e\t' + str(e)) finally: conn.close() return rows
def fill_gen_assign_workshop_attendee(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() workshop_attendees = [] cursor.execute( "select distinct Attendees.AttendeeID, WorkshopReservations.WReservationID " "from Attendees " "inner join Participation on Participation.AttendeeID = Attendees.AttendeeID " "inner join Reservations on Reservations.ReservationID = Participation.ReservationID " "inner join WorkshopReservations on WorkshopReservations.ReservationID = Reservations.ReservationID" ) row = cursor.fetchone() while row: workshop_attendees.append((row[0], row[1])) row = cursor.fetchone() filler = Connector() workshop_attendees_size = len(workshop_attendees) filler.apply_proc_multi('AssignParticipantToWorkshop', workshop_attendees, workshop_attendees_size) conn.close()
def post(self): # Region Set Up #create resultService object _resultsService = ResultsService() #load queries q = Queries() # create connection to postgres database _connector = Connector() conn = _connector.connect(1) # create cursor cur = conn.cursor() #define query parameters _failID = self.get_argument("_failureId") # endregion # Region main block cur.execute(q._email_failure, (_failID, )) _result = cur.fetchall() _result_to_json = _resultsService.parse(_result) self.render("emailMonitor.html", results=_result) cur.close() conn.close
def post(self): # Region Set Up _resultsService = ResultsService() _q = Queries() _connector = Connector() conn = _connector.connect(2) cur = conn.cursor() # endregion # Region Main Block cur.execute(_q._get_all_users) _result = cur.fetchall() _result_to_json = _resultsService.parse(_result) for item in _result: _rID = str(item[3]) _id = self.get_arguments("_checkbox") if _rID in _id: print("Deleting " + str(item[0])) cur.execute(_q._delete_user, (item[3], )) conn.commit() cur.close() conn.close() self.redirect("/users")
def post(self): # Region Set Up #create resultService object _resultsService = ResultsService() #load queries q = Queries() # create connection to postgres database _connector = Connector() conn = _connector.connect(2) # create cursor cur = conn.cursor() #define query parameters _firstname = self.get_argument("_firstname_field") _lastname = self.get_argument("_lastname_field") _timenow = datetime.datetime.now() # endregion # Region main block _query = q._get_all_users cur.execute(_query) _result = cur.fetchall() _result_to_json = _resultsService.parse(_result) _query = q._create_new_user cur.execute(_query, (_firstname, _lastname, _timenow)) conn.commit() cur.close() conn.close self.redirect("/users")
def fill_gen_assign_workshop_attendee(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() workshop_attendees = [] cursor.execute("select distinct Attendees.AttendeeID, WorkshopReservations.WReservationID " "from Attendees " "inner join Participation on Participation.AttendeeID = Attendees.AttendeeID " "inner join Reservations on Reservations.ReservationID = Participation.ReservationID " "inner join WorkshopReservations on WorkshopReservations.ReservationID = Reservations.ReservationID") row = cursor.fetchone() while row: workshop_attendees.append((row[0], row[1])) row = cursor.fetchone() filler = Connector() workshop_attendees_size = len(workshop_attendees) filler.apply_proc_multi('AssignParticipantToWorkshop', workshop_attendees, workshop_attendees_size) conn.close()
def fill_gen_book_places_for_day(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() clients = [] clients_getter = conn.cursor() clients_getter.execute("select clientid from clients") clients_ids = clients_getter.fetchone() while clients_ids: clients.append(clients_ids[0]) clients_ids = clients_getter.fetchone() cursor.execute( "select DayId, conferences.DateFrom, Spots " "from DaysOfConf " "inner join Conferences on conferences.ConferenceId = daysofconf.ConferenceId" ) row = cursor.fetchone() c = Connector() cn = c.connect(secretpassword) while row: dayid = row[0] date = row[1] spots_avail = row[2] (y, m, d) = date.split("-") begin = dt.date(int(y), int(m), int(d)).toordinal() for reservation in range(3): client = random.choice(clients) spots = random.randint(1, int(spots_avail / 4)) book_ord = dt.datetime.fromordinal(begin - random.randint(15, 80)) date_of_book = "/".join( (str(book_ord.year), str(book_ord.month), str(book_ord.day))) result = (dayid, client, spots, date_of_book) print("Book spots for the day " + str(result)) c.apply_proc('GeneratorBookPlacesForDay', result) row = cursor.fetchone() conn.close() cn.close()
def fill_gen_assign_attendee(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() individual_participation = [] cursor.execute( "select distinct Attendees.AttendeeID, Reservations.ReservationID " "from Attendees " "inner join Clients on Attendees.ClientID = Clients.ClientID " "inner join Reservations on Reservations.ClientID = Clients.ClientID " "inner join Individual on Individual.ClientID = Clients.ClientID") row = cursor.fetchone() while row: individual_participation.append((row[0], row[1])) row = cursor.fetchone() company_participation = [] cursor.execute( "select distinct Attendees.AttendeeID, Reservations.ReservationID " "from Attendees " "inner join Clients on Attendees.ClientID = Clients.ClientID " "inner join Reservations on Reservations.ClientID = Clients.ClientID " "inner join Company on Company.ClientID = Clients.ClientID") row = cursor.fetchone() while row: company_participation.append((row[0], row[1])) row = cursor.fetchone() # Two list of (AttendeeID, ReservationID) to apply # The following lines may cause declination of IDE control xD # print(individual_participation) # print(len(individual_participation)) # print(company_participation) # print(len(company_participation)) # Those insertions causes error -> try block filler = Connector() individual_size = len(individual_participation) filler.apply_proc_multi('AssignAttendee', individual_participation, individual_size) company_size = len(company_participation) filler.apply_proc_multi('AssignAttendee', company_participation, company_size) conn.close()
def fill_gen_book_places_for_day(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() clients = [] clients_getter = conn.cursor() clients_getter.execute("select clientid from clients") clients_ids = clients_getter.fetchone() while clients_ids: clients.append(clients_ids[0]) clients_ids = clients_getter.fetchone() cursor.execute("select DayId, conferences.DateFrom, Spots " "from DaysOfConf " "inner join Conferences on conferences.ConferenceId = daysofconf.ConferenceId") row = cursor.fetchone() c = Connector() cn = c.connect(secretpassword) while row: dayid = row[0] date = row[1] spots_avail = row[2] (y, m, d) = date.split("-") begin = dt.date(int(y), int(m), int(d)).toordinal() for reservation in range(3): client = random.choice(clients) spots = random.randint(1, int(spots_avail/4)) book_ord = dt.datetime.fromordinal(begin - random.randint(15, 80)) date_of_book = "/".join((str(book_ord.year), str(book_ord.month), str(book_ord.day))) result = (dayid, client, spots, date_of_book) print("Book spots for the day " + str(result)) c.apply_proc('GeneratorBookPlacesForDay', result) row = cursor.fetchone() conn.close() cn.close()
def fill_gen_add_thresholds(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() cursor.execute("select * from conferences") row = cursor.fetchone() c = Connector() cn = c.connect(secretpassword) while row: idconf = row[0] dayofconf = row[2] for threshold in range(3): res = gen_add_thresholds(idconf, threshold, dayofconf) c.apply_proc('AddPrice', res) print(res) row = cursor.fetchone() conn.close() cn.close()
def get(self): # create connection to postgres database _connector = Connector() conn = _connector.connect(2) # create cursor cur = conn.cursor() cur.execute( "select firstname, lastname, created_on::VARCHAR(255) from public.users LIMIT(100)" ) _result = cur.fetchall() self.render("testInsert.html", result=_result)
def fill_gen_add_workshop(): connector = Connector() conn = connector.connect(secretpassword) workshop_types_id = [] workshop_getter = conn.cursor() workshop_getter.execute("select WorkshopTypeID from WorkshopType") types_row = workshop_getter.fetchone() while types_row: workshop_types_id.append(types_row[0]) types_row = workshop_getter.fetchone() days_id = [] days_getter = conn.cursor() days_getter.execute("select DayID from DaysOfConf") days_row = days_getter.fetchone() while days_row: days_id.append(days_row[0]) days_row = days_getter.fetchone() conn.close() print(workshop_types_id) print(days_id) c = Connector() cn = c.connect(secretpassword) for day in days_id: workshop = random.choice(workshop_types_id) start = random.choice(["14:00:00", "15:00:00", "16:00:00"]) end = random.choice(["18:00:00", "19:00:00", "20:00:00"]) spots = random.choice([5, 10, 15, 20]) price = random.randint(2, 15) * 10 result = (workshop, day, start, end, spots, price) print("Add Workshop " + str(result)) c.apply_proc('AddWorkshop', result) cn.close()
def fill_gen_assign_attendee(): connector = Connector() conn = connector.connect(secretpassword) cursor = conn.cursor() individual_participation = [] cursor.execute("select distinct Attendees.AttendeeID, Reservations.ReservationID " "from Attendees " "inner join Clients on Attendees.ClientID = Clients.ClientID " "inner join Reservations on Reservations.ClientID = Clients.ClientID " "inner join Individual on Individual.ClientID = Clients.ClientID") row = cursor.fetchone() while row: individual_participation.append((row[0], row[1])) row = cursor.fetchone() company_participation = [] cursor.execute("select distinct Attendees.AttendeeID, Reservations.ReservationID " "from Attendees " "inner join Clients on Attendees.ClientID = Clients.ClientID " "inner join Reservations on Reservations.ClientID = Clients.ClientID " "inner join Company on Company.ClientID = Clients.ClientID") row = cursor.fetchone() while row: company_participation.append((row[0], row[1])) row = cursor.fetchone() # Two list of (AttendeeID, ReservationID) to apply # The following lines may cause declination of IDE control xD # print(individual_participation) # print(len(individual_participation)) # print(company_participation) # print(len(company_participation)) # Those insertions causes error -> try block filler = Connector() individual_size = len(individual_participation) filler.apply_proc_multi('AssignAttendee', individual_participation, individual_size) company_size = len(company_participation) filler.apply_proc_multi('AssignAttendee', company_participation, company_size) conn.close()
def get(self): _q = Queries() _resultsService = ResultsService() # create connection to postgres database _connector = Connector() conn = _connector.connect(2) # create cursor cur = conn.cursor() cur.execute(_q._get_all_ship_classes) _result = cur.fetchall() _result_to_json = _resultsService.parse(_result) self.render("shipClass.html", result=_result)
def get(self): # Region Set Up #create resultService object _resultsService = ResultsService() q = Queries() # create connection to postgres database _connector = Connector() conn = _connector.connect(2) # create cursor cur = conn.cursor() # endregion # Region Main Block cur.execute(q._get_all_users) _result = cur.fetchall() _result_to_json = _resultsService.parse(_result) self.render("testInsert.html", result=_result_to_json) cur.close() conn.close()
#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright 2011 Peter Morton & Matthew Yeung # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from PyQt4 import QtGui import numpy as np import pygame import typewriter from setupeventfilters import setupEventFilters from onfocusmanager import onFocusManager from glviewer import Viewer from connector import Connector # Play with these constants to change the system response SAMPLE_STRIDE = 2 # Divide depth map resolution by this amount KB_WIDTH_FAC = 1 # Width of keyboard = Length * KB_WIDTH_FAC KB_HEIGHT_FAC = 1 # Height of keyboard = Length * KB_HEIGHT_FAC
from flask import Flask, jsonify from flask_restful import Resource, Api, reqparse from connector import Connector app = Flask(__name__) api = Api(app) CONN = Connector() CONN.connect() class HospitalAPI(Resource): def get(self): parser = reqparse.RequestParser() parser.add_argument('state') args = parser.parse_args() if args['state']: return jsonify(CONN.get_hospital()) return jsonify(CONN.get_hospital_points(args['state'])) api.add_resource(HospitalAPI, '/hospitals', endpoint='hospitals') if __name__ == '__main__': app.run(debug=True)
#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright 2011 Peter Morton & Matthew Yeung # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from PyQt4 import QtGui import numpy as np import pygame import typewriter from setupeventfilters import setupEventFilters from onfocusmanager import onFocusManager from glviewer import Viewer from connector import Connector # Play with these constants to change the system response SAMPLE_STRIDE = 2 # Divide depth map resolution by this amount KB_WIDTH_FAC = 1 # Width of keyboard = Length * KB_WIDTH_FAC KB_HEIGHT_FAC = 1 # Height of keyboard = Length * KB_HEIGHT_FAC KB_GAP_FAC = 0.01 # Gap between keys = KB Length * KB_GAP_FAC KB_NUM_KEYS = 4 # Only dealing with white keys for now KB_START_KEY = 0 # 0 = C2, 1 = D2, etc... (whites only)
ovpn_path = results.ovpn_path config_path = results.config_path outfile = results.outfile date = results.date timeout = results.timeout try: is_admin = os.getuid() == 0 except AttributeError: is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0 if not is_admin: raise Exception('Please run as root/admin, since OpenVPN needs admin privileges') if os.path.isfile('output/' + outfile): # Check if file is existent, if not, remove dates and proceed combos = 'output/' + outfile else: sv = Sieve(date=date, outfile=outfile) sv.filter() combos = sv.write() if config_path is None: for filename in os.listdir('ovpn/'): if filename.endswith('.ovpn'): config_path = 'ovpn/' + filename break cn = Connector(ovpn_path=ovpn_path, config_path=config_path, combos=combos, timeout=timeout) cn.unpack() cn.connect()
from connector import Connector from mysqlConn import MySqlConnector from redisConn import RedisConnector async_mode = None app = Flask(__name__) # redisConnector = RedisConnector() # connector = Connector(redisConnector) # connector.connect(app) mySqlConnector = MySqlConnector() connector = Connector(mySqlConnector) connector.connect(app) #app.secret_key = 'vuongnq4' #SocketIO socketio = SocketIO(app, async_mode=async_mode) thread = None def background_thread(): count = 0 while True: socketio.sleep(10) count += 1 @app.route('/', methods=['GET', 'POST']) def login():
class Client: def __init__(self): self.connector = Connector() def connect(self, host='127.0.0.1', port=11111): self.connector.connect(host, port) data = self.connector.read_next_packet() packet = CanalProtocol_pb2.Packet() packet.MergeFromString(data) if packet.type != CanalProtocol_pb2.PacketType.HANDSHAKE: raise Exception('connect error') print('connected to %s:%s' % (host, port)) def disconnect(self): self.connector.disconnect() def check_valid(self, username=b'', password=b''): client_auth = CanalProtocol_pb2.ClientAuth() client_auth.username = username client_auth.password = password packet = CanalProtocol_pb2.Packet() packet.type = CanalProtocol_pb2.PacketType.CLIENTAUTHENTICATION packet.body = client_auth.SerializeToString() self.connector.write_with_header(packet.SerializeToString()) data = self.connector.read_next_packet() packet = CanalProtocol_pb2.Packet() packet.MergeFromString(data) if packet.type != CanalProtocol_pb2.PacketType.ACK: raise Exception('Auth error') ack = CanalProtocol_pb2.Ack() ack.MergeFromString(packet.body) if ack.error_code > 0: raise Exception('something goes wrong when doing authentication. error code:%s, error message:%s' % ( ack.error_code, ack.error_message)) print('Auth succed') def subscribe(self, client_id=b'1001', destination=b'example', filter=b'.*\\..*'): self.client_id = client_id self.destination = destination self.rollback(0) sub = CanalProtocol_pb2.Sub() sub.destination = destination sub.client_id = client_id sub.filter = filter packet = CanalProtocol_pb2.Packet() packet.type = CanalProtocol_pb2.PacketType.SUBSCRIPTION packet.body = sub.SerializeToString() self.connector.write_with_header(packet.SerializeToString()) data = self.connector.read_next_packet() packet = CanalProtocol_pb2.Packet() packet.MergeFromString(data) if packet.type != CanalProtocol_pb2.PacketType.ACK: raise Exception('Subscribe error.') ack = CanalProtocol_pb2.Ack() ack.MergeFromString(packet.body) if ack.error_code > 0: raise Exception( 'Failed to subscribe. error code:%s, error message:%s' % (ack.error_code, ack.error_message)) print('Subscribe succed') def unsubscribe(self): pass def get(self, size=100): message = self.get_without_ack(size) self.ack(message['id']) return message def get_without_ack(self, batch_size=10, timeout=-1, unit=-1): get = CanalProtocol_pb2.Get() get.client_id = self.client_id get.destination = self.destination get.auto_ack = False get.fetch_size = batch_size get.timeout = timeout get.unit = unit packet = CanalProtocol_pb2.Packet() packet.type = CanalProtocol_pb2.PacketType.GET packet.body = get.SerializeToString() self.connector.write_with_header(packet.SerializeToString()) data = self.connector.read_next_packet() packet = CanalProtocol_pb2.Packet() packet.MergeFromString(data) message = dict(id=0, entries=[]) if packet.type == CanalProtocol_pb2.PacketType.MESSAGES: messages = CanalProtocol_pb2.Messages() messages.MergeFromString(packet.body) if messages.batch_id > 0: message['id'] = messages.batch_id for item in messages.messages: entry = EntryProtocol_pb2.Entry() entry.MergeFromString(item) message['entries'].append(entry) elif packet.type == CanalProtocol_pb2.PacketType.ACK: ack = CanalProtocol_pb2.PacketType.Ack() ack.MergeFromString(packet.body) if ack.error_code > 0: raise Exception('get data error. error code:%s, error message:%s' % (ack.error_code, ack.error_message)) else: raise Exception('unexpected packet type:%s' % (packet.type)) return message def ack(self, message_id): if message_id: clientack = CanalProtocol_pb2.ClientAck() clientack.destination = self.destination clientack.client_id = self.client_id clientack.batch_id = message_id packet = CanalProtocol_pb2.Packet() packet.type = CanalProtocol_pb2.PacketType.CLIENTACK packet.body = clientack.SerializeToString() self.connector.write_with_header(packet.SerializeToString()) def rollback(self, batch_id): cb = CanalProtocol_pb2.ClientRollback() cb.batch_id = batch_id cb.client_id = self.client_id cb.destination = self.destination packet = CanalProtocol_pb2.Packet() packet.type = CanalProtocol_pb2.PacketType.CLIENTROLLBACK packet.body = cb.SerializeToString() self.connector.write_with_header(packet.SerializeToString())
class Modem(object): """Class modem to control the node via TCP""" class Status: """Internal class where the status sens_ts are defined""" IDLE, BUSY2REQ, BUSY2DATA, BUSY2RECV, BUSY2REQ2DATA, KILL = range(6) class ErrorDict: """Internal class to map the error sens_ts to their error message""" NONE, SYNT_ERR, WRONG_SETTING, NOT_RESPONDING, FILE_NOT_FOUND, \ TX_WHILE_RX, RX_WHILE_TX = range(7) error_dict = { NONE : 'none', SYNT_ERR : 'command syntax error', WRONG_SETTING : 'wrong setting, value not allowed', NOT_RESPONDING : 'device not responding, check the status', FILE_NOT_FOUND : 'file not found error', TX_WHILE_RX : 'you attempt to transmit while receiving, if \ you really want, set the force flag to 1', RX_WHILE_TX : 'you attempt to receive while transmitting if \ you really want set the force flag to 1' } def __init__(self, ip, port, automatic = True, control_buf_size = 32, data_buf_size = 128, \ m_to = 0.01, socket_to = 0.005): """ Constructor, initialize the modem and the connector. Connect the modem to the submerged node @param self pointer to the class object @param ip string cointaining the IP address of the TCP server @param port string with the port of the TCP server socket @param control_buf_size: int with the control buffer size, in bytes @param data_buf_size: int with the data buffer size, in bytes @param m_to: float value time out of the cycle, in [s] @param socket_to: time out of the socket checking operation, [s] """ self.conn = Connector(ip, port, control_buf_size, data_buf_size, socket_to) self.conn.connect() self.m_to = m_to self.status = Modem.Status.IDLE self.node_status = 0 self.automatic = automatic self.interpreter = Interpreter() self.mainPID = os.getpid() self.error_status = Modem.ErrorDict.NONE self.commands_queue = "".split(Interpreter.END_COMMAND) if automatic: thread.start_new_thread(self.run,()) def run(self): """ Run cycle, checks if data available @param self pointer to the class object """ threadPID = os.getpid() index = 0 while True: index += 1 if ((index * self.m_to) > 1): #self.check4kill(threadPID) index = 1 if(self.status == Modem.Status.IDLE or self.status == Modem.Status.BUSY2REQ): r, e = self.conn.dataAvailable() if(e): break if(r): rx = self.recvCommand() if (len(rx) == 0): break elif(self.status == Modem.Status.KILL): break sleep(self.m_to) self.close() print >>sys.stderr, 'Closing' def check4kill(self,threadPID = -1): """ Check if the process has to be killed @param self pointer to the class object """ #TODO: check in the kill log if my main or my thred PID are there. # In case True, kill all. /var/log/check_status/check_kills.log # kill $TOPPID # /var/log/check_status/check_off.log off_log = "/var/log/check_status/check_off.log" kill_log = "/var/log/check_status/check_kills.log" try: f = open (off_log, "r") l = f.read(self.conn.data_buf_size) while (l or self.status != Modem.Status.KILL): if l == "poweroff": self.status = Modem.Status.KILL l = f.read(self.conn.data_buf_size) f.close() except IOError: print off_log + " not found" try: f = open (kill_log, "r") l = f.read(self.conn.data_buf_size) while (l or self.status != Modem.Status.KILL): if (l == ("kill " + str(threadPID)) or \ l == ("kill " + str(self.mainPID))): self.status = Modem.Status.KILL l = f.read(self.conn.data_buf_size) f.close() except IOError: print kill_log + " not found" def kill(self): """ Status kill @param self pointer to the class object """ self.status = Modem.Status.KILL def close(self): """ Close the connection from the modem to the submerged node @param self pointer to the class object """ self.conn.close() def recvData(self): """ Receive the data as they are from the node, it is a blocking function. To check if data has to be received, query the self.conn.dataAvailable() function @param self pointer to the class object @return the incoming string from the TCP connection """ prec_state = self.status self.status = Modem.Status.BUSY2RECV data = self.conn.recvData() self.status = prec_state self.checkConnection(data) return data def recvCommand(self): """ Receive and process a command from the node @param self pointer to the class object @return the incoming string from the TCP connection """ prec_state = self.status self.status = Modem.Status.BUSY2RECV command = self.conn.recvCommand() self.status = prec_state self.checkConnection(command) # self.parseCommand(command.rstrip('\n')) self.parseDivCommands(command) return command def send(self, msg): """ Send a message to the submerged node @param self pointer to the class object @param msg message that has to be conveyed to the submerged node """ sleep(self.m_to) self.conn.send(msg) def sendDataFile(self, file_path): """ Send a file to the submerged node. It may raise an exception. @param self pointer to the class object @param file_path path of the file that has to be sent """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem sendDataFile unexpected status: \ " + str(self.status)) if not os.path.isfile(file_path): raise FileNotFoundError("Modem sendDataFile file does not \ exist: " + file_path) self.status = Modem.Status.BUSY2REQ name = os.path.basename(file_path) size = os.path.getsize(file_path) self.send(self.interpreter.buildSendFile(name,size)) #sleep(self.m_to/2) #to give the rx the time to parse the command while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) # self.recvCommand() if self.status == Modem.Status.KILL: return self.close() self.status = Modem.Status.BUSY2DATA f = open (file_path, "rb") l = f.read(self.conn.data_buf_size) while (l): self.conn.send(l) l = f.read(self.conn.data_buf_size) f.close() self.status = Modem.Status.IDLE def reqDataFile(self, file_name, delete_flag = 1): """ Require a file from the submerged node @param self pointer to the class object @param file_name name of the required file @param delete_flag, if 1 erase it after sending, if 0 not """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem reqDataFile unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildGetFile(file_name, delete_flag)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) # self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqAllData(self, delete_flag = 1): """ Require all the data from the submerged node @param self pointer to the class object @param delete_flag, if 1 erase it after sending, if 0 not """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem reqAllData unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildGetData(delete_flag)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) # self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqRTData(self, ID_list, starting_time, duration, \ chunck_duration = 1, delete = 1, force_flag = 0): """ Require the data real time from the submerged node. @param self pointer to the class object @param ID_list list of the projector IDs used to record the audio @param starting_time Unix timestamp, in second, when to start recording the file @param duration duration, in minutes of the recording @param chunck_duration chunk duration, in seconds @param delete flag, if 1 erase it after sending, otherwise if 0 not @param force_flag if trying to record while transmitting: 0 (default) not allowed (error feedback) 1 (force) stop transmitting and start recording 2 (both) do both the operations together """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem reqRTData unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildGetRTData(ID_list, starting_time, \ duration, chunck_duration, delete , force_flag)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) # self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqSetPower(self, ID_list, s_l): """ Set the projector power. @param self pointer to the class object @param ID_list list of the projector IDs where to play the file @param s_l source level output power """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem setPower unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildSetPower(ID_list, s_l)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) # self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqPlayProj(self, name, ID_list, starting_time, n_rip = 1, \ delete = 1, force_flag = 0): """ Transmit the file with the projector. @param self pointer to the class object @param name of the file that has to be played @param ID_list list of the projector IDs where to play the file @param starting_time Unix timestamp, in second, when to start playing the file @param n_rip number of time it has to be consecutively played @param delete flag, if 1 erase it after playing, if 0 not @param force_flag if trying to transmit while recording: 0 (default) not allowed (error feedback) 1 (force) stop recording and start transmitting 2 (both) do both the operations together """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem playProj unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildPlay(name, ID_list, starting_time, \ n_rip, delete, force_flag)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) # self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqRunScript(self, script_name, output_name, starting_time, duration): """ Run a script in the node. @param self pointer to the class object @param script_name of the file that has to be run @paran output_name where to redirect the output of the script @param starting_time Unix timestamp, in second, when to start playing the file @param duration in minutes of the script """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem reqRunScript unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildRun(script_name, output_name, \ starting_time, duration)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) #self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqRecordData(self, name, sens_t, ID_list, starting_time, duration, \ force_flag = 0): """ record via sensors (either hydrophones, camera or others). @param self pointer to the class object @param name of the file where to record the audio @param sens_t of the sensors that have to record the data: 0 --> hydrophone, 1 --> camera 2 --> others @param ID_list list of the projector IDs used to record the audio @param starting_time Unix timestamp, in second, when to start recording the file @param duration duration, in minutes of the recording @param force_flag if trying to record while transmitting: 0 (default) not allowed (error feedback) 1 (force) stop transmitting and start recording 2 (both) do both the operations together """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem recordAudio unexpected status:\ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildRecordData(name, sens_t, ID_list, \ starting_time, duration, force_flag)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) #self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqNodeStatus(self): """ Require the submerged node status. @param self pointer to the class object """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem getNodeStatus unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildGetStatus()) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) #self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def getNodeStatus(self,status = 0): """ Get the submerged node status. @param status cointaining the status received from the node @param self pointer to the class object """ if status: self.node_status = status return self.node_status def reqResetProj(self, ID_list, force_flag = 0): """ Reset the projectors @param self pointer to the class object @param ID_list list of the projector IDs that has to be resetted @param force_flag if 1 reset also if pending operations, if 0 not """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem resetProj unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildResetProj(ID_list, force_flag)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) #self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqResetSensors(self, sens_t, ID_list, force_flag = 0): """ Reset the sensors (either hydrophones, camera or other) @param self pointer to the class object @param sens_t of the sensors that have to be reset: 0 --> all 1 --> hydrophone, 2 --> camera 3 --> others @param ID_list list of the projector IDs that has to be resetted @param force_flag if 1 reset also if pending operations, if 0 not @return the message """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem reqResetSensor unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildResetSensor(sens_t, ID_list, force_flag)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) #self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqResetAll(self, force_flag = 0): """ Build the reset_all message. This message will reset the node @param self pointer to the class object @param force_flag if 1 reset also if pending operations, if 0 not @return the message """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem resetAll unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildResetAll(force_flag)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) #self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqDeleteAllRec(self, sens_t = 0): """ Delete the recorded files from the node @param self pointer to the class object @param sens_t of the sensors that have the data to be deleted: 0 --> all 1 --> hydrophone, 2 --> camera 3 --> others @return the message """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem deleteAllRec unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildDeleteAllRec(sens_t)) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) #self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def reqDeleteAllSent(self): """ Delete the files sent by the node @param self pointer to the class object @return the message """ while self.status != Modem.Status.IDLE : sleep(0.1) if self.status != Modem.Status.IDLE: raise ValueError("Modem deleteAllSent unexpected status: \ " + str(self.status)) self.status = Modem.Status.BUSY2REQ self.send(self.interpreter.buildDeleteAllSent()) while self.status != Modem.Status.IDLE and self.status != Modem.Status.KILL: sleep(self.m_to) #self.recvCommand() if self.status == Modem.Status.KILL: return self.close() return self.errorCheck() def recvDataFile(self, file_name, length_f, confirm_flag): """ Require all the data from the submerged node @param self pointer to the class object @param file_name name of the required file @param length_f of the file, in bytes @paran confirm_flag True if confirm needed. When stream is false """ while self.status != Modem.Status.IDLE and self.status != Modem.Status.BUSY2REQ: sleep(0.1) if self.status == Modem.Status.IDLE: self.status = Modem.Status.BUSY2DATA elif self.status == Modem.Status.BUSY2REQ: self.status = Modem.Status.BUSY2REQ2DATA else: raise ValueError("Modem recvDataFile unexpected status: \ " + str(self.status)) if(confirm_flag): self.send(self.interpreter.buildConfirm()) self.status = Modem.Status.BUSY2DATA f = open(file_name,'wb') #open in binary n_recv = 0; if self.interpreter.debug: print "Modem::recvDataFile: recvFile", file_name, length_f while (n_recv < length_f and self.status != Modem.Status.KILL): l = self.commands_queue.pop(0) if len(self.commands_queue) else self.recvData() if(n_recv + len(l) < length_f): f.write(l) else: f.write(l[0:length_f - n_recv]) residue = l[length_f - n_recv : ] if not residue == "": if not residue.endswith(Interpreter.END_COMMAND): residue = residue + self.conn.recvCommand() if self.interpreter.debug: print "Modem::recvDataFile residue = ", residue self.commands_queue.extend(residue.split(Interpreter.END_COMMAND)) n_recv += len(l) f.close() if self.status == Modem.Status.KILL: f.close() return self.close() if self.status == Modem.Status.BUSY2DATA: self.status = Modem.Status.IDLE elif self.status == Modem.Status.BUSY2REQ2DATA: self.status = Modem.Status.BUSY2REQ return self.errorCheck() def confirmedMyIstr(self): """ Confirm the instruction has been correctely processed @param self pointer to the class object """ self.status = Modem.Status.IDLE self.error_status = Modem.ErrorDict.NONE def error(self, err_id): """ Signal the instruction has not been correctely processed @param self pointer to the class object @param err_id error identifier """ print >>sys.stderr, 'AN ERROR OCCURS: %s' % Modem.ErrorDict.error_dict[err_id] self.error_status = err_id self.status = Modem.Status.IDLE def errorCheck(self, refresh_error = True): """ Check if the node signal an error and return to the user @param self pointer to the class object @param refresh_error: if True refresh the error status """ error = self.error_status if refresh_error: self.error_status = Modem.ErrorDict.NONE return error def reset_myself(self): """ Reset the modem status due to unexpected behavior @param self pointer to the class object """ print >>sys.stderr, 'UNEXPECTED VALUE' self.status = Modem.Status.IDLE self.error_status = Modem.ErrorDict.NONE def parseDivCommands(self, msg): """ Parse the received message from the node and process it dividing all the commands @param self pointer to the class object """ if(self.status == Modem.Status.KILL): return # commands_queue = msg.split(Interpreter.START_COMMAND) # for command in commands_queue: # fine_command = command.split(Interpreter.END_COMMAND) # print fine_command[0] # self.parseCommand(fine_command[0]) self.commands_queue.extend(msg.split(Interpreter.END_COMMAND)) # for command in commands_queue: # print command # self.parseCommand(command) while len(self.commands_queue): command = self.commands_queue.pop(0) if self.interpreter.debug: print "parseDivCommands command: ",command self.parseCommand(command) def checkConnection(self,msg): """ Check if the socket returned something. If zero string, close it @param self pointer to the class object @param msg to check """ if (len(msg) == 0): sleep(self.m_to/2) print >>sys.stderr, 'Closing due to possible server fault' self.close() def parseCommand(self, msg): """ Parse the received message from the node and process it @param self pointer to the class object """ if msg == "": return if self.interpreter.debug: print "Modem::parseCommand: ", msg if(self.status == Modem.Status.KILL): return command = msg.split(Interpreter.SEPARATOR) if (len(command)==1): if (command[0] == 'OK'): return self.confirmedMyIstr() elif (len(command)==2): if (command[0] == 'error'): return self.error(int(command[1])) elif (len(command)==3): if (command[0] == 'send_file'): cmd2 = re.sub("[^0-9]", "", command[2]) return self.recvDataFile(command[1],int(cmd2),False) elif (command[0] == 'send_stream'): cmd2 = re.sub("[^0-9]", "", command[2]) return self.recvDataFile(command[1],int(cmd2),False) return self.reset_myself()