def check_spool_files(): """ Check all queued spoolfiles if they have already been processed by the Asterisk telephony server; Set the corresponding status for the scheduled events """ queued_events = get_all_queued_events() for event in queued_events: try: status = get_spoolfile_status(event.filename) if status == "Completed": logger.info("Completed sending %s" % unicode(event.sendable)) event.state = "done" elif status == "Expired": # Handle what to do if asterisk gave up event.retry += 1 if event.retry < settings.ASTERISK_RETRY: logger.info("%s expired; rescheduling" % unicode(event.sendable)) event.send_time = datetime.now() + \ timedelta(minutes = settings.ASTERISK_RETRY_TIME) event.state = "new" else: event.state = "failed" logger.error("Sending %s failed" % unicode(event.sendable)) elif status == "Failed": # Something really, really went wrong event.state = "failed" logger.error("Sending %s failed" % unicode(event.sendable)) event.save() except: # This means the file has not been found in the done folder # nothing has to be done here. pass
def save_notification(request): notification = request.session.get('notification', None) patient = request.session.get('patient', None) nexturl = reverse("web_index") backurl = reverse("notifications_create", kwargs={'notification_type_name': notification.notification_type.name }) if not notification or not patient: logger.warning("save_notification: no notification/patient in session") return HttpResponseRedirect(reverse(create_notification)) logger.info("Saving notification: %s with patient: %s" % (notification, patient.phone_number)) notification.save_with_patient(patient) title = _("The \"%s\" notification has been created.") \ % notification.notification_type.verbose_name new_button_label = _("New notification") if notification.notification_type.notify_immediately: message = _("The patient will be informed immediately.") else: message = _("Please tell the patient that he/she will be reminded"\ " one day before the appointemnt.") success = True return render_to_response('web/status_message.html', locals(), context_instance = RequestContext(request))
def send_notification_via_bluetooth(request): if (request.method == "POST"): notification = request.session.get('notification', None) mac_address = request.POST['device_mac'].strip() logger.info("sending notification to mac_address: " + mac_address) logger.info("notification data: " + unicode(notification)) notification.bluetooth_mac_address = mac_address notification.bluetooth_server_address = \ request.META['REMOTE_ADDR'].strip() output_data = notification.get_data_for_sending() result = output_data.send() if(result): return HttpResponse(status = 200) else: return HttpResponse(status = 500) backurl = reverse("web_list_devices") url = reverse("notifications_send") next = reverse("web_index") mac_address = request.GET['device_mac'].strip() return render_to_response('web/send_bluetooth_notification.html', locals(), context_instance=RequestContext(request))
def register(request, group_id): ''' Register a patient to an information group, i.e. a new subscription of the infoservice is created. ''' ajax_url= reverse('web_check_call_received') # ways_of_communication = get_ways_of_communication(immediate = True) ways_of_communication = WayOfCommunication.objects.filter( enabled = True, can_send_immediately = True) if request.method == "POST": set_session_variables_for_register(request) form = RegisterPatientForGroupValidationForm(request.POST) if form.is_valid(): number = fill_authentication_session_variable(request) auth_number = AUTH_NUMBER backurl = reverse('web_index') return redirect_to_authentication_or(reverse \ ('groups_register_save', kwargs = {'group_id': group_id})) else: logger.info("register: Invalid form.") group = get_object_or_404(InfoService,pk = group_id) backurl = reverse("web_index") return render_to_response('groups/register.html', locals(), context_instance = RequestContext(request))
def get_data_for_sending(self): """ Prepare OutputData for selected way_of_communication. Return an object of a subclass of OutputData. """ call = "self.get_data_for_%s()" % self.way_of_communication.name logger.info("sendable.get_data_for_sending() calling method: " + call) return eval(call)
def subscription_save(request, id): ''' save patient and subscription @return subscription ''' patient = request.session['patient'] patient.save() way_of_communication = request.session['way_of_communication'] infoservice = InfoService.objects.filter(pk = id)[0] subscription = Subscription(patient = patient, way_of_communication = way_of_communication, infoservice = infoservice) subscription.save() logger.info("Saved subscription %s of type %s.", (unicode(subscription), unicode(subscription.infoservice.type))) return subscription
def create_messages_for_infoservice(infoservice, text): ''' Put together all information for an infomessage and calls InfoService.create_scheduled_event ''' for patient in infoservice.members.all(): info_message = InfoMessage() info_message.text = text subscription = Subscription.objects.filter(patient = patient, infoservice = infoservice)[0] info_message.recipient = patient info_message.send_time = datetime.now() info_message.way_of_communication = \ subscription.way_of_communication info_message.save() info_message.create_scheduled_event(datetime.now()) logger.info("Created %s", str(info_message))
def check_call_received(request): response_dict = {} try: response_dict["status"] = "failed" number = request.session['authenticate_phonenumber']['number'] start_time = request.session['authenticate_phonenumber']['start_time'] if (start_time + AUTHENTICATION_CALL_TIMEOUT) >= datetime.now(): if check_and_delete_authentication_call(number): response_dict["status"] = "received" logger.info("check_call_received: call received.") else: response_dict["status"] = "waiting" except KeyError: pass return HttpResponse(content = simplejson.dumps(response_dict), content_type = "application/json")
def authenticate_phonenumber(request): nexturl = '' next = '' ajax_url= reverse('web_check_call_received') backurl = reverse('web_index') logger.info("Deleting timed out authentication calls.") delete_timed_out_authentication_calls() try: number = fill_authentication_session_variable(request) logger.info("Starting authentication with %s" % AUTH_NUMBER) auth_number = AUTH_NUMBER next = request.GET.get('next', reverse('notifications_save')) return render_to_response('web/authenticate_phonenumber_call.html', locals(), context_instance = RequestContext(request)) except ValueError, e: error = e
def send_vcal(server_address, mac, data): """ Send a ical/vcal file to a bluetooth device @param serverAddress: FQDN or IP address of the remote host @type serverAddress: String @param mac: MAC Address of the target bluetooth device @type mac: String @param data: plain text vcal data to be sent @type data: String """ conn = set_connection_to_bluetooth(server_address) logger.info("Sending vCal via Bluetooth to: " + mac) result = conn.Actions.sendVCalFile(mac, data, "reminder.vcs") logger.info("Result of Sending: " + str(result)) return (result == "done")
def get_data_for_bluetooth(self): """ Prepare OutputData for voice. Generate the message for a Notification. Return BluetoothOutputData for sending. """ logger.info("starting get_data_for_bluetooth() in Notification") data = BluetoothOutputData() data.bluetooth_mac_address = self.bluetooth_mac_address data.server_address = self.bluetooth_server_address logger.info("Sending to Bluetooth Mac Address " + data.bluetooth_mac_address + " and Bluetooth Server " + data.server_address) try: self.hospital except Hospital.DoesNotExist: self.hospital = Hospital.get_current_hospital() content = self.reminder_text() uid = vcal.get_uid() data.data = vcal.create_vcal_string(self.date, self.hospital, content, uid) logger.info("Created vCal with uid %s" % str(uid)) logger.debug("Created vCal: " + data.data) return data
def run(run_only_one_time = False): """ check every second, if an SMS or a Voicecall needs to be sent and send it if due """ while True: check_spool_files() due_events = get_all_due_events() for event in due_events: event.state = 'pending' try: data = event.sendable.get_data_for_sending() logger.info("Trying to send: %s" % unicode(event.sendable)) except Exception, e: logger.error("Failed to get data for " + unicode(event) + \ " exception " + unicode(e)) event.state = "failed" event.save() continue # TODO error handling if len(get_all_queued_events()) > 0: break try: logger.info(" sending: %s" % unicode(data)) event.filename = data.send() #if not run_only_one_time: #time.sleep(20) except Exception, e: logger.error("Failed to send: " + unicode(data) + \ " exception " + unicode(e)) event.state = "failed" event.save() event.state = 'queued' event.save() del data
def create_infoservice(request, infoservice_type): ''' Display the form and save the new infoservice. Display a successmessage. ''' infoservice_textblocks = InfoService.TYPE_TEXTS[infoservice_type] if request.method == "POST": data = {'name': request.POST['name'], 'type': infoservice_type} form = InfoServiceValidationForm(data) if form.is_valid(): form.save() infoservice = form.instance logger.info("Created InfoService: %s", str(infoservice)) nexturl = reverse('infoservices_index', kwargs={'infoservice_type': infoservice_type}) backurl = reverse('infoservices_create', kwargs={'infoservice_type': infoservice_type}) title = _("Creation successful") message = _("The \"%(infoservice_name)s\" " "%(infoservice_type)s has been created.") \ % {'infoservice_name': infoservice.name, 'infoservice_type': infoservice_textblocks["name"]} new_button_label = _("Create another %(infoservice_type)s") \ % {'infoservice_type': infoservice_textblocks['name']} success = True return render_to_response('web/status_message.html', locals(), context_instance = RequestContext(request)) return render_to_response("infoservices/create.html", locals(), context_instance = RequestContext(request))
def register(request): ''' Register a patient to the waitinglist of a medicine, i.e. a new subscription of the infoservice is created. ''' ajax_url= reverse('web_check_call_received') medicines = InfoService.objects.all().filter(type='medicine') ways_of_communication = get_ways_of_communication(immediate = True) if request.method == "POST": set_session_variables_for_register(request) infoservice = None form = None form = RegisterPatientForMedicineForm(request.POST) request.session['medicine'] = request.POST.get('medicine', '') if form.is_valid(): number = fill_authentication_session_variable(request) auth_number = AUTH_NUMBER backurl = reverse('web_index') return redirect_to_authentication_or( reverse('medicines_register_save', kwargs = {'medicine_id': request.session['medicine']})) else: logger.info("register patient for medicine: Invalid form.") backurl = reverse("web_index") return render_to_response('medicine/register.html', locals(), context_instance = RequestContext(request))
def send(self): logger.info("Sending via SMS") vc = voicecall.Voicecall() return vc.conduct_sms(self.phone_number, self.data, "outbound-sms")
def send(self): logger.info("Sending via Phone Call") call = voicecall.Voicecall() return call.conduct_call(self.phone_number, self.data, "outbound-call")
def create_notification(request, notification_type_name = None): ''' Display the form and creates a new notification, but does not save it yet. Redirect to authentication if switched on ''' notification_type = NotificationType.objects. \ filter(name = notification_type_name)[0] nexturl = "" backurl = reverse('web_index') ways_of_communication = get_ways_of_communication( notification_type.notify_immediately) if request.method == "POST": data = deepcopy(request.POST) if notification_type.notify_immediately: data['date'] = date.today().strftime('%Y-%m-%d') + \ ' ' + DEFAULT_SEND_TIME else: data['date'] = data.get('date', '') + ' ' + DEFAULT_SEND_TIME form = NotificationValidationForm(data) woc = get_woc_by_id( request.POST['way_of_communication'] ) if not woc.can_send_immediately: form = NotificationValidationFormBluetooth(data) if form.is_valid(): notification = Notification() patient = Patient() if woc.can_send_immediately: patient.phone_number = form.cleaned_data['phone_number'] notification.date = form.cleaned_data['date'] notification.notification_type = notification_type notification.hospital = Hospital.get_current_hospital() notification.way_of_communication = \ form.cleaned_data['way_of_communication'] request.session['notification'] = notification request.session['patient'] = patient logger.info("Create notification via %s" % notification.way_of_communication.verbose_name) if notification.way_of_communication == get_woc('bluetooth'): return HttpResponseRedirect(reverse("web_list_devices") + \ "?next=" + reverse("notifications_send")) elif notification.way_of_communication.name in ('sms', 'voice' ): return redirect_to_authentication_or( reverse("notifications_save")) else: logger.error("Unknown way of communication selected.") raise Exception ("Unknown way of communication %s " \ %notification.way_of_communication.\ verbose_name + "(this is neither " + \ "bluetooth nor sms or voice)") else: logger.info("create_notification: Invalid form.") return render_to_response('notifications/create.html', locals(), context_instance=RequestContext(request))
def send(self): logger.info("Sending via Bluetooth") return bluetooth.send_vcal(self.server_address, self.bluetooth_mac_address, self.data)