| 1 | import clr |
|---|
| 2 | clr.AddReference('System.Drawing') |
|---|
| 3 | clr.AddReference('System.Windows.Forms') |
|---|
| 4 | from System.Drawing import * |
|---|
| 5 | from System.Windows.Forms import * |
|---|
| 6 | from System.Net import * |
|---|
| 7 | from System.Net.Sockets import * |
|---|
| 8 | from System.Text import * |
|---|
| 9 | from System import Array |
|---|
| 10 | import System |
|---|
| 11 | |
|---|
| 12 | import string |
|---|
| 13 | |
|---|
| 14 | class IronRadioController: |
|---|
| 15 | def __init__(self, title='IronRadio', debug=False): |
|---|
| 16 | self._telnet = IronTelnet('localhost', 1234, debug) |
|---|
| 17 | |
|---|
| 18 | self._base_form = Form(Text = title, Width = 450, Height = 250) |
|---|
| 19 | self._label = Label(Text = 'IronRadio Controller') |
|---|
| 20 | self._uptime_label = Label() |
|---|
| 21 | self._button = Button(Text = 'Skip to next song') |
|---|
| 22 | self._timer = Timer() |
|---|
| 23 | |
|---|
| 24 | # Setup our timer for updating the uptime |
|---|
| 25 | self._timer.Interval = 10000 |
|---|
| 26 | self._timer.Tick += self.uptimeEvent |
|---|
| 27 | self._timer.Start() |
|---|
| 28 | |
|---|
| 29 | # Setup our header label |
|---|
| 30 | self._label.Width = 300 |
|---|
| 31 | self._label.Height = 55 |
|---|
| 32 | self._label.TextAlign = ContentAlignment.MiddleCenter |
|---|
| 33 | #self._label.BorderStyle = BorderStyle.FixedSingle |
|---|
| 34 | self._label.BackColor = Color.FromArgb(0,0,0,0); |
|---|
| 35 | self._label.Font = Font('Verdana', 18) |
|---|
| 36 | self._label.Dock = DockStyle.Top |
|---|
| 37 | self._base_form.Controls.Add(self._label) |
|---|
| 38 | |
|---|
| 39 | self._button.Height = 45 |
|---|
| 40 | self._button.Dock = DockStyle.Bottom |
|---|
| 41 | self._base_form.Controls.Add(self._button) |
|---|
| 42 | |
|---|
| 43 | self._uptime_label.Width = 300 |
|---|
| 44 | self._uptime_label.Font = Font('Verdana', 14) |
|---|
| 45 | self._uptime_label.BackColor = Color.FromArgb(0,0,0,0); |
|---|
| 46 | self._uptime_label.Location = Point(10, 100) |
|---|
| 47 | self._base_form.Controls.Add(self._uptime_label) |
|---|
| 48 | |
|---|
| 49 | self._base_form.Show() |
|---|
| 50 | self._base_form.Click += self.clkEvent |
|---|
| 51 | self._button.Click += self.skipButtonEvent |
|---|
| 52 | |
|---|
| 53 | # Now that the form has been shown, let's connect to telnet |
|---|
| 54 | self._telnet.connect() |
|---|
| 55 | self._update_uptime() |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | def _update_uptime(self): |
|---|
| 59 | self._telnet.write('uptime\r\n') |
|---|
| 60 | uptime = self._telnet.read() |
|---|
| 61 | # The uptimei will come back in the format: |
|---|
| 62 | # ['5d 14h 22m 27s', 'END', ''] |
|---|
| 63 | if len(uptime) > 0: |
|---|
| 64 | uptime = string.split(uptime, '\n') |
|---|
| 65 | uptime = uptime[0] |
|---|
| 66 | self._uptime_label.Text = 'Radio Uptime: %s' % uptime |
|---|
| 67 | |
|---|
| 68 | def uptimeEvent(self, *a): |
|---|
| 69 | self._update_uptime() |
|---|
| 70 | |
|---|
| 71 | def skipButtonEvent(self, *a): |
|---|
| 72 | self._telnet.write('radio.skip\r\n') |
|---|
| 73 | print self._telnet.read() |
|---|
| 74 | |
|---|
| 75 | def clkEvent(self, *a): |
|---|
| 76 | print repr(a) |
|---|
| 77 | |
|---|
| 78 | def closeEvent(self, *a): |
|---|
| 79 | Application.Stop() |
|---|
| 80 | |
|---|
| 81 | ##################################### |
|---|
| 82 | # IronTelnet related code |
|---|
| 83 | ##################################### |
|---|
| 84 | class IronTelnetException(Exception): |
|---|
| 85 | pass |
|---|
| 86 | |
|---|
| 87 | class IronTelnet: |
|---|
| 88 | def __init__(self, host='localhost', port=23, debug=False): |
|---|
| 89 | self._debug = debug |
|---|
| 90 | self._host = host |
|---|
| 91 | self._address = '' |
|---|
| 92 | self._port = port |
|---|
| 93 | self._blocks = True |
|---|
| 94 | self._socket = Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) |
|---|
| 95 | |
|---|
| 96 | hostentry = Dns.GetHostByName(self._host) |
|---|
| 97 | for address in hostentry.AddressList: |
|---|
| 98 | self._address = address |
|---|
| 99 | break |
|---|
| 100 | |
|---|
| 101 | def __del__(self): |
|---|
| 102 | if self._socket.Connected: |
|---|
| 103 | self._socket.Close() |
|---|
| 104 | |
|---|
| 105 | def connect(self): |
|---|
| 106 | if not self._socket.Connected: |
|---|
| 107 | if self._debug: |
|---|
| 108 | print 'Connecting to %s' % self._address |
|---|
| 109 | |
|---|
| 110 | endpoint = IPEndPoint(self._address, self._port) |
|---|
| 111 | self._socket.Connect(endpoint) |
|---|
| 112 | |
|---|
| 113 | if self._debug: |
|---|
| 114 | if self._socket.Connected: |
|---|
| 115 | print 'Socket connected to %s' % self._address |
|---|
| 116 | else: |
|---|
| 117 | print 'Socket not connected to %s' % self._address |
|---|
| 118 | |
|---|
| 119 | def write(self, command): |
|---|
| 120 | if not self._socket.Connected: |
|---|
| 121 | raise IronTelnetException('Telnet session not connected!') |
|---|
| 122 | |
|---|
| 123 | encoding = ASCIIEncoding() |
|---|
| 124 | |
|---|
| 125 | rc = self._socket.Send(encoding.GetBytes(command)) |
|---|
| 126 | |
|---|
| 127 | if rc > 0 and self._debug: |
|---|
| 128 | print 'Successfully sent %d bytes' % rc |
|---|
| 129 | |
|---|
| 130 | return rc |
|---|
| 131 | |
|---|
| 132 | def read(self): |
|---|
| 133 | if not self._socket.Connected: |
|---|
| 134 | raise IronTelnetException('Telnet session not connected!') |
|---|
| 135 | |
|---|
| 136 | encoding = ASCIIEncoding() |
|---|
| 137 | bytes = Array.CreateInstance(System.Byte, self._socket.Available) |
|---|
| 138 | rc = self._socket.Receive(bytes) |
|---|
| 139 | |
|---|
| 140 | if rc and self._debug: |
|---|
| 141 | print 'Successfully received %d bytes' % rc |
|---|
| 142 | |
|---|
| 143 | return encoding.GetString(bytes) |
|---|
| 144 | |
|---|
| 145 | if __name__ == '__main__': |
|---|
| 146 | form = IronRadioController() |
|---|
| 147 | Application.Run(form._base_form) |
|---|