Skip to content

Commit 69f6c7c

Browse files
authored
Merge pull request #23 from Salamafet/dev
refind ip
2 parents dc8c22a + 9c7bdd4 commit 69f6c7c

File tree

4 files changed

+146
-51
lines changed

4 files changed

+146
-51
lines changed

octoprint_phom/__init__.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def get_api_commands(self):
7676
return dict(
7777
command1=[],
7878
pairing=['ip'],
79+
changeip=['ip'],
7980
configuration=['device_id'],
8081
updaterules=['rules']
8182
)
@@ -92,9 +93,16 @@ def on_api_command(self, command, data):
9293
token = r.json()[0]['success']['username']
9394
self._settings.set(['hue_token'], token)
9495
self._settings.set(['hue_ip'], ip)
96+
self._settings.save()
9597
return flask.jsonify(reponse="success")
98+
elif command == "changeip":
99+
ip = data['ip']
100+
self._settings.set(['hue_ip'], ip)
101+
self._settings.save()
102+
return flask.jsonify(reponse="success")
96103
elif command == "configuration":
97104
self._settings.set(['light_id'], data['device_id'])
105+
self._settings.save()
98106
return flask.jsonify(reponse="success")
99107
elif command == "updaterules":
100108
# Fetch actual rules to get the id of the old one
@@ -174,7 +182,12 @@ def on_api_get(self, request):
174182
elif(token != None and light == None):
175183
return flask.jsonify(reponse=1)
176184
elif(token != None and light != None):
177-
return flask.jsonify(reponse=2)
185+
try:
186+
r = requests.get("http://{}/api/{}/lights/{}".format(self._settings.get(['hue_ip']), self._settings.get(['hue_token']), self._settings.get(['light_id'])), timeout=3)
187+
except:
188+
return flask.jsonify(reponse=3)
189+
else:
190+
return flask.jsonify(reponse=2)
178191
elif(option == "checkplugstatus"):
179192
r = requests.get("http://{}/api/{}/lights/{}".format(self._settings.get(['hue_ip']), self._settings.get(['hue_token']), self._settings.get(['light_id'])))
180193
if(r.json()['state']['on']):

octoprint_phom/templates/ophom_navbar.jinja2

+60-47
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,81 @@
11
<a id="ophom_plugsettings" class="pull-right" title="On/Off Printer" style="cursor: pointer;">
22
<font id="ophom_plugbutton" color='grey'>
3-
<i class="fa fa-plug"></i>
3+
<i class="fa fa-plug fa-spin"></i>
44
</font>
55
</a>
66

77
<script>
88
var plug_button = document.getElementById("ophom_plugbutton");
99
10-
// Vérification du statut actuel de la configuration de Ophom
11-
fetch(API_BASEURL + "plugin/ophom?action=isconfigured").then(function(response) {
12-
response.json().then(function(data) {
13-
if(data.reponse != 2)
14-
{
15-
plug_button.style.color = "grey"
16-
plug_button.addEventListener('click', function(){
17-
alert("Ophom is not configured !\nPlease open settings and go in the \"Plugins -> Ophom\" section.");
18-
})
19-
}
20-
else
21-
{
22-
// Récupération du statut au lancement de la page
23-
checkPlugStatus();
24-
25-
// Boucle de vérification du statut de la prise
26-
var check_plug_status = setInterval(function() {
10+
setTimeout(function() {
11+
// Vérification du statut actuel de la configuration de Ophom
12+
fetch(API_BASEURL + "plugin/ophom?action=isconfigured").then(function(response) {
13+
response.json().then(function(data) {
14+
// Bridge not configured
15+
plug_button.children[0].classList.remove("fa-spin")
16+
if(data.reponse < 2)
17+
{
18+
plug_button.style.color = "grey"
19+
plug_button.addEventListener('click', function(){
20+
alert("Ophom is not configured !\nPlease open settings and go in the \"Plugins -> Ophom\" section.");
21+
})
22+
}
23+
// Bridge configured but not found on network
24+
else if(data.reponse == 3)
25+
{
26+
plug_button.style.color = "grey";
27+
plug_button.addEventListener('click', function(){
28+
alert("Bridge not found !\nPlease open settings and go in the \"Plugins -> Ophom\" section.");
29+
})
30+
}
31+
// Bridge configured and working
32+
else if(data.reponse == 2)
33+
{
34+
// Récupération du statut au lancement de la page
2735
checkPlugStatus();
28-
},5000)
2936
30-
// Envoi de la commande toggle pour allumer ou éteindre la prise
31-
document.getElementById("ophom_plugsettings").addEventListener('click', function(){
37+
// Boucle de vérification du statut de la prise
38+
var check_plug_status = setInterval(function() {
39+
checkPlugStatus();
40+
},5000)
41+
42+
// Envoi de la commande toggle pour allumer ou éteindre la prise
43+
document.getElementById("ophom_plugsettings").addEventListener('click', function(){
3244
33-
fetch(API_BASEURL + "connection").then(function(response) {
34-
response.json().then(function(data) {
35-
if(data.current.state == "Operational")
36-
{
37-
toggle();
38-
}
39-
else if(data.current.state == "Closed")
40-
{
41-
toggle();
42-
}
43-
else if(data.current.state == "Printing")
44-
{
45-
if(plug_button.dataset.status == 1)
45+
fetch(API_BASEURL + "connection").then(function(response) {
46+
response.json().then(function(data) {
47+
if(data.current.state == "Operational")
4648
{
47-
if(confirm("A print is currently running !\nDo you really want turn off the plug ?"))
48-
{
49-
toggle();
50-
}
49+
toggle();
5150
}
52-
else
51+
else if(data.current.state == "Closed")
5352
{
5453
toggle();
5554
}
56-
}
55+
else if(data.current.state == "Printing")
56+
{
57+
if(plug_button.dataset.status == 1)
58+
{
59+
if(confirm("A print is currently running !\nDo you really want turn off the plug ?"))
60+
{
61+
toggle();
62+
}
63+
}
64+
else
65+
{
66+
toggle();
67+
}
68+
}
69+
})
5770
})
58-
})
59-
71+
6072
61-
62-
})
63-
}
64-
})
65-
});
73+
74+
})
75+
}
76+
})
77+
});
78+
}, 2000);
6679
6780
function checkPlugStatus()
6881
{

octoprint_phom/templates/ophom_settings.jinja2

+71-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ Hue Bridge Status: <span id="ophom_generalstatus" class="badge badge-danger" sty
2424
</div>
2525
</div>
2626

27+
<!-- ###### BRIDGE RE FIND ###### -->
28+
<div id="ophom_bridgeunreachable" style="display: none;">
29+
<h4>Looking for the Bridge</h4>
30+
<p>Click on the button below to start searching for your Philips Hue Bridge</p>
31+
<button id="ophom_researchbridgebutton" type="button" class="btn"><i class="fa fa-search"></i> Search my bridge</button>
32+
<br /><br />
33+
<span id="ophom_researchbridgestatus" style="display: none;"></span>
34+
<br />
35+
<div id="ophom_bridgerefound" style="display: none;">
36+
<br />
37+
<button id="ophom_researchbridgesave" type="button" class="btn"><i class="fa fa-save"></i> Apply new IP</button>
38+
</div>
39+
</div>
40+
2741
<!-- ###### BRIDGE CONFIGURATION ###### -->
2842
<div id="ophom_bridgenotconfigured" style="display: none;">
2943
<h4>Bridge Configuration</h4>
@@ -48,8 +62,6 @@ Hue Bridge Status: <span id="ophom_generalstatus" class="badge badge-danger" sty
4862

4963
<br />
5064
<button id="ophom_validplugselection" class="btn"><i class="fa fa-save"></i> Save Configuration</button>
51-
52-
5365
</div>
5466

5567
<!-- #### AUTO OFF CONFIGURATION ####-->
@@ -260,6 +272,9 @@ Hue Bridge Status: <span id="ophom_generalstatus" class="badge badge-danger" sty
260272
else if (data['reponse'] == 2) {
261273
autOffConfiguration();
262274
}
275+
else if(data['reponse'] == 3) {
276+
refindBridge();
277+
}
263278
})
264279
});
265280
@@ -377,6 +392,60 @@ Hue Bridge Status: <span id="ophom_generalstatus" class="badge badge-danger" sty
377392
}
378393
});
379394
395+
function refindBridge()
396+
{
397+
general_status.innerHTML = "Bridge configured but unreachable";
398+
general_status.style.backgroundColor = "rgb(17, 99, 255)";
399+
document.getElementById("ophom_bridgenotlinked").style.display = "none";
400+
document.getElementById("ophom_bridgenotconfigured").style.display = "none";
401+
document.getElementById("ophom_bridgeunreachable").style.display = "";
402+
403+
document.getElementById("ophom_researchbridgebutton").addEventListener('click', function() {
404+
var search_button = this
405+
search_button.innerHTML = '<i class="fa fa-search"></i> Searching...';
406+
search_button.disabled = true;
407+
document.getElementById("ophom_researchbridgestatus").style.display = "none";
408+
document.getElementById("ophom_bridgerefound").style.display = "none";
409+
410+
fetch(API_BASEURL + "plugin/ophom?action=discover").then(function(response) {
411+
response.json().then(function(data) {
412+
console.log(data);
413+
if(data.length == 0)
414+
{
415+
search_button.innerHTML = '<i class="fa fa-search"></i> Search my bridge';
416+
search_button.disabled = false;
417+
document.getElementById("ophom_researchbridgestatus").style.display = "";
418+
document.getElementById("ophom_researchbridgestatus").innerHTML = "<font color='red'>Unable to find your Hue Bridge. Are you on the same network ?</font>";
419+
}
420+
else
421+
{
422+
bridge_ip_address = data[0].internalipaddress;
423+
search_button.innerHTML = '<i class="fa fa-search"></i> Search my bridge';
424+
search_button.disabled = false;
425+
document.getElementById("ophom_researchbridgestatus").style.display = "";
426+
document.getElementById("ophom_researchbridgestatus").innerHTML = "<font color='green'>Brige found (<i>"+ data[0].internalipaddress+"</i>) !</font>";
427+
document.getElementById("ophom_bridgerefound").style.display = "";
428+
}
429+
});
430+
});
431+
});
432+
433+
document.getElementById("ophom_researchbridgesave").addEventListener('click', function() {
434+
fetch(API_BASEURL + "plugin/ophom", {
435+
method: "POST",
436+
body : JSON.stringify({command: "changeip", ip: bridge_ip_address}),
437+
headers: { 'Content-Type': 'application/json'}
438+
}).then(function(response) {
439+
response.json().then(function(data) {
440+
if(data.reponse == "success")
441+
{
442+
document.location.reload(true);
443+
}
444+
});
445+
})
446+
})
447+
}
448+
380449
function autOffConfiguration()
381450
{
382451
general_status.innerHTML = "Linked and Configured";

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
plugin_name = "Ophom"
1515

1616
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
17-
plugin_version = "1.3.0"
17+
plugin_version = "1.3.1"
1818

1919
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
2020
# module

0 commit comments

Comments
 (0)