#include "net.th" #include "relay.th" #include "config_work.th" #include "braker.th" int num_sdf = 0; #define IP_STR "IP=" string buff[4]; void (*net_handlers[4])(string); void net_openSocket(unsigned char socket,unsigned int port, bool cmd, void (*handler)(string)){ d("Сеть ["+str(socket)+"]: Открытие "+ (cmd? "коммандного ":"") +"порта #"+str(port)); sock.num = socket; //net_handlers[socket] = handler; sock.protocol = PL_SOCK_PROTOCOL_TCP; sock.localportlist = port; sock.rxbuffrq(1); sock.txbuffrq(1); if (cmd){ sock.cmdbuffrq(2); sock.rplbuffrq(2); sock.tx2buffrq(5); sock.inbandcommands= YES; sock.escchar = '@'; sock.endchar = '&'; } sys.buffalloc(); sock.reconmode = PL_SOCK_RECONMODE_1; sock.inconmode = PL_SOCK_INCONMODE_ANY_IP_ANY_PORT; } void net_init(){ // string ip = get_parameter("IP"); net.ip=validate_id(config.IP); d("Network initialized"); } void net_start(){ net_openSocket(0,1000,false);//, &net_sensor_handle); // sensors net_openSocket(1,999,false);//, &net_relay_handle); // relay net_openSocket(2,998,false);//, &net_diag_handle); // diag net_openSocket(3,997,true);//, &net_config_handle); // config } string validate_id(string ip){ unsigned char x; string _ip = ddval(ip); // first number can't be 0 or >223 x = asc(left(_ip,1)); if (x==0 || x>223){ insert(_ip,1,chr(1)); } // last number can't 0 or 255 x = asc(right(_ip,1)); if (x == 0 || x == 255){ insert(_ip,4,chr(1)); } return ddstr(_ip); } void add_buff(int num, string message){ buff[num]+= message; } string get_line(int num){ int pos; string line; pos = instr(1,buff[num],chr(13)+chr(10),1); if (pos>0){ line = left(buff[num],pos-1); buff[num] = right(buff[num],(len(buff[num])-pos)-1); }else line = ""; return line; } string relay_buff; void net_relay_handle(string message){ add_buff(0, message); string line = get_line(0); while (len(line)>0){ string relay; if (len(line)==3) relay = left(line,2); else relay = left(line,1); int state = val(right(line,1)); if (relay=="G" || relay == "B") braker_set(line); else relay_set(val(relay),state>0); line = get_line(0); } } void net_diag_handle(string message){ } void net_config_handle(string message){ } void net_sensor_handle(string message){ } void send_sensor(int number, bool state){ sock.num = 0; if (sock.statesimple == PL_SSTS_EST){ sock.setdata(str(number)+(state?"1":"0")+chr(13)+chr(10)); sock.send(); } } void on_sock_data_arrival() { switch (sock.num){ case 0: net_sensor_handle(sock.getdata(sock.rxlen)); break; case 1: net_relay_handle(sock.getdata(sock.rxlen)); break; case 2: net_diag_handle(sock.getdata(sock.rxlen)); break; case 3: //net_config_handle(sock.getdata(sock.rxlen)); net_relay_handle(sock.getdata(sock.rxlen)); break; default: sock.getdata(sock.rxlen); } }