Skip to content

Commit

Permalink
start thread on runserver
Browse files Browse the repository at this point in the history
Signed-off-by: okkype <okkype@gmail.com>
  • Loading branch information
okkype committed Jun 26, 2019
1 parent 99b9beb commit a3fecc0
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 43 deletions.
8 changes: 6 additions & 2 deletions sihipo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# Application definition

INSTALLED_APPS = [
'sihipo_root',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
Expand All @@ -39,7 +40,7 @@
'bootstrap3',
'django_nvd3',
'pwa',
'sihipo_root.apps.SihipoRootConfig',
# 'sihipo_root.apps.SihipoRootConfig',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -119,7 +120,10 @@
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
# STATIC_ROOT = '/home/okky/Dokumen/projek/sihipo/sihipo/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'sihipo/static/')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'sihipo_root/static/'),
]

PWA_SERVICE_WORKER_PATH = os.path.join(BASE_DIR, 'sihipo_root/static/js', 'serviceworker.js')
PWA_APP_NAME = 'SIHIPO'
Expand Down
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions sihipo_root/management/commands/_private.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'''
Created on 26 Jun 2019
@author: alif
'''
44 changes: 44 additions & 0 deletions sihipo_root/management/commands/runserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''
Created on 26 Jun 2019
@author: alif
'''
from django.contrib.staticfiles.management.commands.runserver import (
Command as StaticRunserverCommand
)
from sihipo_root.threads import *
import threading


class Command(StaticRunserverCommand):

# def handle(self, *args, **options):
# super(Command, self).handle(*args, **options)

def inner_run(self, *args, **options):
thread_sensor_run = False
thread_control_run = False
thread_eval_run = False

for t in threading.enumerate():
if t.getName() == 'thread_sensor':
thread_sensor_run = True
if t.getName() == 'thread_control':
thread_control_run = True
if t.getName() == 'thread_eval':
thread_eval_run = True

if not thread_sensor_run:
tf_sensor = SensorThread(interval=36000)
tf_sensor.start()
self.stdout.write(self.style.SUCCESS('Successfully start Sensor Thread'))
if not thread_control_run:
tf_control = ControlThread()
tf_control.start()
self.stdout.write(self.style.SUCCESS('Successfully start Control Thread'))
if not thread_eval_run:
tf_eval = EvalThread()
tf_eval.start()
self.stdout.write(self.style.SUCCESS('Successfully start Eval Thread'))
self.stdout.write('')
super(Command, self).inner_run(*args, **options)
12 changes: 5 additions & 7 deletions sihipo_root/templates/blank.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,20 @@ <h3 class="page-header">
function pwa_link() {
$('a[href^="/"]').each(function(){
var oldUrl = $(this).attr("href");
if(oldUrl.startsWith('/#target=')) {
if(oldUrl.startsWith('/#')) {
/* RESERVED */
} else {
var newUrl = '/#target=' + oldUrl;
var newUrl = '/#' + oldUrl;
$(this).attr("href", newUrl);
}
});
}
function pwa_page() {
pwa_link();
var pwa_url = $(location).attr("href").split('#');
var pwa_var = pwa_url.length > 1 ? pwa_url[1] : '';
pwa_var.split('&').forEach(function (item, index) {
if(item.startsWith('target=')) {
var pwa_target = item.replace('target=', '');
var pwa_var = pwa_url.length > 1 ? pwa_url[1] : false;
if (pwa_var != false) {
var pwa_target = pwa_var;
Pace.restart();
$('.container-fluid').html('');
$.get(pwa_target, function (data) {
Expand All @@ -270,7 +269,6 @@ <h3 class="page-header">
Pace.stop();
});
}
});
}
// START : Code to handle install prompt on desktop
let deferredPrompt;
Expand Down
6 changes: 3 additions & 3 deletions sihipo_root/templates/empty.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ <h3 class="page-header">
{% if page_obj.has_previous %}
<a class="btn btn-outline btn-primary" href="{% url name|add:'_list' %}?page={{ page_obj.previous_page_number }}">&lt;</a>
{% endif %}
<a class="btn btn-outline btn-primary" href="javascript:var jump=parseInt(prompt('Loncat ke halaman?'));location.href='#target={% url name|add:'_list' %}?page='+((jump>{{ page_obj.paginator.num_pages }}?{{ page_obj.paginator.num_pages }}:jump)||1);location.reload();">
<a class="btn btn-outline btn-primary" href="javascript:var jump=parseInt(prompt('Loncat ke halaman?'));location.href='/#{% url name|add:'_list' %}?page='+((jump>{{ page_obj.paginator.num_pages }}?{{ page_obj.paginator.num_pages }}:jump)||1);location.reload();">
{{ page_obj.number }}/{{ page_obj.paginator.num_pages }}
</a>
{% if page_obj.has_next %}
<a class="btn btn-outline btn-primary" href="#target={% url name|add:'_list' %}?page={{ page_obj.next_page_number }}">&gt;</a>
<a class="btn btn-outline btn-primary" href="/#{% url name|add:'_list' %}?page={{ page_obj.next_page_number }}">&gt;</a>
{% endif %}
</div>
</div>
Expand All @@ -30,7 +30,7 @@ <h3 class="page-header">
</div>
<script type="text/javascript">
if(document.head.innerHTML == '') {
location.href = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + '/#target=' + window.location.pathname;
location.href = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port + '/#' + window.location.pathname;
}
{% block javascript %}{% endblock %}
$(document).ready(function() {
Expand Down
4 changes: 2 additions & 2 deletions sihipo_root/templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
$.get(target, function () {})
.done(function () {
{% if pwa_app_single %}
location.href = '#target=' + target;
location.href = '/#' + target;
{% else %}
location.href = target;
{% endif %}
Expand All @@ -45,7 +45,7 @@
$.get(target, function () {})
.done(function () {
{% if pwa_app_single %}
location.href = '#target=' + target;
location.href = '/#' + target;
{% else %}
location.href = target;
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion sihipo_root/templates/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</thead>
<tbody>
{% for object in object_list %}
<tr oncontextmenu="location.href='{{ pwa_app_single|yesno:'#target=,' }}{% url name|add:'_update' object.id %}';return false;">
<tr oncontextmenu="location.href='{{ pwa_app_single|yesno:'/#,' }}{% url name|add:'_update' object.id %}';return false;">
{% block list_body %}
{% for table_field in table_fields %}
<td {% if object|dict_get_field_type:table_field in numeric_field %}data-type="numeric"{% endif %}>
Expand Down
28 changes: 0 additions & 28 deletions sihipo_root/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from . import views
from django.conf.urls import url, include
from django.contrib.auth import views as auth_views
from sihipo_root.threads import *
import threading


urlpatterns = [
url(r'^$', views.HomeView.as_view(), name='home'),
Expand Down Expand Up @@ -114,28 +111,3 @@
url(r'^plantalert/de/(?P<pk>\d+)$', views.PlantAlertDe, name='plantalert_de'),
# url(r'^plantduplicate/<str:model>/(?P<pk>\d+)$', views.PlantDuplicate, name='plantduplicate'),
]

# thread_sensor_run = False
# thread_control_run = False
# thread_eval_run = False
# thread_telegram_run = False
#
# for t in threading.enumerate():
# if t.getName() == 'thread_sensor':
# thread_sensor_run = True
# if t.getName() == 'thread_control':
# thread_control_run = True
# if t.getName() == 'thread_eval':
# thread_eval_run = True
# if t.getName() == 'thread_telegram':
# thread_telegram_run = True
#
# if not thread_control_run:
# tf_control = ControlThread()
# tf_control.start()
# if not thread_eval_run:
# tf_eval = EvalThread()
# tf_eval.start()
# if not thread_telegram_run:
# tf_sensor = SensorThread(interval=36000)
# tf_sensor.start()

0 comments on commit a3fecc0

Please sign in to comment.