forked from LeoJr2015/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroid-server1.py
More file actions
131 lines (116 loc) · 3.71 KB
/
Copy pathandroid-server1.py
File metadata and controls
131 lines (116 loc) · 3.71 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# untitled.py
#
# Copyright 2012 Scott Thomson <scotty3785@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import socket
import select
import time
import pynotify
from SerialComms import *
UDP_IP=""
UDP_PORT=51234
UDP_PORT2 = 54321
sock = socket.socket( socket.AF_INET,socket.SOCK_DGRAM )
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((UDP_IP,UDP_PORT2))
sock.settimeout(0.0)
##data format
## command_type, param1, param2 .... paramN
## Supported Command Examples
## lights, 1, on
## message, from, message_text
def init_logging():
import logging
global logger
logger = logging.getLogger('android_server')
hdlr = logging.FileHandler('android_server.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
def notification(title,message):
if pynotify.init("icon-summary-body"):
n = pynotify.Notification(title,message,"notification-message-im").show()
def process_command(message):
command = message['data'].split(",")
#print command
if command[0] == 'lights':
## Recieved command to control lights
#print "Controlling Lights"
light_num = command[1]
light_state = command[2]
logger.info('Controlling Lights %s,%s' % (light_num,light_state))
message = "Turning Light %s %s" % (light_num,light_state)
notification("Light Control",message)
### Send Command to Mains Power Controller ###
set_light = int(light_num)
if light_state == 'on':
set_state = 1
else:
set_state = 0
ports[0].write(struct.pack('BBBBB', 0xFE,2,1,set_light,set_state))
elif command[0] == 'message':
## Recieved Text Message
sender = command[1]
message_text = command[2]
logger.info("Text Message: %s - %s" % (sender,message_text))
notification(sender,message_text)
elif command[0] == 'logon':
reply_addr = message['addr']
sock.sendto("Welcome",(reply_addr,UDP_PORT2))
print "New Client"
else:
print command[0]
def init_serialcomms():
global ports
global protocol
ports = []
try:
ports.append(serial.Serial("/dev/rfcomm1",9600,timeout=1))
except:
print "Home Automation Server - Bluetooth Device Not Available"
quit()
protocol = SerialComms()
def start_server():
print "Home Automation Server - Listening..."
while True:
readable,writable,errs = select.select([sock],[],[],0.1)
##print len(readable)
for item in readable:
if item is sock:
data,(addr,port) = sock.recvfrom(1024)
message = {'data':data,'addr':addr,'port':port}
#print "Data Processing"
#print (addr,port), data
process_command(message)
time.sleep(0.1)
if __name__ == '__main__':
init_logging()
#init_serialcomms()
sock.sendto("logon",("localhost",UDP_PORT))
start_server()
#data = "lights,1,on"
#message = {'data':data,'addr':"192.168.1.5",'port':51234}
#process_command(message)
#data = "message,Test User,Hey! I just met you. This is crazy. Call me maybe x"
#message = {'data':data,'addr':"192.168.1.5",'port':51234}
#process_command(message)