def addNewContainer(self, containername): if containername == Names.Containers["SMS"]: self.myContainers[containername] = SMS.SMS(containername, self) elif containername == Names.Containers["RPDU"]: self.myContainers[containername] = RPDU.RPDU(containername, self) elif containername == Names.Containers["UD"]: self.myContainers[containername] = UD.UD(containername, self) elif containername == Names.Containers["WSP"]: self.myContainers[containername] = WSP.WSP(containername, self) elif containername == Names.Containers["WBXML"]: self.myContainers[containername] = WBXML.WBXML(containername, self) elif containername == Names.Containers["SIMCMD"]: self.myContainers[containername] = SIMCmd.SIMCmd( containername, self) elif containername == Names.Containers["MMS"]: self.myContainers[containername] = MMS.MMS(containername, self) elif containername == Names.Containers["ENVELOPE"]: self.myContainers[containername] = Envelope.Envelope( containername, self)
import Cliente, Factura, SMS, Call, CallInternational if __name__ == "__main__": # principal: cli = Cliente.Cliente("Juan Perez", 600998833, "56.777.888G") fact = Factura.Factura("30/09/2016", "16/0001", cli) fact.addDetalle(SMS.SMS("1/09/2016", "14:15", 808585585, 0.05)) fact.addDetalle(SMS.SMS("10/09/2016", "08:09", 608585445, 0.05)) fact.addDetalle(SMS.SMS("12/09/2016", "12:58", 608564585, 0.05)) fact.addDetalle(SMS.SMS("19/09/2016", "14:15", 208585585, 0.05)) fact.addDetalle(Call.Call("9/09/2016", "14:15", 208585585, 0.025, 0.023)) fact.addDetalle(Call.Call("19/09/2016", "4:15", 208585585, 0.025, 0.08)) fact.addDetalle(Call.Call("29/09/2016", "16:15", 208555585, 0.025, 0.12)) fact.addDetalle( CallInternational.CallInternational("2/09/2016", "09:15", 208555585, 0.025, 0.12, 1)) fact.generar()
def main(): path = "/home/ashley/thesis" internet_connection = False DEVNULL = open(os.devnull, 'wb') print "------------------------------------------------------------------------" sys.stdout.flush() sys.stderr.write( "------------------------------------------------------------------------\n" ) #Preemptively create debug file in case of major issue call(["touch", path + "debug"]) flags = [] i2c = i2c_link.I2CConnection() dw = data_writer.DataWriter() #Check if mobile broadband USB is connected if call(["ls /dev/tty* | grep USB"], shell=True, stderr=DEVNULL, stdout=DEVNULL) != 0: sys.stderr.write("ERROR: Modem is not connected. Exiting\n") return 3 sms = SMS.SMS() #Start the internet connection if DEBUG: print "Starting Internet connection" sys.stdout.flush() call(["sudo", path + "/internet.sh"]) if call(["ping", "-c", "4", "www.google.com.au"], stderr=DEVNULL, stdout=DEVNULL) == 0: internet_connection = True else: internet_connection = False sys.stderr.write("Ping failed. No internet connection\n") dbHelper = Dropbox.DropboxHelper() #Sychronise time if DEBUG: print "Attempting to update time" sys.stdout.flush() if internet_connection and call(["sudo sntp -s 0.au.pool.ntp.org"], shell=True) != 0: sys.stderr.write("Cannot sychronise time\n") now = datetime.datetime.now() if DEBUG: print now.ctime() sys.stdout.flush() sys.stderr.write(now.ctime() + '\n') #Update config file if DEBUG: print "Updating Dropbox" sys.stdout.flush() try: if internet_connection: dbHelper.get_file('config.ini', path + '/config.ini') except rest.ErrorResponse as details: sys.stderr.write( "MONITORING: Error getting config file from Dropbox\n" + str(details) + '\n') pass except rest.RESTSocketError as details: sys.stderr.write( "MONITORING: Error getting config file from Dropbox\n" + str(details) + '\n') pass #Parse config file if DEBUG: print "Reading config file" config = monitor_config.Config() config.read_config_file() i2c.update_off_period(config.off_time) prevData = dw.get_previous_datapoints(5) #TODO: Add max if DEBUG: print "Getting and validating datapoint" for retries in range(10): #Get sensor readings datapoint = i2c.get_distance() print "Raw datapoint = " + str(datapoint) now = datetime.datetime.now() if datapoint < 0 and retries < 9: time.sleep(config.retry_time) #Try again later continue elif datapoint == -1: #No or invalid response sys.stderr.write("Problem with ultrasonic sensor\n") if call(["ls " + path + " | grep error_alarm"], shell=True, stderr=DEVNULL, stdout=DEVNULL) != 0: call(["touch", path + "/error_alarm"]) msg = "Problem with sensor for tank {0} @ {1}".format( config.name, now.strftime("%H:%M %d/%m/%Y")) sms.sendMessage(config.master, msg) else: print "Muted alarm" return 1 elif datapoint == -2: #No echo recieved print "Exceeded max retries with ultrasonic sensor. Tank may be full, very empty or the sensor needs to be checked" #Check for previous extrapolated data extraps = 0 for i in range(1, len(prevData) + 1): dp = prevData[-i] if isinstance(dp, list) and len(dp) > 2 and 'extrapolated' in dp: extraps = extraps + 1 else: extraps = 0 if extraps > 3: sys.stderr.write( "Too many extrapolated datapoints in a row. Exiting without writing datapoint to file.\n" ) if call(["ls " + path + " | grep error_alarm"], shell=True, stderr=DEVNULL, stdout=DEVNULL) != 0: call(["touch", path + "/error_alarm"]) msg = "Problem with sensor for tank {0} @ {1}".format( config.name, now.strftime("%H:%M %d/%m/%Y")) sms.sendMessage(config.master, msg) else: print "Muted alarm" return 2 #If ok to, set datapoint to be previous datapoint datapoint = prevData[-1][1] print "Setting datapoint to {0} from {1}".format( prevData[-1][1], prevData[-1][0].strftime("%H:%M %d/%m/%Y")) flags.append('extrapolated') elif datapoint >= 0: datapoint = 100 - ( (float(datapoint) - config.sensorheightabovewater) / config.maxwaterheight * 100) #Convert to perentage call(["rm", path + "/error_alarm"], stderr=DEVNULL, stdout=DEVNULL) break #Check if any alerts need to be sent prevData = dw.get_previous_datapoints(24) oneDay = datetime.timedelta(days=1) prevAlarms = 0 for dp in prevData: if dp[0] > (now - oneDay) and len(dp) > 2 and 'alarm' in dp: #TODO: fix this prevAlarms = prevAlarms + 1 if datapoint < config.low_water_level: if DEBUG: print "Low water alarm" if now.time() < config.quiet_time_start and now.time( ) > config.quiet_time_end and prevAlarms < config.max_alarms_per_day: flags.append('alarm') else: print "Muted alarm due to max alarms in a day or it is quiet time" print "Previous alarms = " + str( prevAlarms) + " before start_time = " + str(now.time( ) < config.quiet_time_start) + "after end_time = " + str( now.time() < config.quiet_time_end) flags.append('muted') #Write datapoint to file if DEBUG: print "Writing data to file" filename = dw.write_datapoint(datapoint, flags) #Write datafile to dropbox if DEBUG: print "Updating datafile in Dropbox" try: if internet_connection: dbHelper.put_file(filename, filename, True) except rest.ErrorResponse as details: sys.stderr.write("MONITORING: Error updating datafile in Dropbox\n" + str(details) + '\n') pass except rest.RESTSocketError as details: sys.stderr.write("MONITORING: Error updating datafile in Dropbox\n" + str(details) + '\n') pass #Send datapoint to Xively if DEBUG: print "Sending data to Xively" if datapoint > -1: try: xively = Xively.XivelyHelper() if not xively.get_datastream("test"): xively.create_datastream("test", "test") xively.put_datapoint(datapoint) except Exception as details: sys.stderr.write("XIVELY: Error writing to feed\n" + str(details) + '\n') #TODO: SMS stuff here if 'alarm' in flags: sms.sendCommand("") #Read the \r\n dummy = sms.serialReadline(eol='\n') #Read response response = sms.serialReadline().strip() if response != "OK": print "Not ready to send message" del sms return 3 msg = "ALERT: Water level at {0:.2f}% in {1} tank @ {2}".format( datapoint, config.name, now.strftime("%H:%M %d/%m/%Y")) for no in config.white_list: sms.sendMessage(no, msg) #Send stopping and shutdown command return 0 #Need to make function to check for new SMS's """ Can call and immediately hang up. First check for new SMS's, then check number it came from. Then check first part of message and if it is "You missed a call from" then send most recent data. +CMTI: "ME", 0 Need to delete message Do I need a mute SMS function? Yes I do in case it gets annoying So check for new message. Check if number is on the whitelist. if msg.lower().strip(' .?,!-:;()$%#"') == "mute": #Mute all msgs for the next 8 hours To show muting is enabled, create a file and check its last modification time using os.path.getmtime(<file>) Then add the "muted" to the flags """ """
import time cap = cv2.VideoCapture(0) length = 0 seconds = 1 message_sent = {'sent': False, 'time': time.time() - 5 * 60} matchers = {'bus', 'school', 'public', 'transportation'} phone = '2677508123' email = '*****@*****.**' password = '******' if __name__ == "__main__": sms = SMS(phone, email, password) sms.send('Program Starting!') winsound.Beep(440, 200) while (1): # Take each frame _, frame = cap.read() k = cv2.waitKey(5) & 0xFF cv2.imwrite(os.path.join(os.path.dirname(__file__), 'frame.jpg'), frame) if length % (30 * seconds) == 0: thread = threading.Thread(target=find_labels) thread.start() image = putText(frame, label_list)