forked from PhilineGraystone/bdsm-trainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
punisher.py
executable file
·318 lines (280 loc) · 11.1 KB
/
punisher.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python3
# coding=utf-8
# Bibliotheken importieren
import sys
punisherdir = sys.path[0]
sys.path.append( punisherdir+'/libs' )
from lib_oled96 import ssd1306
from smbus import SMBus
from PIL import Image
from time import sleep
from collections import defaultdict
from random import seed
from random import random
from random import randint
from datetime import datetime
from urllib.request import urlopen
from torturedevices import torturedevices
from slaves import slaves
from rpi_rf import RFDevice
from punmqtt import PunMQTT
import logging
import configparser
#import paho.mqtt.client as paho
import RPi.GPIO as GPIO
import MFRC522
import signal
import lcddriver
import json
import random
import pymysql
import os
import time
import signal
stamp = dict()
realstamp = dict()
mqttdevices = dict()
tordevices = torturedevices()
slaves = slaves()
rfdevice = RFDevice( 27 )
rfsend = RFDevice( 17 )
rfdevice.enable_rx()
rfsend.enable_tx()
rfdevice.tx_repeat = 10
timestamp = None
old_slave_count = -1
old_device_count = -1
logger = logging.getLogger( 'Punisher' )
logger.setLevel( logging.DEBUG )
log = logging.FileHandler('logs/punisher.log')
log.setLevel( logging.DEBUG )
logger.addHandler( log )
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log.setFormatter( formatter )
logger.addHandler( log )
config = configparser.ConfigParser()
config.read('config/config.ini')
def cleanup():
lcd.lcd_clear()
oled.cls()
oled.display()
GPIO.cleanup()
db.close()
rfdevice.cleanup()
rfsend.cleanup()
def showLogo():
oled.cls()
oled.display()
draw.bitmap((32, 0), Image.open(punisherdir+'/pics/bdsm.png'), fill=1)
oled.display()
def showfunc( pic ):
oled.cls()
oled.display()
draw.bitmap((32, 0), Image.open(punisherdir+'/pics/'+pic), fill=1)
oled.display()
def showLCD(line1, line2):
lcd.lcd_clear()
lcd.lcd_display_string(line1, 1)
lcd.lcd_display_string(line2, 2)
def listToString(s):
str1 = ""
for ele in s:
str1 += str(ele)
return str1
def defaultLCD():
global tordevices, old_slave_count, old_device_count
if old_slave_count != slaves.count_slaves() or old_device_count != tordevices.count_devices():
if slaves.count_slaves() == 0:
showLCD('BDSM Trainer', str(tordevices.count_devices())+' Devices found')
else:
showLCD(str(slaves.count_slaves())+' Slave avail.', str(tordevices.count_devices())+' Devices found')
old_slave_count = slaves.count_slaves()
old_device_count = tordevices.count_devices()
def shock( rfid, mode ):
global stamp, realstamp, punmqtt
devices = slaves.get_device( rfid )
slave_id = slaves.get_id( rfid )
if realstamp[ rfid ] > 0 and realstamp[ rfid ] < int( datetime.timestamp( datetime.now()) ):
showLogo()
realstamp[ rfid ] = 0
for dev in devices:
if tordevices.support_function( dev['device'], 'tens' ):
device = tordevices.get_device( dev['device'] )
funcs = tordevices.get_functions( dev['device'], 'tens' )
for i in funcs:
if int(datetime.timestamp( datetime.now()) ) > stamp[ rfid ]:
value = random.randint(0, 30)
counter = random.randint(0, 30)
if value == 1:
mode = randint(1,6)
if mode == 1:
seconds = 10
elif mode == 2:
seconds = 30
elif mode == 3:
seconds = 60
elif mode == 4:
seconds = 90
elif mode == 5:
seconds = 120
elif mode == 6:
seconds = 160
stamp[ rfid ] = int(datetime.timestamp( datetime.now() )) + seconds + counter +random.randint(10, 120)
realstamp[ rfid ] = int(datetime.timestamp( datetime.now() )) + seconds + counter
if device['protocol'] == "MQTT":
punmqtt.publish('punisher/slave/'+str(slave_id)+'/shock', '{"seconds": '+str(seconds)+', "countdown": '+str(counter)+'}' )
if i['image'] != "":
showfunc( i['image'] )
def shock_punish( rfid, seconds ):
global stamp, realstamp
devices = slaves.get_device( rfid )
slave_id = slaves.get_id( rfid )
for dev in devices:
if tordevices.support_function( dev['device'], 'tens' ):
device = tordevices.get_device( dev['device'] )
funcs = tordevices.get_functions( dev['device'], 'tens' )
for i in funcs:
if device['protocol'] == "MQTT":
punmqtt.publish('punisher/slave/'+str(slave_id)+'/shock', '{"seconds": '+str(seconds)+', "countdown": '+str(counter)+'}' )
if i['image'] != "":
showfunc( i['image'] )
def blowjob( rfid, seconds ):
global stamp, realstamp
devices = slaves.get_device( rfid )
if realstamp[ rfid ] > 0 and realstamp[ rfid ] < int( datetime.timestamp( datetime.now()) ):
showLogo()
realstamp[ rfid ] = 0
for dev in devices:
if tordevices.support_function( dev['device'], 'blowjob' ):
device = tordevices.get_device( dev['device'] )
funcs = tordevices.get_functions( dev['device'], 'blowjob' )
for i in funcs:
if int(datetime.timestamp( datetime.now()) ) > stamp[ rfid ]:
seconds = int(seconds)
stamp[ rfid ] = int(datetime.timestamp( datetime.now() )) + seconds + random.randint(10, 120)
realstamp[ rfid ] = int(datetime.timestamp( datetime.now() )) + seconds
if device['protocol'] == "MQTT":
punmqtt.publish('punisher/functions/blowjob', str(seconds)+',10' )
if i['image'] != "":
showfunc( i['image'] )
def painplay( rfid ):
global stamp, slaves
modes = slaves.get_modes( rfid )
modes = modes.split(',')
for mode in modes:
func = mode.split('|')
if func[0] == "tens":
shock( rfid, func[1] )
def blowjobtraining( rfid ):
global stamp, slaves
modes = slaves.get_modes( rfid )
modes = modes.split(',')
for mode in modes:
func = mode.split('|')
if func[0] == "blowjob":
blowjob( rfid, func[1] )
def program():
global stamp
for slave in slaves.all_slaves():
if slaves.torture_possible( slave['rfid'] ):
if slave['program'] == "Painplay":
painplay( slave['rfid'] )
elif slave['program'] == "Petplay":
print('Start Petplay')
elif slave['program'] == "Condition":
print('Start Condition')
elif slave['program'] == "Blowjob":
blowjobtraining( slave['rfid'] )
else:
logger.error('Training program not found')
else:
logger.warning('Slave has no training program')
def rfid():
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
if status == MIFAREReader.MI_OK:
(status,uid) = MIFAREReader.MFRC522_Anticoll()
key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
try:
MIFAREReader.MFRC522_SelectTag(uid)
except:
log.warning('RFID has an error')
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
if status == MIFAREReader.MI_OK:
data = MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
slaveid = listToString(data[:9])
cursor.execute('SELECT * FROM slaves, program WHERE slave_rfid = "'+slaveid+'" AND pro_id = slave_program')
slave = cursor.fetchone()
if slave is None:
logger.info( 'Slave not found - RFID '+slaveid )
showLCD('Slave not found','')
sleep(2)
defaultLCD()
else:
if slaves.slave_online( slave[1] ):
logger.debug( "Slave '"+slave[2]+"' disconnected - RFID "+slaveid )
devices = slaves.get_device( slave[1] )
for dev in devices:
device = tordevices.get_device( dev['device'] )
if device['protocol'] == "MQTT":
punmqtt.publish("punisher/devices/"+dev['device']+"/settings", "{\"slave_id\": 0, \"slave_name\": \""+slave[2]+"\"} ");
slaves.remove_slave( slave[1] )
showLCD(slave[2], 'Slave disconnected')
sleep(2)
showLCD('Training program', 'disabled...')
sleep(2)
defaultLCD()
else:
logger.debug( "Slave '"+slave[2]+"' connected - RFID "+slaveid )
slaves.add_slave( slave[0], slave[1], slave[2], slave[3], slave[6], slave[7] )
stamp[ slave[1] ] = datetime.timestamp( datetime.now() )
realstamp[ slave[1] ] = datetime.timestamp( datetime.now() )
cursor.execute('SELECT * FROM devtoslave, devices WHERE dts_slaveid = "'+str(slave[0])+'" AND dev_id = dts_deviceid')
device = cursor.fetchall()
for dev in device:
if tordevices.device_online( dev[4] ):
slaves.add_device( slave[1], slave[2], dev[4] )
punmqtt.publish("punisher/devices/"+dev[4]+"/settings", "{\"slave_id\": "+str(slave[0])+", \"slave_name\": \""+slave[2]+"\"} ");
showLCD(slave[2], 'Slave connected')
sleep(2)
showLCD('Training program', slave[6]+' started...')
sleep(2)
defaultLCD()
deviceavail = defaultdict(list)
i2cbus = SMBus(1)
oled = ssd1306(i2cbus)
lcd = lcddriver.lcd()
draw = oled.canvas
MIFAREReader = MFRC522.MFRC522()
try:
db = pymysql.connect( config['database']['hostname'], config['database']['username'], config['database']['password'], config['database']['database'] )
except:
print("Error: MySQL connection failed, program start failed")
lcd.lcd_clear()
oled.cls()
oled.display()
GPIO.cleanup()
db.close()
rfdevice.cleanup()
rfsend.cleanup()
mqtt.loop.stop()
sys.exit(1)
cursor = db.cursor()
punmqtt = PunMQTT('Punisher')
punmqtt.run( config, logger, tordevices, cursor )
cursor.execute('SELECT * FROM devices, torfunctions WHERE tf_devid = dev_id')
devices = cursor.fetchall()
for device in devices:
tordevices.add_device( device[1], device[2], device[3], False )
tordevices.add_functions( device[1], device[6], device[7], device[8], device[9] )
timestamp = None
showLogo()
defaultLCD()
try:
while True:
rfid()
defaultLCD();
if slaves.count_slaves() > 0 and tordevices.count_devices() > 0:
program()
except KeyboardInterrupt:
cleanup()