Subversion Repositories NedoOS

Rev

Rev 2368 | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <oscalls.h>
  4. #include <tcp.h>
  5. #include <intrz80.h>
  6. #include <stdlib.h>
  7. #include <../common/terminal.c>
  8. #include <osfs.h>
  9. //
  10. #define true 1
  11. #define false 0
  12.  
  13. FILE *fp2;
  14.  
  15. char cmd[128];
  16. char curPath[256];
  17. char holidays[13][32];
  18. unsigned char netbuf[2048];
  19. char calbuf[24000];
  20. struct window
  21. {
  22.         unsigned char x;
  23.         unsigned char y;
  24.         unsigned char w;
  25.         unsigned char h;
  26.         unsigned char text;
  27.         unsigned char back;
  28.         unsigned char tittle[80];
  29. } curWin;
  30.  
  31. struct rtc
  32. {
  33.         unsigned char hours;
  34.         unsigned char minutes;
  35.         unsigned char seconds;
  36.         unsigned char day;
  37.         unsigned char month;
  38.         unsigned int year;
  39. } clock;
  40.  
  41. struct params
  42. {
  43.         char useProdCalendar;
  44.         char currentCountry[3];
  45.         char machineType;
  46. } ini;
  47.  
  48. struct sockaddr_in dnsaddress;
  49. struct sockaddr_in targetadr;
  50. struct readstructure readStruct;
  51.  
  52. unsigned int RBR_THR = 0xf8ef;
  53. unsigned int IER = 0xf9ef;
  54. unsigned int IIR_FCR = 0xfaef;
  55. unsigned int LCR = 0xfbef;
  56. unsigned int MCR = 0xfcef;
  57. unsigned int LSR = 0xfdef;
  58. unsigned int MSR = 0xfeef;
  59. unsigned int SR = 0xffef;
  60. unsigned int divider = 1;
  61. unsigned int comType = 0;
  62. unsigned int espType = 32;
  63. unsigned int espRetry = 5;
  64. unsigned long factor, timerok;
  65.  
  66. unsigned int odoa = 12;
  67. char foreColor;
  68. unsigned int errn, headlng;
  69. unsigned long contLen;
  70. const unsigned char sendOk[] = "SEND OK";
  71. const unsigned char gotWiFi[] = "WIFI GOT IP";
  72. unsigned char userAgent[] = "Host: xmlcalendar.ru\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n";
  73. char country2[5][2] = {"ru", "kz", "by", "uz", "ua"};
  74. char country10[5][11] = {"Россия", "Казахстан", "Беларусь", "Узбекистан", "Украина"};
  75.  
  76. void delay(unsigned long counter)
  77. {
  78.         unsigned long start, finish;
  79.         counter = counter / 20;
  80.         if (counter < 1)
  81.         {
  82.                 counter = 1;
  83.         }
  84.         start = time();
  85.         finish = start + counter;
  86.  
  87.         while (start < finish)
  88.         {
  89.                 start = time();
  90.         }
  91. }
  92.  
  93. void spaces(unsigned char number)
  94. {
  95.         while (number > 0)
  96.         {
  97.                 putchar(' ');
  98.                 number--;
  99.         }
  100. }
  101.  
  102. void clearStatus(void)
  103. {
  104.         ATRIB(40);
  105.         AT(1, 24);
  106.         spaces(80);
  107.         AT(1, 24);
  108. }
  109.  
  110. void readClock(void)
  111. {
  112.         unsigned long dosTime;
  113.         dosTime = OS_GETTIME();
  114.  
  115.         clock.hours = dosTime >> 11 & 31;        // 0b00011111
  116.         clock.minutes = (dosTime >> 5) & 63; // 0b00111111
  117.         clock.seconds = (dosTime & 31) * 2;      // 0b00011111
  118.         clock.day = dosTime >> 16 & 31;
  119.         clock.month = dosTime >> 21 & 15;
  120.         clock.year = (dosTime >> 25 & 63) + 1980;
  121. }
  122.  
  123. void calendarBox(struct window w, const unsigned char *message)
  124. {
  125.         unsigned char wcount, tempx, tittleStart;
  126.  
  127.         w.h++;
  128.         AT(w.x, w.y - 1);
  129.         BOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  130.         AT(w.x, w.y);
  131.         ATRIB(w.text);
  132.         putchar(201);
  133.         for (wcount = 0; wcount < w.w; wcount++)
  134.         {
  135.                 putchar(205);
  136.         }
  137.         putchar(187);
  138.         AT(w.x, w.y + w.h);
  139.         putchar(200);
  140.         for (wcount = 0; wcount < w.w; wcount++)
  141.         {
  142.                 putchar(205);
  143.         }
  144.         putchar(188);
  145.  
  146.         tempx = w.x + w.w + 1;
  147.         for (wcount = 1; wcount < w.h; wcount++)
  148.         {
  149.                 AT(w.x, w.y + wcount);
  150.                 putchar(186);
  151.                 AT(tempx, w.y + wcount);
  152.                 putchar(186);
  153.         }
  154.         tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  155.         AT(tittleStart, w.y);
  156.         printf("[%s]", w.tittle);
  157.         AT(w.x + 1, w.y + 1);
  158.         ATRIB(w.back);
  159.         tittleStart = w.x + (w.w / 2) - (strlen(message) / 2);
  160.         AT(tittleStart, w.y + 1);
  161.         printf("%s", message);
  162. }
  163.  
  164. void printMonthNoProdCal(int month, int year, char xPos, char yPos)
  165. {
  166.         int y, k, j, count, prevMonth;
  167.         char monthsList[12][10] = {"Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"};
  168.         int mDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  169.         static int t[] = {6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
  170.         int days;
  171.         int current;
  172.         char toDay = 0;
  173.  
  174.         curWin.w = 22;
  175.         curWin.x = xPos;
  176.         curWin.y = yPos;
  177.         curWin.h = 7;
  178.         curWin.text = 30;
  179.         curWin.back = 47;
  180.         strcpy(curWin.tittle, monthsList[month - 1]);
  181.         calendarBox(curWin, "");
  182.  
  183.         /*
  184.          1) Определим номер дня недели, где:
  185.  
  186.          0 - Понедельник
  187.          1 - Вторник
  188.          2 - Среда
  189.          3 - Четверг
  190.          4 - Пятница
  191.          5 - Суббота
  192.          6 - Воскресенье
  193.  
  194.          */
  195.  
  196.         if (month == clock.month && year == clock.year)
  197.         {
  198.                 toDay = true;
  199.         }
  200.         else
  201.         {
  202.                 toDay = false;
  203.         }
  204.  
  205.         y = year % 100;
  206.         current = y / 12 + y % 12 + y % 12 / 4 + t[month - 1] +
  207.                           (20 - year / 100);
  208.  
  209.         if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) &&
  210.                 month <= 2)
  211.                 current--;
  212.  
  213.         current = current % 7;
  214.  
  215.         /*
  216.          2) Проверка на високосность начиная с нулевого месяца:
  217.          0 - январь
  218.          ...
  219.          11 - декабрь
  220.          */
  221.         if (month ==
  222.                 2) // 1 - это февраль месяц, так как счёт начинается с 0.
  223.                 if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
  224.                         days = 29; // Если високосный
  225.                 else
  226.                         days = mDays[month - 1];
  227.         else
  228.                 days = mDays[month - 1];
  229.  
  230.         AT(curWin.x + 1, curWin.y + 1);
  231.         puts(" Пн Вт Ср Чт Пт Сб Вс");
  232.         /*
  233.          4) Вводим доп. переменные k и j:
  234.          k - количество дней в неделе от 0 до 6 (0 - ПН; 6 - ВС)
  235.          j - количество дней в месяце (от 1 до общего в месяце)
  236.          */
  237.         AT(curWin.x + 1, curWin.y + 2);
  238.         ATRIB(47);
  239.  
  240.         if (month != 1)
  241.         {
  242.                 prevMonth = month - 2;
  243.         }
  244.         else
  245.         {
  246.                 prevMonth = 11;
  247.         }
  248.         ATRIB(90);
  249.         for (k = 0; k < current; k++)
  250.         {
  251.                 printf("%3d", k + 1 + mDays[prevMonth] - current);
  252.         }
  253.         ATRIB(foreColor);
  254.         for (j = 1; j <= days; j++)
  255.         {
  256.                 k++;
  257.  
  258.                 if (k > 5)
  259.                 {
  260.                         ATRIB(31);
  261.                 }
  262.  
  263.                 if (toDay && (j == clock.day))
  264.                 {
  265.                         putchar(' ');
  266.                         ATRIB(44);
  267.                         if (k > 5)
  268.                         {
  269.                                 ATRIB(41);
  270.                                 ATRIB(foreColor);
  271.                         }
  272.                         printf("%2d", j);
  273.                 }
  274.                 else
  275.                 {
  276.                         printf("%3d", j);
  277.                 }
  278.  
  279.                 if (toDay && (j == clock.day))
  280.                 {
  281.                         ATRIB(47);
  282.                 }
  283.  
  284.                 if (k > 6)
  285.                 {
  286.                         k = 0;
  287.                         ATRIB(foreColor);
  288.                         AT(curWin.x + 1, curWin.y++ + 3);
  289.                 }
  290.         }
  291.         ATRIB(90);
  292.         for (count = 1; count < 8 - k; count++)
  293.         {
  294.                 printf("%3d", count);
  295.         }
  296. }
  297.  
  298. void printMonth(int month, int year, char xPos, char yPos)
  299. {
  300.         int y, k, j, count, prevMonth;
  301.         char monthsList[12][10] = {"Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"};
  302.         int mDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  303.         static int t[] = {6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
  304.         int days;
  305.         int current;
  306.         char toDay = 0;
  307.  
  308.         curWin.w = 22;
  309.         curWin.x = xPos;
  310.         curWin.y = yPos;
  311.         curWin.h = 7;
  312.         curWin.text = 30;
  313.         curWin.back = 47;
  314.         strcpy(curWin.tittle, monthsList[month - 1]);
  315.         calendarBox(curWin, "");
  316.  
  317.         /*
  318.          1) Определим номер дня недели, где:
  319.  
  320.          0 - Понедельник
  321.          1 - Вторник
  322.          2 - Среда
  323.          3 - Четверг
  324.          4 - Пятница
  325.          5 - Суббота
  326.          6 - Воскресенье
  327.  
  328.          */
  329.  
  330.         if (month == clock.month && year == clock.year)
  331.         {
  332.                 toDay = true;
  333.         }
  334.         else
  335.         {
  336.                 toDay = false;
  337.         }
  338.  
  339.         y = year % 100;
  340.         current = y / 12 + y % 12 + y % 12 / 4 + t[month - 1] +
  341.                           (20 - year / 100);
  342.  
  343.         if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) &&
  344.                 month <= 2)
  345.                 current--;
  346.  
  347.         current = current % 7;
  348.  
  349.         /*
  350.          2) Проверка на високосность начиная с нулевого месяца:
  351.          0 - январь
  352.          ...
  353.          11 - декабрь
  354.          */
  355.         if (month ==
  356.                 2) // 1 - это февраль месяц, так как счёт начинается с 0.
  357.                 if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
  358.                         days = 29; // Если високосный
  359.                 else
  360.                         days = mDays[month - 1];
  361.         else
  362.                 days = mDays[month - 1];
  363.  
  364.         AT(curWin.x + 1, curWin.y + 1);
  365.         puts(" Пн Вт Ср Чт Пт Сб Вс");
  366.         /*
  367.          4) Вводим доп. переменные k и j:
  368.          k - количество дней в неделе от 0 до 6 (0 - ПН; 6 - ВС)
  369.          j - количество дней в месяце (от 1 до общего в месяце)
  370.          */
  371.         AT(curWin.x + 1, curWin.y + 2);
  372.         ATRIB(47);
  373.  
  374.         if (month != 1)
  375.         {
  376.                 prevMonth = month - 2;
  377.         }
  378.         else
  379.         {
  380.                 prevMonth = 11;
  381.         }
  382.         ATRIB(90);
  383.         for (k = 0; k < current; k++)
  384.         {
  385.                 printf("%3d", k + 1 + mDays[prevMonth] - current);
  386.         }
  387.         ATRIB(foreColor);
  388.         for (j = 1; j <= days; j++)
  389.         {
  390.                 k++;
  391.                 if (holidays[month][j])
  392.                 {
  393.                         ATRIB(31);
  394.                 }
  395.                 else
  396.                 {
  397.                         ATRIB(foreColor);
  398.                 }
  399.                 if (toDay && (j == clock.day))
  400.                 {
  401.                         putchar(' ');
  402.                         ATRIB(44);
  403.                         if (holidays[month][j])
  404.                         {
  405.                                 ATRIB(41);
  406.                                 ATRIB(foreColor);
  407.                         }
  408.                         printf("%2d", j);
  409.                 }
  410.                 else
  411.                 {
  412.                         printf("%3d", j);
  413.                 }
  414.  
  415.                 if (toDay && (j == clock.day))
  416.                 {
  417.                         ATRIB(47);
  418.                 }
  419.  
  420.                 if (k > 6)
  421.                 {
  422.                         k = 0;
  423.                         AT(curWin.x + 1, curWin.y++ + 3);
  424.                 }
  425.         }
  426.         ATRIB(90);
  427.         for (count = 1; count < 8 - k; count++)
  428.         {
  429.                 printf("%3d", count);
  430.         }
  431. }
  432.  
  433. unsigned char inputBox(struct window w, unsigned char *prefilled)
  434. {
  435.         unsigned char wcount, tempx, tittleStart;
  436.         unsigned char byte, counter;
  437.         w.h++;
  438.         AT(w.x, w.y - 1);
  439.         BOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  440.         AT(w.x, w.y);
  441.         ATRIB(w.text);
  442.         putchar(201);
  443.         for (wcount = 0; wcount < w.w; wcount++)
  444.         {
  445.                 putchar(205);
  446.         }
  447.         putchar(187);
  448.         AT(w.x, w.y + w.h);
  449.         putchar(200);
  450.         for (wcount = 0; wcount < w.w; wcount++)
  451.         {
  452.                 putchar(205);
  453.         }
  454.         putchar(188);
  455.  
  456.         tempx = w.x + w.w + 1;
  457.         for (wcount = 1; wcount < w.h; wcount++)
  458.         {
  459.                 AT(w.x, w.y + wcount);
  460.                 putchar(186);
  461.                 AT(tempx, w.y + wcount);
  462.                 putchar(186);
  463.         }
  464.         tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  465.         AT(tittleStart, w.y);
  466.         printf("[%s]", w.tittle);
  467.         AT(w.x + 1, w.y + 1);
  468.         ATRIB(w.back);
  469.         // putchar(219);
  470.  
  471.         cmd[0] = 0;
  472.  
  473.         counter = strlen(prefilled);
  474.         if (counter != 0)
  475.         {
  476.                 strcpy(cmd, prefilled);
  477.                 goto skipKeys;
  478.         }
  479.  
  480.         do
  481.         {
  482.                 byte = getchar();
  483.                 if (byte != 0)
  484.                 {
  485.                         switch (byte)
  486.                         {
  487.                         case 0x08:
  488.                                 if (counter > 0)
  489.                                 {
  490.                                         counter--;
  491.                                         cmd[counter] = 0;
  492.                                 }
  493.                                 break;
  494.                         case 0x0d:
  495.  
  496.                                 if (counter == 0)
  497.                                 {
  498.                                         return false;
  499.                                 }
  500.                                 else
  501.                                 {
  502.                                         return true;
  503.                                 }
  504.  
  505.                         case 31:
  506.                                 break;
  507.                         case 250:
  508.                                 break;
  509.                         case 249:
  510.                                 break;
  511.                         case 248:
  512.                                 break;
  513.                         case 251: // Right
  514.                                 break;
  515.                         case 252: // Del
  516.                                 AT(w.x + 1, w.y + 1);
  517.                                 spaces(counter + 1);
  518.                                 cmd[0] = 0;
  519.                                 counter = 0;
  520.                                 break;
  521.                         case 27:
  522.                                 cmd[0] = 0;
  523.                                 return false;
  524.                         default:
  525.                                 if (counter < w.w - 1)
  526.                                 {
  527.                                         cmd[counter] = byte;
  528.                                         counter++;
  529.                                         cmd[counter] = 0;
  530.                                 }
  531.                                 break;
  532.                         }
  533.                 skipKeys:
  534.                         AT(w.x + 1, w.y + 1);
  535.                         printf("%s", cmd);
  536.                         // putchar(219);
  537.                         if (byte == 0x08)
  538.                         {
  539.                                 putchar(' ');
  540.                         }
  541.                 }
  542.                 YIELD();
  543.         } while (42);
  544.         return false;
  545. }
  546.  
  547. void clearHolidays(void)
  548. {
  549.         char month, day;
  550.         for (month = 0; month < 13; month++)
  551.         {
  552.                 for (day = 0; day < 32; day++)
  553.                 {
  554.                         holidays[month][day] = false;
  555.                 }
  556.         }
  557. }
  558.  
  559. char readParamFromIni(void)
  560. {
  561.         FILE *fpini;
  562.         //char skip2end = false;
  563.         //unsigned int count = 0;
  564.         //unsigned long loop = 0;
  565.         unsigned char *count1;
  566.  
  567.         const char useProdCalendar[] = "useProdCalendar";
  568.         const char currentCountry[] = "currentCountry";
  569.  
  570.         OS_GETPATH((unsigned int)&curPath);
  571.         OS_SETSYSDRV();
  572.         OS_CHDIR("/");
  573.         OS_CHDIR("ini");
  574.  
  575.         fpini = OS_OPENHANDLE("calendar.ini", 0x80);
  576.         if (((int)fpini) & 0xff)
  577.         {
  578.                 clearStatus();
  579.                 printf("calendar.ini not found.\r\n");
  580.                 getchar();
  581.                 return false;
  582.         }
  583.  
  584.         OS_READHANDLE(calbuf, fpini, 512);
  585.         OS_CLOSEHANDLE(fpini);
  586.  
  587.         //calbuf[loop + 1] = 0;
  588.  
  589.         count1 = strstr(calbuf, useProdCalendar);
  590.         if (count1 != NULL)
  591.         {
  592.                 sscanf(count1 + strlen(useProdCalendar) + 1, "%d", &ini.useProdCalendar);
  593.         }
  594.  
  595.         count1 = strstr(calbuf, currentCountry);
  596.         if (count1 != NULL)
  597.         {
  598.                 sscanf(count1 + strlen(currentCountry) + 1, "%c", &ini.currentCountry[0]);
  599.                 sscanf(count1 + strlen(currentCountry) + 2, "%c", &ini.currentCountry[1]);
  600.                 ini.currentCountry[2] = 0;
  601.         }
  602.  
  603.         OS_CHDIR(curPath);
  604.         return true;
  605. }
  606.  
  607. char loadProdCalDisk(int year)
  608. {
  609.         FILE *fpdat;
  610.         unsigned long loaded, total = 0;
  611.         //int lineYear = 0;
  612.         //int lineMonth = 0;
  613.         //int lineDay = 0;
  614.         odoa = 12;
  615.         clearStatus();
  616.         printf("Загрузка производственного кадендаря с диска на %d год", year);
  617.  
  618.         OS_GETPATH((unsigned int)&curPath);
  619.         OS_SETSYSDRV();
  620.         OS_CHDIR("/");
  621.         OS_CHDIR("ini");
  622.         fpdat = OS_OPENHANDLE("calendar.ini", 0x80);
  623.         if (((int)fpdat) & 0xff)
  624.         {
  625.                 clearStatus();
  626.                 printf("calendar.ini not found.\r\n");
  627.                 getchar();
  628.                 return false;
  629.         }
  630.  
  631.         do
  632.         {
  633.                 loaded = OS_READHANDLE(calbuf + total, fpdat, sizeof(calbuf));
  634.                 total = total + loaded;
  635.         } while (loaded != 0);
  636.         OS_CLOSEHANDLE(fpdat);
  637.         OS_CHDIR(curPath);
  638.         return 1;
  639. }
  640.  
  641. char fillProdCal(int year)
  642. {
  643.         int lineYear = 0;
  644.         int lineMonth = 0;
  645.         int lineDay = 0;
  646.         char result = false;
  647.         unsigned int count;
  648.         char *yptr;
  649.  
  650.         clearHolidays();
  651.  
  652.         sprintf(cmd, "%d", year);
  653.         yptr = strstr(calbuf, cmd);
  654.  
  655.         if (yptr == NULL)
  656.         {
  657.                 return result;
  658.         }
  659.  
  660.         count = 0;
  661.         clearStatus();
  662.         while (42)
  663.         {
  664.  
  665.                 // 2024.01.01CL
  666.                 sscanf(yptr + 0 + count, "%d", &lineYear);
  667.                 sscanf(yptr + 5 + count, "%d", &lineMonth);
  668.                 sscanf(yptr + 8 + count, "%d", &lineDay);
  669.                 count = count + odoa;
  670.  
  671.                 if (lineYear != year)
  672.                 {
  673.                         return result;
  674.                 }
  675.  
  676.                 if (lineDay < 32 && lineMonth < 13 && lineDay > 0 && lineMonth > 0)
  677.                 {
  678.                         holidays[lineMonth][lineDay] = true;
  679.                         result = true;
  680.                         //      printf("lineDay=[%02d] lineMonth=[%02d] lineYear=[%04d]\r\n", lineDay, lineMonth, lineYear);
  681.                 }
  682.         }
  683.         return result;
  684. }
  685.  
  686. #include <../common/network.c>
  687. #include <../common/esp-com.c>
  688.  
  689. int cutHeader(unsigned int todo)
  690. {
  691.         unsigned int err;
  692.         unsigned char *count1;
  693.  
  694.         err = httpError();
  695.         if (err != 200)
  696.         {
  697.                 /*
  698.                                 BOX(1, 1, 80, 25, 40, 32);
  699.                                 AT(1, 1);
  700.                                 printf("HTTP ERROR %u", err);
  701.                                 puts("^^^^^^^^^^^^^^^^^^^^^");
  702.                                 puts(netbuf);
  703.                                 getchar();
  704.                 */
  705.                 return -1;
  706.         }
  707.         count1 = strstr(netbuf, "Content-Length:");
  708.         if (count1 == NULL)
  709.         {
  710.                 printf("contLen  not found \r\n");
  711.                 contLen = 0;
  712.         }
  713.         else
  714.         {
  715.                 contLen = atol(count1 + 15);
  716.                 // printf("Content-Length: %lu \n\r", contLen);
  717.         }
  718.  
  719.         count1 = strstr(netbuf, "\r\n\r\n");
  720.         if (count1 == NULL)
  721.         {
  722.                 printf("header not found\r\n");
  723.         }
  724.         else
  725.         {
  726.                 headlng = ((unsigned int)count1 - (unsigned int)netbuf + 4);
  727.                 // printf("header %u bytes\r\n", headlng);
  728.         }
  729.         return todo - headlng;
  730. }
  731.  
  732. char fillBuffer(signed char socket)
  733. {
  734.         int todo;
  735.         unsigned int w, pPos, headskip;
  736.  
  737.         headskip = 0;
  738.         pPos = 0;
  739.         while (42)
  740.         {
  741.                 headlng = 0;
  742.                 todo = tcpRead(socket, 20);
  743.                 testOperation("OS_WIZNETREAD", todo);
  744.  
  745.                 if (headskip == 0)
  746.                 {
  747.                         headskip = 1;
  748.                         todo = cutHeader(todo);
  749.  
  750.                         if (todo == -1)
  751.                         {
  752.                                 return false;
  753.                         }
  754.                 }
  755.                 for (w = 0; w < todo; w++)
  756.                 {
  757.                         calbuf[w + pPos] = netbuf[w + headlng];
  758.                 }
  759.                 pPos = pPos + todo;
  760.                 if (pPos == contLen)
  761.                 {
  762.                         break;
  763.                 }
  764.         }
  765.         netShutDown(socket, 0);
  766.         calbuf[pPos + 0] = 0;
  767.         strcat(calbuf, "\n9999.12.31\n");
  768.         return true;
  769. }
  770.  
  771. unsigned char loadProdCalNet(int year, const char *country)
  772. {
  773.         int todo;
  774.         char socket;
  775.         odoa = 11;
  776.  
  777.         clearStatus();
  778.         printf("Загрузка производственного кадендаря через NedoNet на %d год", year);
  779.  
  780.         dnsResolve("xmlcalendar.ru");
  781.         socket = OpenSock(AF_INET, SOCK_STREAM);
  782.         testOperation("OS_NETSOCKET", socket);
  783.  
  784.         todo = netConnect(socket, 10);
  785.         testOperation("OS_NETCONNECT", todo);
  786.  
  787.         sprintf(netbuf, "GET /data/%s/%d/calendar.txt HTTP/1.1\r\n%s", country, year, userAgent);
  788.  
  789.         todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf), 10);
  790.         testOperation("OS_WIZNETWRITE", todo);
  791.         fillBuffer(socket);
  792.         return 2;
  793. }
  794.  
  795. unsigned char loadProdCalEsp(int year, const char *country)
  796. {
  797.         unsigned char sizeLink = 0;
  798.         unsigned long downloaded = 0;
  799.         unsigned char byte, count = 0, try = 0;
  800.         unsigned int dataSize;
  801.         unsigned char skipHeader = 0;
  802.         const unsigned char *count1;
  803.         odoa = 11;
  804.  
  805.         sprintf(curPath, "GET /data/%s/%d/calendar.txt HTTP/1.1\r\n%s", country, year, userAgent);
  806.         sizeLink = strlen(curPath);
  807.         try = 0;
  808.         do
  809.         {
  810.                 try++;
  811.                 if (try > 1)
  812.                 {
  813.                         printf("----->Retry:%u\r\n", try);
  814.                         delay(500);
  815.                 }
  816.                 sendcommand("AT+CIPSTART=\"TCP\",\"xmlcalendar.ru\",80");
  817.                 getAnswer3(); // CONNECT or ERROR or link is not valid
  818.                 count1 = strstr(netbuf, "CONNECT");
  819.         } while (count1 == NULL);
  820.  
  821.         getAnswer3(); // OK
  822.  
  823.         sprintf(netbuf, "AT+CIPSEND=%u", sizeLink + 2); // second CRLF in send command
  824.         sendcommand(netbuf);
  825.         getAnswer3();
  826.         do
  827.         {
  828.                 byte = uartReadBlock();
  829.                 // putchar(byte);
  830.         } while (byte != '>');
  831.         sendcommand(curPath);
  832.  
  833.         count = 0;
  834.  
  835.         do
  836.         {
  837.                 byte = uartReadBlock();
  838.                 if (byte == sendOk[count])
  839.                 {
  840.                         count++;
  841.                 }
  842.                 else
  843.                 {
  844.                         count = 0;
  845.                 }
  846.         } while (count < strlen(sendOk));
  847.         uartReadBlock(); // CR
  848.         uartReadBlock(); // LF
  849.         skipHeader = 0;
  850.         downloaded = 0;
  851.         do
  852.         {
  853.                 headlng = 0;
  854.                 dataSize = recvHead();
  855.                 getdataEsp(dataSize); // Requested size
  856.                 if (skipHeader == 0)
  857.                 {
  858.                         dataSize = cutHeader(dataSize);
  859.                         skipHeader = 1;
  860.                         if (dataSize == -1)
  861.                         {
  862.                                 return false;
  863.                         }
  864.                 }
  865.                 memcpy(calbuf + downloaded, netbuf + headlng, dataSize);
  866.                 downloaded = downloaded + dataSize;
  867.  
  868.         } while (downloaded < contLen);
  869.         sendcommand("AT+CIPCLOSE");
  870.         getAnswer3(); // CLOSED
  871.         getAnswer3(); // OK
  872.         calbuf[downloaded + 1] = 0;
  873.         strcat(calbuf, "\n9999.12.31\n");
  874.  
  875.         return 3;
  876. }
  877.  
  878. C_task main(int argc, char *argv[])
  879. {
  880.         int x, y, year, half;
  881.         char key;
  882.         char ci = 0;
  883.  
  884.         targetadr.porth = 00;
  885.         targetadr.portl = 80;
  886.         strcpy(ini.currentCountry, "ru");
  887.         ini.useProdCalendar = false;
  888.  
  889.         os_initstdio();
  890.         CLS();
  891.         printf("[Build:%s  %s]",__DATE__, __TIME__);
  892.         loadEspConfig();
  893.         get_dns();
  894.         AT(3, 25);
  895.         ATRIB(40);
  896.         ATRIB(90);
  897.         printf("Онлайн производственный календарь предоставлен сайтом http://xmlcalendar.ru/");
  898.         ATRIB(97);
  899.         YIELD();
  900.  
  901.         readParamFromIni();
  902.  
  903.         ini.machineType = (unsigned char)OS_GETCONFIG();
  904.         // L= 1-Evo 2-ATM2 3-ATM3 6-p2.666 ;E=pgsys(system page) D= TR-DOS page
  905.         if (ini.machineType == 1)
  906.         {
  907.                 foreColor = 97;
  908.         }
  909.         else
  910.         {
  911.                 foreColor = 30;
  912.         }
  913.  
  914.         if (ini.useProdCalendar == 3)
  915.         {
  916.                 uart_init(divider);
  917.                 espReBoot();
  918.         }
  919.  
  920.         BOX(1, 1, 80, 24, 44, 32);
  921.  
  922.         AT(1, 1);
  923.         ATRIB(97);
  924.         ATRIB(44);
  925.  
  926.         half = 0;
  927.  
  928.         if (argc == 1)
  929.         {
  930.                 readClock();
  931.                 year = clock.year;
  932.                 AT(4, 2);
  933.                 printf("Сегодня: %02u-%02u-%04u", clock.day, clock.month, clock.year);
  934.  
  935.                 if (clock.month > 5)
  936.                 {
  937.                         half = 6;
  938.                 }
  939.         }
  940.         else if (argc == 2)
  941.         {
  942.                 char *p = argv[1];
  943.                 sscanf(p, "%d", &year);
  944.         }
  945.  
  946.         x = 4;
  947.         y = 4;
  948.  
  949.         AT(1, 25);
  950.         ATRIB(40);
  951.         spaces(79);
  952.  
  953. loop:
  954.         clearStatus();
  955.         switch (ini.useProdCalendar)
  956.         {
  957.         case 0:
  958.                 break;
  959.         case 1:
  960.                 ini.useProdCalendar = loadProdCalDisk(year);
  961.                 break;
  962.         case 2:
  963.                 ini.useProdCalendar = loadProdCalNet(year, ini.currentCountry);
  964.                 break;
  965.         case 3:
  966.                 ini.useProdCalendar = loadProdCalEsp(year, ini.currentCountry);
  967.                 break;
  968.         default:
  969.                 break;
  970.         }
  971.         if (ini.useProdCalendar != 0)
  972.         {
  973.                 if (fillProdCal(year) == false)
  974.                 {
  975.                         ini.useProdCalendar = false;
  976.                         clearStatus();
  977.                         printf("Не найден и выключен производственный календарь на %d год. ", year);
  978.                 }
  979.         }
  980. loop2:
  981.  
  982.         // 0 - not use; 1 - use file; 2 - use NedoNet; 3 - use ESP-COM;
  983.         switch (ini.useProdCalendar)
  984.         {
  985.         case 0:
  986.                 strcpy(cmd, "Выключены");
  987.                 break;
  988.  
  989.         case 1:
  990.                 strcpy(cmd, " Из файла");
  991.                 break;
  992.         case 2:
  993.         case 3:
  994.                 strcpy(cmd, "  Сетевые");
  995.                 break;
  996.         default:
  997.                 break;
  998.         }
  999.  
  1000.         AT(60, 2);
  1001.         ATRIB(97);
  1002.         ATRIB(44);
  1003.         printf("Выходные:%s", cmd);
  1004.         clearStatus();
  1005.  
  1006.         ATRIB(93);
  1007.         ATRIB(44);
  1008.  
  1009.         switch (ini.currentCountry[0])
  1010.         {
  1011.         case 'r':
  1012.                 ci = 0;
  1013.                 break;
  1014.         case 'k':
  1015.                 ci = 1;
  1016.                 break;
  1017.         case 'b':
  1018.                 ci = 2;
  1019.                 break;
  1020.         case 'u':
  1021.                 if (ini.currentCountry[1] == 'z')
  1022.                 {
  1023.                         ci = 3;
  1024.                 }
  1025.                 else if (ini.currentCountry[1] == 'a')
  1026.                 {
  1027.                         ci = 4;
  1028.                 }
  1029.                 break;
  1030.         }
  1031.         AT(40 - (strlen(country10[ci]) / 2) - 3, 2);
  1032.         printf("[%s %d]", country10[ci], year);
  1033.  
  1034.         if (ini.useProdCalendar == 0)
  1035.         {
  1036.                 printMonthNoProdCal(1 + half, year, x + 00, y + 00);
  1037.                 printMonthNoProdCal(2 + half, year, x + 25, y + 00);
  1038.                 printMonthNoProdCal(3 + half, year, x + 50, y + 00);
  1039.                 printMonthNoProdCal(4 + half, year, x + 00, y + 10);
  1040.                 printMonthNoProdCal(5 + half, year, x + 25, y + 10);
  1041.                 printMonthNoProdCal(6 + half, year, x + 50, y + 10);
  1042.                 ATRIB(40);
  1043.                 ATRIB(37);
  1044.         }
  1045.         else
  1046.         {
  1047.                 printMonth(1 + half, year, x + 00, y + 00);
  1048.                 printMonth(2 + half, year, x + 25, y + 00);
  1049.                 printMonth(3 + half, year, x + 50, y + 00);
  1050.                 printMonth(4 + half, year, x + 00, y + 10);
  1051.                 printMonth(5 + half, year, x + 25, y + 10);
  1052.                 printMonth(6 + half, year, x + 50, y + 10);
  1053.                 ATRIB(40);
  1054.                 ATRIB(37);
  1055.         }
  1056.  
  1057.         key = getchar();
  1058.         switch (key)
  1059.         {
  1060.         case 250: // Up
  1061.                 year++;
  1062.                 half = 0;
  1063.                 break;
  1064.         case 249: // down
  1065.                 year--;
  1066.                 half = 0;
  1067.                 break;
  1068.         case 'Y':
  1069.         case 'y':
  1070.  
  1071.                 curWin.w = 14;
  1072.                 curWin.x = 80 / 2 - curWin.w / 2;
  1073.                 curWin.y = 9;
  1074.                 curWin.h = 1;
  1075.                 curWin.text = 97;
  1076.                 curWin.back = 42;
  1077.                 strcpy(curWin.tittle, "Введите год:");
  1078.  
  1079.                 if (inputBox(curWin, ""))
  1080.                 {
  1081.                         sscanf(cmd, "%d", &year);
  1082.                         half = 0;
  1083.                 }
  1084.  
  1085.                 break;
  1086.  
  1087.         case 'h': // File
  1088.         case 'H':
  1089.                 if (ini.useProdCalendar != 0)
  1090.                         ini.useProdCalendar = 0;
  1091.                 else
  1092.                         ini.useProdCalendar = 1;
  1093.                 break;
  1094.  
  1095.         case 'n': // NedoNET
  1096.         case 'N':
  1097.                 ini.useProdCalendar = 2;
  1098.                 break;
  1099.         case 'e': // ESP-COM
  1100.         case 'E':
  1101.                 ini.useProdCalendar = 3;
  1102.                 break;
  1103.         default:
  1104.                 if (half == 0)
  1105.                 {
  1106.                         half = 6;
  1107.                 }
  1108.                 else
  1109.                 {
  1110.                         half = 0;
  1111.                 }
  1112.                 goto loop2;
  1113.         }
  1114.         goto loop;
  1115. }
  1116.