summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-config/mv_ir.py
blob: f0863c4319dff6df00bd56591e94c3a5ef076fd2 (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- coding: utf-8 -*-
import logging, mv_common
import os, re , glob
from time import time, localtime, strftime
import time
import pwd

def setup_lirc_links(mythhome):
    logging.debug("    Creating links for lirc")
    try:
        mythuid = pwd.getpwnam('mythtv')[2]
        mythgid = pwd.getpwnam('mythtv')[3]
    except:
        logging.critical("*    mythuid not found")
        mythuid = '1000'
        mythgid = '1000'
    if not os.path.exists(mythhome+"/.mythtv"):
        logging.debug("    Creating %s/.mythtv",mythhome)
        try:
            os.mkdir(mythhome+"/.mythtv")
        except:
            logging.debug("   Couldn't create .mythtv ")
            return
        try:
            os.chown(mythhome+"/.mythtv", mythuid, mythgid)
            logging.debug("*    Couldn't chown of %s", mythhome)
        except:
            cmd = ''' chown -R mythtv %s/.mythtv''' %mythhome
            mv_common.runcmd(cmd)
            cmd = ''' chgrp -R mythtv %s/.mythtv''' %mythhome
            mv_common.runcmd(cmd)
            pass

    if os.path.exists("/etc/lircrc"):
        if not os.path.exists(mythhome+"/.mythtv/lircrc"):
            logging.debug("    Creating symlink for myth lircrc")
            os.symlink("/etc/lircrc",mythhome+"/.mythtv/lircrc")
        else:
            logging.debug("    %s/lircrc already present",mythhome)

        if not os.path.exists(mythhome+"/.lircrc"):
            logging.debug("    Creating symlink for lircrc")
            os.symlink("/etc/lircrc",mythhome+"/.lircrc")
        else:
            logging.debug("    %s/.lircrc already present",mythhome)
    else:
        logging.debug("    /etc/lircrc does not exist, can not link it")

def scrub_modules(filename,module):
    logging.debug("    scrubbing %s from %s",module,filename)
    cmd = '''sed -i -e '/.*#%s/d' %s''' %(module,filename)
    mv_common.runcmd(cmd)

def add_module(filename,module):
    logging.debug("    adding  %s to %s",module,filename)
    cmd = ''' echo "%s" >> %s ''' %(module,filename)
    mv_common.runcmd(cmd)

def unload_all_lirc():
    logging.debug("    Unloading all remote modules")
    cmd = "sv stop remotes"
    mv_common.runcmd(cmd)
    cmd = "killall -9 lircd"
    mv_common.runcmd(cmd)
    cmd = '''modprobe -r $(lsmod |grep ^lirc |awk '{print $1}') '''
    mv_common.runcmd(cmd)



def include_file( incfile,filename):
    logging.debug("     including %s within %s",filename,incfile)
    try:
        f = open( incfile, 'a')
    except:
        logging.debug("    Couldn't open %s for writing",incfile)
        return
    line = '''include "%s" ''' %filename
    logging.debug("   %s",line)
    f.write(line)
    f.write("\n")
    f.close()

def setup_ir_remote(Remotetype,templates,mythhome,HostBlasterType,remotebucket):
    logging.debug("    Setup of remote type %s",Remotetype)
    if Remotetype == "no_remote" and HostBlasterType == "None" :
        mv_common.remove_service("remotes")
    elif Remotetype == "tinker":
        pass #do nothing
    else:
        currenttime = strftime("%b-%d-%Y-%H:%M:%S", localtime())
        cmd = "mv /etc/lircd.conf /etc/lircd.conf-%s" %currenttime
        mv_common.runcmd(cmd)
        cmd = "mv /etc/lircrc /etc/lircrc-%s" %currenttime
        mv_common.runcmd(cmd)
        cmd = "mv /etc/remote.run /etc/remote.run-%s" %currenttime
        mv_common.runcmd(cmd)
        if remotebucket == "user":
            remote_path = "%s/remotes/%s"  %(mythhome,Remotetype)
        else:
            remote_path="%s/remotes/%s/%s" %(templates,remotebucket,Remotetype)

        files =  remote_path + "/lircd*"
        logging.debug("    Using %s",files)
        for filename in glob.glob(files):
            # Ignore subfolders
            if os.path.isdir (filename):
                logging.debug("    %s is a dir, skipping",filename)
                continue
            include_file("/etc/lircd.conf",filename)
        #files = templates + "/remotes/" + Remotetype + "/lircrc*"
        files = remote_path + "/lircrc*"
        logging.debug("    Using %s",files)
        for filename in glob.glob(files):
            # Ignore subfolders
            if os.path.isdir (filename):
                logging.debug("    %s is a dir, skipping",filename)
                continue
            include_file("/etc/lircrc",filename)
        files = remote_path + "/remote.run*"
        logging.debug("    Using %s",files)
        for filename in glob.glob(files):
            # Ignore subfolders
            if os.path.isdir (filename):
                logging.debug("    %s is a dir, skipping",filename)
                continue
            cmd = "cp %s /etc/remote.run" %filename
            mv_common.runcmd(cmd)


        cmd =  "chmod 755 /etc/lircrc /etc/remote.run"
        mv_common.runcmd(cmd)
        mv_common.add_service("remotes")


def setup_ir_receiver(ReceiverType,TEMPLATES,HDHRlirc_device,myip):
    logging.info("    Configuring receiver %s", ReceiverType)
    if ReceiverType == "Serial":
        scrub_modules("/etc/modules.mythvantage","lirc")
        add_module("/etc/modules.mythvantage","lirc_serial    #lirc")
        cmd = "load-modules-mythvantage.sh"
        mv_common.runcmd(cmd)
        mv_common.remove_service("igdaemon")
        mv_common.remove_service("tatir")

    elif ReceiverType == "Usb-imon":
        scrub_modules("/etc/modules.mythvantage","lirc")
        add_module("/etc/modules.mythvantage","lirc_imon    #lirc")
        cmd = "load-modules-mythvantage.sh"
        mv_common.runcmd(cmd)
        mv_common.remove_service("igdaemon")
        mv_common.remove_service("tatir")

    elif ReceiverType == "Hauppauge":
        scrub_modules("/etc/modules.mythvantage","lirc")
        add_module("/etc/modules.mythvantage","lirc_i2c    #lirc")
        mv_common.cp_and_log(TEMPLATES+"/modules/lirc_i2c.conf" ,
                             "/etc/modprobe.d/lirc_i2c.conf")
        cmd = "rmmod lirc_i2c"
        mv_common.runcmd(cmd)
        cmd = "load-modules-mythvantage.sh"
        mv_common.runcmd(cmd)
        mv_common.remove_service("igdaemon")
        mv_common.remove_service("tatir")
    elif ReceiverType == "pvr150":
        scrub_modules("/etc/modules.mythvantage","lirc")
        add_module("/etc/modules.mythvantage","lirc_i2c    #lirc")
        mv_common.cp_and_log(TEMPLATES+"/modules/lirc_i2c.conf" ,
                             "/etc/modprobe.d/lirc_i2c.conf")
        cmd = "rmmod lirc_i2c"
        mv_common.runcmd(cmd)
        cmd = "load-modules-mythvantage.sh"
        mv_common.runcmd(cmd)
        mv_common.remove_service("igdaemon")
        mv_common.remove_service("tatir")

    elif ReceiverType == "iguanaIR-usb":
        scrub_modules("/etc/modules.mythvantage","lirc")
        mv_common.add_service("igdaemon")
        mv_common.remove_service("tatir")

    elif ReceiverType == "tatir":
        scrub_modules("/etc/modules.mythvantage","lirc")
        mv_common.add_service("tatir")
        mv_common.remove_service("igdaemon")
    elif ReceiverType == "SiliconDust_HDHR":
        mv_common.remove_service("igdaemon")
        mv_common.remove_service("tatir")
        cmd = '''hdhomerun_config %s set /ir/target %s:5000 store''' %(HDHRlirc_device,myip)
        print cmd
        mv_common.runcmd(cmd)

    else:
        logging.debug("    didn't match receiver, using genric case")
        scrub_modules("/etc/modules.mythvantage","lirc")
        mv_common.remove_service("igdaemon")
        mv_common.remove_service("tatir")

def setup_blaster_proto(HostBlasterType,Hostnumblaster,TEMPLATES,systemconfig):
    logging.debug("    Start of setup_blaster_proto")
    logging.debug("    HostBlasterType: %s", HostBlasterType)
    logging.debug("    Hostnumblaster: %s" , Hostnumblaster)
    if not HostBlasterType == "pvr150" and not HostBlasterType == "None":
        for i in range(int(Hostnumblaster)):
            try:
                proto = systemconfig["HostTransmitproto_"+str(i+1)]
                logging.debug("    proto: %s",proto)
            except:
               logging.info("    unkown proto for %s",i+1)
               continue
            template=TEMPLATES+"/transmit/%s/lircd.conf" %proto
            if os.path.exists(template):
                logging.debug("    include blaster template: %s",proto)
                include_file("/etc/lircd.conf",template)
            else:
                logging.debug("    template: %s is not present",template)



def setup_blaster_transmiter(HostBlasterType):
    logging.debug("    Start of setup_blaster_transmiter")
    logging.info("    HostBlasterType : %s", HostBlasterType)

    if  HostBlasterType == "Receiver":
        logging.info("    Blaster is receiver, skipping module config for Blaster")
        return

    if HostBlasterType == "no blaster":
        scrub_modules("/etc/modules.mythvantage","blaster")
    elif HostBlasterType == "Serial":
        scrub_modules("/etc/modules.mythvantage","blaster")
        add_module("/etc/modules/.mythvantage","lirc_serial    #blaster")
        cmd = "load-modules-mythvantage.sh"
        mv_common.runcmd(cmd)
    elif HostBlasterType == "pvr150":
        scrub_modules("/etc/modules.mythvantage", "blaster")
        scrub_modules("/etc/modules.mythvantage", "lirc")
        add_module("/etc/modules.mythvantage", "lirc_pvr150    #blaster")
        add_module("/etc/modules.mythvantage", "lirc_pvr150    #lirc")
        cmd = "rmmod lirc_i2c"
        mv_common.runcmd(cmd)
        cmd = "load-modules-mythvantage.sh"
        mv_common.runcmd(cmd)
    else:
        scrub_modules("/etc/modules.mythvantage","blaster")

def setup_lcd(LCDtype, TEMPLATES, Remotetype):
    logging.debug("____Start of setup_lcd____")
    logging.debug("    LCDType: %s", LCDtype)
    logging.debug("    Remotetype: %s", Remotetype)
    if LCDtype == "tinker":
        logging.info("   tinker mode detected")
        return

    if LCDtype == "no_lcd":
        logging.info("    Disabling LCD support")
        cmd = "sv stop lcdd"
        mv_common.runcmd(cmd)
        #sometimes it doesn't want to die
        cmd = "killall -9 LCDd"
        mv_common.runcmd(cmd)
        cmd = "load-modules-mythvantage.sh  UNLOAD LCD"
        mv_common.runcmd(cmd)
        scrub_modules("/etc/modules.mythvantage", "LCD")
        mv_common.remove_service("lcdd")
    else:
        lcdmodule="%s/LCD/%s/modules" %(TEMPLATES,LCDtype)
        if os.path.exists(lcdmodule):
            logging.debug("    Adding module for LCD")
            scrub_modules("/etc/modules.mythvantage", "LCD")
            cmd = "cat %s >> /etc/modules.mythvantage" %lcdmodule
            mv_common.runcmd(cmd)
        if Remotetype != "tinker":
            lcdconf = "%s/LCD/%s/LCDd.conf" %(TEMPLATES,LCDtype)
            lcdlirc = "%s/LCD/%s" %(TEMPLATES,LCDtype)
            mv_common.pacinstall("lcdproc")
            if os.path.exists(lcdconf):
                logging.debug("    Copying in lcdconf template file: %s",lcdconf)
                mv_common.cp_and_log(lcdconf, "/etc/LCDd.conf")
            else:
                logging.debug("    %s is not present, will not copy it")

            if os.path.exists(lcdlirc+"/lircrc"):
                logging.debug("    LCD seems to support lirc")
                #setup the lirc aspect of the lcd
                #copied from setup_ir_remote
                files = lcdlirc+"/lircd*"
                logging.debug("    Using %s",files)
                for filename in glob.glob(files):
                    # Ignore subfolders
                    if os.path.isdir (filename):
                        logging.debug("    %s is a dir, skipping",filename)
                        continue
                    include_file("/etc/lircd.conf",filename)
                files = lcdlirc+"/lircrc*"
                logging.debug("    Using %s",files)
                for filename in glob.glob(files):
                    # Ignore subfolders
                    if os.path.isdir (filename):
                        logging.debug("    %s is a dir, skipping",filename)
                        continue
                    include_file("/etc/lircrc",filename)
                cmd =  "chmod 755 /etc/lircrc"
                mv_common.runcmd(cmd)

            mv_common.add_service("lcdd")
            mv_common.restart_service("lcdd")
    logging.debug("__End of setup_lcd")

def setup_ir(systemconfig, data_config):
    rc = [ False , False ]
    if  mv_common.read_config(mv_common.module_config,"ir")  == False  :
        logging.info("____Skipping of IR, config disabled____")
        return rc

    logging.info("____Start of IR____")
    if systemconfig.get("Remotetype") == "tinker":
        logging.info("    Tinker mode detected")
    else:
        if not systemconfig.get("HostBlasterType"):
            HostBlasterType = "unknown"
            Hostnumblaster = "0"
        else:
            HostBlasterType = systemconfig.get("HostBlasterType")
            Hostnumblaster  = systemconfig.get("Hostnumblaster")

        if systemconfig.get("HostBlasterType") == "pvr150":
            remotetype = "hauppauge-blaster"
            logging.debug("    pvr150 blaster selected, setting remote to hauppauge-blaster")
        else:
            remotetype = systemconfig.get("Remotetype")

        setup_ir_remote(remotetype,systemconfig.get("TEMPLATES"),
                            data_config.MYTHHOME,
                            systemconfig.get("HostBlasterType"),
                            systemconfig.get("Remotebucket"))
        setup_lirc_links(data_config.MYTHHOME)

        if systemconfig.get("ReceiverType"):
            setup_ir_receiver(systemconfig.get("ReceiverType"),
                              systemconfig.get("TEMPLATES"),
                              systemconfig.get("HDHRlirc_device"),
                              systemconfig.get("mythip"))
        else:
            setup_ir_receiver("unknown", systemconfig.get("TEMPLATES"),'','127.0.0.1')
        unload_all_lirc()
        setup_blaster_proto(HostBlasterType,
                            Hostnumblaster,
                            systemconfig.get("TEMPLATES"),
                            systemconfig)
        setup_blaster_transmiter(HostBlasterType)
        cmd = "load-modules-mythvantage.sh"
        mv_common.runcmd(cmd)
        time.sleep(2)
        mv_common.udev_trigger()
        cmd="sv start remotes"
        mv_common.runcmd(cmd)
        rc = [ True , True ]
    if systemconfig.get("LCDtype"):
        setup_lcd(systemconfig.get("LCDtype"),
                  systemconfig.get("TEMPLATES"),
                  systemconfig.get("Remotetype"))
    else:
        logging.debug("    LCD not defined")
    logging.info("__End IR\n")
    return rc