본문 바로가기

3D Printer/OctoPrint

OctoPrint Plugin 만들기 #6 (Setting UI 추가)

옥토 프린트 플러그인 만들기 (챔버 매니저)


□ Setting UI 추가




□ Source Code


[__init__.py]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    def connectToDevice(self, port, baudrate, timeout) :
        try:
            self.device.port = port
            self.device.baudrate = baudrate
            self.device.timeout = timeout
            self.device.open()
            self.device.flush()
            msg = 'CMD O0 R0\n'
            self.device.writelines(msg.encode('ascii','ignore'))
            
            self._logger.info("Connected to device. [port="+port+" baudrate="+str(baudrate)+"]")
        except SerialException as e:
            self._logger.error("Failed to open serial port")
        
 
    # ~~ Blueprintplugin mixin
    @octoprint.plugin.BlueprintPlugin.route("/getConnectionStatus", methods=["GET"])
    def getConnectionStatus(self):
        return flask.Response(json.dumps(self.device.is_open), mimetype='application/json')
 
 
    @octoprint.plugin.BlueprintPlugin.route("/setConnectionStatus", methods=["GET"])
    def setConnectionStatus(self):
        try:
            serial_cmd = flask.request.values["cmd"]
            serial_port = None
            serial_baudrate = None
            serial_timeout = None
            
            if serial_cmd.upper()=='CONNECTED':
                serial_port = flask.request.values["port"]
                serial_baudrate = flask.request.values["baudrate"]
                serial_timeout = flask.request.values["timeout"]
                
                self._logger.debug('Try connect to device');
 
                if self.device.is_open == True :
                    self.device.close()
                self.connectToDevice(serial_port, Decimal(serial_baudrate), float(serial_timeout))
            elif serial_cmd.upper()=='DISCONNECTED':
                self._logger.debug('Try disconnect from device')
                if self.device.is_open == True :
                    self.device.close()
        except SerialException as e:
            self._logger.error(e)
        except Exception as e:
            self._logger.error(e)
            
        return flask.Response(json.dumps(self.device.is_open), mimetype='application/json')
cs



[chambermanager_setting.jinja2]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<fieldset>
    <h4>{{ _('Serial Port') }}</h4>
    <table>
        <tr>
            <td width="200" valign="center">
                <label class="control-label" for="settings-cm_serialPort">{{ _('Serial Port') }}</label>
            </td>
            <td width="400">
                <select id="settings-cm_serialPort" data-bind="options: serial_portOptions, value: serial_port"></select>
            </td>
        </tr>
        <tr>
            <td valign="center">
                <label class="control-label" for="settings-cm_baudrate">{{ _('Baudrate') }}</label>
            </td>
            <td>
                <select id="settings-cm_baudrate" data-bind="options: serial_baudrateOptions, value: serial_baudrate"></select>
            </td>
        </tr>
        <tr>
            <td valign="center">
                <label class="control-label" for="settings-cm_timeout">{{ _('Timeout(ms)') }}</label>
            </td>
            <td>
                <input id="settings-cm_timeout" type="number" min="0" max="3000" class="input-mini input-nospin" style="width: 100px" data-bind="value: serial_timeout_ms">
            </td>
        </tr>
        <tr>
            <td valign="center">
                <label class="control-label" for="settings-cm_connection">{{ _('Connection Status') }}</label>
            </td>
            <td>
                <div class="serial_connectionOptions">
                    <div class="btn-group" data-toggle="buttons-radio" id="serial_connectionStatus">
                        <!-- ko foreach: serial_connectionOptions -->
                        <button type="button" class="btn distance" style="width: 130px;padding: 3px 0;height: 30px" data-bind="text: $data, click: function() { $root.changeConnectionStatus($data) }, css: { active: $root.serial_connectionStatus() === $data }, attr: { id: 'serial_connectionStatus_' + $data }"></button>
                        <!-- /ko -->
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="checkbox" data-bind="checked: serial_autoconnect" id="settings-serialAutoconnect"> {{ _('Auto-connect to printer on server start') }}
            </td>
        </tr>
    </table>
</fieldset>
 
<br>
<fieldset">
    <h4>{{ _('Doors') }}</h4>
    <table>
        <tr>
            <th width="200"> </th>
            <th width="200" align="left">Max Position</th>
            <th width="200" align="left">Min Position</th>
        </tr>
    </table>
    <table>
        <tr>
            <td width="200"><label class="control-label" for="settings-cm_tempControlTarget">{{ _('Ventilator') }}</label></td>
            <td width="200" align="left">
                <input id="settings-cm_doorVentilMaxPos" type="number" min="0" max="255" class="input-mini input-nospin" style="width: 100px" data-bind="value: doorVentilMaxPos">
            </td>
            <td width="200" align="left">
                <input id="settings-cm_doorVentilMinPos" type="number" min="0" max="255" class="input-mini input-nospin" style="width: 100px" data-bind="value: doorVentilMinPos">
            </td>
        </tr>
        <tr>
            <td width="200"><label class="control-label" for="settings-cm_tempControlActual">{{ _('Filter') }}</label></td>
            <td width="200" align="left">
                <input id="settings-cm_doorFilterMaxPos" type="number" min="0" max="255" class="input-mini input-nospin" style="width: 100px" data-bind="value: doorFilterMaxPos">
            </td>
            <td width="200" align="left">
                <input id="settings-cm_doorFilterMinPos" type="number" min="0" max="255" class="input-mini input-nospin" style="width: 100px" data-bind="value: doorFilterMinPos">
            </td>
        </tr>
    </table>
</fieldset>
<br>
<fieldset>
    <h4>{{ _('Lights') }}</h4>
    <table>
        <tr>
            <td width="200">
                <label class="control-label" for="settings-cm_sensitivity">{{ _('Sensitivity') }}</label>
            </td>
            <td width="400">
                <input id="settings-cm_sensitivity" type="number" min="0" max="100" class="input-mini input-nospin" style="width: 100px" data-bind="value: lightControlSensitive">
            </td>
        </tr>
    </table>
</fieldset>
cs



[chambermanager.js]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        self.changeConnectionStatus = function(mode) {
            if(self.serial_connectionStatus()!=mode) {
                self.setSerialConnection(mode)
            }
        }
 
 
        self.onBeforeBinding = function() {
 
            self.bindFromSettings();
            self.getSerialConnectionStatus();
        }
 
 
        self.getSerialConnectionStatus = function(value) {
            var request = { value };
 
            $.ajax({
                url: self.buildPluginUrl("/getConnectionStatus"),
                type: "GET",
                dataType: "json",
                data: request,
                success: function(data) {
                    if (data == true) {
                        self.serial_connectionStatus('Connected');
                    } else {
                        self.serial_connectionStatus('Disconnected');
                    }
                },
                error: function(textStatus, errorThrown) {
                    new PNotify({
                        title: "Chamber Manager",
                        text: textStatus,
                        type: "error"
                    });
                }
            });
        }
 
        self.setSerialConnection = function(cmd) {
            var port = self.serial_port();
            var baudrate = self.serial_baudrate();
            var timeout = self.serial_timeout();
 
            var request = { cmd, port, baudrate, timeout};
 
            $.ajax({
                url: self.buildPluginUrl("/setConnectionStatus"),
                type: "GET",
                dataType: "json",
                data: request,
                success: function(data) {
                    if (data == true) {
                        self.serial_connectionStatus('Connected');
                    } else {
                        self.serial_connectionStatus('Disconnected');
                    }
                },
                error: function(textStatus, errorThrown) {
                    new PNotify({
                        title: "Chamber Manager",
                        text: textStatus,
                        type: "error"
                    });
                }
            });
        }