We spent in the office a second line of the Internet. Since the main one (further I will call it first) is good in speed, but it is limited by traffic. The second is a bit slower, but unlimited. During the day, the second line is almost free and gives a good speed, so it was chosen as the main one for the working day. By evening, the speed drops dramatically due to the load on the channel and you have to switch to the first one. This is not always the case, but often enough.



uftdi_load="YES" /* * net_switch.c * * Created: 09.09.2014 10:07:41 * Author: exp131 */ #define F_CPU 1000000UL // 1, #define BAUD 2400 // UART, 2400 #define MYUBRR F_CPU/16/BAUD-1 #include <avr/io.h> #include <avr/interrupt.h> // , void ReportStatus(); // ISR(TIMER0_OVF_vect) { ReportStatus(); // } // 0 1, UART void ReportStatus() { cli(); // unsigned char a; if((PINC & (1<<PINC0)) && (!(PINC & (1<<PINC1)))) // 0 . 1 1 0, a = 'A'; // else a = 'B'; // , // while(!(UCSRA & (1<<UDRE))); UDR = a; sei(); // } // void init(void) { // UART, 2400 UCSRB = (1<<TXEN); unsigned int ubrr = MYUBRR; UBRRH = (unsigned char)(ubrr >> 8); UBRRL = (unsigned char)ubrr; // , . TCCR0 = (1<<CS02)|(1<<CS00); TIMSK = (1<<TOIE0); sei(); } int main(void) { // init(); while(1) // { } } #!/usr/bin/env python import sys, os, time, atexit from signal import SIGTERM class Daemon: def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): self.stdin = stdin self.stdout = stdout self.stderr = stderr self.pidfile = pidfile def demonize(self): try: pid = os.fork() if pid > 0: sys.exit(0) except OSError, e: sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)) sys.exit(1) os.chdir("/") os.setsid() os.umask(0) sys.stdout.flush() sys.stderr.flush() si = file(self.stdin, 'r') so = file(self.stdout, 'a+') se = file(self.stderr, 'a+', 0) os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) atexit.register(self.delpid) pid = str(os.getpid()) file(self.pidfile, 'w+').write("%s\n" % pid) def delpid(self): os.remove(self.pidfile) def start(self): try: pf = file(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if pid: message = "Pidfile %s already exists. Deamon already running?\n" sys.stderr.write(message % self.pidfile) sys.exit(1) self.demonize() self.run() def stop(self): try: pf = file(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if not pid: message = "Pidfile %s does not exists. Daemon is not running?\n" sys.stderr.write(message % self.pidfile) return try: while 1: os.kill(pid, SIGTERM) time.sleep(0.1) except OSError, err: err = str(err) if err.find("No such process") > 0: if os.path.exists(self.pidfile): os.remove(self.pidfile) else: print str(err) sys.exit(1) def restart(self): self.stop() self.start() def run(self): """ Need to be overriden """ [global] port=/dev/cuaU1 rate=2400 log=/var/log/net_switch.log cmdA=/bin/vist cmdB=/bin/unico #!/usr/bin/env python import sys, os, time, serial, ConfigParser from daemon import Daemon class NetSwitch(Daemon): def run(self): file(self.logfile, 'a+').write("Net switch started\n") while True: ser = serial.Serial(self.port, self.rate, timeout=1) x = ser.read() if not x == self.state: self.state = x if x == 'A': os.system(self.cmdA) else: os.system(self.cmdB) file(self.logfile, 'a+').write("State changed %s\n" % x) time.sleep(0.2) def loadConfig(self, configPath): try: config = ConfigParser.RawConfigParser() config.read(configPath) self.port = config.get('global', 'port') self.rate = config.getint('global', 'rate') self.logfile = config.get('global','log') self.cmdA = config.get('global', 'cmdA') self.cmdB = config.get('global', 'cmdB') self.state = 'A' return True except: return False if __name__ == "__main__": daemon = NetSwitch('/var/run/net_switch.pid') if len(sys.argv) == 2: if 'start' == sys.argv[1]: print "Usage: %s start path_to_config" % sys.argv[0] elif 'stop' == sys.argv[1]: daemon.stop() elif 'restart' == sys.argv[1]: daemon.restart() else: print "Unknown command" sys.exit(2) sys.exit(0) elif len(sys.argv) == 3: if 'start' == sys.argv[1]: configPath = sys.argv[2] if daemon.loadConfig(configPath): daemon.start() else: print "Unable to load config file\n" else: print "Usage %s start path_to_config" % sys.argv[0] else: print "Usage: %s start|stop|restart" % sys.argv[0] sys.exit(2) #!/bin/sh . /etc/rc.subr name=net_switch rcvar=`set_rcvar` #reading the config load_rc_config $name : ${net_switch_enable:="NO"} : ${net_switch_config:="/usr/local/etc/net_switch/config.conf"} pidfile="/var/run/net_switch.pid" command="/usr/local/sbin/${name}.py" start_cmd="start_cmd" stop_cmd="stop_cmd" restart_cmd="restart_cmd" start_cmd() { ${command} start ${net_switch_config} } stop_cmd() { ${command} stop } restart_cmd() { ${command} restart } run_rc_command "$1" net_switch_enable="YES" Source: https://habr.com/ru/post/241245/
All Articles