Subversion Repositories NedoOS

Rev

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